Esempio n. 1
0
    def test_format_notification(self):
        """Test format notification."""
        user = mocks.create_user()
        institution = mocks.create_institution()
        notification_type = self.get_notification_type()

        message = create_message(
            sender_key=user.key,
            current_institution_key=institution.key,
            receiver_institution_key=institution.key,
            sender_institution_key=institution.key,
        )

        notification = Notification(message=message,
                                    entity_key=institution.key.urlsafe(),
                                    notification_type=notification_type,
                                    receiver_key=user.key.urlsafe())

        expected_format = {
            'receiver_key': user.key.urlsafe(),
            'message': message,
            'notification_type': notification_type,
            'entity_key': institution.key.urlsafe()
        }

        self.assertEqual(
            notification.format_notification(), expected_format,
            'Formated notification must be the same as expected.')
    def test_resolve_notification_task(self, send_message_notification):
        """Test resolve notification task."""
        user = mocks.create_user()
        institution = mocks.create_institution()
        notification_type = self.get_notification_type()

        message = create_message(
            sender_key=user.key,
            current_institution_key=institution.key,
            receiver_institution_key=institution.key,
            sender_institution_key=institution.key,
        )

        notification = Notification(message=message,
                                    entity_key=institution.key.urlsafe(),
                                    notification_type=notification_type,
                                    receiver_key=user.key.urlsafe())

        num_tasks = self.queue.fetch_statistics().tasks
        self.assertEqual(num_tasks, 0, 'num_tasks must be equal to 0')

        id_notification = NotificationsQueueManager.create_notification_task(
            notification)

        num_tasks = self.queue.fetch_statistics().tasks
        self.assertEqual(num_tasks, 1, 'num_tasks must be equal to 1')

        NotificationsQueueManager.resolve_notification_task(id_notification)

        send_message_notification.assert_called_with(
            **notification.format_notification())
        num_tasks = self.queue.fetch_statistics().tasks
        self.assertEqual(num_tasks, 0, 'num_tasks must be equal to 0')
    def test_create_notification_task(self):
        """Test create notification task."""
        user = mocks.create_user()
        institution = mocks.create_institution()
        notification_type = self.get_notification_type()

        message = create_message(
            sender_key=user.key,
            current_institution_key=institution.key,
            receiver_institution_key=institution.key,
            sender_institution_key=institution.key,
        )

        notification = Notification(message=message,
                                    entity_key=institution.key.urlsafe(),
                                    notification_type=notification_type,
                                    receiver_key=user.key.urlsafe())

        num_tasks = self.queue.fetch_statistics().tasks
        self.assertEqual(num_tasks, 0, 'num_tasks must be equal to 0')

        id_notification = NotificationsQueueManager.create_notification_task(
            notification)

        num_tasks = self.queue.fetch_statistics().tasks
        self.assertEqual(num_tasks, 1, 'num_tasks must be equal to 0')
        self.assertEqual(id_notification, notification.key,
                         'id_notification must equal the notification key.')
Esempio n. 4
0
    def test_create_message(self):
        """Test create_message method."""
        sender = mocks.create_user()
        sender.photo_url = "photo-url"
        sender.put()
        institution = mocks.create_institution()
        current_institution = institution.key
        message = service_messages.create_message(sender.key,
                                                  current_institution)
        expected_message = {
            'from': {
                'name': sender.name.encode('utf8'),
                'photo_url': sender.photo_url,
                'institution_name': ''
            },
            'to': {
                'institution_name': ''
            },
            'current_institution': {
                'name': institution.name
            }
        }

        self.assertEquals(
            message, json.dumps(expected_message),
            "The created message should be igual to the expected one but was: "
            + message)
Esempio n. 5
0
 def create_notification_message(self, user_key, current_institution_key, sender_institution_key=None):
     """ Create message that will be used in notification.
         user_key -- The user key that made the action.
         current_institution_key -- The institution that user was in the moment that made the action.
         sender_institution_key -- The institution by which the post was created,
             if it hasn't been defined yet, the sender institution should be the current institution. 
     """
     return create_message(
         sender_key= user_key,
         current_institution_key=current_institution_key,
         sender_institution_key=sender_institution_key or current_institution_key
     )
Esempio n. 6
0
 def create_notification_message(self, user_key, current_institution_key, 
         receiver_institution_key=None, sender_institution_key=None):
     """ Create message that will be used in notification. 
         user_key -- The user key that made the action.
         current_institution -- The institution that user was in the moment that made the action.
         sender_institution_key -- The institution in which action should be taken,
             when it is not specified it will be the current_institution.
         receiver_institution -- The institution to which the notification is directed. 
     """
     return create_message(
         sender_key= user_key,
         current_institution_key=current_institution_key,
         receiver_institution_key=receiver_institution_key,
         sender_institution_key= sender_institution_key or current_institution_key
     )
