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')
Ejemplo n.º 2
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.')
Ejemplo n.º 3
0
def create_notification(user, receiver_institution, sender_institution,
                        create_message):
    """
    Create new notification and add in Queue.

    Keyword arguments:
    user -- Use to receive notification.
    receiver_institution -- Institution key in which the notification is directed.
    sender_institution --Institution in which the user took action.
    create_message -- Message of notification.
    """
    message = create_message(user_key=user.key,
                             current_institution_key=user.current_institution,
                             receiver_institution_key=receiver_institution.key,
                             sender_institution_key=sender_institution.key)

    notification = Notification(
        message=message,
        entity_key=receiver_institution.key.urlsafe(),
        notification_type='REMOVE_INSTITUTION_LINK',
        receiver_key=receiver_institution.admin.urlsafe())

    notification_id = NotificationsQueueManager.create_notification_task(
        notification)
    return notification_id
    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.')
Ejemplo n.º 5
0
    def put(self, user, request_key):
        """Handler PUT Requests. Change status of parent_request from 'sent' to 'accepted'."""
        request = ndb.Key(urlsafe=request_key).get()
        user.check_permission(
            'answer_link_inst_request',
            'User is not allowed to accept link between institutions',
            request.institution_requested_key.urlsafe())
        request.change_status('accepted')

        parent_institution = request.institution_requested_key.get()
        parent_institution.add_child(request.institution_key)

        institution_children = request.institution_key.get()

        request.send_response_notification(user.current_institution, user.key,
                                           'ACCEPT')
        request.send_response_email('ACCEPT')

        notification = Notification(
            entity_key=institution_children.key.urlsafe(),
            receiver_key=user.key.urlsafe(),
            notification_type='ADD_ADM_PERMISSIONS',
            message=create_system_message(institution_children.key))

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

        self.response.write(json.dumps(request.make()))
Ejemplo n.º 6
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())
Ejemplo n.º 7
0
    def create_system_notification(self):
        """
        Create a new system notification for the new administrator 
        to inform you that administrative permissions have been transferred.
        """
        message = create_system_message(self.institution_key)

        notification = Notification(
            message=message,
            entity_key=self.institution_key.urlsafe(),
            notification_type="TRANSFER_ADM_PERMISSIONS",
            receiver_key=self.invitee_key.urlsafe()
        )

        notification_id = NotificationsQueueManager.create_notification_task(notification)
        return notification_id
Ejemplo n.º 8
0
def create_system_notification(receiver_institution_key, receiver_key):
    """
    Create new system notification and add in queue.
    
    Keyword arguments:
    receiver_institution_key -- Institution key in which the notification is directed.
    receiver_key -- User key that notification will be sent.
    """
    message = create_system_message(receiver_institution_key)

    notification = Notification(message=message,
                                entity_key=receiver_institution_key.urlsafe(),
                                notification_type='ADD_ADM_PERMISSIONS',
                                receiver_key=receiver_key)

    notification_id = NotificationsQueueManager.create_notification_task(
        notification)
    return notification_id
Ejemplo n.º 9
0
    def create_accept_response_notification(self, current_institution,
                                            invitee_key):
        """Create accept notification to sender of invite"""
        notification_message = self.create_notification_message(
            user_key=invitee_key,
            current_institution_key=current_institution,
            receiver_institution_key=self.institution_key,
            sender_institution_key=self.institution_requested_key)

        notification = Notification(
            entity_key=self.key.urlsafe(),
            receiver_key=self.sender_key.urlsafe()
            if self.sender_key else self.admin_key.urlsafe(),
            notification_type='ACCEPT_INSTITUTION_LINK',
            message=notification_message)

        notification_id = NotificationsQueueManager.create_notification_task(
            notification)
        return notification_id
Ejemplo 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')
Ejemplo 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.')
Ejemplo n.º 13
0
    def create_accept_response_notification(self, current_institution):
        """
        Create a new accept response notification.
        
        Keyword arguments:
        current_institution -- Current institution of user.
        """
        admin = self.institution_key.get().admin
        message = self.create_notification_message(
            user_key=self.invitee_key,
            current_institution_key=current_institution,
            receiver_institution_key=self.institution_key
        )

        notification = Notification(
            message=message,
            entity_key=self.key.urlsafe(),
            notification_type="ACCEPT_INVITE_USER_ADM",
            receiver_key=admin.urlsafe()
        )

        notification_id = NotificationsQueueManager.create_notification_task(notification)
        return notification_id
Ejemplo n.º 14
0
    def create_sent_invites_notification(self, current_institution_key):
        """
        Create a notification and enqueue it to be be sent
        when the invites are sent.
        
        Keyword arguments:
        current_institution -- Current institution of user.
        """
        sender_key = self.sender_key or self.admin_key

        message = self.create_notification_message(
            user_key=sender_key,
            current_institution_key=current_institution_key
        )

        notification = Notification(
            message=message,
            entity_key=self.key.urlsafe(),
            notification_type='USER_INVITES_SENT',
            receiver_key=sender_key.urlsafe()
        )

        notification_id = NotificationsQueueManager.create_notification_task(notification)
        return notification_id