From 74f743a6aba9de76edd607b008dab62be23e4304 Mon Sep 17 00:00:00 2001 From: mforesti Date: Wed, 13 Nov 2024 15:38:38 +0100 Subject: [PATCH] fix(tests): add tests for user profiles --- test/repositories_test.dart | 63 ++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/test/repositories_test.dart b/test/repositories_test.dart index bacf6481..2f7213e3 100644 --- a/test/repositories_test.dart +++ b/test/repositories_test.dart @@ -46,27 +46,42 @@ Future<(AuthResponse, T)> runWithUser(RunWithUserCallback callback, {String? email, String? phone}) async { final supabase = supabaseAnonClient(); final user = await signUpWithNewUser(supabase, email: email, phone: phone); - final result = await callback(supabase, user); - return (user, result); + try { + final result = await callback(supabase, user); + return (user, result); + } catch (e) { + supabaseAdmin.auth.admin.deleteUser(user.user!.id); + rethrow; + } } /// Run a closure with a temporary user created just for this and then deleted. -Future runWithTemporaryUser(RunWithUserCallback callback, +Future runWithTemporaryUser(RunWithUserCallback callback, {String? email, String? phone}) async { - final (user, result) = - await runWithUser(callback, email: email, phone: phone); - await supabaseAdmin.auth.admin.deleteUser(user.user!.id); - return result; + try { + final (user, result) = + await runWithUser(callback, email: email, phone: phone); + await supabaseAdmin.auth.admin.deleteUser(user.user!.id); + return result; + } catch (e) { + rethrow; + } } /// Run a closure with a temporary group created just for this and then deleted. -Future runWithTemporaryGroup(RunWithGroupCallback callback) { +Future runWithTemporaryGroup(RunWithGroupCallback callback) { return runWithTemporaryUser((supabase, user) async { final groupsRepository = GroupsRepository(supabase: supabase); final group = await groupsRepository.createGroup(Fake.group()); - final result = await callback(supabase, group, groupsRepository); - await groupsRepository.deleteGroup(group.id); - return result; + late T result; + try { + result = await callback(supabase, group, groupsRepository); + await groupsRepository.deleteGroup(group.id); + return result; + } catch (e) { + await groupsRepository.deleteGroup(group.id); + rethrow; + } }); } @@ -216,6 +231,32 @@ void main() { }), ), ); + + test( + 'users can see their groups and members', + () => runWithTemporaryUser( + (supabase, user) => + runWithTemporaryGroup((supabase, group, groupsRepository) async { + final membersRepository = MembersRepository(supabase: supabase); + + final member = await membersRepository.addMemberToGroup(group.id, + displayName: 'A member'); + + final userGroups = await groupsRepository.getUserGroups(); + expect(userGroups.groups, hasLength(1)); + + final userGroup = userGroups.groups.first; + final members = + userGroups.members.where((m) => m.groupId == userGroup.id); + + expect(userGroup.id, equals(group.id)); + expect(members, hasLength(2)); + + final userMember = members.where((m) => m.id == member.id).first; + expect(userMember.id, equals(member.id)); + }), + ), + ); }); group('invites', () {