diff options
author | Simponic <loganhunt@simponic.xyz> | 2022-12-19 20:45:01 -0700 |
---|---|---|
committer | Simponic <loganhunt@simponic.xyz> | 2022-12-19 20:45:01 -0700 |
commit | 110eb0b1990d5d5ee77f9368a9b7653cfdc07131 (patch) | |
tree | bfe5744255cdfbdc9412d148236a2bf5f8aad7c1 /test/schema | |
parent | fe661a935ac887b11aec31ff049ace2db7ccbf84 (diff) | |
download | chessh-110eb0b1990d5d5ee77f9368a9b7653cfdc07131.tar.gz chessh-110eb0b1990d5d5ee77f9368a9b7653cfdc07131.zip |
Implement public key and add tests
Diffstat (limited to 'test/schema')
-rw-r--r-- | test/schema/key_test.exs | 1 | ||||
-rw-r--r-- | test/schema/register_test.exs | 51 |
2 files changed, 51 insertions, 1 deletions
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 |