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_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_when_connection_error_is_raised_by_pyfcm_notification_is_not_send(
            self):
        def send_notification(topic_name, message_title, message_body,
                              data_message):
            raise ConnectionError

        sys.modules["pyfcm"].FCMNotification(
        ).notify_topic_subscribers = MagicMock(side_effect=send_notification)

        NotificationService.notify_team_invitation(mock, mock)
        self.assertIsNone(MockedNotificationServer.notification)
        NotificationService.notify_channel_invitation(mock, mock)
        self.assertIsNone(MockedNotificationServer.notification)
        NotificationService.notify_message(mock, mock)
        self.assertIsNone(MockedNotificationServer.notification)
        NotificationService.notify_mention(mock, mock)
        self.assertIsNone(MockedNotificationServer.notification)
Пример #4
0
    def invite_user(cls, invite_data):
        team_mod = Authenticator.authenticate_team(invite_data.authentication,
                                                   TeamRoles.is_team_moderator)

        invited_user = UserDatabaseClient.get_user_by_email(invite_data.email)
        if invited_user is not None and invited_user.role == UserRoles.ADMIN.value:
            cls.logger().info(
                f"Mod #{team_mod.id} tried to invite admin #{invited_user.id} to team #{team_mod.team_id}."
            )
            return BadRequestTeamMessageResponse(
                "You cannot invite an admin to a team!",
                TeamResponseStatus.ROLE_UNAVAILABLE.value)

        already_member = TeamDatabaseClient.get_user_in_team_by_email(
            invite_data.email, team_mod.team_id)
        if already_member is not None:
            cls.logger().info(
                f"Mod #{team_mod.id} tried to invite user #{already_member.user_id} to team "
                f"#{team_mod.team_id}, but it already belongs to that team.")
            return BadRequestTeamMessageResponse(
                "This user already belongs to the team.",
                TeamResponseStatus.ALREADY_REGISTERED.value)

        if TeamDatabaseClient.get_team_invite(team_mod.team_id,
                                              invite_data.email) is not None:
            cls.logger().info(
                f"Mod #{team_mod.id} tried to invite an user already invited to team #{team_mod.team_id}"
            )
            return BadRequestTeamMessageResponse(
                "This user was already invited to join the team.",
                TeamResponseStatus.ALREADY_INVITED.value)

        invite_token = Authenticator.generate_team_invitation()
        new_invite = TeamInvite(team_id=team_mod.team_id,
                                email=invite_data.email,
                                token=invite_token)

        try:
            TeamDatabaseClient.add_invite(new_invite)
            team = TeamDatabaseClient.get_team_by_id(team_mod.team_id)
            DatabaseClient.commit()
            cls.logger().info(
                f"New invitation for {new_invite.email} to join team #{team_mod.team_id}, by user #"
                f"{team_mod.id}.")

            email_data = TeamInvitationEmailDTO(
                email=invite_data.email,
                team_name=team.name,
                inviter_name=team_mod.username,
                token=invite_token,
                message_template=EmailService.team_invitation_message)
            EmailService.send_email(email_data)
            NotificationService.notify_team_invitation(new_invite, team_mod.id)
            cls.logger().info(
                f"Team #{team_mod.team_id} invitation email sent to {new_invite.email}."
            )

        except IntegrityError:
            DatabaseClient.rollback()
            cls.logger().error(
                f"Couldn't invite user {new_invite.email} to team #{team_mod.team_id}."
            )
            return UnsuccessfulTeamMessageResponse(
                "Couldn't invite user to team.")
        else:
            return SuccessfulTeamMessageResponse(
                "User invited.", TeamResponseStatus.INVITED.value)