logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git
commit: 0ef0aed205f7298d82649f55615193baac4b3667
parent ba1997583842bf18949334e4f2d9c05946fc4b17
Author: lain <lain@soykaf.club>
Date:   Wed, 16 Dec 2020 10:39:36 +0100

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.

Diffstat:

Mtest/pleroma/chat_test.exs3++-
Mtest/pleroma/conversation/participation_test.exs5++---
Mtest/pleroma/web/pleroma_api/controllers/chat_controller_test.exs8++++----
Mtest/pleroma/web/twitter_api/password_controller_test.exs5++---
Mtest/support/helpers.ex8++++++++
5 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/test/pleroma/chat_test.exs b/test/pleroma/chat_test.exs @@ -73,7 +73,8 @@ defmodule Pleroma.ChatTest 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 @@ -96,12 +96,11 @@ defmodule Pleroma.Conversation.ParticipationTest 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 @@ -394,11 +394,11 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do 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 @@ -37,8 +37,7 @@ defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest 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 @@ defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest 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 @@ -55,6 +55,14 @@ defmodule Pleroma.Tests.Helpers 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")