diff --git a/lib/plausible/site/cache.ex b/lib/plausible/site/cache.ex index 67944cca1c98..1514bc006ae3 100644 --- a/lib/plausible/site/cache.ex +++ b/lib/plausible/site/cache.ex @@ -48,13 +48,13 @@ defmodule Plausible.Site.Cache do def base_db_query() do from s in Site, left_join: rg in assoc(s, :revenue_goals), - inner_join: owner in assoc(s, :owner), + inner_join: team in assoc(s, :team), select: { s.domain, s.domain_changed_from, %{struct(s, ^@cached_schema_fields) | from_cache?: true} }, - preload: [revenue_goals: rg, owner: owner] + preload: [revenue_goals: rg, team: team] end @impl true diff --git a/lib/plausible/site/gate_keeper.ex b/lib/plausible/site/gate_keeper.ex index 7515f8ca9cd8..d3d2736f1892 100644 --- a/lib/plausible/site/gate_keeper.ex +++ b/lib/plausible/site/gate_keeper.ex @@ -44,7 +44,7 @@ defmodule Plausible.Site.GateKeeper do defp policy(domain, opts) when is_binary(domain) do with from_cache <- Cache.get(domain, Keyword.get(opts, :cache_opts, [])), - site = %Site{owner: %{accept_traffic_until: accept_traffic_until}} <- from_cache do + site = %Site{team: %{accept_traffic_until: accept_traffic_until}} <- from_cache do if not is_nil(accept_traffic_until) and Date.after?(Date.utc_today(), accept_traffic_until) do :payment_required diff --git a/test/plausible/ingestion/event_test.exs b/test/plausible/ingestion/event_test.exs index 554efbc48d9e..bc9d98b6cc6d 100644 --- a/test/plausible/ingestion/event_test.exs +++ b/test/plausible/ingestion/event_test.exs @@ -1,5 +1,6 @@ defmodule Plausible.Ingestion.EventTest do use Plausible.DataCase, async: true + use Plausible.Teams.Test import Phoenix.ConnTest @@ -7,7 +8,7 @@ defmodule Plausible.Ingestion.EventTest do alias Plausible.Ingestion.Event test "processes a request into an event" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -27,7 +28,7 @@ defmodule Plausible.Ingestion.EventTest do for {user_agent, idx} <- Enum.with_index(@regressive_user_agents) do test "processes user agents known to cause problems parsing in the past (case #{idx})" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -45,7 +46,7 @@ defmodule Plausible.Ingestion.EventTest do end test "drops verification agent" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -76,7 +77,7 @@ defmodule Plausible.Ingestion.EventTest do end test "drops a request when referrer is spam" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -93,7 +94,7 @@ defmodule Plausible.Ingestion.EventTest do end test "drops a request when referrer is spam for multiple domains" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -110,7 +111,7 @@ defmodule Plausible.Ingestion.EventTest do end test "selectively drops an event for multiple domains" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -126,7 +127,7 @@ defmodule Plausible.Ingestion.EventTest do end test "selectively drops an event when rate-limited" do - site = insert(:site, ingest_rate_limit_threshold: 1) + site = new_site(ingest_rate_limit_threshold: 1) payload = %{ name: "pageview", @@ -143,7 +144,7 @@ defmodule Plausible.Ingestion.EventTest do end test "drops a request when header x-plausible-ip-type is dc_ip" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -160,7 +161,7 @@ defmodule Plausible.Ingestion.EventTest do end test "drops a request when ip is on blocklist" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -180,7 +181,7 @@ defmodule Plausible.Ingestion.EventTest do end test "drops a request when country is on blocklist" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -201,7 +202,7 @@ defmodule Plausible.Ingestion.EventTest do end test "drops a request when page is on blocklist" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -220,7 +221,7 @@ defmodule Plausible.Ingestion.EventTest do end test "drops a request when hostname allowlist is defined and hostname is not on the list" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -239,7 +240,7 @@ defmodule Plausible.Ingestion.EventTest do end test "passes a request when hostname allowlist is defined and hostname is on the list" do - site = insert(:site) + site = new_site() payload = %{ name: "pageview", @@ -259,11 +260,8 @@ defmodule Plausible.Ingestion.EventTest do test "drops events for site with accept_trafic_until in the past" do yesterday = Date.add(Date.utc_today(), -1) - site = - insert(:site, - ingest_rate_limit_threshold: 1, - members: [build(:user, accept_traffic_until: yesterday)] - ) + owner = new_user(team: [accept_traffic_until: yesterday]) + site = new_site(ingest_rate_limit_threshold: 1, owner: owner) payload = %{ name: "pageview", @@ -280,7 +278,7 @@ defmodule Plausible.Ingestion.EventTest do @tag :slow test "drops events on session lock timeout" do - site = insert(:site) + site = new_site() very_slow_buffer = fn sessions -> Process.sleep(1000) @@ -319,7 +317,7 @@ defmodule Plausible.Ingestion.EventTest do end test "drops pageleave event when no session found from cache" do - site = insert(:site) + site = new_site() payload = %{ name: "pageleave", @@ -336,7 +334,7 @@ defmodule Plausible.Ingestion.EventTest do @tag :ee_only test "saves revenue amount" do - site = insert(:site) + site = new_site() _goal = insert(:goal, event_name: "checkout", currency: "USD", site: site) payload = %{ @@ -353,7 +351,7 @@ defmodule Plausible.Ingestion.EventTest do end test "does not save revenue amount when there is no revenue goal" do - site = insert(:site) + site = new_site() payload = %{ name: "checkout", @@ -369,7 +367,7 @@ defmodule Plausible.Ingestion.EventTest do end test "IPv4 hostname is stored without public suffix processing" do - _site = insert(:site, domain: "192.168.0.1") + _site = new_site(domain: "192.168.0.1") payload = %{ name: "checkout", @@ -384,7 +382,7 @@ defmodule Plausible.Ingestion.EventTest do end test "Hostname is stored with public suffix processing" do - _site = insert(:site, domain: "foo.netlify.app") + _site = new_site(domain: "foo.netlify.app") payload = %{ name: "checkout", @@ -399,7 +397,7 @@ defmodule Plausible.Ingestion.EventTest do end test "hostname is (none) when no hostname can be derived from the url" do - site = insert(:site, domain: "foo.example.com") + site = new_site(domain: "foo.example.com") payload = %{ domain: site.domain, diff --git a/test/plausible/site/cache_test.exs b/test/plausible/site/cache_test.exs index e5f001aa0a06..3befbaa05d42 100644 --- a/test/plausible/site/cache_test.exs +++ b/test/plausible/site/cache_test.exs @@ -6,7 +6,6 @@ defmodule Plausible.Site.CacheTest do alias Plausible.Site.Cache describe "public cache interface" do - @tag :skip test "cache caches sites", %{test: test} do {:ok, _} = Supervisor.start_link([{Cache, [cache_name: test, child_id: :test_cache_caches_id]}], @@ -36,7 +35,7 @@ defmodule Plausible.Site.CacheTest do assert %Site{from_cache?: true} = Cache.get("site2.example.com", force?: true, cache_name: test) - assert %Site{from_cache?: false, owner: %{accept_traffic_until: ~D[2021-01-01]}} = + assert %Site{from_cache?: false, team: %{accept_traffic_until: ~D[2021-01-01]}} = Cache.get("site2.example.com", cache_name: test) refute Cache.get("site3.example.com", cache_name: test, force?: true) diff --git a/test/plausible/site/gate_keeper_test.exs b/test/plausible/site/gate_keeper_test.exs index 13b590968eca..8b69b72f3620 100644 --- a/test/plausible/site/gate_keeper_test.exs +++ b/test/plausible/site/gate_keeper_test.exs @@ -1,5 +1,6 @@ defmodule Plausible.Site.GateKeeperTest do use Plausible.DataCase, async: true + use Plausible.Teams.Test alias Plausible.Site.Cache alias Plausible.Site.GateKeeper @@ -18,10 +19,12 @@ defmodule Plausible.Site.GateKeeperTest do domain = "expired.example.com" yesterday = Date.utc_today() |> Date.add(-1) + owner = new_user(team: [accept_traffic_until: yesterday]) + %{id: _} = add_site_and_refresh_cache(test, domain: domain, - members: [build(:user, accept_traffic_until: yesterday)] + owner: owner ) assert {:deny, :payment_required} = GateKeeper.check(domain, opts) @@ -88,7 +91,7 @@ defmodule Plausible.Site.GateKeeperTest do {:ok, _} = Plausible.Repo.delete(site) # We need some dummy site, otherwise the cache won't refresh in case the DB # is completely empty - insert(:site) + new_site() deleted_site_id = site.id assert {:allow, %Plausible.Site{id: ^deleted_site_id, from_cache?: true}} = @@ -104,7 +107,7 @@ defmodule Plausible.Site.GateKeeperTest do end defp add_site_and_refresh_cache(cache_name, site_data) do - site = insert(:site, site_data) + site = new_site(site_data) Cache.refresh_updated_recently(cache_name: cache_name, force?: true) site