fix: owner event bookings list and paid plan durations from plan_to_months.
Fixes EventHub/EventHubBack#51 Fixes EventHub/EventHubBack#52 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -27,6 +27,10 @@ core_subscription_test_() ->
|
||||
[
|
||||
{"Create trial subscription", fun test_create_trial/0},
|
||||
{"Create paid subscription", fun test_create_paid/0},
|
||||
{"Create monthly duration", fun test_create_monthly_duration/0},
|
||||
{"Create quarterly duration", fun test_create_quarterly_duration/0},
|
||||
{"Create biannual duration", fun test_create_biannual_duration/0},
|
||||
{"Create annual duration", fun test_create_annual_duration/0},
|
||||
{"Get active by user", fun test_get_active_by_user/0},
|
||||
{"List by user", fun test_list_by_user/0},
|
||||
{"Update status", fun test_update_status/0},
|
||||
@@ -51,6 +55,35 @@ test_create_paid() ->
|
||||
?assertEqual(true, Sub#subscription.trial_used),
|
||||
?assertEqual(active, Sub#subscription.status).
|
||||
|
||||
%% Длительность не зависит от trial_used — только от Plan (месяц ≈ 30 дней).
|
||||
%% add_months считает по календарным дням от полуночи даты старта.
|
||||
assert_plan_duration(Sub, Months) ->
|
||||
{{Y1, M1, D1}, _} = Sub#subscription.started_at,
|
||||
{{Y2, M2, D2}, Time2} = Sub#subscription.expires_at,
|
||||
StartDays = calendar:date_to_gregorian_days({Y1, M1, D1}),
|
||||
EndDays = calendar:date_to_gregorian_days({Y2, M2, D2}),
|
||||
?assertEqual(Months * 30, EndDays - StartDays),
|
||||
?assertEqual({0, 0, 0}, Time2).
|
||||
|
||||
test_create_monthly_duration() ->
|
||||
%% Даже при trial_used=false длительность = 1 месяц, не «trial 30 дней» отдельно
|
||||
{ok, Sub} = core_subscription:create(<<"u-m">>, monthly, false),
|
||||
?assertEqual(false, Sub#subscription.trial_used),
|
||||
assert_plan_duration(Sub, 1).
|
||||
|
||||
test_create_quarterly_duration() ->
|
||||
{ok, Sub} = core_subscription:create(<<"u-q">>, quarterly, false),
|
||||
?assertEqual(false, Sub#subscription.trial_used),
|
||||
assert_plan_duration(Sub, 3).
|
||||
|
||||
test_create_biannual_duration() ->
|
||||
{ok, Sub} = core_subscription:create(<<"u-b">>, biannual, true),
|
||||
assert_plan_duration(Sub, 6).
|
||||
|
||||
test_create_annual_duration() ->
|
||||
{ok, Sub} = core_subscription:create(<<"u-a">>, annual, false),
|
||||
assert_plan_duration(Sub, 12).
|
||||
|
||||
test_get_active_by_user() ->
|
||||
UserId = <<"user123">>,
|
||||
{ok, Sub1} = core_subscription:create(UserId, trial, false),
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-define(TABLES, [user, calendar, event, booking]).
|
||||
-define(TABLES, [user, calendar, event, booking, admin]).
|
||||
|
||||
setup() ->
|
||||
eh_test_support:start_mnesia(),
|
||||
@@ -29,6 +29,8 @@ logic_booking_test_() ->
|
||||
{"Cancel booking by participant", fun test_cancel_booking/0},
|
||||
{"Cancel booking access denied", fun test_cancel_access_denied/0},
|
||||
{"List event bookings", fun test_list_event_bookings/0},
|
||||
{"List event bookings as owner", fun test_list_event_bookings_owner/0},
|
||||
{"List event bookings non-owner denied", fun test_list_event_bookings_non_owner/0},
|
||||
{"List user bookings", fun test_list_user_bookings/0}
|
||||
]}.
|
||||
|
||||
@@ -163,6 +165,31 @@ test_list_event_bookings() ->
|
||||
{ok, Bookings} = logic_booking:list_event_bookings(EventId),
|
||||
?assertEqual(2, length(Bookings)).
|
||||
|
||||
test_list_event_bookings_owner() ->
|
||||
OwnerId = create_test_user(user),
|
||||
Participant1Id = create_test_user(user),
|
||||
Participant2Id = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, manual),
|
||||
EventId = create_test_event(CalendarId),
|
||||
|
||||
{ok, _} = logic_booking:create_booking(Participant1Id, EventId),
|
||||
{ok, _} = logic_booking:create_booking(Participant2Id, EventId),
|
||||
|
||||
{ok, Bookings} = logic_booking:list_event_bookings(OwnerId, EventId),
|
||||
?assertEqual(2, length(Bookings)).
|
||||
|
||||
test_list_event_bookings_non_owner() ->
|
||||
OwnerId = create_test_user(user),
|
||||
ParticipantId = create_test_user(user),
|
||||
StrangerId = create_test_user(user),
|
||||
CalendarId = create_test_calendar(OwnerId, manual),
|
||||
EventId = create_test_event(CalendarId),
|
||||
|
||||
{ok, _} = logic_booking:create_booking(ParticipantId, EventId),
|
||||
|
||||
{error, access_denied} = logic_booking:list_event_bookings(StrangerId, EventId),
|
||||
{error, access_denied} = logic_booking:list_event_bookings(ParticipantId, EventId).
|
||||
|
||||
test_list_user_bookings() ->
|
||||
OwnerId = create_test_user(user),
|
||||
ParticipantId = create_test_user(user),
|
||||
|
||||
@@ -11,6 +11,10 @@ logic_subscription_test_() ->
|
||||
{"Start trial duplicate", fun test_start_trial_duplicate/0},
|
||||
{"Activate subscription (no trial)", fun test_activate_subscription_no_trial/0},
|
||||
{"Activate subscription (after trial)", fun test_activate_subscription_after_trial/0},
|
||||
{"Activate quarterly duration", fun test_activate_quarterly_duration/0},
|
||||
{"Activate monthly duration", fun test_activate_monthly_duration/0},
|
||||
{"Activate biannual duration", fun test_activate_biannual_duration/0},
|
||||
{"Activate annual duration", fun test_activate_annual_duration/0},
|
||||
{"Check subscription - free", fun test_check_free/0},
|
||||
{"Check subscription - trial", fun test_check_trial/0},
|
||||
{"Check subscription - paid", fun test_check_paid/0},
|
||||
@@ -76,6 +80,38 @@ test_activate_subscription_after_trial() ->
|
||||
?assertEqual(monthly, Sub#subscription.plan),
|
||||
?assertEqual(true, Sub#subscription.trial_used).
|
||||
|
||||
%% Длительность при активации (в т.ч. первой платной с trial_used=false) из Plan.
|
||||
assert_plan_duration(Sub, Months) ->
|
||||
{{Y1, M1, D1}, _} = Sub#subscription.started_at,
|
||||
{{Y2, M2, D2}, Time2} = Sub#subscription.expires_at,
|
||||
StartDays = calendar:date_to_gregorian_days({Y1, M1, D1}),
|
||||
EndDays = calendar:date_to_gregorian_days({Y2, M2, D2}),
|
||||
?assertEqual(Months * 30, EndDays - StartDays),
|
||||
?assertEqual({0, 0, 0}, Time2).
|
||||
|
||||
test_activate_monthly_duration() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Sub} = logic_subscription:activate_subscription(UserId, monthly, #{card => "4242"}),
|
||||
?assertEqual(false, Sub#subscription.trial_used),
|
||||
assert_plan_duration(Sub, 1).
|
||||
|
||||
test_activate_quarterly_duration() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Sub} = logic_subscription:activate_subscription(UserId, quarterly, #{card => "4242"}),
|
||||
?assertEqual(quarterly, Sub#subscription.plan),
|
||||
?assertEqual(false, Sub#subscription.trial_used),
|
||||
assert_plan_duration(Sub, 3).
|
||||
|
||||
test_activate_biannual_duration() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Sub} = logic_subscription:activate_subscription(UserId, biannual, #{card => "4242"}),
|
||||
assert_plan_duration(Sub, 6).
|
||||
|
||||
test_activate_annual_duration() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Sub} = logic_subscription:activate_subscription(UserId, annual, #{card => "4242"}),
|
||||
assert_plan_duration(Sub, 12).
|
||||
|
||||
test_check_free() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, free, free} = logic_subscription:check_user_subscription(UserId).
|
||||
|
||||
Reference in New Issue
Block a user