Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More fix spring auth #277

Merged
Merged
9 changes: 8 additions & 1 deletion lib/teiserver/libs/test_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ defmodule Teiserver.TeiserverTestLib do
client.tcp_pid
end

# Ensure that the user is cleanly disconnected. Otherwise it t doesn't fails tests,
# but you get a ton of errors in the console about connection
# pool errors like `Client XXX is still using a connection from owner at location…`
# This is because upon disconnecting, the server does a bunch of DB call for logging
# and telemetry. The disconnection happen when the tcp socket is closed, and by
# that time, the test has ended and the SQL sandbox closed.
ExUnit.Callbacks.on_exit(fn -> Teiserver.Client.disconnect(user.id) end)
%{socket: socket, user: user, pid: pid}
end

Expand Down Expand Up @@ -652,7 +659,7 @@ defmodule Teiserver.TeiserverTestLib do
:telemetry_simple_client_event_types_cache,
:telemetry_simple_lobby_event_types_cache,
:telemetry_simple_match_event_types_cache,
:telemetry_simple_server_event_types_cache,
:telemetry_simple_server_event_types_cache
]

Enum.each(cache_list, fn cache ->
Expand Down
15 changes: 9 additions & 6 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ defmodule Teiserver.DataCase do
end
end

setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Teiserver.Repo)

unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Teiserver.Repo, {:shared, self()})
end
@doc """
Sets up the sandbox based on the test tags.
"""
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Teiserver.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end

setup tags do
setup_sandbox(tags)
:ok
end

Expand Down
11 changes: 1 addition & 10 deletions test/support/server_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,8 @@ defmodule Teiserver.ServerCase do
end

setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Teiserver.Repo)

unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Teiserver.Repo, {:shared, self()})
end

Teiserver.DataCase.setup_sandbox(tags)
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
58 changes: 29 additions & 29 deletions test/teiserver/protocols/spring/spring_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ defmodule Teiserver.SpringAuthTest do

# Now lets ignore them
_send_raw(socket1, "IGNORE userName=#{user2.name}\n")
_send_raw(socket1, "IGNORELIST\n")
reply = _recv_raw(socket1)
assert reply == "IGNORELISTBEGIN
IGNORELIST userName=#{user2.name}
Expand All @@ -149,6 +150,7 @@ IGNORELISTEND\n"

# Now unignore them
_send_raw(socket1, "UNIGNORE userName=#{user2.name}\n")
_send_raw(socket1, "IGNORELIST\n")
reply = _recv_raw(socket1)
assert reply == "IGNORELISTBEGIN
IGNORELISTEND\n"
Expand Down Expand Up @@ -194,49 +196,38 @@ IGNORELISTEND\n"

# Now we send the friend request
_send_raw(socket2, "FRIENDREQUEST userName=#{user.name}\n")

# and the target receives it
reply = _recv_raw(socket1)
assert reply == "FRIENDREQUESTLISTBEGIN
FRIENDREQUESTLIST userName=#{user2.name}
FRIENDREQUESTLISTEND\n"
assert reply == "s.user.new_incoming_friend_request #{user2.id}\n"

# Accept the friend request
_send_raw(socket1, "ACCEPTFRIENDREQUEST userName=#{user2.name}\n")
reply = _recv_raw(socket1)
assert reply == "FRIENDLISTBEGIN
FRIENDLIST userName=#{user2.name}
FRIENDLISTEND
FRIENDREQUESTLISTBEGIN
FRIENDREQUESTLISTEND\n"
# there's no expected response for this command

reply = _recv_raw(socket2)
assert reply == "FRIENDLISTBEGIN
FRIENDLIST userName=#{user.name}
FRIENDLISTEND\n"
assert reply == "s.user.friend_request_accepted #{user.id}\n"

# Change of plan, remove them
_send_raw(socket1, "UNFRIEND userName=#{user2.name}\n")
reply = _recv_raw(socket1)
assert reply == "FRIENDLISTBEGIN
FRIENDLISTEND\n"
assert reply == "s.user.friend_deleted #{user2.id}\n"

reply = _recv_raw(socket2)
assert reply == "FRIENDLISTBEGIN
FRIENDLISTEND\n"
assert reply == "s.user.friend_deleted #{user.id}\n"

# Request a friend again so we can decline it
_send_raw(socket2, "FRIENDREQUEST userName=#{user.name}\n")
reply = _recv_raw(socket1)
assert reply == "FRIENDREQUESTLISTBEGIN
FRIENDREQUESTLIST userName=#{user2.name}
FRIENDREQUESTLISTEND\n"
assert reply == "s.user.new_incoming_friend_request #{user2.id}\n"

