Merge pull request 'fix flaky test filter_controller_test.exs:200' (#239) from ilja...
authorfloatingghost <hannah@coffee-and-dreams.uk>
Tue, 1 Nov 2022 14:42:43 +0000 (14:42 +0000)
committerfloatingghost <hannah@coffee-and-dreams.uk>
Tue, 1 Nov 2022 14:42:43 +0000 (14:42 +0000)
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/239

test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs

index 66f7ed5793877a5948cd7ed6425ff681ddd4ead2..99f0374838a48d58cae3a4d585e5058ad4424d87 100644 (file)
@@ -3,9 +3,10 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do
-  use Pleroma.Web.ConnCase, async: true
+  use Pleroma.Web.ConnCase, async: false
   use Oban.Testing, repo: Pleroma.Repo
 
+  import Mock
   import Pleroma.Factory
 
   alias Pleroma.Filter
@@ -53,25 +54,20 @@ defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do
       in_seconds = 600
 
       response =
-        conn
-        |> put_req_header("content-type", "application/json")
-        |> post("/api/v1/filters", %{
-          "phrase" => "knights",
-          context: ["home"],
-          expires_in: in_seconds
-        })
-        |> json_response_and_validate_schema(200)
+        with_mock NaiveDateTime, [:passthrough], utc_now: fn -> ~N[2017-03-17 17:09:58] end do
+          conn
+          |> put_req_header("content-type", "application/json")
+          |> post("/api/v1/filters", %{
+            "phrase" => "knights",
+            context: ["home"],
+            expires_in: in_seconds
+          })
+          |> json_response_and_validate_schema(200)
+        end
 
       assert response["irreversible"] == false
 
-      expires_at =
-        NaiveDateTime.utc_now()
-        |> NaiveDateTime.add(in_seconds)
-
-      assert NaiveDateTime.diff(
-               NaiveDateTime.from_iso8601!(response["expires_at"]),
-               expires_at
-             ) < 5
+      assert response["expires_at"] == "2017-03-17T17:19:58.000Z"
 
       filter = Filter.get(response["id"], user)
 
@@ -183,26 +179,21 @@ defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do
       in_seconds = 600
 
       response =
-        conn
-        |> put_req_header("content-type", "application/json")
-        |> put("/api/v1/filters/#{filter.filter_id}", %{
-          phrase: "nii",
-          context: ["public"],
-          expires_in: in_seconds,
-          irreversible: true
-        })
-        |> json_response_and_validate_schema(200)
+        with_mock NaiveDateTime, [:passthrough], utc_now: fn -> ~N[2017-03-17 17:09:58] end do
+          conn
+          |> put_req_header("content-type", "application/json")
+          |> put("/api/v1/filters/#{filter.filter_id}", %{
+            phrase: "nii",
+            context: ["public"],
+            expires_in: in_seconds,
+            irreversible: true
+          })
+          |> json_response_and_validate_schema(200)
+        end
 
       assert response["irreversible"] == true
 
-      expected_time =
-        NaiveDateTime.utc_now()
-        |> NaiveDateTime.add(in_seconds)
-
-      assert NaiveDateTime.diff(
-               NaiveDateTime.from_iso8601!(response["expires_at"]),
-               expected_time
-             ) < 5
+      assert response["expires_at"] == "2017-03-17T17:19:58.000Z"
 
       filter = Filter.get(response["id"], user)