From 110eb0b1990d5d5ee77f9368a9b7653cfdc07131 Mon Sep 17 00:00:00 2001 From: Simponic Date: Mon, 19 Dec 2022 20:45:01 -0700 Subject: Implement public key and add tests --- test/auth/password_test.exs | 27 +++++++++++++++++++++++++++ test/auth/pubkey_test.exs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 test/auth/password_test.exs create mode 100644 test/auth/pubkey_test.exs (limited to 'test/auth') 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 -- cgit v1.2.3-70-g09d2