-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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}" | ||
|
@@ -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, | ||
|
@@ -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, | ||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
|
@@ -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) | ||
) | ||
|
||
|
@@ -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) | ||
|
@@ -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) | ||
|
||
|
@@ -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]") | ||
|
||
|
@@ -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", [ | ||
|
@@ -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", [ | ||
|
@@ -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}" | ||
|
||
|
@@ -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}" | ||
|
||
|
@@ -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}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters