summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSimponic <loganhunt@simponic.xyz>2022-12-19 20:45:01 -0700
committerSimponic <loganhunt@simponic.xyz>2022-12-19 20:45:01 -0700
commit110eb0b1990d5d5ee77f9368a9b7653cfdc07131 (patch)
treebfe5744255cdfbdc9412d148236a2bf5f8aad7c1 /test
parentfe661a935ac887b11aec31ff049ace2db7ccbf84 (diff)
downloadchessh-110eb0b1990d5d5ee77f9368a9b7653cfdc07131.tar.gz
chessh-110eb0b1990d5d5ee77f9368a9b7653cfdc07131.zip
Implement public key and add tests
Diffstat (limited to 'test')
-rw-r--r--test/auth/password_test.exs27
-rw-r--r--test/auth/pubkey_test.exs35
-rw-r--r--test/schema/key_test.exs1
-rw-r--r--test/schema/register_test.exs51
-rw-r--r--test/support/repo_case.ex2
5 files changed, 113 insertions, 3 deletions
diff --git a/test/auth/password_test.exs b/test/auth/password_test.exs
new file mode 100644
index 0000000..974f2fa
--- /dev/null
+++ b/test/auth/password_test.exs
@@ -0,0 +1,27 @@
+defmodule Chessh.Auth.PasswordAuthenticatorTest do
+ use ExUnit.Case
+ alias Chessh.Player
+ alias Chessh.Repo
+
+ @valid_user %{username: "logan", password: "password"}
+
+ setup do
+ :ok = Ecto.Adapters.SQL.Sandbox.checkout(Chessh.Repo)
+
+ {:ok, _user} = Repo.insert(Player.registration_changeset(%Player{}, @valid_user))
+
+ :ok
+ end
+
+ test "User can sign in with their password" do
+ assert Chessh.Auth.PasswordAuthenticator.authenticate(
+ String.to_charlist(@valid_user.username),
+ String.to_charlist(@valid_user.password)
+ )
+
+ refute Chessh.Auth.PasswordAuthenticator.authenticate(
+ String.to_charlist(@valid_user.username),
+ String.to_charlist("a_bad_password")
+ )
+ end
+end
diff --git a/test/auth/pubkey_test.exs b/test/auth/pubkey_test.exs
new file mode 100644
index 0000000..78eecfb
--- /dev/null
+++ b/test/auth/pubkey_test.exs
@@ -0,0 +1,35 @@
+defmodule Chessh.Auth.PublicKeyAuthenticatorTest do
+ use ExUnit.Case
+ alias Chessh.Key
+ alias Chessh.Repo
+ alias Chessh.Player
+
+ @valid_user %{username: "logan", password: "password"}
+ @valid_key %{
+ name: "The Gamer Machine",
+ key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ/2LOJGGEd/dhFgRxJ5MMv0jJw4s4pA8qmMbZyulN44"
+ }
+
+ setup do
+ :ok = Ecto.Adapters.SQL.Sandbox.checkout(Chessh.Repo)
+
+ {:ok, player} = Repo.insert(Player.registration_changeset(%Player{}, @valid_user))
+
+ {:ok, _key} =
+ Repo.insert(
+ Key.changeset(%Key{}, @valid_key)
+ |> Ecto.Changeset.put_assoc(:player, player)
+ )
+
+ :ok
+ end
+
+ test "User can sign in with their ssh key from raw string" do
+ assert Chessh.Auth.KeyAuthenticator.authenticate(@valid_user.username, @valid_key.key)
+ end
+
+ test "User can sign in with erlang decoded ssh key" do
+ [key] = :ssh_file.decode(@valid_key.key, :openssh_key)
+ assert Chessh.Auth.KeyAuthenticator.authenticate(@valid_user.username, key)
+ end
+end
diff --git a/test/schema/key_test.exs b/test/schema/key_test.exs
index 2c5409d..6dbb574 100644
--- a/test/schema/key_test.exs
+++ b/test/schema/key_test.exs
@@ -28,7 +28,6 @@ defmodule Chessh.Schema.KeyTest do
@empty_attrs %{}
test "changeset with valid attributes" do
- IO.puts(inspect(Key.changeset(%Key{}, @valid_attrs)))
assert Key.changeset(%Key{}, @valid_attrs).valid?
assert Key.changeset(%Key{}, @valid_key_attrs).valid?
end
diff --git a/test/schema/register_test.exs b/test/schema/register_test.exs
new file mode 100644
index 0000000..5705d31
--- /dev/null
+++ b/test/schema/register_test.exs
@@ -0,0 +1,51 @@
+defmodule Chessh.Auth.UserRegistrationTest do
+ use Chessh.RepoCase
+ use ExUnit.Case
+ alias Chessh.Player
+ alias Chessh.Repo
+
+ @valid_user %{username: "logan", password: "password"}
+ @invalid_username %{username: "a", password: "password"}
+ @invalid_password %{username: "aasdf", password: "pass"}
+ @repeated_username %{username: "LoGan", password: "password"}
+
+ test "Password must be at least 8 characters and username must be at least 2" do
+ refute Player.registration_changeset(%Player{}, @invalid_password).valid?
+ refute Player.registration_changeset(%Player{}, @invalid_username).valid?
+ end
+
+ test "Password changeset must match" do
+ refute Player.password_changeset(
+ %Player{},
+ Map.put(@valid_user, :password_confirmation,
+ password_confirmation: @valid_user.password <> "a"
+ )
+ ).valid?
+
+ valid_user_changed_password = Map.put(@valid_user, :password, "a_new_password")
+
+ assert Player.password_changeset(
+ %Player{},
+ Map.put(
+ valid_user_changed_password,
+ :password_confirmation,
+ valid_user_changed_password.password
+ )
+ ).valid?
+ end
+
+ test "Password is hashed" do
+ changeset = Player.registration_changeset(%Player{}, @valid_user)
+ assert_raise KeyError, fn -> changeset.changes.password end
+ assert changeset.changes.hashed_password
+ refute changeset.changes.hashed_password == @valid_user.password
+ end
+
+ test "Username is uniquely case insensitive" do
+ assert Repo.insert(Player.registration_changeset(%Player{}, @valid_user))
+
+ assert {:error,
+ %{errors: [{:username, {_, [{:constraint, :unique}, {:constraint_name, _}]}}]}} =
+ Repo.insert(Player.registration_changeset(%Player{}, @repeated_username))
+ end
+end
diff --git a/test/support/repo_case.ex b/test/support/repo_case.ex
index 73abcff..e5f4a6f 100644
--- a/test/support/repo_case.ex
+++ b/test/support/repo_case.ex
@@ -8,8 +8,6 @@ defmodule Chessh.RepoCase do
import Ecto
import Ecto.Query
import Chessh.RepoCase
-
- # and any other stuff
end
end