Esempio n. 7
0
    def test_create_sent_invites_notification(self,
                                              create_notification_message,
                                              create_notification_task):
        """Test method create_sent_invites_notification."""

        message = create_message(self.admin.key, self.institution.key)
        create_notification_message.return_value = message

        self.invite_user.create_sent_invites_notification(self.institution.key)

        create_notification_message.assert_called_with(
            user_key=self.admin.key,
            current_institution_key=self.institution.key)

        create_notification_task.assert_called()
Esempio n. 8
0
 def create_notification_message(self,
                                 user_key,
                                 current_institution_key=None,
                                 sender_institution_key=None,
                                 receiver_institution_key=None):
     """ Create message that will be used in notification. 
         user_key -- The user key that made the action.
         current_institution_key -- The institution that user was in the moment that made the action,
              in case that user is inactive he didn't have current institution.
         sender_institution_key -- The institution in which action should be taken.
         receiver_institution -- The institution to which the notification is directed. 
     """
     return create_message(
         sender_key=user_key,
         current_institution_key=current_institution_key,
         sender_institution_key=sender_institution_key
         or self.institution_key,
         receiver_institution_key=receiver_institution_key
         or self.institution_requested_key,
     )
Esempio n. 9
0
    def test_send_notification(self, send_message_notification):
        """Test send notification."""
        user = mocks.create_user()
        institution = mocks.create_institution()
        notification_type = self.get_notification_type()

        message = create_message(
            sender_key=user.key,
            current_institution_key=institution.key,
            receiver_institution_key=institution.key,
            sender_institution_key=institution.key,
        )

        notification = Notification(message=message,
                                    entity_key=institution.key.urlsafe(),
                                    notification_type=notification_type,
                                    receiver_key=user.key.urlsafe())

        notification.send_notification()
        send_message_notification.assert_called_with(
            **notification.format_notification())
Esempio n. 10
0
    def test_create_notification(self, time):
        """Test create new notification."""
        timeMock = TimeMock()
        time.time.side_effect = timeMock.time

        user = mocks.create_user()
        institution = mocks.create_institution()
        notification_type = self.get_notification_type()

        message = create_message(
            sender_key=user.key,
            current_institution_key=institution.key,
            receiver_institution_key=institution.key,
            sender_institution_key=institution.key,
        )

        notification = Notification(message=message,
                                    entity_key=institution.key.urlsafe(),
                                    notification_type=notification_type,
                                    receiver_key=user.key.urlsafe())

        id = get_notification_id(notification_type)
        entity_hash = hash(institution.key.urlsafe())
        receiver_hash = hash(user.key.urlsafe())
        timestamp = timeMock.rand_time

        expected_key = id + '-' + str(entity_hash) + str(receiver_hash) + str(
            timestamp)

        self.assertEqual(notification.key, expected_key,
                         'Notification key must be the same as expected.')
        self.assertEqual(notification.message, message,
                         'Notification message must be the same as expected.')
        self.assertEqual(notification.entity_key, institution.key.urlsafe(),
                         'entity_key must be equal to institution key.')
        self.assertEqual(notification.notification_type, notification_type,
                         'notification_type must be the same as expected.')
        self.assertEqual(notification.receiver_key, user.key.urlsafe(),
                         'receiver_key must be equal to user key.')
def remake_link(request, requested_inst_key, child_institution, user):
    notification_message = create_message(
        sender_key=user.key,
        current_institution_key=child_institution.key,
        receiver_institution_key=requested_inst_key,
        sender_institution_key=child_institution.key)

    notification = Notification(
        entity_key=child_institution.key.urlsafe(),
        receiver_key=requested_inst_key.get().admin.urlsafe(),
        notification_type='RE_ADD_ADM_PERMISSIONS',
        message=notification_message)

    notification_id = NotificationsQueueManager.create_notification_task(
        notification)
    enqueue_task(
        'add-admin-permissions', {
            'institution_key': child_institution.key.urlsafe(),
            'notifications_ids': [notification_id]
        })

    request.change_status('accepted')
Esempio n. 12
0
    def test_create_notification_with_unregistred_type(self):
        """Test create notification with unregistred_type."""
        user = mocks.create_user()
        institution = mocks.create_institution()
        notification_type = 'ANY_NOTIFICATIONS'

        message = create_message(
            sender_key=user.key,
            current_institution_key=institution.key,
            receiver_institution_key=institution.key,
            sender_institution_key=institution.key,
        )

        notification = Notification(message=message,
                                    entity_key=institution.key.urlsafe(),
                                    notification_type=notification_type,
                                    receiver_key=user.key.urlsafe())

        self.assertEqual(notification.notification_type, 'ANY_NOTIFICATIONS',
                         'notification_type must be the same as expected.')

        self.assertEqual(notification.notification_group, 'ALL_NOTIFICATIONS',
                         'notification_group must be the same as expected.')