Skip to content

Commit

Permalink
Merge pull request #4484 from santiment/fix-mox-errors-tests
Browse files Browse the repository at this point in the history
Fix missing expectation in tests
  • Loading branch information
tspenov authored Nov 22, 2024
2 parents 870c7a9 + a6c183d commit 758a2fc
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,17 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do
when event_type == :create_subscription do
subscription = Sanbase.Billing.Subscription.by_id(event.data.subscription_id)

user = Sanbase.Accounts.get_user!(subscription.user_id)
Sanbase.Accounts.get_user(subscription.user_id)
|> case do
{:ok, user} ->
{:ok, _settings} =
Sanbase.Accounts.UserSettings.update_settings(user, %{
is_subscribed_metric_updates: true
})

{:ok, _settings} =
Sanbase.Accounts.UserSettings.update_settings(user, %{
is_subscribed_metric_updates: true
})
_ ->
:ok
end

cond do
Subscription.trialing_sanbase_pro?(subscription) ->
Expand Down Expand Up @@ -108,12 +113,18 @@ defmodule Sanbase.EventBus.BillingEventSubscriber do
defp do_handle(:cancel_subscription_at_period_end, event_type, event)
when event_type == :cancel_subscription_at_period_end do
subscription = Sanbase.Billing.Subscription.by_id(event.data.subscription_id)
user = Sanbase.Accounts.get_user!(subscription.user_id)

{:ok, _settings} =
Sanbase.Accounts.UserSettings.update_settings(user, %{
is_subscribed_metric_updates: false
})
Sanbase.Accounts.get_user(subscription.user_id)
|> case do
{:ok, user} ->
{:ok, _settings} =
Sanbase.Accounts.UserSettings.update_settings(user, %{
is_subscribed_metric_updates: false
})

_ ->
:ok
end
end

defp do_handle(:send_discord_notification, event_type, event)
Expand Down
4 changes: 2 additions & 2 deletions lib/sanbase/event_bus/user_events_subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Sanbase.EventBus.UserEventsSubscriber do
{:ok, _} = Sanbase.Accounts.EmailJobs.schedule_emails_after_sign_up(user_id)

email = Sanbase.Accounts.get_user!(user_id).email
Sanbase.Email.MailjetApi.client().subscribe(:monthly_newsletter, email)
if email, do: Sanbase.Email.MailjetApi.client().subscribe(:monthly_newsletter, email)

EventBus.mark_as_completed({__MODULE__, event_shadow})
state
Expand All @@ -53,7 +53,7 @@ defmodule Sanbase.EventBus.UserEventsSubscriber do
state
) do
email = Sanbase.Accounts.get_user!(user_id).email
Sanbase.Email.MailjetApi.client().subscribe(:monthly_newsletter, email)
if email, do: Sanbase.Email.MailjetApi.client().subscribe(:monthly_newsletter, email)
EventBus.mark_as_completed({__MODULE__, event_shadow})
state
end
Expand Down
5 changes: 5 additions & 0 deletions test/sanbase/auth/user_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Sanbase.Accounts.UserTest do
use Sanbase.DataCase, async: false

import Mock
import Mox
import Mockery
import Sanbase.Factory
import ExUnit.CaptureLog
Expand All @@ -13,6 +14,9 @@ defmodule Sanbase.Accounts.UserTest do
alias Sanbase.StripeApiTestResponse
alias Sanbase.Accounts.User.UniswapStaking

setup :set_mox_from_context
setup :verify_on_exit!

