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_with_invalid_notification(self): """Test create notification task wiht invalid notification.""" with self.assertRaises(TypeError) as raises_context: NotificationsQueueManager.create_notification_task("Notification") self.assertEqual( str(raises_context.exception), 'Expected type Notification but got str.', 'Exception message must be equal to Expected type Notification but got str.' )
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_resolve_notification_task_with_invalid_id(self): """Test resolve notification task with invalid id.""" with self.assertRaises(QueueException) as raises_context: NotificationsQueueManager.resolve_notification_task( '03-jksahdjkasjksahdshadkjsdhjksakjdhsajhkdhsajkdhas') self.assertEqual(str(raises_context.exception), 'Task not found.', 'Exception message must be equal to Task not found.') with self.assertRaises(QueueException) as raises_context: NotificationsQueueManager.resolve_notification_task( 'ab-jksahdjkasjksahdshadkjsdhjksakjdhsajhkdhsajkdhas') self.assertEqual( str(raises_context.exception), 'Invalid task key.', 'Exception message must be equal to Invalid task key.')
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.')
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()))
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
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
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
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')
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
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