Example #1
0
 def test_send_message(self):
     a_medicine = InfoService(name='Malarone', type='medicine')
     a_medicine.save()
     subscription = Subscription(patient = Patient.objects.all()[0],
                             way_of_communication = get_woc("sms"),
                             infoservice = a_medicine)
     subscription.save()
     subscription = Subscription(patient = Patient.objects.all()[1],
                             way_of_communication = get_woc("sms"),
                             infoservice = a_medicine)
     subscription.save()
     info_service_count = InfoService.objects.all().count()
     members_count = a_medicine.members.count()
     scheduled_events_count = ScheduledEvent.objects.all().count()
     a_text = 'Hello, the medicine Malarone is now available at your ' + \
              'clinic. Please come and pick it up.'
     response = self.client.post(reverse('medicines_send_message'),
                                  { 'medicine': a_medicine.pk, 
                                    'text': a_text })
                                    
     self.assertEquals(InfoService.objects.all().count() +  1, 
                       info_service_count)
     self.assertEquals(ScheduledEvent.objects.all().count(),
                       scheduled_events_count + members_count)
     self.assertTemplateUsed(response, 'web/status_message.html')
     self.assertTemplateNotUsed(response, 'medicine/message_create.html')
 def test_save_notification_voice(self):
     woc_voice = get_woc("voice")
     
     status_save = woc_voice.enabled
     
     woc_voice.enabled = True
     woc_voice.save()
 
     self.save_notification_woc(get_woc('voice'))
     
     woc_voice.enabled = status_save
     woc_voice.save()
Example #3
0
 def manage_groups(self, infoservice_type, table_head, member_button, 
                   remove_button, remove_patient_button, group_name):
     info = InfoService(name = "testgroup", type = infoservice_type)
     info.save()
     
     patient = Patient(phone_number = "012345")
     patient.save()
     
     subscription = Subscription(infoservice = info, 
                                 patient = patient,
                                 way_of_communication = get_woc("voice"))
     subscription.save()
     
     response = self.client.get(reverse("infoservices_index",
                                 kwargs = {'infoservice_type': infoservice_type}))
     self.assertContains(response, member_button)
     self.assertContains(response, remove_button)
     self.assertContains(response, table_head)
     
     response = self.client.get(reverse("infoservices_members", 
                                                kwargs={"id": info.id,
                                                }))
                                                        
     self.assertContains(response, patient.phone_number)
     self.assertContains(response, remove_patient_button)
     self.assertContains(response, group_name)
    def test_get_all_queued_events(self):
        patient = Patient()
        patient.save()

        sendable = InfoMessage(text="Test Message",
                               way_of_communication = get_woc("sms"))
        sendable.recipient = patient
        sendable.save()

        self.assertEquals(scheduler.get_all_queued_events().count(), 0)

        schedule1 = ScheduledEvent(sendable=sendable,
                               send_time=datetime.now(),
                               state = "queued")
        schedule1.save()

        self.assertEquals(scheduler.get_all_queued_events().count(), 1)

        schedule2 = ScheduledEvent(sendable=sendable,
                               send_time=(datetime.now() - timedelta(days=1)),
                               state = "sent")
        schedule2.save()

        self.assertTrue(schedule1 in scheduler.get_all_queued_events())
        self.assertFalse(schedule2 in scheduler.get_all_queued_events())

        self.assertEquals(scheduler.get_all_queued_events().count(), 1)

        schedule2.state = "queued"
        schedule2.save()

        self.assertEquals(scheduler.get_all_queued_events().count(), 2)

        schedule1.delete()
        schedule2.delete()
Example #5
0
 def setUp(self):
     self.info = InfoService(name = "testinfoservice", type="information")
     self.info.save()
     self.patient = Patient(name="eu",phone_number = "01234")
     self.patient.save()
     self.subscription = Subscription(infoservice = self.info, 
                                      way_of_communication = get_woc("sms"), 
                                      patient = self.patient)
     self.subscription.save()
