Fix test flakiness due to shared state in asynchronous run (#3184)

* Fix flaky test

* Prevent this in the future
pull/3182/head^2
hq1 3 years ago committed by GitHub
parent b606fc1809
commit 928691158f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,18 @@
defmodule PlausibleWeb.Api.InternalController.SyncTest do
use PlausibleWeb.ConnCase, async: false
use Plausible.Repo
describe "PUT /api/:domain/disable-feature" do
setup [:create_user, :log_in]
test "when the logged-in user is an super-admin", %{conn: conn, user: user} do
site = insert(:site)
patch_env(:super_admin_user_ids, [user.id])
conn = put(conn, "/api/#{site.domain}/disable-feature", %{"feature" => "conversions"})
assert json_response(conn, 200) == "ok"
assert %{conversions_enabled: false} = Plausible.Sites.get_by_domain(site.domain)
end
end
end

@ -100,16 +100,6 @@ defmodule PlausibleWeb.Api.InternalControllerTest do
assert %{conversions_enabled: false} = Plausible.Sites.get_by_domain(site.domain)
end
test "when the logged-in user is an super-admin", %{conn: conn, user: user} do
site = insert(:site)
patch_env(:super_admin_user_ids, [user.id])
conn = put(conn, "/api/#{site.domain}/disable-feature", %{"feature" => "conversions"})
assert json_response(conn, 200) == "ok"
assert %{conversions_enabled: false} = Plausible.Sites.get_by_domain(site.domain)
end
test "returns 401 when the logged-in user is a viewer of the site", %{conn: conn, user: user} do
site = insert(:site)
insert(:site_membership, user: user, site: site, role: :viewer)

@ -11,6 +11,10 @@ defmodule Plausible.TestUtils do
defmacro patch_env(env_key, value) do
quote do
if __MODULE__.__info__(:attributes)[:ex_unit_async] == [true] do
raise "Patching env is unsafe in asynchronous tests. maybe extract the case elsewhere?"
end
original_env = Application.get_env(:plausible, unquote(env_key))
Application.put_env(:plausible, unquote(env_key), unquote(value))

Loading…
Cancel
Save