Leon4gr45 commited on
Commit
1fdbee6
·
verified ·
1 Parent(s): 08e9699

Delete test/plausible/billing/billing_test.exs with huggingface_hub

Browse files
test/plausible/billing/billing_test.exs DELETED
@@ -1,690 +0,0 @@
1
- defmodule Plausible.BillingTest do
2
- use Plausible.DataCase
3
- use Bamboo.Test, shared: true
4
- require Plausible.Billing.Subscription.Status
5
- alias Plausible.Billing
6
- alias Plausible.Billing.Subscription
7
- alias Plausible.Repo
8
-
9
- describe "check_needs_to_upgrade" do
10
- @describetag :ee_only
11
-
12
- test "is false for a trial user" do
13
- team = new_user(trial_expiry_date: Date.utc_today()) |> team_of()
14
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team) == :no_upgrade_needed
15
- end
16
-
17
- test "is true for a user with an expired trial" do
18
- team = new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1)) |> team_of()
19
-
20
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team) ==
21
- {:needs_to_upgrade, :no_active_trial_or_subscription}
22
- end
23
-
24
- test "is true for a user with empty trial expiry date" do
25
- assert Plausible.Teams.Billing.check_needs_to_upgrade(nil) ==
26
- {:needs_to_upgrade, :no_active_trial_or_subscription}
27
- end
28
-
29
- test "is false for user with empty trial expiry date but with an active subscription" do
30
- team =
31
- new_user()
32
- |> subscribe_to_growth_plan()
33
- |> team_of()
34
- |> Ecto.Changeset.change(trial_expiry_date: nil)
35
- |> Repo.update!()
36
-
37
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team) == :no_upgrade_needed
38
- end
39
-
40
- test "is false for a user with an expired trial but an active subscription" do
41
- team =
42
- new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1))
43
- |> subscribe_to_growth_plan()
44
- |> team_of()
45
-
46
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team) == :no_upgrade_needed
47
- end
48
-
49
- test "is false for a user with a cancelled subscription IF the billing cycle isn't completed yet" do
50
- team =
51
- new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1))
52
- |> subscribe_to_growth_plan(
53
- status: Subscription.Status.deleted(),
54
- next_bill_date: Date.utc_today()
55
- )
56
- |> team_of()
57
-
58
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team) == :no_upgrade_needed
59
- end
60
-
61
- test "is true for a user with a cancelled subscription IF the billing cycle is complete" do
62
- team =
63
- new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1))
64
- |> subscribe_to_growth_plan(
65
- status: Subscription.Status.deleted(),
66
- next_bill_date: Date.shift(Date.utc_today(), day: -1)
67
- )
68
- |> team_of()
69
-
70
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team) ==
71
- {:needs_to_upgrade, :no_active_trial_or_subscription}
72
- end
73
-
74
- test "is true for a deleted subscription if no next_bill_date specified" do
75
- team =
76
- new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1))
77
- |> subscribe_to_growth_plan(
78
- status: Subscription.Status.deleted(),
79
- next_bill_date: nil
80
- )
81
- |> team_of()
82
-
83
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team) ==
84
- {:needs_to_upgrade, :no_active_trial_or_subscription}
85
- end
86
-
87
- test "needs to upgrade if subscription active and grace period ended when usage still over limits" do
88
- user =
89
- new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1))
90
- |> subscribe_to_growth_plan(
91
- status: Subscription.Status.deleted(),
92
- next_bill_date: Date.utc_today()
93
- )
94
-
95
- team = user |> team_of() |> Repo.reload!() |> Plausible.Teams.end_grace_period()
96
-
97
- over_limits_usage_stub = monthly_pageview_usage_stub(100_000_000, 100_000_000)
98
-
99
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team, over_limits_usage_stub) ==
100
- {:needs_to_upgrade, :grace_period_ended}
101
- end
102
-
103
- test "no upgrade needed if subscription active and grace period ended but usage below limits" do
104
- user =
105
- new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1))
106
- |> subscribe_to_growth_plan(
107
- status: Subscription.Status.deleted(),
108
- next_bill_date: Date.utc_today()
109
- )
110
-
111
- team = user |> team_of() |> Repo.reload!() |> Plausible.Teams.end_grace_period()
112
-
113
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team, Plausible.Teams.Billing) ==
114
- :no_upgrade_needed
115
- end
116
- end
117
-
118
- @subscription_id "subscription-123"
119
- @plan_id_10k "654177"
120
- @plan_id_100k "654178"
121
-
122
- @subscription_created_params %{
123
- "event_time" => "2019-05-01 01:03:52",
124
- "alert_name" => "subscription_created",
125
- "passthrough" => "",
126
- "email" => "",
127
- "subscription_id" => @subscription_id,
128
- "subscription_plan_id" => @plan_id_10k,
129
- "update_url" => "update_url.com",
130
- "cancel_url" => "cancel_url.com",
131
- "status" => "active",
132
- "next_bill_date" => "2019-06-01",
133
- "unit_price" => "6.00",
134
- "currency" => "EUR"
135
- }
136
-
137
- @subscription_updated_params %{
138
- "alert_name" => "subscription_updated",
139
- "passthrough" => "",
140
- "subscription_id" => "",
141
- "subscription_plan_id" => @plan_id_10k,
142
- "update_url" => "update_url.com",
143
- "cancel_url" => "cancel_url.com",
144
- "old_status" => "active",
145
- "status" => "active",
146
- "next_bill_date" => "2019-06-01",
147
- "new_unit_price" => "12.00",
148
- "currency" => "EUR"
149
- }
150
-
151
- describe "subscription_created" do
152
- test "fails on callback without passthrough" do
153
- _user = new_user()
154
-
155
- assert_raise RuntimeError, ~r/Missing passthrough/, fn ->
156
- @subscription_created_params
157
- |> Map.delete("passthrough")
158
- |> Billing.subscription_created()
159
- end
160
- end
161
-
162
- test "fails on callback without valid passthrough" do
163
- _user = new_user()
164
-
165
- assert_raise RuntimeError, ~r/Invalid passthrough sent via Paddle/, fn ->
166
- %{@subscription_created_params | "passthrough" => "invalid"}
167
- |> Billing.subscription_created()
168
- end
169
-
170
- assert_raise RuntimeError, ~r/Invalid passthrough sent via Paddle/, fn ->
171
- %{@subscription_created_params | "passthrough" => "ee:true;user:invalid"}
172
- |> Billing.subscription_created()
173
- end
174
-
175
- assert_raise RuntimeError, ~r/Invalid passthrough sent via Paddle/, fn ->
176
- %{@subscription_created_params | "passthrough" => "ee:true;user:123;team:invalid"}
177
- |> Billing.subscription_created()
178
- end
179
-
180
- assert_raise RuntimeError, ~r/Invalid passthrough sent via Paddle/, fn ->
181
- %{
182
- @subscription_created_params
183
- | "passthrough" => "ee:true;user:123;team:456;some:invalid"
184
- }
185
- |> Billing.subscription_created()
186
- end
187
- end
188
-
189
- test "fails on callback with non-existent user" do
190
- user = new_user()
191
- Repo.delete!(user)
192
-
193
- assert_raise Ecto.NoResultsError, fn ->
194
- %{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id}"}
195
- |> Billing.subscription_created()
196
- end
197
- end
198
-
199
- test "fails on callback with non-existent team" do
200
- user = new_user()
201
- {:ok, team} = Plausible.Teams.get_or_create(user)
202
- Repo.delete!(team)
203
-
204
- assert_raise Ecto.NoResultsError, fn ->
205
- %{
206
- @subscription_created_params
207
- | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
208
- }
209
- |> Billing.subscription_created()
210
- end
211
- end
212
-
213
- test "creates a subscription with teams passthrough" do
214
- user = new_user()
215
- {:ok, team} = Plausible.Teams.get_or_create(user)
216
-
217
- %{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"}
218
- |> Billing.subscription_created()
219
-
220
- subscription =
221
- user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
222
-
223
- assert subscription.paddle_subscription_id == @subscription_id
224
- assert subscription.next_bill_date == ~D[2019-06-01]
225
- assert subscription.last_bill_date == ~D[2019-05-01]
226
- assert subscription.next_bill_amount == "6.00"
227
- assert subscription.currency_code == "EUR"
228
- end
229
-
230
- test "identical notification sent twice is ignored at 2nd try" do
231
- user = new_user()
232
- {:ok, team} = Plausible.Teams.get_or_create(user)
233
-
234
- Plausible.Test.Support.Sentry.setup(self())
235
-
236
- Billing.subscription_created(%{
237
- @subscription_created_params
238
- | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
239
- })
240
-
241
- Billing.subscription_created(%{
242
- @subscription_created_params
243
- | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
244
- })
245
-
246
- subscription =
247
- user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
248
-
249
- assert subscription.paddle_subscription_id == @subscription_id
250
- assert subscription.next_bill_date == ~D[2019-06-01]
251
- assert subscription.last_bill_date == ~D[2019-05-01]
252
- assert subscription.next_bill_amount == "6.00"
253
- assert subscription.currency_code == "EUR"
254
-
255
- assert [] = Sentry.Test.pop_sentry_reports()
256
- end
257
-
258
- test "secondary notification sent logs diff" do
259
- user = new_user()
260
- {:ok, team} = Plausible.Teams.get_or_create(user)
261
-
262
- Plausible.Test.Support.Sentry.setup(self())
263
-
264
- Billing.subscription_created(%{
265
- @subscription_created_params
266
- | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
267
- })
268
-
269
- Billing.subscription_created(%{
270
- @subscription_created_params
271
- | "subscription_plan_id" => "9999",
272
- "currency" => "USD",
273
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
274
- })
275
-
276
- assert [report] = Sentry.Test.pop_sentry_reports()
277
-
278
- assert report.message.formatted =~
279
- "Duplicate subscription_created webhook for paddle_subscription_id=subscription-123"
280
-
281
- assert report.extra.paddle_subscription_id == @subscription_id
282
- assert report.extra.team_id == team.id
283
- assert report.extra.diff.paddle_plan_id == %{existing: "654177", incoming: "9999"}
284
- assert report.extra.diff.currency_code == %{existing: "EUR", incoming: "USD"}
285
-
286
- subscription =
287
- user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
288
-
289
- assert subscription.paddle_subscription_id == @subscription_id
290
- assert subscription.currency_code == "EUR"
291
- end
292
-
293
- test "supports user without a team case" do
294
- user = new_user()
295
-
296
- %{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id}"}
297
- |> Billing.subscription_created()
298
-
299
- subscription =
300
- user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
301
-
302
- assert subscription.paddle_subscription_id == @subscription_id
303
- assert subscription.next_bill_date == ~D[2019-06-01]
304
- assert subscription.last_bill_date == ~D[2019-05-01]
305
- assert subscription.next_bill_amount == "6.00"
306
- assert subscription.currency_code == "EUR"
307
- end
308
-
309
- test "unlocks sites if user has any locked sites" do
310
- user = new_user()
311
- site = new_site(owner: user)
312
- site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
313
- team = team_of(user)
314
-
315
- %{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"}
316
- |> Billing.subscription_created()
317
-
318
- refute Repo.reload!(team).locked
319
- end
320
-
321
- @tag :ee_only
322
- test "updates accept_traffic_until" do
323
- user = new_user()
324
- new_site(owner: user)
325
- team = team_of(user)
326
-
327
- %{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"}
328
- |> Billing.subscription_created()
329
-
330
- next_bill = Date.from_iso8601!(@subscription_created_params["next_bill_date"])
331
-
332
- assert Repo.reload!(team_of(user)).accept_traffic_until ==
333
- Date.add(next_bill, 30)
334
- end
335
-
336
- test "sets user.allow_next_upgrade_override field to false" do
337
- user = new_user(team: [allow_next_upgrade_override: true])
338
- team = team_of(user)
339
-
340
- %{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"}
341
- |> Billing.subscription_created()
342
-
343
- refute Repo.reload!(team_of(user)).allow_next_upgrade_override
344
- end
345
-
346
- test "if user upgraded to an enterprise plan, their API key limits are automatically adjusted" do
347
- user = new_user()
348
-
349
- subscribe_to_enterprise_plan(user,
350
- hourly_api_request_limit: 10_000,
351
- paddle_plan_id: @plan_id_10k
352
- )
353
-
354
- team = team_of(user)
355
-
356
- _api_key = insert(:api_key, user: user)
357
-
358
- assert team.hourly_api_request_limit != 10_000
359
-
360
- %{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"}
361
- |> Billing.subscription_created()
362
-
363
- assert Repo.reload!(team).hourly_api_request_limit == 10_000
364
- end
365
- end
366
-
367
- describe "subscription_updated" do
368
- test "updates an existing subscription" do
369
- user = new_user()
370
- subscribe_to_growth_plan(user)
371
- team = team_of(user)
372
-
373
- @subscription_updated_params
374
- |> Map.merge(%{
375
- "subscription_id" => subscription_of(user).paddle_subscription_id,
376
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
377
- })
378
- |> Billing.subscription_updated()
379
-
380
- subscription = subscription_of(user)
381
- assert subscription.paddle_plan_id == @plan_id_10k
382
- assert subscription.next_bill_amount == "12.00"
383
- end
384
-
385
- test "updates subscription with user/team passthrough" do
386
- user = new_user()
387
- subscribe_to_growth_plan(user)
388
- team = team_of(user)
389
-
390
- subscription_id = subscription_of(user).paddle_subscription_id
391
-
392
- @subscription_updated_params
393
- |> Map.merge(%{
394
- "next_bill_date" => "2021-01-01",
395
- "subscription_id" => subscription_id,
396
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
397
- })
398
- |> Billing.subscription_updated()
399
-
400
- subscription =
401
- user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
402
-
403
- assert subscription.next_bill_date == ~D[2021-01-01]
404
- end
405
-
406
- test "status update from 'paused' to 'past_due' is ignored" do
407
- user = new_user()
408
- subscribe_to_growth_plan(user, status: Subscription.Status.paused())
409
- team = team_of(user)
410
-
411
- %{@subscription_updated_params | "old_status" => "paused", "status" => "past_due"}
412
- |> Map.merge(%{
413
- "subscription_id" => subscription_of(user).paddle_subscription_id,
414
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
415
- })
416
- |> Billing.subscription_updated()
417
-
418
- assert subscription_of(user).status == Subscription.Status.paused()
419
- end
420
-
421
- test "unlocks sites if subscription is changed from past_due to active" do
422
- user = new_user()
423
- subscribe_to_growth_plan(user, status: Subscription.Status.past_due())
424
- site = new_site(owner: user)
425
- site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
426
- team = team_of(user)
427
-
428
- @subscription_updated_params
429
- |> Map.merge(%{
430
- "subscription_id" => subscription_of(user).paddle_subscription_id,
431
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}",
432
- "old_status" => "past_due"
433
- })
434
- |> Billing.subscription_updated()
435
-
436
- refute Repo.reload!(team).locked
437
- end
438
-
439
- @tag :ee_only
440
- test "updates accept_traffic_until" do
441
- user = new_user() |> subscribe_to_growth_plan()
442
- team = team_of(user)
443
-
444
- @subscription_updated_params
445
- |> Map.merge(%{
446
- "subscription_id" => subscription_of(user).paddle_subscription_id,
447
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
448
- })
449
- |> Billing.subscription_updated()
450
-
451
- next_bill = Date.from_iso8601!(@subscription_updated_params["next_bill_date"])
452
-
453
- assert Repo.reload!(team_of(user)).accept_traffic_until ==
454
- Date.add(next_bill, 30)
455
- end
456
-
457
- test "sets user.allow_next_upgrade_override field to false" do
458
- user = new_user(team: [allow_next_upgrade_override: true])
459
- subscribe_to_growth_plan(user)
460
- team = team_of(user)
461
-
462
- assert Repo.reload!(team_of(user)).allow_next_upgrade_override
463
-
464
- subscription_id = subscription_of(user).paddle_subscription_id
465
-
466
- @subscription_updated_params
467
- |> Map.merge(%{
468
- "subscription_id" => subscription_id,
469
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
470
- })
471
- |> Billing.subscription_updated()
472
-
473
- refute Repo.reload!(team_of(user)).allow_next_upgrade_override
474
- end
475
-
476
- test "if user upgraded to an enterprise plan, their API key limits are automatically adjusted" do
477
- user =
478
- new_user()
479
- |> subscribe_to_enterprise_plan(
480
- paddle_plan_id: "new-plan-id",
481
- hourly_api_request_limit: 10_000
482
- )
483
-
484
- team = team_of(user)
485
-
486
- _api_key = insert(:api_key, user: user)
487
-
488
- assert team.hourly_api_request_limit != 10_000
489
-
490
- @subscription_updated_params
491
- |> Map.merge(%{
492
- "subscription_id" => subscription_of(user).paddle_subscription_id,
493
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}",
494
- "subscription_plan_id" => "new-plan-id"
495
- })
496
- |> Billing.subscription_updated()
497
-
498
- assert Repo.reload!(team).hourly_api_request_limit == 10_000
499
- end
500
-
501
- test "if teams's grace period has ended, upgrading will unlock sites and remove grace period" do
502
- grace_period = %Plausible.Teams.GracePeriod{
503
- end_date: Date.shift(Date.utc_today(), day: -1)
504
- }
505
-
506
- user = new_user(team: [grace_period: grace_period])
507
-
508
- subscribe_to_growth_plan(user)
509
-
510
- team = team_of(user)
511
-
512
- site = new_site(owner: user)
513
- site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
514
-
515
- subscription_id = subscription_of(user).paddle_subscription_id
516
-
517
- @subscription_updated_params
518
- |> Map.merge(%{
519
- "subscription_id" => subscription_id,
520
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}",
521
- "subscription_plan_id" => @plan_id_100k
522
- })
523
- |> Billing.subscription_updated()
524
-
525
- assert Repo.reload!(team).locked == false
526
- assert Repo.reload!(team).grace_period == nil
527
- end
528
-
529
- test "ignores if subscription cannot be found" do
530
- user = insert(:user)
531
- _site = new_site(owner: user)
532
- team = team_of(user)
533
-
534
- res =
535
- @subscription_updated_params
536
- |> Map.merge(%{
537
- "subscription_id" => "666",
538
- "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"
539
- })
540
- |> Billing.subscription_updated()
541
-
542
- assert {:ok, nil} = res
543
- end
544
- end
545
-
546
- describe "subscription_cancelled" do
547
- test "sets the status to deleted" do
548
- user = new_user()
549
- subscribe_to_growth_plan(user, status: Subscription.Status.active())
550
-
551
- subscription_id = subscription_of(user).paddle_subscription_id
552
-
553
- Billing.subscription_cancelled(%{
554
- "alert_name" => "subscription_cancelled",
555
- "subscription_id" => subscription_id,
556
- "status" => "deleted"
557
- })
558
-
559
- assert user
560
- |> team_of()
561
- |> Repo.reload!()
562
- |> Plausible.Teams.with_subscription()
563
- |> Map.fetch!(:subscription)
564
- |> Subscription.Status.deleted?()
565
- end
566
-
567
- test "ignores if the subscription cannot be found" do
568
- res =
569
- Billing.subscription_cancelled(%{
570
- "alert_name" => "subscription_cancelled",
571
- "subscription_id" => "some_nonexistent_id",
572
- "status" => "deleted"
573
- })
574
-
575
- assert res == {:ok, nil}
576
- end
577
-
578
- test "sends an email to confirm cancellation" do
579
- user = new_user()
580
- subscribe_to_growth_plan(user, status: Subscription.Status.active())
581
-
582
- team = team_of(user)
583
- billing_member = new_user()
584
- add_member(team, user: billing_member, role: :billing)
585
-
586
- Billing.subscription_cancelled(%{
587
- "alert_name" => "subscription_cancelled",
588
- "subscription_id" => subscription_of(user).paddle_subscription_id,
589
- "status" => "deleted"
590
- })
591
-
592
- assert_email_delivered_with(
593
- to: [nil: user.email],
594
- subject: "Mind sharing your thoughts on Plausible?"
595
- )
596
-
597
- assert_email_delivered_with(
598
- to: [nil: billing_member.email],
599
- subject: "Mind sharing your thoughts on Plausible?"
600
- )
601
- end
602
- end
603
-
604
- describe "subscription_payment_succeeded" do
605
- @tag :ee_only
606
- test "updates accept_traffic_until" do
607
- user = new_user() |> subscribe_to_growth_plan()
608
-
609
- subscription_id = subscription_of(user).paddle_subscription_id
610
-
611
- Billing.subscription_payment_succeeded(%{
612
- "alert_name" => "subscription_payment_succeeded",
613
- "subscription_id" => subscription_id
614
- })
615
-
616
- team = user |> team_of() |> Repo.reload!() |> Plausible.Teams.with_subscription()
617
- assert team.accept_traffic_until == Date.add(team.subscription.next_bill_date, 30)
618
- end
619
-
620
- test "sets the next bill amount and date, last bill date" do
621
- user = new_user() |> subscribe_to_growth_plan()
622
-
623
- Billing.subscription_payment_succeeded(%{
624
- "alert_name" => "subscription_payment_succeeded",
625
- "subscription_id" => subscription_of(user).paddle_subscription_id
626
- })
627
-
628
- subscription = subscription_of(user)
629
- assert subscription.next_bill_date == ~D[2019-07-10]
630
- assert subscription.next_bill_amount == "6.00"
631
- assert subscription.last_bill_date == ~D[2019-06-10]
632
- end
633
-
634
- test "ignores if the subscription cannot be found" do
635
- res =
636
- Billing.subscription_payment_succeeded(%{
637
- "alert_name" => "subscription_payment_succeeded",
638
- "subscription_id" => "nonexistent_subscription_id",
639
- "next_bill_date" => Date.shift(Date.utc_today(), day: 30),
640
- "unit_price" => "12.00"
641
- })
642
-
643
- assert res == {:ok, nil}
644
- end
645
- end
646
-
647
- describe "change_plan" do
648
- test "sets the next bill amount and date" do
649
- team = new_user() |> subscribe_to_growth_plan() |> team_of()
650
-
651
- Plausible.Teams.Billing.change_plan(team, "123123")
652
-
653
- subscription = subscription_of(team)
654
- assert subscription.paddle_plan_id == "123123"
655
- assert subscription.next_bill_date == ~D[2019-07-10]
656
- assert subscription.next_bill_amount == "6.00"
657
- end
658
- end
659
-
660
- test "active_subscription_for/1 returns active subscription" do
661
- active_team =
662
- new_user()
663
- |> subscribe_to_growth_plan(status: Subscription.Status.active())
664
- |> team_of()
665
- |> Plausible.Teams.with_subscription()
666
-
667
- paused_team =
668
- new_user()
669
- |> subscribe_to_growth_plan(status: Subscription.Status.paused())
670
- |> team_of()
671
-
672
- assert Plausible.Teams.Billing.active_subscription_for(active_team).id ==
673
- active_team.subscription.id
674
-
675
- assert Plausible.Teams.Billing.active_subscription_for(paused_team) == nil
676
- assert Plausible.Teams.Billing.active_subscription_for(nil) == nil
677
- end
678
-
679
- test "has_active_subscription?/1 returns whether the user has an active subscription" do
680
- active_team =
681
- new_user() |> subscribe_to_growth_plan(status: Subscription.Status.active()) |> team_of()
682
-
683
- paused_team =
684
- new_user() |> subscribe_to_growth_plan(status: Subscription.Status.paused()) |> team_of()
685
-
686
- assert Plausible.Teams.Billing.has_active_subscription?(active_team)
687
- refute Plausible.Teams.Billing.has_active_subscription?(paused_team)
688
- refute Plausible.Teams.Billing.has_active_subscription?(nil)
689
- end
690
- end