Example #6
0
 def setUp(self):
     self.woc = get_woc("sms")
 
     self.infoservice = InfoService(name = "Gruppe", 
                                    type = "information")
     self.infoservice.save()
     self.patient = Patient()
     self.patient.save()
     self.subscription = Subscription(patient = self.patient, 
                                      infoservice = self.infoservice,
                                      way_of_communication = self.woc)
     self.subscription.save()
 def test_create_notification_bluetooth(self):
     response = self.create_notification(get_woc('bluetooth'))
     self.assertRedirects(response, 
                          reverse('web_list_devices') + \
                          "?next=" + \
                          reverse('notifications_send'))
     self.assertTrue(self.client.session.has_key('patient'))
     self.assertTrue(self.client.session.has_key('notification'))
     
     response = self.client.get( reverse('notifications_send'), 
             { 'device_mac': '01733685224',
              'date': '2012-08-12',
              'way_of_communication': 1  })
     self.assertEquals(response.status_code, 200)
 def test_notification_form_validation(self):
     data = {"phone_number" : "0123456789",
             "way_of_communication" : get_woc('sms').id,
             "date" : "2010-08-12 12:00"}
     form = NotificationValidationForm(data)
     self.assertTrue(form.is_valid())
     
     data = { "phone_number" : "0123456rttddbh789",
              "way_of_communication" : 4,
              "date" : "abc" }
     form = NotificationValidationForm(data)
     self.assertFalse(form.is_valid())
     self.assertEquals(len(form['phone_number'].errors), 1)
     self.assertEquals(len(form['way_of_communication'].errors), 1)
     self.assertEquals(len(form['date'].errors), 1)
Example #9
0
    def test_register_save(self):
        subscription_count = Subscription.objects.all().count()

        self.client.post(reverse('groups_register',
                         kwargs={'group_id': self.info.id}),
                         {"way_of_communication": 1,
                          "phone_number": "0123456"})
                          
        response = self.client.get(reverse('groups_register_save',
                                   kwargs={'group_id': self.info.id}))
                                                               
        self.assertEquals(Subscription.objects.all().count(),
                          subscription_count + 1)
        new_subscription = last(Subscription)
        self.assertEquals(new_subscription.patient.phone_number, "0123456")
        self.assertEquals(new_subscription.infoservice, self.info)
        self.assertEquals(new_subscription.way_of_communication, get_woc("sms"))
Example #10
0
 def test_get_all_due_events(self):
     patient = Patient()
     patient.save()
     
     # The fixtures already contain two due events        
     self.assertEquals(scheduler.get_all_due_events().count(), 2)
     
     sendable = InfoMessage(text="Test Message",
                            way_of_communication = get_woc("sms"))
     sendable.recipient = patient
     sendable.save()
     
     schedule1 = ScheduledEvent(sendable=sendable, send_time=datetime.now())
     schedule1.save()
     
     schedule2 = ScheduledEvent(sendable=sendable, 
                            send_time=(datetime.now() - timedelta(days=1)))
     schedule2.save()
     
     schedule3 = ScheduledEvent(sendable=sendable, 
                            send_time=(datetime.now() + timedelta(days=1)))
     schedule3.save()
     
     self.assertEquals(scheduler.get_all_due_events().count(), 4)
     self.assertTrue(schedule1 in scheduler.get_all_due_events())
     self.assertTrue(schedule2 in scheduler.get_all_due_events())
     self.assertFalse(schedule3 in scheduler.get_all_due_events())
     
     schedule4 = ScheduledEvent(sendable=sendable, 
                            send_time=datetime.now(),
                            state = "failed")
     schedule4.save()
     
     schedule5 = ScheduledEvent(sendable=sendable, 
                            send_time=(datetime.now() - timedelta(days=1)),
                            state = "sent")
     schedule5.save()
     
     self.assertEquals(scheduler.get_all_due_events().count(), 4)
     
     schedule1.delete()
     schedule2.delete()
     schedule3.delete()
     schedule4.delete()
     schedule5.delete()
Example #11
0
    def test_register_save(self):
        subscription_count = Subscription.objects.all().count()

        # pk = 3 is a medicine in fixtures
        self.client.post(reverse('medicines_register'),
                         {"way_of_communication": 1,
                          "phone_number": "0123456",
                          "medicine": "3"})
                          
        response = self.client.get(
                                reverse('medicines_register_save',
                                kwargs={'medicine_id': '3'}))
                                      
        self.assertEquals(Subscription.objects.all().count(),
                          subscription_count + 1)
        new_subscription = last(Subscription)
        self.assertEquals(new_subscription.patient.phone_number, "0123456")
        self.assertEquals(new_subscription.infoservice.id, 3)
        self.assertEquals(new_subscription.way_of_communication, get_woc("sms"))    
