def test_calling_send_notification_with_missing_parameters(self): """Tests send notification function missing parameters""" mixer.blend('base.State', name='Sent') mixer.blend('base.State', name='Failed') state = mixer.blend('base.State', name='Active') system = mixer.blend('core.System', state=state) mixer.blend('base.EscalationLevel') recipient = mixer.blend(User, first_name='Kevin', phone_number=+254776054478) mixer.blend(User, first_name='Elly', email='*****@*****.**') message_type = mixer.blend('base.NotificationType', name='SMS') notification = NotificationLogger().send_notification( recipients=[recipient.id], message='sample', message_type=message_type.name, system_id=system.id) notification2 = NotificationLogger().send_notification( recipients=[recipient.id], message='sample', message_type=message_type.id, system_id=system.id) notification3 = NotificationLogger().send_notification(recipients='', message='', message_type='', system_id='') assert notification.get( 'code' ) == '800.200.001', "Should return code for missing parameters" assert notification2.get('code') == '200.400.005', "Should return error code showing no message data was " \ "created" assert notification3.get( 'code') == '800.400.002', "error code for missing parameters"
def test_notification_logger(self): """Tests successful notification sending""" mixer.blend('base.State', name='Sent') mixer.blend('base.State', name='Failed') state = mixer.blend('base.State', name='Active') mixer.cycle(2).blend(User, state=state) message_type = mixer.blend('base.NotificationType', name='Email') system = mixer.blend('core.System', name='System 1') notification = NotificationLogger().send_notification( recipients=["*****@*****.**", "*****@*****.**"], message='Hey Listen to BBC news today', message_type=message_type.name, system_id=system.id) assert notification.get( 'code' ) == '800.200.001', "Should successfully send and log notifications"
def get_logged_in_user_recent_notifications(request): """ @param request: The Django WSGI Request to process @return: dict """ try: data = get_request_data(request) notifications = NotificationLogger.get_logged_in_user_recent_notifications(token = data.get('token')) return JsonResponse(notifications) except Exception as ex: lgr.exception('get logged in user recent notification Exception: %s' % ex) return JsonResponse({'code': '800.500.001'})
def get_notifications(request): """ Delete a specific Recipient @param request:The Django WSGI Request to process @return:dict """ try: data = get_request_data(request) notifications = NotificationLogger.get_system_notification(system_id = data.get('system_id')) return JsonResponse(notifications) except Exception as ex: lgr.exception('notifications get Exception: %s' % ex) return JsonResponse({'code': '800.500.001'})
def log_incident(incident_type, system, escalation_level, name, description, priority_level, event_type=None, state="Investigating", escalated_events=None, scheduled_for=None, scheduled_until=None, **kwargs): """ Creates a realtime incident based on escalated events or scheduled incident based on user reports @param incident_type: Type of the incident to be created @type incident_type: str @param system: The system which the incident will be associated with @type system: str @param name: Title of the incident @type name: str @param description: Details on the incident @type description: str @param event_type: Type of the event(s) that triggered creation of the incident, if its event driven. @type event_type: str | None @param escalated_events: One or more events in the escalation if the incident is event driven. @type escalated_events: list | None @param state: Initial resolution state of the incident. Defaults to Investigating if left blank @type state: str @param priority_level: The level of importance to be assigned to the incident. @type priority_level: str @param escalation_level: Level at which an escalation is configured with a set of recipients @type escalation_level: str @param scheduled_for: Time the scheduled maintenance should begin if the incident is scheduled @type scheduled_for: str | None @param scheduled_until: Time the scheduled maintenance should end if the incident is scheduled @type scheduled_until: str | None @param kwargs: Extra key-value arguments to pass for incident logging @return: Response code dictionary to indicate if the incident was created or not @rtype: dict """ try: system = SystemService().get(pk=system, state__name="Active") incident_type = IncidentTypeService().get(name=incident_type, state__name="Active") try: state = StateService().get(pk=uuid.UUID(state)) except ValueError: state = StateService().get( name=state ) if incident_type.name == 'Realtime' else StateService().get( name='Scheduled') escalation_level = EscalationLevelService().get( pk=escalation_level, state__name="Active") if system is None or incident_type is None or escalation_level is None: return {"code": "800.400.002"} if incident_type.name == "Realtime" and event_type is not None: incident = IncidentService().filter( event_type__name=event_type, system=system).exclude( Q(state__name='Resolved'), Q(state__name='Completed')).order_by( '-date_created').first() if incident and int(priority_level) < 5: priority_level = incident.priority_level + 1 return IncidentAdministrator().update_incident( incident_id=incident.id, escalation_level=escalation_level.name, name=incident.name, state=incident.state.id, priority_level=str(priority_level), description= "Priority level of %s incident changed to %s" % (incident.name, priority_level)) if incident_type.name == 'Scheduled': scheduled_for = dateutil.parser.parse(scheduled_for) scheduled_until = dateutil.parser.parse(scheduled_until) incident = IncidentService().create( name=name, description=description, state=StateService().get(name=state), system=system, incident_type=incident_type, scheduled_for=scheduled_for, scheduled_until=scheduled_until, event_type=EventTypeService().get(name=event_type), priority_level=int(priority_level)) incident_log = IncidentLogService().create( description=description, incident=incident, priority_level=priority_level, state=StateService().get(name=state), escalation_level=escalation_level) if incident is not None and incident_log is not None: if escalated_events: for event in escalated_events: incident_event = IncidentEventService().create( event=event, incident=incident, state=StateService().get(name="Active")) if not incident_event: lgr.error("Error creating incident-events") email_system_recipients = SystemRecipientService().filter( escalation_level=escalation_level, system=incident.system, state__name='Active', notification_type__name='Email').values('recipient__id') sms_system_recipients = SystemRecipientService().filter( escalation_level=escalation_level, system=incident.system, state__name='Active', notification_type__name='Sms').values('recipient__id') sms_notification = NotificationLogger().send_notification( message=incident.description, message_type="Sms", system_id=incident.system.id, recipients=[ str(recipient["phone_number"]) for recipient in User.objects.filter( id__in=sms_system_recipients, is_active=True).values("phone_number") ]) email_notification = NotificationLogger().send_notification( message=incident.description, message_type="Email", system_id=incident.system.id, recipients=[ str(recipient['user__email']) for recipient in User.objects.filter(id__in=email_system_recipients, is_active=True).values('email') ]) if sms_notification.get( 'code') != '800.200.001' or email_notification.get( 'code') != '800.200.001': lgr.exception("Notification sending failed") return {'code': '800.200.001'} except Exception as ex: lgr.exception("Incident Logger exception %s" % ex) return {"code": "800.400.001"}
def update_incident(incident_id, name, escalation_level, state, description, user=None, priority_level=None): """ Logs incident updates e.g changes in resolution state or priority level of an incident @param incident_id: The id of the incident to be updated @type incident_id: str @param name: The name of the incident to be updated @type name: str @param escalation_level: Level at which to send notifications to configured users @type escalation_level: str @param state: New resolution state of the incident @type state: str @param description: Detailed information on the incident update @type description: str @param user: User assigned to the incident @type user: str | None @param priority_level: New priority level of the incident @type priority_level: str | None @return: Response code in a dictionary to indicate if the incident was updated or not @rtype: dict """ try: state = StateService().get(pk=state) incident = IncidentService().get(pk=incident_id) escalation_level = EscalationLevelService().get( pk=escalation_level, state__name="Active") user = User.objects.filter(id=user).first() if user else None if incident is None or escalation_level is None or state is None: return {'code': '800.400.002'} priority_level = int( priority_level ) if priority_level is not None else incident.priority_level incident_log = IncidentLogService().create( description=description, incident=incident, user=user, priority_level=priority_level, state=state, escalation_level=escalation_level) if state.name == 'Completed' or state.name == 'Resolved': IncidentService().update(pk=incident.id, priority_level=priority_level, state=state, name=name, description=description) else: IncidentService().update(pk=incident.id, priority_level=priority_level) if incident_log: email_system_recipients = SystemRecipientService().filter( escalation_level=escalation_level, system=incident.system, state__name='Active', notification_type__name='Email').values('recipient__id') sms_system_recipients = SystemRecipientService().filter( escalation_level=escalation_level, system=incident.system, state__name='Active', notification_type__name='Sms').values('recipient__id') sms_notification = NotificationLogger().send_notification( message=incident_log.description, message_type="Sms", system_id=incident.system.id, recipients=[ str(recipient["phone_number"]) for recipient in User.objects.filter( id__in=sms_system_recipients, is_active=True).values("phone_number") ]) email_notification = NotificationLogger().send_notification( message=incident_log.description, message_type="Email", system_id=incident.system.id, recipients=[ str(recipient['user__email']) for recipient in User.objects.filter(id__in=email_system_recipients, is_active=True).values('email') ]) if sms_notification.get( 'code') != '800.200.001' or email_notification.get( 'code') != '800.200.001': lgr.warning("Notification sending failed") return {'code': '800.200.001'} except Exception as ex: lgr.exception("Incident Administration exception %s" % ex) return {'code': '800.400.001'}