def test_accept_invite_to_friend(self): # INVITE TO FRIEND self.invite_to_friend_id_test_accept_invite = user_utils.send_invite( self.sender_invites, self.receiver_invites, InviteType.FRIEND) # check invite id and invite existing self.assertTrue( invite_dao.is_exists(self.invite_to_friend_id_test_accept_invite)) invite_to_friend = invite_dao.get_invite( self.invite_to_friend_id_test_accept_invite) # accept invite user_utils.accept_invite(self.invite_to_friend_id_test_accept_invite) # check invite type self.assertEqual(invite_to_friend.type, InviteType.FRIEND) receiver_user = user_dao.get_user(invite_to_friend.receiver_id) sender_user = user_dao.get_user(invite_to_friend.sender_id) # check that users became friends self.assertIn(invite_to_friend.sender_id, receiver_user.friend_id_list) self.assertIn(invite_to_friend.receiver_id, sender_user.friend_id_list) # check that invite was delete self.assertFalse( invite_dao.is_exists(self.invite_to_friend_id_test_accept_invite)) self.assertNotIn(self.invite_to_friend_id_test_accept_invite, receiver_user.invite_id_list)
def test_create_and_delete_dialog(self): dialog = dialog_utils.create_dialog(self.user_id_1, self.user_id_2) self.assertIsNotNone(dialog) self.dialog_id = dialog.id # check that dialog was create self.assertTrue(chat_dao.dialog_is_exist(dialog.id)) user1 = user_dao.get_user(self.user_id_1) user2 = user_dao.get_user(self.user_id_2) # check that dialog was add to users self.assertIn(dialog.id, user1.chat_id_list) self.assertIn(dialog.id, user2.chat_id_list) dialog_utils.delete_dialog(dialog.id) user1 = user_dao.get_user(self.user_id_1) user2 = user_dao.get_user(self.user_id_2) # check that dialog was remove from users self.assertNotIn(dialog.id, user1.chat_id_list) self.assertNotIn(dialog.id, user2.chat_id_list) # check that dialog was delete self.assertFalse(chat_dao.dialog_is_exist(dialog.id))
def test_send_invite(self): # check that users were create self.assertTrue(user_dao.is_exist_by_id(self.sender_invites)) self.assertTrue(user_dao.is_exist_by_id(self.receiver_invites)) # send invite from user_1 to user_2 in friends self.invite_to_friend_id_test_send = user_utils.send_invite( self.sender_invites, self.receiver_invites, InviteType.FRIEND) # check invite id and invite existing self.assertTrue( invite_dao.is_exists(self.invite_to_friend_id_test_send)) # check that invite is added to user user = user_dao.get_user(self.receiver_invites) self.assertIn(self.invite_to_friend_id_test_send, user.invite_id_list) # send invite from user_1 to user_2 in event self.invite_to_event_id_test_send = user_utils.send_invite( self.sender_invites, self.receiver_invites, InviteType.EVENT, self.group_event_id) # check invite id and invite existing self.assertTrue(invite_dao.is_exists( self.invite_to_event_id_test_send)) # check that invite is added to user user = user_dao.get_user(self.receiver_invites) self.assertIn(self.invite_to_event_id_test_send, user.invite_id_list)
def test_delete_event(self): group_event = GroupEvent("name", True, datetime.today(), "address", "description") group_event.member_id_list.clear() # hz, no pochemuto was not empty # create self.group_event_id_test_delete = user_utils.create_group_event( self.user_id_1, group_event) # add member self.member_id_test_delete_2 = group_event_utils.add_member( self.group_event_id_test_delete, self.user_id_2) self.assertIsNotNone(self.member_id_test_delete_2) group_event = group_event_dao.get(self.group_event_id_test_delete) event_chat = chat_dao.get_event_chat(group_event.chat_id) self.member_id_test_delete_1 = group_event.member_id_list[0] self.assertNotEqual(self.member_id_test_delete_1, self.member_id_test_delete_2) # delete by member without permission on it user_utils.delete_group_event(self.member_id_test_delete_2, group_event.id) # check that group_event already is exist self.assertTrue( group_event_dao.is_exist(self.group_event_id_test_delete)) # delete by member with permission on it user_utils.delete_group_event(self.member_id_test_delete_1, group_event.id) # check that members was remove self.assertFalse( event_member_dao.is_exists(self.member_id_test_delete_1)) self.assertFalse( event_member_dao.is_exists(self.member_id_test_delete_2)) # check that chat was remove self.assertFalse(chat_dao.event_chat_is_exist(event_chat.id)) # check that users were clear user1 = user_dao.get_user(self.user_id_1) self.assertNotIn(event_chat.id, user1.chat_id_list) self.assertNotIn(group_event.id, user1.event_id_list) user2 = user_dao.get_user(self.user_id_2) self.assertNotIn(event_chat.id, user2.chat_id_list) self.assertNotIn(group_event.id, user2.event_id_list) # check that event was remove self.assertFalse(group_event_dao.is_exist(group_event.id))
def get_friend(friend_id): if friend_id is None or not id_is_valid(friend_id): return abort(400) friend = user_dao.get_user(friend_id) if friend is None: return abort(404) return json_util.dumps(friend.to_friend_json()), 200
def test_create_single_event(self): single_event = SingleEvent("name", True, datetime.today(), "address", "descisadsda") self.single_event_id_test_create = user_utils.create_single_event(self.user_id, single_event) self.assertTrue(single_event_dao.is_exist(single_event.id)) user = user_dao.get_user(self.user_id) self.assertIn(single_event.id, user.event_id_list)
def test_create_group_event(self): group_event = GroupEvent("name", True, datetime.today(), "address", "description") user = user_dao.get_user(self.user_id_1) self.group_event_id_test_create = user_utils.create_group_event( user.id, group_event) group_event = group_event_dao.get(self.group_event_id_test_create) # check that group event was create self.assertTrue( group_event_dao.is_exist(self.group_event_id_test_create)) event_member = event_member_dao.get_by_user_event( user.id, group_event.id) # check that event member was create for this user self.assertIsNotNone(event_member) # check count members in event self.assertEqual(len(group_event.member_id_list), 1) # check that member was create self.member_id_test_create = group_event.member_id_list[0] self.assertTrue(event_member_dao.is_exists(self.member_id_test_create)) # check that member was create for this user event_member = event_member_dao.get(self.member_id_test_create) self.assertEqual(event_member.user_id, user.id) # check chat for event self.event_chat_id = group_event.chat_id self.assertIsNotNone(self.event_chat_id) self.assertNotEqual(len(str(self.event_chat_id)), 0) self.assertTrue(chat_dao.event_chat_is_exist(self.event_chat_id)) # check that update user user = user_dao.get_user(self.user_id_1) self.assertEqual(len(user.event_id_list), 1) self.assertEqual(len(user.chat_id_list), 1)
def get_friends(user: User): friend_list = [] for friend_id in user.friend_id_list: friend = user_dao.get_user(friend_id) if friend is None: continue friend_list.append(friend.to_overview_friend_json()) if len(friend_list) == 0: return '', 204 return json_util.dumps(friend_list), 200
def remove_friend(friend_id, user: User): if friend_id is None or not id_is_valid(friend_id): return abort(400) friend = user_dao.get_user(friend_id) if friend is None: return abort(404) success_delete_friends = user_dao.delete_friend(user.id, friend_id) if not user_dao.delete_friend(friend_id, user.id): success_delete_friends = False if not success_delete_friends: return abort(500) return '', 204
def test_leave_event(self): group_event = GroupEvent("name", True, datetime.today(), "address", "description") group_event.member_id_list.clear() # hz, no pochemuto was not empty # create self.group_event_id_test_leave = user_utils.create_group_event( self.user_id_1, group_event) # add member self.member_id_test_leave_2 = group_event_utils.add_member( self.group_event_id_test_leave, self.user_id_2) self.assertIsNotNone(self.member_id_test_leave_2) group_event = group_event_dao.get(self.group_event_id_test_leave) event_chat = chat_dao.get_event_chat(group_event.chat_id) # for clearing after test self.event_chat_id = event_chat.id self.member_id_test_leave_1 = group_event.member_id_list[0] # check that in group 2 members self.assertEqual(len(group_event.member_id_list), 2) user_utils.leave_group_event(self.member_id_test_leave_2, group_event.id) # check that user were clear user2 = user_dao.get_user(self.user_id_2) self.assertNotIn(event_chat.id, user2.chat_id_list) self.assertNotIn(group_event.id, user2.event_id_list) # check that member was remove self.assertFalse( event_member_dao.is_exists(self.member_id_test_delete_1)) # check that member was remove from event group_event = group_event_dao.get(self.group_event_id_test_leave) self.assertNotIn(self.member_id_test_leave_2, group_event.member_id_list) self.member_id_test_leave_1 = group_event.member_id_list[0] user_utils.leave_group_event(self.member_id_test_leave_1, group_event.id) # check that event was remove because this member is last self.assertFalse(group_event_dao.is_exist(group_event.id))
def test_decline_invite(self): self.invite_id_test_decline_invite = user_utils.send_invite( self.sender_invites, self.receiver_invites, InviteType.FRIEND) # check that invite was sent self.assertTrue( invite_dao.is_exists(self.invite_id_test_decline_invite)) user_utils.decline_invite(self.invite_id_test_decline_invite) receiver_user = user_dao.get_user(self.receiver_invites) # check that invite was delete self.assertFalse( invite_dao.is_exists(self.invite_id_test_decline_invite)) self.assertNotIn(self.invite_id_test_decline_invite, receiver_user.invite_id_list)
def get_sorted_chats(user_id): """Return all chats from user sorted by last message datetime :return List<Chat>""" user = user_dao.get_user(user_id) user_chats = [] for chat_id in user.chat_id_list: chat = chat_dao.get_chat(chat_id) if chat is not None: user_chats.append(chat) if len(user_chats) == 0: return [] user_chats.sort(key=lambda x: get_datetime_last_msg(x), reverse=True) return user_chats
def get_event_member(member_id): member = event_member_dao.get(member_id) if not member: return abort(404) user = user_dao.get_user(member.user_id) if not user: return abort(404) return json_util.dumps({ 'user_id': user.id, 'name': user.name, 'email': user.email, 'profile_pic': user.profile_pic, 'member_id': member.id, 'is_can_invite_user': member.is_can_invite_user, 'is_can_delete_user': member.is_can_delete_user, 'is_can_change_event': member.is_can_change_event, 'is_can_delete_event': member.is_can_delete_event, 'event_id': member.event_id })
def test_accept_invite_to_event(self): # INVITE TO EVENT self.invite_to_event_id_test_accept_invite = user_utils.send_invite( self.sender_invites, self.receiver_invites, InviteType.EVENT, self.group_event_id) # check invite id and invite existing self.assertTrue( invite_dao.is_exists(self.invite_to_event_id_test_accept_invite)) invite_to_event = invite_dao.get_invite( self.invite_to_event_id_test_accept_invite) # accept invite user_utils.accept_invite(self.invite_to_event_id_test_accept_invite) # check invite type self.assertEqual(invite_to_event.type, InviteType.EVENT) receiver_user = user_dao.get_user(invite_to_event.receiver_id) event = group_event_dao.get(invite_to_event.event_id) member = event_member_dao.get_by_user_event(receiver_user.id, event.id) # id's for removing after tests self.chat_id = event.chat_id self.member_id = member.id # check that user became event member self.assertIn(event.id, receiver_user.event_id_list) self.assertIn(member.id, event.member_id_list) # check that invite was delete self.assertFalse( invite_dao.is_exists(self.invite_to_event_id_test_accept_invite)) self.assertNotIn(self.invite_to_event_id_test_accept_invite, receiver_user.invite_id_list) self.assertEqual(event_member_dao.delete(member.id), 1)
def load_user(user_id): return user_dao.get_user(user_id)