Skip to content

Commit

Permalink
Fix HelpScout tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol committed Nov 27, 2024
1 parent 4b1f618 commit 7c4b976
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 79 deletions.
22 changes: 12 additions & 10 deletions extra/lib/plausible/help_scout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ defmodule Plausible.HelpScout do
Routes.kaffy_resource_url(PlausibleWeb.Endpoint, :show, :auth, :user, user.id),
plan_label: plan_label(subscription, plan),
plan_link: plan_link(subscription),
sites_count: Plausible.Sites.owned_sites_count(user),
sites_count: Plausible.Teams.owned_sites_count(team),
sites_link:
Routes.kaffy_resource_url(PlausibleWeb.Endpoint, :index, :sites, :site,
search: user.email
Expand All @@ -120,8 +120,9 @@ defmodule Plausible.HelpScout do

domain_query =
from(s in Plausible.Site,
inner_join: sm in assoc(s, :memberships),
where: sm.user_id == parent_as(:user).id and sm.role == :owner,
inner_join: t in assoc(s, :team),
inner_join: tm in assoc(t, :team_memberships),
where: tm.user_id == parent_as(:user).id and tm.role == :owner,
where: ilike(s.domain, ^search_term) or ilike(s.domain_changed_from, ^search_term),
select: 1
)
Expand All @@ -132,7 +133,7 @@ defmodule Plausible.HelpScout do
like(u.email, ^search_term) or exists(domain_query)
)
|> limit(5)
|> select([user: u, site_membership: sm], %{email: u.email, sites_count: count(sm.id)})
|> select([user: u, sites: s], %{email: u.email, sites_count: count(s.id)})
|> Repo.all()
end

Expand Down Expand Up @@ -237,13 +238,14 @@ defmodule Plausible.HelpScout do
defp users_query() do
from(u in Plausible.Auth.User,
as: :user,
left_join: sm in assoc(u, :site_memberships),
on: sm.role == :owner,
as: :site_membership,
left_join: s in assoc(sm, :site),
as: :site,
left_join: tm in assoc(u, :team_memberships),
on: tm.role == :owner,
as: :team_memberships,
left_join: t in assoc(tm, :team),
left_join: s in assoc(t, :sites),
as: :sites,
group_by: u.id,
order_by: [desc: count(sm.id)]
order_by: [desc: count(s.id)]
)
end

Expand Down
11 changes: 11 additions & 0 deletions lib/plausible/teams.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ defmodule Plausible.Teams do
)
end

def owned_sites_count(nil), do: 0

def owned_sites_count(team) do
Repo.aggregate(
from(s in Plausible.Site,
where: s.team_id == ^team.id
),
:count
)
end