# Decline the friend request
_send_raw(socket1, "DECLINEFRIENDREQUEST userName=#{user2.name}\n")
reply = _recv_raw(socket1)
assert reply == "FRIENDREQUESTLISTBEGIN
FRIENDREQUESTLISTEND\n"

# the other users receive the notice that it was declined
reply = _recv_raw(socket2)
assert reply == "s.user.friend_request_declined #{user.id}\n"

reply = _recv_raw(socket1)
assert reply == :timeout
end

Expand Down Expand Up @@ -291,6 +282,9 @@ CLIENTS test_room #{user.name}\n"
test "JOINBATTLE, SAYBATTLE, MYBATTLESTATUS, LEAVEBATTLE", %{socket: socket1, user: user1} do
hash = "-1540855590"

# Give user Bot role so they can manage battle
UserCacheLib.update_user(%{user1 | roles: ["Bot" | user1.roles]}, persist: false)

_send_raw(
socket1,
"OPENBATTLE 0 0 empty 52200 16 #{hash} 0 1565299817 spring\t104.0.1-1784-gf6173b4 BAR\tauth_joinbattle_test\tEU - 00\tBeyond All Reason test-15658-85bf66d\n"
Expand Down Expand Up @@ -338,22 +332,25 @@ CLIENTS test_room #{user.name}\n"
host_bstatus,
# bstatus,
request,
client_status,
""
] = reply

assert joinbattle == "JOINBATTLE #{lobby_id} #{hash}"
assert joinedbattle == "JOINEDBATTLE #{lobby_id} #{user2.name} sPassword"
assert tags =~ "SETSCRIPTTAGS server/match/uuid="
assert host_bstatus == "CLIENTBATTLESTATUS #{user1.name} 8388608 0"
assert tags =~ ~r"SETSCRIPTTAGS .*server/match/uuid="
# the \d+ is the team colour
assert host_bstatus =~ ~r"CLIENTBATTLESTATUS #{user1.name} \d+ 0"
assert request == "REQUESTBATTLESTATUS"
assert client_status == "CLIENTSTATUS #{user2.name} 0"

_send_raw(socket2, "SAYBATTLE Hello there!\n")
reply = _recv_raw(socket2)
assert reply == "SAIDBATTLE #{user2.name} Hello there!\n"

_send_raw(socket2, "MYBATTLESTATUS 12 0\n")
reply = _recv_raw(socket2)
assert reply == "CLIENTBATTLESTATUS #{user2.name} 8388620 0\n"
assert reply =~ ~r"CLIENTBATTLESTATUS #{user2.name} \d+ 0\n"

# SAYBATTLEPRIVATEEX
_send_raw(socket1, "SAYBATTLEPRIVATEEX #{user2.name} This is a test priv battle msg\n")
Expand Down Expand Up @@ -508,6 +505,8 @@ CLIENTS test_room #{user.name}\n"

# Un-flood them
CacheUser.set_flood_level(userid, 0)
# And re-verify them
user.id |> Teiserver.CacheUser.get_user_by_id() |> Teiserver.CacheUser.verify_user()

# Now they can log in again
_send_raw(
Expand Down Expand Up @@ -567,7 +566,7 @@ CLIENTS test_room #{user.name}\n"
assert reply == "SERVERMSG You do not have permission to execute that command\n"

# Give mod access, recache the user
UserCacheLib.update_user(%{user | moderator: true}, persist: false)
UserCacheLib.update_user(%{user | roles: ["Moderator" | user.roles]}, persist: false)
:timer.sleep(100)

_send_raw(socket, "CREATEBOTACCOUNT test_bot_account #{user.name}\n")
Expand All @@ -582,6 +581,7 @@ CLIENTS test_room #{user.name}\n"

assert reply ==
"SERVERMSG No incomming match for CREATEBOTACCOUNT with data '\"nomatchname\"'. Userid #{user.id}\n"

end

# test "c.moderation.report", %{socket: socket, user: user} do
Expand Down Expand Up @@ -678,7 +678,7 @@ CLIENTS test_room #{user.name}\n"
reply = _recv_raw(socket)
assert reply == :timeout

UserCacheLib.update_user(%{user | moderator: true}, persist: false)
UserCacheLib.update_user(%{user | roles: ["Moderator" | user.roles]}, persist: false)
:timer.sleep(500)
_recv_until(socket)

Expand All @@ -697,7 +697,7 @@ CLIENTS test_room #{user.name}\n"
reply = _recv_raw(socket)
assert reply == :timeout

UserCacheLib.update_user(%{user | moderator: true}, persist: false)
UserCacheLib.update_user(%{user | roles: ["Moderator" | user.roles]}, persist: false)
:timer.sleep(500)
_recv_until(socket)

Expand Down
Loading