feat(calendar): validate org defaults in calendar.settings
Documented keys default_location, default_duration_minutes, default_recurrence; unknown keys (week_patterns) passthrough; invalid known shape returns 400. Refs EventHub/EventHubBack#63
This commit is contained in:
@@ -37,6 +37,8 @@ logic_calendar_test_() ->
|
||||
{"Update calendar test", fun test_update_calendar/0},
|
||||
{"Persist calendar settings (week_patterns)", fun test_update_settings/0},
|
||||
{"Reject non-map settings", fun test_update_settings_invalid/0},
|
||||
{"Org defaults settings round-trip", fun test_update_settings_org_defaults/0},
|
||||
{"Reject invalid default_location shape", fun test_update_settings_invalid_location/0},
|
||||
{"Delete calendar test", fun test_delete_calendar/0},
|
||||
{"Access control test", fun test_access_control/0},
|
||||
{"Ensure default calendar test", fun test_ensure_default_calendar/0}
|
||||
@@ -152,6 +154,66 @@ test_update_settings_invalid() ->
|
||||
UserId, Calendar#calendar.id, [{settings, [<<"not-a-map">>]}]),
|
||||
?assertEqual(#{}, Same#calendar.settings).
|
||||
|
||||
test_update_settings_org_defaults() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Salon">>, <<"">>, manual),
|
||||
Patterns = [
|
||||
#{
|
||||
<<"id">> => <<"p1">>,
|
||||
<<"name">> => <<"Weekdays">>,
|
||||
<<"slots">> => [
|
||||
#{<<"weekday">> => 1, <<"time">> => <<"10:00">>, <<"duration">> => 60}
|
||||
]
|
||||
}
|
||||
],
|
||||
Settings = #{
|
||||
<<"week_patterns">> => Patterns,
|
||||
<<"default_location">> => #{
|
||||
<<"address">> => <<"ул. Пример, 1">>,
|
||||
<<"lat">> => 55.75,
|
||||
<<"lon">> => 37.61
|
||||
},
|
||||
<<"default_duration_minutes">> => 90,
|
||||
<<"default_recurrence">> => #{
|
||||
<<"enabled">> => true,
|
||||
<<"freq">> => <<"WEEKLY">>,
|
||||
<<"interval">> => 1
|
||||
}
|
||||
},
|
||||
{ok, Updated} = logic_calendar:update_calendar(
|
||||
UserId, Calendar#calendar.id, [{settings, Settings}]),
|
||||
?assertEqual(Settings, Updated#calendar.settings),
|
||||
{ok, Reloaded} = logic_calendar:get_calendar(UserId, Calendar#calendar.id),
|
||||
?assertEqual(Settings, Reloaded#calendar.settings),
|
||||
%% null clears recurrence default
|
||||
Settings2 = Settings#{<<"default_recurrence">> => null},
|
||||
{ok, Updated2} = logic_calendar:update_calendar(
|
||||
UserId, Calendar#calendar.id, [{settings, Settings2}]),
|
||||
?assertEqual(null, maps:get(<<"default_recurrence">>, Updated2#calendar.settings)).
|
||||
|
||||
test_update_settings_invalid_location() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Salon">>, <<"">>, manual),
|
||||
?assertMatch({error, {invalid_settings, <<"default_location">>}},
|
||||
logic_calendar:update_calendar(UserId, Calendar#calendar.id, [
|
||||
{settings, #{<<"default_location">> => #{<<"address">> => <<"">>}}}
|
||||
])),
|
||||
?assertMatch({error, {invalid_settings, <<"default_location">>}},
|
||||
logic_calendar:update_calendar(UserId, Calendar#calendar.id, [
|
||||
{settings, #{<<"default_location">> => #{<<"address">> => <<"X">>, <<"lat">> => 1}}}
|
||||
])),
|
||||
?assertMatch({error, {invalid_settings, <<"default_duration_minutes">>}},
|
||||
logic_calendar:update_calendar(UserId, Calendar#calendar.id, [
|
||||
{settings, #{<<"default_duration_minutes">> => 0}}
|
||||
])),
|
||||
?assertEqual(#{},
|
||||
begin
|
||||
{ok, Same} = logic_calendar:get_calendar(UserId, Calendar#calendar.id),
|
||||
Same#calendar.settings
|
||||
end),
|
||||
?assertMatch({error, {invalid_settings, <<"default_location">>}},
|
||||
logic_calendar:normalize_settings(#{<<"default_location">> => <<"street">>})).
|
||||
|
||||
test_delete_calendar() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Test">>, <<"">>, manual),
|
||||
|
||||
Reference in New Issue
Block a user