@doc """
Create (when necessary) and load team relation for provided site.
Expand Down
129 changes: 65 additions & 64 deletions test/plausible/help_scout_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Plausible.HelpScoutTest do
use Plausible.DataCase, async: true
use Plausible.Teams.Test
use Plausible

@moduletag :ee_only
Expand Down Expand Up @@ -54,7 +55,7 @@ defmodule Plausible.HelpScoutTest do

describe "get_details_for_customer/2" do
test "returns details for user on trial" do
%{id: user_id, email: email} = insert(:user)
%{id: user_id, email: email} = new_user()
stub_help_scout_requests(email)

crm_url = "#{PlausibleWeb.Endpoint.url()}/crm/auth/user/#{user_id}"
Expand All @@ -74,7 +75,7 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user without trial or subscription" do
%{email: email} = insert(:user, trial_expiry_date: nil)
%{email: email} = new_user(trial_expiry_date: nil)
stub_help_scout_requests(email)

assert {:ok,
Expand All @@ -87,7 +88,9 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with trial expired" do
%{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
%{email: email} =
new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

stub_help_scout_requests(email)

assert {:ok,
Expand All @@ -100,10 +103,15 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with paid subscription on standard plan" do
user = %{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

subscribe_to_plan(user, @v4_business_monthly_plan_id)

%{paddle_subscription_id: paddle_subscription_id} =
insert(:subscription, user: user, paddle_plan_id: @v4_business_monthly_plan_id)
paddle_subscription_id =
user
|> team_of(with_subscription?: true)
|> Map.fetch!(:subscription)
|> Map.fetch!(:paddle_subscription_id)

stub_help_scout_requests(email)

Expand All @@ -120,10 +128,15 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with paid subscription on standard yearly plan" do
user = %{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

subscribe_to_plan(user, @v4_business_yearly_plan_id)

%{paddle_subscription_id: paddle_subscription_id} =
insert(:subscription, user: user, paddle_plan_id: @v4_business_yearly_plan_id)
paddle_subscription_id =
user
|> team_of(with_subscription?: true)
|> Map.fetch!(:subscription)
|> Map.fetch!(:paddle_subscription_id)

stub_help_scout_requests(email)

Expand All @@ -140,9 +153,9 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with paid subscription on free 10k plan" do
user = %{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

insert(:subscription, user: user, paddle_plan_id: "free_10k")
subscribe_to_plan(user, "free_10k")

stub_help_scout_requests(email)

Expand All @@ -156,15 +169,11 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with paid subscription on enterprise plan" do
user = %{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

ep =
insert(:enterprise_plan,
features: [Plausible.Billing.Feature.StatsAPI],
user_id: user.id
)

insert(:subscription, user: user, paddle_plan_id: ep.paddle_plan_id)
subscribe_to_enterprise_plan(user,
features: [Plausible.Billing.Feature.StatsAPI]
)

stub_help_scout_requests(email)

Expand All @@ -178,16 +187,12 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with paid subscription on yearly enterprise plan" do
user = %{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))

ep =
insert(:enterprise_plan,
features: [Plausible.Billing.Feature.StatsAPI],
user_id: user.id,
billing_interval: :yearly
)
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

insert(:subscription, user: user, paddle_plan_id: ep.paddle_plan_id)
subscribe_to_enterprise_plan(user,
features: [Plausible.Billing.Feature.StatsAPI],
billing_interval: :yearly
)

stub_help_scout_requests(email)

Expand All @@ -201,12 +206,10 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with subscription pending cancellation" do
user = %{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

insert(:subscription,
user: user,
status: Subscription.Status.deleted(),
paddle_plan_id: @v4_business_monthly_plan_id
subscribe_to_plan(user, @v4_business_monthly_plan_id,
status: Subscription.Status.deleted()
)

stub_help_scout_requests(email)
Expand All @@ -221,12 +224,10 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with canceled subscription" do
user = %{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

insert(:subscription,
user: user,
subscribe_to_plan(user, @v4_business_monthly_plan_id,
status: Subscription.Status.deleted(),
paddle_plan_id: @v4_business_monthly_plan_id,
next_bill_date: Date.add(Date.utc_today(), -1)
)

Expand All @@ -242,12 +243,10 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with paused subscription" do
user = %{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

insert(:subscription,
user: user,
status: Subscription.Status.paused(),
paddle_plan_id: @v4_business_monthly_plan_id
subscribe_to_plan(user, @v4_business_monthly_plan_id,
status: Subscription.Status.paused()
)

stub_help_scout_requests(email)
Expand All @@ -262,11 +261,10 @@ defmodule Plausible.HelpScoutTest do
end

test "returns for user with locked site" do
user = %{email: email} = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))

insert(:site, members: [user], locked: true)
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))

insert(:subscription, user: user, paddle_plan_id: @v4_business_monthly_plan_id)
new_site(owner: user, locked: true)
subscribe_to_plan(user, @v4_business_monthly_plan_id)

stub_help_scout_requests(email)

Expand All @@ -281,7 +279,7 @@ defmodule Plausible.HelpScoutTest do
end

test "returns error when no matching user found in database" do
insert(:user)
new_user()

stub_help_scout_requests("[email protected]")

Expand Down Expand Up @@ -329,7 +327,7 @@ defmodule Plausible.HelpScoutTest do
end

test "uses existing access token when available" do
%{email: email} = insert(:user)
%{email: email} = new_user()
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)

Repo.insert_all("help_scout_credentials", [
Expand Down Expand Up @@ -359,7 +357,7 @@ defmodule Plausible.HelpScoutTest do
end

test "refreshes token on expiry" do
%{email: email} = insert(:user)
%{email: email} = new_user()
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)

Repo.insert_all("help_scout_credentials", [
Expand Down Expand Up @@ -407,7 +405,7 @@ defmodule Plausible.HelpScoutTest do

describe "get_details_for_emails/2" do
test "returns details for user and persists mapping" do
%{id: user_id, email: email} = insert(:user)
%{id: user_id, email: email} = new_user()

crm_url = "#{PlausibleWeb.Endpoint.url()}/crm/auth/user/#{user_id}"

Expand All @@ -428,21 +426,24 @@ defmodule Plausible.HelpScoutTest do
end

test "updates mapping if one already exists" do
user = insert(:user)
%{email: new_email} = insert(:user)
user = new_user()
%{email: new_email} = new_user()

HelpScout.set_mapping("123", user.email)

assert {:ok, _} = HelpScout.get_details_for_emails([new_email], "123")
assert {:ok, ^new_email} = HelpScout.lookup_mapping("123")
end

test "picks the match with largest number of owned sites" do
user1 = insert(:user)
insert(:site, members: [user1])
insert(:site_membership, site: build(:site), user: user1, role: :viewer)
insert(:site_membership, site: build(:site), user: user1, role: :admin)
user2 = insert(:user)
insert_list(2, :site, members: [user2])
user1 = new_user()
new_site(owner: user1)
add_guest(new_site(), user: user1, role: :viewer)
add_guest(new_site(), user: user1, role: :editor)

user2 = new_user()
new_site(owner: user2)
new_site(owner: user2)

crm_url = "#{PlausibleWeb.Endpoint.url()}/crm/auth/user/#{user2.id}"

Expand Down Expand Up @@ -473,14 +474,14 @@ defmodule Plausible.HelpScoutTest do

describe "search_users/2" do
test "lists matching users by email or site domain ordered by site counts" do
user1 = insert(:user, email: "[email protected]")
user1 = new_user(email: "[email protected]")

user2 = insert(:user, email: "[email protected]")
insert(:site, members: [user2])
user2 = new_user(email: "[email protected]")
new_site(owner: user2)

user3 = insert(:user, email: "[email protected]")
insert(:site, members: [user3])
insert(:site, domain: "big.match.example.com/hit", members: [user3])
user3 = new_user(email: "[email protected]")
new_site(owner: user3)
new_site(domain: "big.match.example.com/hit", owner: user3)

assert HelpScout.search_users("match.example.co", "123") == [
%{email: user3.email, sites_count: 2},
Expand Down
10 changes: 5 additions & 5 deletions test/support/teams/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ defmodule Plausible.Teams.Test do
if user.trial_expiry_date do
{:ok, team} = Teams.get_or_create(user)

if team_args != [] do
team
|> Ecto.Changeset.change(team_args)
|> Repo.update!()
end
team_args = Keyword.merge(team_args, trial_expiry_date: user.trial_expiry_date)

team
|> Ecto.Changeset.change(team_args)
|> Repo.update!()
end

Repo.preload(user, team_memberships: :team)
Expand Down

0 comments on commit 7c4b976

Please sign in to comment.