From 0ef0aed205f7298d82649f55615193baac4b3667 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 16 Dec 2020 10:39:36 +0100 Subject: [PATCH] Tests: Add a helper method to reduce sleeping times in test. This will 'time travel', i.e. change the inserted_at and update_at fields of the object in question. This is used to backdate things were we used sleeping before to ensure time differences. --- test/pleroma/chat_test.exs | 3 ++- test/pleroma/conversation/participation_test.exs | 5 ++--- .../web/pleroma_api/controllers/chat_controller_test.exs | 8 ++++---- test/pleroma/web/twitter_api/password_controller_test.exs | 5 ++--- test/support/helpers.ex | 8 ++++++++ 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/test/pleroma/chat_test.exs b/test/pleroma/chat_test.exs index 9e8a9ebf0..1dd04916c 100644 --- a/test/pleroma/chat_test.exs +++ b/test/pleroma/chat_test.exs @@ -73,7 +73,8 @@ test "a returning chat will have an updated `update_at` field" do other_user = insert(:user) {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id) - :timer.sleep(1500) + {:ok, chat} = time_travel(chat, -2) + {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id) assert chat.id == chat_two.id diff --git a/test/pleroma/conversation/participation_test.exs b/test/pleroma/conversation/participation_test.exs index e72c49b29..122b10486 100644 --- a/test/pleroma/conversation/participation_test.exs +++ b/test/pleroma/conversation/participation_test.exs @@ -96,12 +96,11 @@ test "it creates a participation for a conversation and a user" do {:ok, %Participation{} = participation} = Participation.create_for_user_and_conversation(user, conversation) + {:ok, participation} = time_travel(participation, -2) + assert participation.user_id == user.id assert participation.conversation_id == conversation.id - # Needed because updated_at is accurate down to a second - :timer.sleep(1000) - # Creating again returns the same participation {:ok, %Participation{} = participation_two} = Participation.create_for_user_and_conversation(user, conversation) diff --git a/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs index a6c9d0c1b..415c3decd 100644 --- a/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs @@ -394,11 +394,11 @@ test "it return a list of chats the current user is participating in, in descend tridi = insert(:user) {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id) - :timer.sleep(1000) - {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id) - :timer.sleep(1000) + {:ok, chat_1} = time_travel(chat_1, -3) + {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id) + {:ok, _chat_2} = time_travel(chat_2, -2) {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id) - :timer.sleep(1000) + {:ok, chat_3} = time_travel(chat_3, -1) # bump the second one {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id) diff --git a/test/pleroma/web/twitter_api/password_controller_test.exs b/test/pleroma/web/twitter_api/password_controller_test.exs index 6d08075cc..c1f5bc5c7 100644 --- a/test/pleroma/web/twitter_api/password_controller_test.exs +++ b/test/pleroma/web/twitter_api/password_controller_test.exs @@ -37,8 +37,7 @@ test "it returns an error when the token has expired", %{conn: conn} do user = insert(:user) {:ok, token} = PasswordResetToken.create_token(user) - - :timer.sleep(2000) + {:ok, token} = time_travel(token, -2) response = conn @@ -55,7 +54,7 @@ test "it fails for an expired token", %{conn: conn} do user = insert(:user) {:ok, token} = PasswordResetToken.create_token(user) - :timer.sleep(2000) + {:ok, token} = time_travel(token, -2) {:ok, _access_token} = Token.create(insert(:oauth_app), user, %{}) params = %{ diff --git a/test/support/helpers.ex b/test/support/helpers.ex index 224034521..15e8cbd9d 100644 --- a/test/support/helpers.ex +++ b/test/support/helpers.ex @@ -55,6 +55,14 @@ defmacro __using__(_opts) do clear_config: 2 ] + def time_travel(entity, seconds) do + new_time = NaiveDateTime.add(entity.inserted_at, seconds) + + entity + |> Ecto.Changeset.change(%{inserted_at: new_time, updated_at: new_time}) + |> Pleroma.Repo.update() + end + def to_datetime(%NaiveDateTime{} = naive_datetime) do naive_datetime |> DateTime.from_naive!("Etc/UTC")