Skip to content

Commit

Permalink
Merge pull request #246 from geekingfrog/fix-spring-auth-async-tests
Browse files Browse the repository at this point in the history
Fix spring auth async tests
  • Loading branch information
Beherith authored Apr 10, 2024
2 parents 18db542 + f4dd22f commit 4661360
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
37 changes: 37 additions & 0 deletions lib/teiserver/libs/test_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,43 @@ defmodule Teiserver.TeiserverTestLib do

seed_badge_types()
end

@doc """
Traverse most ConCache tables and delete everything from them.
Because ETS tables are global, failing to clear the cache between tests may introduce errors
since SQL transactions are rolled back, and so the caches hold values that are no longer in the
sandboxed DB used in test.
TODO:
Some tables declared in lib/teiserver/application.ex aren't cleared here, it seems they are important
for some tests, but this needs further investigation.
See https://github.com/sasa1977/con_cache?tab=readme-ov-file#testing-in-your-application
returns :ok
"""
def clear_all_con_caches() do
cache_list = [
:telemetry_complex_client_event_types_cache,
:telemetry_complex_lobby_event_types_cache,
:telemetry_complex_match_event_types_cache,
:telemetry_complex_server_event_types_cache,
:telemetry_property_types_cache,
:telemetry_simple_client_event_types_cache,
:telemetry_simple_lobby_event_types_cache,
:telemetry_simple_match_event_types_cache,
:telemetry_simple_server_event_types_cache,
]

Enum.each(cache_list, fn cache ->
cache
|> ConCache.ets()
|> :ets.tab2list()
|> Enum.each(fn {key, _} -> ConCache.delete(cache, key) end)
end)

:ok
end
end

defmodule Teiserver.FakeTransport do
Expand Down
5 changes: 5 additions & 0 deletions test/support/server_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ defmodule Teiserver.ServerCase do
Ecto.Adapters.SQL.Sandbox.mode(Teiserver.Repo, {:shared, self()})
end

on_exit(&Teiserver.TeiserverTestLib.clear_all_con_caches/0)

:ok = Supervisor.terminate_child(Teiserver.Supervisor, Teiserver.Repo)
{:ok, _} = Supervisor.restart_child(Teiserver.Supervisor, Teiserver.Repo)

:ok
end

Expand Down
5 changes: 3 additions & 2 deletions test/teiserver/protocols/spring/spring_auth_async_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ defmodule Teiserver.SpringAuthAsyncTest do

setup do
%{user: user, state: state} = async_auth_setup(Spring)
on_exit(fn -> teardown(user) end)
{:ok, state: state, user: user}
end

defp teardown(user) do
Client.disconnect(user.id)
end

test "PING", %{state: state} do
test "PING", %{state: state, user: user} do
_send_lines(state, "#4 PING\n")
reply = _recv_lines()
teardown(user)
assert reply == "#4 PONG\n"
end

test "GETUSERINFO", %{state: state, user: user} do
_send_lines(state, "GETUSERINFO\n")
reply = _recv_lines(3)
teardown(user)
assert reply =~ "SERVERMSG Registration date: "
assert reply =~ "SERVERMSG Email address: #{user.email}"
assert reply =~ "SERVERMSG Ingame time: "
Expand Down
18 changes: 9 additions & 9 deletions test/teiserver/protocols/spring/spring_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Teiserver.SpringAuthTest do
use Teiserver.ServerCase, async: false
require Logger
alias Teiserver.BitParse
alias Teiserver.{User, Account, Client}
alias Teiserver.{CacheUser, Account, Client}
alias Teiserver.Account.UserCacheLib
import Teiserver.Helper.NumberHelper, only: [int_parse: 1]

Expand Down Expand Up @@ -85,15 +85,15 @@ defmodule Teiserver.SpringAuthTest do
reply = _recv_raw(socket)
assert reply == "SERVERMSG Current password entered incorrectly\n"
user = UserCacheLib.get_user_by_name(user.name)
assert User.test_password("X03MO1qnZdYdgyfeuILPmQ==", user.password_hash)
assert CacheUser.test_password("X03MO1qnZdYdgyfeuILPmQ==", user.password_hash)

# Change it correctly
_send_raw(socket, "CHANGEPASSWORD X03MO1qnZdYdgyfeuILPmQ== new_pass\n")
:timer.sleep(1000)
reply = _recv_raw(socket)
assert reply == "SERVERMSG Password changed, you will need to use it next time you login\n"
user = UserCacheLib.get_user_by_name(user.name)
assert User.test_password("new_pass", user.password_hash)
assert CacheUser.test_password("new_pass", user.password_hash)

# Test no match
_send_raw(socket, "CHANGEPASSWORD nomatchname\n")
Expand Down Expand Up @@ -507,7 +507,7 @@ CLIENTS test_room #{user.name}\n"
assert accepted == "DENIED Flood protection - Please wait 20 seconds and try again"

# Un-flood them
User.set_flood_level(userid, 0)
CacheUser.set_flood_level(userid, 0)

# Now they can log in again
_send_raw(
Expand Down Expand Up @@ -619,8 +619,8 @@ CLIENTS test_room #{user.name}\n"
# assert Enum.count(Account.list_reports(search: [filter: {"target", target_user.id}])) == 2

# # Reporting a friend
# User.create_friend_request(user.id, target_user.id)
# User.accept_friend_request(user.id, target_user.id)
# CacheUser.create_friend_request(user.id, target_user.id)
# CacheUser.accept_friend_request(user.id, target_user.id)

# _send_raw(socket, "c.moderation.report_user #{target_user.name}\tlocation_type\t123\treason with spaces\n")
# reply = _recv_raw(socket)
Expand All @@ -641,7 +641,7 @@ CLIENTS test_room #{user.name}\n"

test "Bad id ADDUSER", %{user: user, socket: socket} do
{:ok, bad_user} =
User.user_register_params_with_md5(
CacheUser.user_register_params_with_md5(
"test_user_bad_id",
"[email protected]",
"X03MO1qnZdYdgyfeuILPmQ=="
Expand All @@ -651,10 +651,10 @@ CLIENTS test_room #{user.name}\n"
bad_user
|> UserCacheLib.convert_user()
|> UserCacheLib.add_user()
|> User.verify_user()
|> CacheUser.verify_user()

# Need to add it as a client for the :add_user command to work
Client.login(User.get_user_by_id(bad_user.id), :spring, "127.0.0.1")
Client.login(CacheUser.get_user_by_id(bad_user.id), :spring, "127.0.0.1")

# Now see what happens when we add user
pid = Client.get_client_by_id(user.id).tcp_pid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ defmodule Teiserver.Protocols.Spring.SpringBattleHostAsyncTest do

setup do
%{user: user, state: state} = async_auth_setup(Spring)
on_exit(fn -> teardown(user) end)
{:ok, state: state, user: user}
end

defp teardown(user) do
Client.disconnect(user.id)
end

test "battle commands when not in a battle", %{state: state} do
test "battle commands when not in a battle", %{state: state, user: user} do
_send_lines(state, "LEAVEBATTLE\n")
reply = _recv_lines()
assert reply == ""

_send_lines(state, "MYBATTLESTATUS 123 123\n")
reply = _recv_lines()
teardown(user)
assert reply == ""
end
end

0 comments on commit 4661360

Please sign in to comment.