diff options
author | Simponic <loganhunt@simponic.xyz> | 2022-12-30 05:46:35 -0700 |
---|---|---|
committer | Simponic <loganhunt@simponic.xyz> | 2022-12-30 05:46:35 -0700 |
commit | 42425b02260d279cd9c12fb3e625282979b9e308 (patch) | |
tree | 0412bf9f39d44266cff94082c499e44e5f6e60f5 /test | |
parent | 60eea1b4ed65bc7cfce1e383dac6de9d004540eb (diff) | |
download | chessh-42425b02260d279cd9c12fb3e625282979b9e308.tar.gz chessh-42425b02260d279cd9c12fb3e625282979b9e308.zip |
Add scalable session thresholds
Diffstat (limited to 'test')
-rw-r--r-- | test/auth/password_test.exs | 11 | ||||
-rw-r--r-- | test/ssh/ssh_auth_test.exs | 75 | ||||
-rw-r--r-- | test/test_helper.exs | 1 |
3 files changed, 75 insertions, 12 deletions
diff --git a/test/auth/password_test.exs b/test/auth/password_test.exs index 8c93ea9..348032f 100644 --- a/test/auth/password_test.exs +++ b/test/auth/password_test.exs @@ -8,7 +8,7 @@ defmodule Chessh.Auth.PasswordAuthenticatorTest do Ecto.Adapters.SQL.Sandbox.checkout(Repo) Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()}) - {:ok, _user} = Repo.insert(Player.registration_changeset(%Player{}, @valid_user)) + {:ok, _player} = Repo.insert(Player.registration_changeset(%Player{}, @valid_user)) :ok end @@ -24,4 +24,13 @@ defmodule Chessh.Auth.PasswordAuthenticatorTest do "a_bad_password" ) end + + test "Password can authenticate a user instance" do + player = Repo.get_by(Player, username: "logan") + + assert Chessh.Auth.PasswordAuthenticator.authenticate( + player, + @valid_user.password + ) + end end diff --git a/test/ssh/ssh_auth_test.exs b/test/ssh/ssh_auth_test.exs index 1f17d9f..92c4b22 100644 --- a/test/ssh/ssh_auth_test.exs +++ b/test/ssh/ssh_auth_test.exs @@ -1,6 +1,6 @@ defmodule Chessh.SSH.AuthTest do - use ExUnit.Case - alias Chessh.{Player, Repo, Key} + use ExUnit.Case, async: false + alias(Chessh.{Player, Repo, Key, PlayerSession}) @localhost '127.0.0.1' @localhost_inet {{127, 0, 0, 1}, 1} @@ -26,6 +26,14 @@ defmodule Chessh.SSH.AuthTest do :ok end + def cleanup() do + Process.sleep(1_000) + PlayerSession.delete_all_on_node(System.fetch_env!("NODE_ID")) + + # Wait for (what I believe to be the) DB Connection queue to clear? + Process.sleep(1_000) + end + test "Password attempts are rate limited" do jail_attempt_threshold = Application.get_env(:chessh, RateLimits) @@ -49,7 +57,7 @@ defmodule Chessh.SSH.AuthTest do test_pid = self() Task.Supervisor.start_child(sup, fn -> - {:ok, _pid} = + {:ok, conn} = :ssh.connect(@localhost, Application.fetch_env!(:chessh, :port), user: String.to_charlist(@valid_user.username), password: String.to_charlist(@valid_user.password), @@ -57,11 +65,12 @@ defmodule Chessh.SSH.AuthTest do silently_accept_hosts: true ) + :ssh.close(conn) send(test_pid, :connected_via_password) end) Task.Supervisor.start_child(sup, fn -> - {:ok, _pid} = + {:ok, conn} = :ssh.connect(@localhost, Application.fetch_env!(:chessh, :port), user: String.to_charlist(@valid_user.username), auth_methods: 'publickey', @@ -69,15 +78,61 @@ defmodule Chessh.SSH.AuthTest do user_dir: String.to_charlist(@client_test_keys_dir) ) + :ssh.close(conn) send(test_pid, :connected_via_public_key) end) - assert_receive(:connected_via_password, 1000) - assert_receive(:connected_via_public_key, 1000) + assert_receive(:connected_via_password, 2_000) + assert_receive(:connected_via_public_key, 2_000) + + cleanup() end - # TODO - # test "INTEGRATION - User cannot have more than specified concurrent sessions" do - # :ok - # end + test "INTEGRATION - Player cannot have more than specified concurrent sessions" do + max_concurrent_user_sessions = + Application.get_env(:chessh, RateLimits) + |> Keyword.get(:max_concurrent_user_sessions) + + player = Repo.get_by(Player, username: @valid_user.username) + + {:ok, sup} = Task.Supervisor.start_link() + test_pid = self() + + Enum.reduce(0..(max_concurrent_user_sessions + 1), fn i, _ -> + Task.Supervisor.start_child(sup, fn -> + case :ssh.connect(@localhost, Application.fetch_env!(:chessh, :port), + user: String.to_charlist(@valid_user.username), + password: String.to_charlist(@valid_user.password), + auth_methods: if(rem(i, 2) == 0, do: 'publickey', else: 'password'), + silently_accept_hosts: true, + user_dir: String.to_charlist(@client_test_keys_dir) + ) do + {:ok, conn} -> + send( + test_pid, + {:attempted, {:ok, conn}} + ) + + x -> + send(test_pid, {:attempted, x}) + end + end) + end) + + Enum.reduce(0..max_concurrent_user_sessions, fn _, _ -> + assert_receive({:attempted, {:ok, _conn}}, 2000) + end) + + assert_receive( + {:attempted, {:error, 'Unable to connect using the available authentication methods'}}, + 2000 + ) + + # Give it time to send back the disconnection payload after session was opened + # but over threshold + :timer.sleep(100) + assert PlayerSession.concurrent_sessions(player) == max_concurrent_user_sessions + + cleanup() + end end diff --git a/test/test_helper.exs b/test/test_helper.exs index e568cdd..04774b3 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,3 +1,2 @@ ExUnit.start() - Ecto.Adapters.SQL.Sandbox.mode(Chessh.Repo, :manual) |