Example #12
0
def create_notification(request, notification_type_name = None):
    '''
        Display the form and creates a new notification, but does not
        save it yet. Redirect to authentication if switched on
    '''
    notification_type = NotificationType.objects. \
                              filter(name = notification_type_name)[0]
    nexturl = ""
    backurl = reverse('web_index')
    
    ways_of_communication = get_ways_of_communication(
                                    notification_type.notify_immediately)
    
    if request.method == "POST":
        data = deepcopy(request.POST)
        if notification_type.notify_immediately:
            data['date'] = date.today().strftime('%Y-%m-%d') + \
                            ' ' + DEFAULT_SEND_TIME
        else:
            data['date'] = data.get('date', '') + ' ' + DEFAULT_SEND_TIME
			
        form = NotificationValidationForm(data)

        woc = get_woc_by_id( request.POST['way_of_communication'] )
        if not woc.can_send_immediately:
		    form = NotificationValidationFormBluetooth(data)
		
        if form.is_valid():
            notification = Notification()
            patient = Patient()
            if woc.can_send_immediately:
                patient.phone_number = form.cleaned_data['phone_number']
            notification.date = form.cleaned_data['date']
            notification.notification_type = notification_type
            notification.hospital = Hospital.get_current_hospital()
            notification.way_of_communication = \
                                    form.cleaned_data['way_of_communication']
                                    
            request.session['notification'] = notification
            request.session['patient'] = patient            
            
            logger.info("Create notification via %s" %
                            notification.way_of_communication.verbose_name)
            if notification.way_of_communication == get_woc('bluetooth'):
                return HttpResponseRedirect(reverse("web_list_devices") + \
                                "?next=" + reverse("notifications_send"))
            elif notification.way_of_communication.name in ('sms', 'voice' ):
                return redirect_to_authentication_or(
                                reverse("notifications_save"))

            else:
                logger.error("Unknown way of communication selected.")
                raise Exception ("Unknown way of communication %s " \
                                 %notification.way_of_communication.\
                                 verbose_name + "(this is neither " + \
                                 "bluetooth nor sms or voice)") 
                                
        else:
        
            logger.info("create_notification: Invalid form.")
        
    return render_to_response('notifications/create.html',
                            locals(),
                            context_instance=RequestContext(request))
 def test_save_notification_sms(self):
      self.save_notification_woc(get_woc('sms'))
 def test_invalid_create_notification(self):
     response = self.create_notification(get_woc('voice'))
     
     self.failUnlessEqual(response.status_code, 200)        
     self.assertFalse(self.client.session.has_key('patient'))
     self.assertFalse(self.client.session.has_key('notification'))
 def test_valid_create_notification(self):
     response = self.create_notification(get_woc('sms'))
     
     self.assertRedirects(response, reverse('notifications_save'))
     self.assertTrue(self.client.session.has_key('patient'))
     self.assertTrue(self.client.session.has_key('notification'))
 def test_get_woc(self):
     woc = get_woc("voice")
     
     self.assertEquals(woc.name, "voice")
Example #17
0
    def test_check_spool_files(self):
        def get_mock_status(filename):
            """
                This is a mock method to represent check_spoolfile_status
                It is told which status to return by using the (in this case
                useless) filename parameter
            """
            return filename

        original_get_spoolfile_status = scheduler.get_spoolfile_status
        scheduler.get_spoolfile_status = get_mock_status

        patient = Patient()
        patient.save()

        sendable = InfoMessage(text="Test Message",
                               way_of_communication = get_woc("sms"))
        sendable.recipient = patient
        sendable.save()

        event1 = ScheduledEvent(sendable=sendable,
                               send_time=datetime.now(),
                               state = "queued",
                               filename = "Completed")
        event1.save()

        event2 = ScheduledEvent(sendable=sendable,
                               send_time=datetime.now(),
                               state = "queued",
                               filename = "Expired")
        event2.save()


        # now: the real testing
               
        scheduler.check_spool_files()
       
        self.assertEquals(ScheduledEvent.objects.get(pk = event1.pk).state, \
                                    "done")

        self.assertEquals(ScheduledEvent.objects.get(pk = event2.pk).state, \
                                    "new")

        event2.filename = "Expired"
        event2.save()

        scheduler.check_spool_files()

        self.assertEquals(ScheduledEvent.objects.get(pk = event2.pk).state, \
                                    "new")

        self.assertEquals(ScheduledEvent.objects.get(pk = event2.pk).retry, 1)

        event2.retry = settings.ASTERISK_RETRY
        event2.filename = "Expired"
        event2.save()

        scheduler.check_spool_files()

        self.assertEquals(ScheduledEvent.objects.get(pk = event2.pk).state, \
                                    "failed")

        event3 = ScheduledEvent(sendable=sendable,
                               send_time=datetime.now(),
                               state = "queued",
                               filename = "Failed")


        event3.save()
        scheduler.check_spool_files()


        self.assertEquals(ScheduledEvent.objects.get(pk = event3.pk).state, \
                                    "failed")



        # change everything back to normal
        event1.delete()
        event2.delete()
        event3.delete()

        scheduler.get_spoolfile_status = original_get_spoolfile_status