test "Delete user and associations" do
with_mocks([
{Sanbase.KafkaExporter, [:passthrough],
Expand All @@ -26,6 +30,7 @@ defmodule Sanbase.Accounts.UserTest do
{StripeApi, [:passthrough],
[create_subscription: fn _ -> StripeApiTestResponse.create_subscription_resp() end]}
]) do
expect(Sanbase.Email.MockMailjetApi, :subscribe, fn _, _ -> :ok end)
user = insert(:user, stripe_customer_id: "test")

# UserToken
Expand Down
7 changes: 6 additions & 1 deletion test/sanbase/billing/sanr_nft_subscription_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
defmodule Sanbase.Billing.SanrNFTSubscriptionTest do
use Sanbase.DataCase, async: false
import Sanbase.Factory

import Mox
alias Sanbase.Accounts.EthAccount
alias Sanbase.Billing.Subscription

setup :set_mox_from_context
setup :verify_on_exit!

setup do
user = insert(:user)

Expand All @@ -25,6 +28,7 @@ defmodule Sanbase.Billing.SanrNFTSubscriptionTest do
end

test "create subscription", context do
expect(Sanbase.Email.MockMailjetApi, :subscribe, fn _, _ -> :ok end)
%{user: user, address: address, start_date: start_date, end_date: end_date} = context

Sanbase.Mock.prepare_mock(
Expand Down Expand Up @@ -55,6 +59,7 @@ defmodule Sanbase.Billing.SanrNFTSubscriptionTest do
end

test "remove subscription", %{user: user} do
expect(Sanbase.Email.MockMailjetApi, :subscribe, fn _, _ -> :ok end)
end_date = DateTime.shift(DateTime.utc_now(), month: 12) |> DateTime.truncate(:second)
Subscription.NFTSubscription.create_nft_subscription(user.id, :sanr_points_nft, end_date)

Expand Down
76 changes: 43 additions & 33 deletions test/sanbase/email/emails_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Sanbase.EmailsTest do
use Oban.Testing, repo: Sanbase.Repo

import Mock
import Mox
import SanbaseWeb.Graphql.TestHelpers
import Sanbase.Factory
import Sanbase.Email.Template
Expand All @@ -14,6 +15,9 @@ defmodule Sanbase.EmailsTest do
alias Sanbase.StripeApi
alias Sanbase.StripeApiTestResponse

setup :set_mox_from_context
setup :verify_on_exit!

setup_with_mocks([
{StripeApi, [:passthrough],
[
Expand All @@ -38,47 +42,51 @@ defmodule Sanbase.EmailsTest do

describe "schedule emails" do
test "on user registration", context do
expect(Sanbase.Email.MockMailjetApi, :subscribe, fn _, _ -> :ok end)

{:ok, user} = context.not_registered_user |> User.Email.update_email_token()

execute_mutation(build_conn(), email_login_verify_mutation(user))
# FIXME: it works in isolation but jobs are not enqueued when running all tests

# args = %{
# user_id: context.not_registered_user.id,
# vars: %{
# name: context.not_registered_user.username,
# username: context.not_registered_user.username
# }
# }

# assert_enqueued(
# [
# worker: Sanbase.Mailer,
# args: Map.put(args, :template, sign_up_templates()[:welcome_email])
# ],
# 500
# )

# assert_enqueued(
# [
# worker: Sanbase.Mailer,
# args: Map.put(args, :template, sign_up_templates()[:first_education_email]),
# scheduled_at: {days_after(4), delta: 10}
# ],
# 500
# )

# assert_enqueued(
# [
# worker: Sanbase.Mailer,
# args: Map.put(args, :template, sign_up_templates()[:second_education_email]),
# scheduled_at: {days_after(7), delta: 10}
# ],
# 500
# )
args = %{
user_id: context.not_registered_user.id,
vars: %{
name: context.not_registered_user.username,
username: context.not_registered_user.username
}
}

assert_enqueued(
[
worker: Sanbase.Mailer,
args: Map.put(args, :template, sign_up_templates()[:welcome_email])
],
500
)

assert_enqueued(
[
worker: Sanbase.Mailer,
args: Map.put(args, :template, sign_up_templates()[:first_education_email]),
scheduled_at: {days_after(4), delta: 10}
],
500
)

assert_enqueued(
[
worker: Sanbase.Mailer,
args: Map.put(args, :template, sign_up_templates()[:second_education_email]),
scheduled_at: {days_after(7), delta: 10}
],
500
)
end

test "on Sanbase PRO trial started", context do
expect(Sanbase.Email.MockMailjetApi, :subscribe, fn _, _ -> :ok end)

Subscription.create(%{
stripe_id: "123",
user_id: context.user.id,
Expand Down Expand Up @@ -114,6 +122,7 @@ defmodule Sanbase.EmailsTest do
end

test "on Sanbase PRO subscription started", context do
expect(Sanbase.Email.MockMailjetApi, :subscribe, fn _, _ -> :ok end)
query = subscribe_mutation(context.plans.plan_pro_sanbase.id)

execute_mutation(context.conn, query)
Expand All @@ -133,6 +142,7 @@ defmodule Sanbase.EmailsTest do
end

test "on API PRO subscription started", context do
expect(Sanbase.Email.MockMailjetApi, :subscribe, fn _, _ -> :ok end)
query = subscribe_mutation(context.plans.plan_pro.id)

execute_mutation(context.conn, query)
Expand Down
5 changes: 5 additions & 0 deletions test/sanbase/event_bus/kafka_exporter_subscriber_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
defmodule Sanbase.EventBus.KafkaExporterSubscriberTest do
use Sanbase.DataCase, async: false
import Sanbase.Factory
import Mox

setup :set_mox_from_context
setup :verify_on_exit!

test "check kafka message format" do
expect(Sanbase.Email.MockMailjetApi, :subscribe, fn _, _ -> :ok end)
# NOTE: This test is consistently failing for unknown reasons. Remove it for
# now to unblock the other PRs and it will be revised later

Expand Down
44 changes: 15 additions & 29 deletions test/sanbase/notifications/notification_actions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ defmodule Sanbase.Notifications.NotificationActionsTest do

describe "metric_created notification" do
test "creates notification and sends to discord" do
expect(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook,
content,
_opts ->
# Allow the mock to be called multiple times
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, content, _opts ->
assert content =~ "metric A"
assert content =~ "metric B"
assert content =~ "For more information, please visit #changelog"
Expand All @@ -42,8 +41,8 @@ defmodule Sanbase.Notifications.NotificationActionsTest do
end

test "marks discord channel as processed after successful sending" do
Sanbase.Notifications.MockDiscordClient
|> expect(:send_message, fn _webhook, content, _opts ->
# Change expect to stub to allow multiple calls
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, content, _opts ->
assert content =~ "metric A"
:ok
end)
Expand All @@ -66,9 +65,7 @@ defmodule Sanbase.Notifications.NotificationActionsTest do

describe "metric_created email notifications" do
test "sends daily digest email for new metrics" do
expect(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook,
_content,
_opts ->
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, _content, _opts ->
:ok
end)

Expand Down Expand Up @@ -103,9 +100,7 @@ defmodule Sanbase.Notifications.NotificationActionsTest do
end

test "handles email sending failure" do
expect(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook,
_content,
_opts ->
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, _content, _opts ->
:ok
end)

Expand Down Expand Up @@ -135,9 +130,8 @@ defmodule Sanbase.Notifications.NotificationActionsTest do

describe "metric_deleted notification" do
test "creates notifications and sends immediate discord message" do
expect(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook,
content,
_opts ->
# Change expect to stub to allow multiple calls
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, content, _opts ->
assert content =~ "metric A"
assert content =~ "metric B"
:ok
Expand Down Expand Up @@ -179,9 +173,8 @@ defmodule Sanbase.Notifications.NotificationActionsTest do
end

test "sends daily digest email for deleted metrics" do
expect(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook,
_content,
_opts ->
# Change expect to stub to allow multiple calls
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, _content, _opts ->
:ok
end)

Expand Down Expand Up @@ -220,9 +213,7 @@ defmodule Sanbase.Notifications.NotificationActionsTest do

describe "manual notification" do
test "sends notifications to specified channels" do
expect(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook,
content,
_opts ->
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, content, _opts ->
assert content == "Discord message"
:ok
end)
Expand Down Expand Up @@ -261,9 +252,7 @@ defmodule Sanbase.Notifications.NotificationActionsTest do
end

test "only sends to channels with content" do
expect(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook,
content,
_opts ->
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, content, _opts ->
assert content == "Discord only"
:ok
end)
Expand All @@ -289,9 +278,7 @@ defmodule Sanbase.Notifications.NotificationActionsTest do

describe "alert notification" do
test "sends detected alert to discord" do
expect(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook,
content,
_opts ->
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, content, _opts ->
assert content =~ "Metric XYZ"
assert content =~ "Category A"
assert content =~ "Category B"
Expand Down Expand Up @@ -322,9 +309,8 @@ defmodule Sanbase.Notifications.NotificationActionsTest do
end

test "sends resolved alert to discord" do
expect(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook,
content,
_opts ->
# Change expect to stub to allow multiple calls
stub(Sanbase.Notifications.MockDiscordClient, :send_message, fn _webhook, content, _opts ->
assert content =~ "Metric XYZ"
assert content =~ "resolved"
:ok
Expand Down
6 changes: 6 additions & 0 deletions test/sanbase_web/api_call_limit/api_call_limit_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ defmodule SanbaseWeb.ApiCallLimitTest do

import Sanbase.Factory
import SanbaseWeb.Graphql.TestHelpers
import Mox

@remote_ip "91.246.248.228"

setup :set_mox_from_context
setup :verify_on_exit!

setup do
san_user = insert(:user, email: "santiment@santiment.net")
{:ok, san_apikey} = Sanbase.Accounts.Apikey.generate_apikey(san_user)
Expand Down Expand Up @@ -493,6 +497,8 @@ defmodule SanbaseWeb.ApiCallLimitTest do
alias Sanbase.StripeApiTestResponse, as: SATR

test "subscribe while rate limited", context do
expect(Sanbase.Email.MockMailjetApi, :subscribe, fn _, _ -> :ok end)

Sanbase.Mock.prepare_mocks2([
{&StripeApi.create_customer_with_card/2, SATR.create_or_update_customer_resp()},
{&StripeApi.create_subscription/1, SATR.create_subscription_resp()}
Expand Down
Loading

0 comments on commit 758a2fc

Please sign in to comment.