From db9599cc136ee3d1374a558846f96848df49b698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Sat, 9 Nov 2024 14:50:33 +0000 Subject: [PATCH 1/5] Fix the tests related to rename error messages --- test/teiserver/data/user_test.exs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/test/teiserver/data/user_test.exs b/test/teiserver/data/user_test.exs index 8353270ba..a3d68e72d 100644 --- a/test/teiserver/data/user_test.exs +++ b/test/teiserver/data/user_test.exs @@ -71,12 +71,13 @@ defmodule Teiserver.Data.UserTest do test "renaming" do user = TeiserverTestLib.new_user() + expected_rename_error = + {:error, "Rename limit reached (2 times in 5 days or 3 times in 30 days)"} + assert CacheUser.rename_user(user.id, "rename1") == :success assert CacheUser.rename_user(user.id, "rename2") == :success - assert CacheUser.rename_user(user.id, "rename3") == - {:error, - "If you keep changing your name people won't know who you are; give it a bit of time (5 days)"} + assert CacheUser.rename_user(user.id, "rename3") == expected_rename_error # Lets make it so they can do it again Account.update_user_stat(user.id, %{ @@ -86,9 +87,7 @@ defmodule Teiserver.Data.UserTest do assert CacheUser.rename_user(user.id, "rename4") == :success assert CacheUser.rename_user(user.id, "rename44") == :success - assert CacheUser.rename_user(user.id, "rename5") == - {:error, - "If you keep changing your name people won't know who you are; give it a bit of time (5 days)"} + assert CacheUser.rename_user(user.id, "rename5") == expected_rename_error # What if they've done it many times before but nothing recent? Account.update_user_stat(user.id, %{ @@ -98,9 +97,7 @@ defmodule Teiserver.Data.UserTest do assert CacheUser.rename_user(user.id, "rename6") == :success assert CacheUser.rename_user(user.id, "rename66") == :success - assert CacheUser.rename_user(user.id, "rename7") == - {:error, - "If you keep changing your name people won't know who you are; give it a bit of time (5 days)"} + assert CacheUser.rename_user(user.id, "rename7") == expected_rename_error # Nothing in the last 15 days but enough in the last 30 now = System.system_time(:second) @@ -114,9 +111,7 @@ defmodule Teiserver.Data.UserTest do ] }) - assert CacheUser.rename_user(user.id, "rename8") == - {:error, - "If you keep changing your name people won't know who you are; give it a bit of time (30 days)"} + assert CacheUser.rename_user(user.id, "rename8") == expected_rename_error end test "valid_email?" do From d11415499ce1fb337ab5ac14fb5ab1e9bbcdedfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Sat, 9 Nov 2024 14:56:30 +0000 Subject: [PATCH 2/5] Disable call to geoip command in tests --- test/support/conn_case.ex | 1 + test/support/server_case.ex | 1 + 2 files changed, 2 insertions(+) diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 8d0a55f87..bfbdf4612 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -35,6 +35,7 @@ defmodule TeiserverWeb.ConnCase do setup tags do :ok = Ecto.Adapters.SQL.Sandbox.checkout(Teiserver.Repo) Teiserver.TeiserverTestLib.clear_all_con_caches() + Teiserver.Config.update_site_config("system.Use geoip", false) on_exit(&Teiserver.TeiserverTestLib.clear_all_con_caches/0) unless tags[:async] do diff --git a/test/support/server_case.ex b/test/support/server_case.ex index fc55dca68..79b11acec 100644 --- a/test/support/server_case.ex +++ b/test/support/server_case.ex @@ -33,6 +33,7 @@ defmodule Teiserver.ServerCase do # more false failures. Teiserver.TeiserverTestLib.clear_all_con_caches() Teiserver.DataCase.setup_sandbox(tags) + Teiserver.Config.update_site_config("system.Use geoip", false) on_exit(&Teiserver.TeiserverTestLib.clear_all_con_caches/0) :ok end From 0e4694ac75c160b2f94a54c26b8402a6fdd8732f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Sat, 9 Nov 2024 15:03:17 +0000 Subject: [PATCH 3/5] Refactor: extract the function to clear a given cache --- test/support/teiserver_test_lib.ex | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/support/teiserver_test_lib.ex b/test/support/teiserver_test_lib.ex index 86ef591ae..f1ed67b0c 100644 --- a/test/support/teiserver_test_lib.ex +++ b/test/support/teiserver_test_lib.ex @@ -571,16 +571,18 @@ defmodule Teiserver.TeiserverTestLib do :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) + Enum.each(cache_list, &clear_cache/1) :ok end + def clear_cache(cache) do + cache + |> ConCache.ets() + |> :ets.tab2list() + |> Enum.each(fn {key, _} -> ConCache.delete(cache, key) end) + end + def seed_matchmaking_queues() do alias Teiserver.Game From 780493d031cf9ca340acfd7b1da7da21de732c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Sat, 9 Nov 2024 15:03:40 +0000 Subject: [PATCH 4/5] Avoid cache between tests leading to foreign key errors The usual issue when caches are used so much. --- test/teiserver_web/controllers/api/spads_controller_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/teiserver_web/controllers/api/spads_controller_test.exs b/test/teiserver_web/controllers/api/spads_controller_test.exs index 9e432862d..3bf81e04b 100644 --- a/test/teiserver_web/controllers/api/spads_controller_test.exs +++ b/test/teiserver_web/controllers/api/spads_controller_test.exs @@ -38,6 +38,7 @@ defmodule TeiserverWeb.API.SpadsControllerTest do test "existing user", %{conn: conn} do user = new_user() + Teiserver.TeiserverTestLib.clear_cache(:teiserver_game_rating_types) rating_type_id = MatchRatingLib.rating_type_name_lookup()["Team"] {:ok, _} = From 5a2230c1e1996c244cfb04f16f66f5beab43c70a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Sat, 9 Nov 2024 15:19:09 +0000 Subject: [PATCH 5/5] Fix spec for update_site_config --- lib/teiserver/config.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/teiserver/config.ex b/lib/teiserver/config.ex index 27c9ee4aa..9748c9af3 100644 --- a/lib/teiserver/config.ex +++ b/lib/teiserver/config.ex @@ -312,7 +312,7 @@ defmodule Teiserver.Config do Repo.one(query) end - @spec update_site_config(String.t(), String.t()) :: :ok + @spec update_site_config(String.t(), term()) :: :ok def update_site_config(key, value) do query = from site_config in SiteConfig,