Stage 3.2 + tests
This commit is contained in:
145
test/logic_calendar_tests.erl
Normal file
145
test/logic_calendar_tests.erl
Normal file
@@ -0,0 +1,145 @@
|
||||
-module(logic_calendar_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
setup() ->
|
||||
mnesia:start(),
|
||||
mnesia:create_table(user, [
|
||||
{attributes, record_info(fields, user)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
mnesia:create_table(calendar, [
|
||||
{attributes, record_info(fields, calendar)},
|
||||
{ram_copies, [node()]}
|
||||
]),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
mnesia:delete_table(calendar),
|
||||
mnesia:delete_table(user),
|
||||
mnesia:stop(),
|
||||
ok.
|
||||
|
||||
logic_calendar_test_() ->
|
||||
{foreach,
|
||||
fun setup/0,
|
||||
fun cleanup/1,
|
||||
[
|
||||
{"Create calendar test", fun test_create_calendar/0},
|
||||
{"Get calendar test", fun test_get_calendar/0},
|
||||
{"List calendars test", fun test_list_calendars/0},
|
||||
{"Update calendar test", fun test_update_calendar/0},
|
||||
{"Delete calendar test", fun test_delete_calendar/0},
|
||||
{"Access control test", fun test_access_control/0}
|
||||
]}.
|
||||
|
||||
create_test_user() ->
|
||||
UserId = base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}),
|
||||
User = #user{
|
||||
id = UserId,
|
||||
email = <<"test@example.com">>,
|
||||
password_hash = <<"hash">>,
|
||||
role = user,
|
||||
status = active,
|
||||
created_at = calendar:universal_time(),
|
||||
updated_at = calendar:universal_time()
|
||||
},
|
||||
mnesia:dirty_write(User),
|
||||
UserId.
|
||||
|
||||
test_create_calendar() ->
|
||||
UserId = create_test_user(),
|
||||
Title = <<"Test Calendar">>,
|
||||
Description = <<"Test Description">>,
|
||||
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, Title, Description),
|
||||
?assertEqual(UserId, Calendar#calendar.owner_id),
|
||||
?assertEqual(Title, Calendar#calendar.title),
|
||||
?assertEqual(personal, Calendar#calendar.type).
|
||||
|
||||
test_get_calendar() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Test">>, <<"">>),
|
||||
|
||||
% Владелец имеет доступ
|
||||
case logic_calendar:get_calendar(UserId, Calendar#calendar.id) of
|
||||
{ok, Found} ->
|
||||
?assertEqual(Calendar#calendar.id, Found#calendar.id);
|
||||
Other ->
|
||||
?assert(false, {unexpected_result, Other})
|
||||
end,
|
||||
|
||||
% Другой пользователь не имеет доступа к personal календарю
|
||||
OtherUserId = create_test_user(),
|
||||
?assertMatch({error, access_denied},
|
||||
logic_calendar:get_calendar(OtherUserId, Calendar#calendar.id)),
|
||||
|
||||
% Делаем календарь коммерческим
|
||||
{ok, Commercial} = logic_calendar:update_calendar(UserId, Calendar#calendar.id, [{type, commercial}]),
|
||||
|
||||
% Теперь другой пользователь имеет доступ
|
||||
{ok, _} = logic_calendar:get_calendar(OtherUserId, Commercial#calendar.id).
|
||||
|
||||
test_list_calendars() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, _} = logic_calendar:create_calendar(UserId, <<"Calendar 1">>, <<"">>),
|
||||
{ok, _} = logic_calendar:create_calendar(UserId, <<"Calendar 2">>, <<"">>),
|
||||
|
||||
{ok, Calendars} = logic_calendar:list_calendars(UserId),
|
||||
?assertEqual(2, length(Calendars)).
|
||||
|
||||
test_update_calendar() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Original">>, <<"">>),
|
||||
|
||||
Updates = [{title, <<"Updated">>}, {type, commercial}],
|
||||
{ok, Updated} = logic_calendar:update_calendar(UserId, Calendar#calendar.id, Updates),
|
||||
?assertEqual(<<"Updated">>, Updated#calendar.title),
|
||||
?assertEqual(commercial, Updated#calendar.type),
|
||||
|
||||
% Другой пользователь не может обновить
|
||||
OtherUserId = create_test_user(),
|
||||
?assertMatch({error, access_denied},
|
||||
logic_calendar:update_calendar(OtherUserId, Calendar#calendar.id, Updates)).
|
||||
|
||||
test_delete_calendar() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Test">>, <<"">>),
|
||||
|
||||
{ok, Deleted} = logic_calendar:delete_calendar(UserId, Calendar#calendar.id),
|
||||
?assertEqual(deleted, Deleted#calendar.status),
|
||||
|
||||
% После удаления доступ запрещён
|
||||
?assertMatch({error, access_denied}, logic_calendar:get_calendar(UserId, Calendar#calendar.id)).
|
||||
|
||||
test_access_control() ->
|
||||
OwnerId = create_test_user(),
|
||||
OtherId = create_test_user(),
|
||||
|
||||
% Создаём personal календарь
|
||||
{ok, PersonalCalendar} = logic_calendar:create_calendar(OwnerId, <<"Personal">>, <<"">>),
|
||||
|
||||
% Владелец может редактировать
|
||||
?assert(logic_calendar:can_edit(OwnerId, PersonalCalendar)),
|
||||
|
||||
% Другой пользователь не может редактировать
|
||||
?assertNot(logic_calendar:can_edit(OtherId, PersonalCalendar)),
|
||||
|
||||
% Другой пользователь не может просматривать personal календарь
|
||||
?assertNot(logic_calendar:can_access(OtherId, PersonalCalendar)),
|
||||
|
||||
% Делаем календарь коммерческим
|
||||
{ok, CommercialCalendar} = logic_calendar:update_calendar(OwnerId, PersonalCalendar#calendar.id, [{type, commercial}]),
|
||||
|
||||
% Теперь другой пользователь может просматривать
|
||||
?assert(logic_calendar:can_access(OtherId, CommercialCalendar)),
|
||||
|
||||
% Но всё ещё не может редактировать
|
||||
?assertNot(logic_calendar:can_edit(OtherId, CommercialCalendar)),
|
||||
|
||||
% Замораживаем календарь
|
||||
{ok, Frozen} = core_calendar:update(CommercialCalendar#calendar.id, [{status, frozen}]),
|
||||
|
||||
% После заморозки доступ запрещён всем (кроме владельца для редактирования?)
|
||||
?assertNot(logic_calendar:can_access(OtherId, Frozen)),
|
||||
?assertNot(logic_calendar:can_access(OwnerId, Frozen)).
|
||||
Reference in New Issue
Block a user