def test_user_teams_for_user_with_two_team_returns_full_list(self): data = MagicMock() '''Mocked outputs''' user = User(user_id=0) teams = [Team(name="TEST1"), Team(name="TEST2")] sys.modules[ "models.authentication"].Authenticator.authenticate.return_value = user sys.modules[ "daos.teams"].TeamDatabaseClient.get_user_teams_by_user_id.return_value = teams response = UserService.teams_for_user(data) self.assertIsInstance(response, SuccessfulTeamsListResponse) self.assertEqual(2, len(response.teams))
def test_notify_team_invitation_to_non_registered_user_does_not_send_notification( self): invitation = MagicMock() inviter_id = MagicMock() '''Mocked outputs''' inviter_user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") team = Team(team_id=0, name="Test-Team") def send_notification(topic_name, message_title, message_body, data_message): from tests.test_services import test_notifications MockedNotificationServer.notification = Notification( topic_name, message_title, message_body, data_message) sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = inviter_user sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_email.return_value = None sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_team_invitation(invitation, inviter_id) self.assertIsNone(MockedNotificationServer.notification)
def test_user_added_to_team_with_welcome_message_does_not_receives_titos_welcome( self): user_id = 1 team_id = 0 '''Mocked outputs''' tito = Bot(bot_id=0, name="tito", callback=None, token=None) team = Team(team_id=team_id, name="Test-Team", welcome_message="Helloooooou!") def post(url, json, headers): from tests.test_services import test_bots MockedBotDatabase.posted_json = json sys.modules[ "daos.bots"].BotDatabaseClient.get_bot_by_id.return_value = tito sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules["requests"].post = MagicMock(side_effect=post) BotService.tito_welcome(user_id, team_id) self.assertIsNotNone(MockedBotDatabase.posted_json) self.assertEqual(0, MockedBotDatabase.posted_json.get("team_id")) self.assertEqual(1, MockedBotDatabase.posted_json.get("user_id")) self.assertEqual("welcome-user", MockedBotDatabase.posted_json.get("params")) self.assertEqual("Helloooooou!", MockedBotDatabase.posted_json.get("message"))
def to_team(cls, team_entry): return Team(team_id=team_entry.team_id, name=team_entry.team_name, picture=team_entry.picture, location=team_entry.location, description=team_entry.description, welcome_message=team_entry.welcome_message ) if team_entry is not None else None
def test_notify_channel_message_from_user_sends_notification(self): message = Message(sender_id=0, receiver_id=1, team_id=0, content="Sarasa", send_type=SendMessageType.DIRECT.value, message_type=MessageType.TEXT.value) is_user_receiver = False '''Mocked outputs''' user_sender = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") channel = Channel(channel_id=0, team_id=0, name="Test-Channel", creator=ChannelCreator(0, "Tester0", "Test0", "Test0")) team = Team(team_id=0, name="Test-Team") def send_notification(topic_name, message_title, message_body, data_message): from tests.test_services import test_notifications MockedNotificationServer.notification = Notification( topic_name, message_title, message_body, data_message) return {"failure": 0} sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user_sender sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = channel sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_message(message, is_user_receiver) self.assertEqual(1, MockedNotificationServer.notification.topic_name) self.assertEqual("Hypechat", MockedNotificationServer.notification.message_title) self.assertEqual("You receive a channel message!", MockedNotificationServer.notification.message_body) self.assertEqual( "Test-Team", MockedNotificationServer.notification.data_message.get( "team_name")) self.assertEqual( "Test-Channel", MockedNotificationServer.notification.data_message.get( "channel_name")) self.assertEqual( 0, MockedNotificationServer.notification.data_message.get( "sender_id")) self.assertEqual( NotificationType.MESSAGE.value, MockedNotificationServer.notification.data_message.get( "notification_type"))
def test_notify_channel_invitation_sends_notification(self): user_channel = ChannelUser(user_id=1, channel_id=0) inviter_id = 0 '''Mocked outputs''' inviter_user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") channel = Channel(channel_id=0, team_id=0, name="Test-Channel", creator=ChannelCreator(0, "Tester0", "Test0", "Test0")) team = Team(team_id=0, name="Test-Team") def send_notification(topic_name, message_title, message_body, data_message): from tests.test_services import test_notifications MockedNotificationServer.notification = Notification( topic_name, message_title, message_body, data_message) return {"failure": 0} sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = inviter_user sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = channel sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_channel_invitation(user_channel, inviter_id) self.assertEqual(1, MockedNotificationServer.notification.topic_name) self.assertEqual("Hypechat", MockedNotificationServer.notification.message_title) self.assertEqual( "You have been added to channel Test-Channel in team Test-Team!", MockedNotificationServer.notification.message_body) self.assertEqual( "Test-Team", MockedNotificationServer.notification.data_message.get( "team_name")) self.assertEqual( "Test-Channel", MockedNotificationServer.notification.data_message.get( "channel_name")) self.assertEqual( 0, MockedNotificationServer.notification.data_message.get( "inviter_id")) self.assertEqual( NotificationType.CHANNEL_INVITATION.value, MockedNotificationServer.notification.data_message.get( "notification_type"))
def to_teams(cls, team_entries): teams = [] for team_entry in team_entries: teams += [ Team(team_id=team_entry.team_id, name=team_entry.team_name, picture=team_entry.picture, location=team_entry.location, description=team_entry.description, welcome_message=team_entry.welcome_message, role=team_entry.role) ] return teams
def test_user_profile_with_two_teams_returns_full_list(self): data = MagicMock() '''Mocked outputs''' user = User(user_id=0, username="******") teams = [Team(team_id=1, name="TEST1"), Team(team_id=2, name="TEST2")] user_with_teams1 = MagicMock() user_with_teams1.id = 0 user_with_teams1.username = "******" user_with_teams1.team_id = 1 user_with_teams1.team_name = "TEST1" user_with_teams1.team_role = "CREATOR" user_with_teams2 = MagicMock() user_with_teams2.id = 0 user_with_teams2.username = "******" user_with_teams2.team_id = 2 user_with_teams2.team_name = "TEST2" user_with_teams2.team_role = "MEMBER" user_with_teams = [user_with_teams1, user_with_teams2] sys.modules[ "models.authentication"].Authenticator.authenticate.return_value = user sys.modules[ "daos.teams"].TeamDatabaseClient.get_user_teams_by_user_id.return_value = teams sys.modules[ "daos.users"].UserDatabaseClient.get_user_profile.return_value = user_with_teams response = UserService.user_profile(data) self.assertEqual(0, response.client.id) self.assertEqual("TEST", response.client.username) self.assertEqual(2, len(response.client.teams)) self.assertEqual(1, response.client.teams[0]["id"]) self.assertEqual("TEST1", response.client.teams[0]["name"]) self.assertEqual("CREATOR", response.client.teams[0]["role"]) self.assertEqual(2, response.client.teams[1]["id"]) self.assertEqual("TEST2", response.client.teams[1]["name"]) self.assertEqual("MEMBER", response.client.teams[1]["role"]) self.assertIsInstance(response, SuccessfulFullUserResponse)
def test_notify_team_invitation_to_registered_user_does_send_notification( self): invitation = MagicMock() inviter_id = MagicMock() '''Mocked outputs''' inviter_user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") invited_user = PublicUser(user_id=1, username="******", first_name="Test1", last_name="Test1") team = Team(team_id=0, name="Test-Team") def send_notification(topic_name, message_title, message_body, data_message): from tests.test_services import test_notifications MockedNotificationServer.notification = Notification( topic_name, message_title, message_body, data_message) return {"failure": 0} sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = inviter_user sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_email.return_value = invited_user sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_team_invitation(invitation, inviter_id) self.assertEqual(1, MockedNotificationServer.notification.topic_name) self.assertEqual("Hypechat", MockedNotificationServer.notification.message_title) self.assertEqual("You have been invited to join a team!", MockedNotificationServer.notification.message_body) self.assertEqual( "Test-Team", MockedNotificationServer.notification.data_message.get( "team_name")) self.assertEqual( 0, MockedNotificationServer.notification.data_message.get( "inviter_id")) self.assertEqual( NotificationType.TEAM_INVITATION.value, MockedNotificationServer.notification.data_message.get( "notification_type"))
def test_app_user_doesnt_belonging_to_team_throws_exception(self): user_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = User( user_id=user_id, token=token) sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = None sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = Team( name=None) self.assertRaises(NoPermissionsError, Authenticator.authenticate_team, authentication)
def test_notify_direct_message_from_bot_sends_notification(self): message = Message(sender_id=0, receiver_id=1, team_id=0, content="Sarasa", send_type=SendMessageType.DIRECT.value, message_type=MessageType.TEXT.value) is_user_receiver = True '''Mocked outputs''' bot_sender = Bot(bot_id=0, name="Test-Bot", callback=None, token=None) team = Team(team_id=0, name="Test-Team") def send_notification(topic_name, message_title, message_body, data_message): from tests.test_services import test_notifications MockedNotificationServer.notification = Notification( topic_name, message_title, message_body, data_message) return {"failure": 0} sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = None sys.modules[ "daos.bots"].BotDatabaseClient.get_bot_by_id.return_value = bot_sender sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = None sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_message(message, is_user_receiver) self.assertEqual(1, MockedNotificationServer.notification.topic_name) self.assertEqual("Hypechat", MockedNotificationServer.notification.message_title) self.assertEqual("You receive a direct message!", MockedNotificationServer.notification.message_body) self.assertEqual( "Test-Team", MockedNotificationServer.notification.data_message.get( "team_name")) self.assertEqual( 0, MockedNotificationServer.notification.data_message.get( "sender_id")) self.assertEqual( NotificationType.MESSAGE.value, MockedNotificationServer.notification.data_message.get( "notification_type"))
def test_notify_team_role_change_sends_notification(self): user_team = TeamUser(user_id=1, team_id=0, role=TeamRoles.MODERATOR.value) admin_id = 0 '''Mocked outputs''' admin = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") old_role = TeamRoles.MEMBER.value team = Team(team_id=0, name="Test-Team") def send_notification(topic_name, message_title, message_body, data_message): from tests.test_services import test_notifications MockedNotificationServer.notification = Notification( topic_name, message_title, message_body, data_message) return {"failure": 0} sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = admin sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_change_role(user_team, old_role, admin_id) self.assertEqual(1, MockedNotificationServer.notification.topic_name) self.assertEqual("Hypechat", MockedNotificationServer.notification.message_title) self.assertEqual("You have been upgraded in team Test-Team!", MockedNotificationServer.notification.message_body) self.assertEqual( "Test-Team", MockedNotificationServer.notification.data_message.get( "team_name")) self.assertEqual( 0, MockedNotificationServer.notification.data_message.get("admin_id")) self.assertEqual( NotificationType.TEAM_ROLE_CHANGE.value, MockedNotificationServer.notification.data_message.get( "notification_type"))
def test_user_profile_from_different_team_returns_none(self): searched_user_id = 1 active_user_team_id = 0 '''Mocked outputs''' user = User(user_id=1, username="******") teams = [Team(team_id=1, name="TEST1")] user_with_teams = MagicMock() user_with_teams.id = 0 user_with_teams.username = "******" user_with_teams.team_id = 1 user_with_teams.team_name = "TEST1" user_with_teams.team_role = "MEMBER" sys.modules[ "models.authentication"].Authenticator.authenticate.return_value = user sys.modules[ "daos.teams"].TeamDatabaseClient.get_user_teams_by_user_id.return_value = teams sys.modules[ "daos.users"].UserDatabaseClient.get_user_profile.return_value = user_with_teams response = UserService.team_user_profile(searched_user_id, active_user_team_id) self.assertIsNone(response)