Exemplo n.º 1
0
    def test_should_filter_messages_by_date_range(self):
        message_attr = self.message.copy()
        date_1_jan = datetime.datetime(2014, 01, 01)
        message_attr['received_at'] = date_1_jan
        message_now = RapidProMessage(**message_attr).save()

        message_attr2 = self.message.copy()
        date_3_jan = datetime.datetime(2014, 01, 03)
        message_attr2['received_at'] = date_3_jan
        message_2days_later = RapidProMessage(**message_attr2).save()

        response = self.client.get(self.API_ENDPOINT, {"to": "2014-01-02", "format": "json"})

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual(str(message_now.id), response.data[0]['id'])
        expected_message = self.expected_message.copy()
        expected_message['time'] = date_1_jan.replace(tzinfo=pytz.utc)
        self.assertDictContainsSubset(expected_message, response.data[0])

        response = self.client.get(self.API_ENDPOINT, {"from": "2014-01-02", "format": "json"})

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual(str(message_2days_later.id), response.data[0]['id'])
        expected_message2 = self.expected_message.copy()
        expected_message2['time'] = date_3_jan.replace(tzinfo=pytz.utc)
        self.assertDictContainsSubset(expected_message2, response.data[0])

        response = self.client.get(self.API_ENDPOINT, {"from": "2014-01-02", "to": "2014-01-02",  "format": "json"})

        self.assertEqual(200, response.status_code)
        self.assertEqual(0, len(response.data))
Exemplo n.º 2
0
    def test_should_filter_messages_by_disaster_association(self):
        self.message['text'] = 'unassociate-able disaster text text' #short circuit auto disaster association
        self.expected_message['text'] = 'unassociate-able disaster text text'
        uncategorized_message = RapidProMessage(**self.message).save()
        message_2 = self.message.copy()
        message_2['text'] = 'some other text'
        message_2['disaster'] = self.disaster

        categorized_message = RapidProMessage(**message_2).save()

        response = self.client.get(self.API_ENDPOINT, {"disaster": "", "format": "json"})

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual(str(uncategorized_message.id), response.data[0]['id'])
        self.assertDictContainsSubset(self.expected_message, response.data[0])

        response = self.client.get(self.API_ENDPOINT, {"disaster": str(self.disaster.id), "format": "json"})

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual(str(categorized_message.id), response.data[0]['id'])
        expected_message2= self.expected_message.copy()
        expected_message2['text'] = message_2['text']
        self.assertDictContainsSubset(expected_message2, response.data[0])
Exemplo n.º 3
0
    def test_should_filter_csv_when_date_range_is_specified(self):
        new_date_time = datetime.datetime(2015, 9, 17, 16, 0, 49, 807000)
        new_date_time = new_date_time.replace(tzinfo=pytz.utc)
        disaster_type = DisasterType(**dict(name='Fire', description="Some fire")).save()
        disaster_attr = dict(name=disaster_type, locations=[self.district], description="Big Fire",
                                  date=self.date_time,
                                  status="Assessment")
        kampala_disaster = Disaster(**disaster_attr).save()

        RapidProMessage(**self.message).save()
        self.message['received_at'] = new_date_time
        RapidProMessage(**self.message).save()
        expected_response = "phone,text,source,location,time\n" \
                            "+256775019449,NECOC.Bukoto. There is a fire,timothy,Kampala >> Bukoto,%s,Fire" % self.date_time

        response = self.client.get(self.CSV_ENDPOINT, {'dfrom' : 'undefined', 'dto': '2014-11-26', 'format': 'csv'})
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))

        response = self.client.get(self.CSV_ENDPOINT, {'dfrom' : '2015-01-01', 'dto': 'undefined', 'format': 'csv'})
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))

        response = self.client.get(self.CSV_ENDPOINT, {'dfrom' : '2014-01-01', 'dto': '2014-11-26', 'format': 'csv'})
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))

        response = self.client.get(self.CSV_ENDPOINT, {'dfrom' : '2014-01-01', 'dto': '2015-11-26', 'format': 'csv'})
        self.assertEqual(200, response.status_code)
        self.assertEqual(2, len(response.data))

        self.assertTrue(isinstance(response.accepted_renderer, CSVRenderer))
Exemplo n.º 4
0
    def test_location_filter_should_return_messages_in_all_children_location_and_in_order(self):
        Disaster.objects.delete() #Remove all disasters to avoid interference with this test
        bukoto_message = RapidProMessage(**self.message).save()
        wakiso = Location(**(dict(name='Wakiso', type='village', parent=self.district))).save()
        other_phone_number = '1234'
        one_hour_later_date = self.date_time + datetime.timedelta(hours=1)
        one_hour_later_date = one_hour_later_date.replace(tzinfo=pytz.utc)
        other_message_options = dict(phone_no=other_phone_number, text=self.text_format % wakiso.name, received_at=one_hour_later_date,
                                     relayer_id=234, run_id=23243)
        user_profile = UserProfile(**dict(name='timothy', phone=other_phone_number, location=wakiso)).save()
        wakiso_message = RapidProMessage(**other_message_options).save()

        response = self.client.get(self.API_ENDPOINT, {"location": self.district.id, "format": "json"})

        wakiso_expected_message = {'phone': other_phone_number, 'time': one_hour_later_date, 'relayer': 234, 'run': 23243,
                                   'text': self.text_format % wakiso.name, 'disaster': None}
        wakiso_expected_message = dict(wakiso_expected_message.items() + {
            'source': user_profile.name,
            'id': str(wakiso_message.id),
            'location': str(wakiso),
            'profile_id': str(user_profile.id),
            'auto_associated': False}.items())

        bukoto_expected_message = dict(self.expected_message.items() + {
            'source': self.mobile_user.name,
            'id': str(bukoto_message.id),
            'disaster': None,
            'location': str(self.village),
            'profile_id': str(self.mobile_user.id),
            'auto_associated': False}.items())

        self.assertEqual(200, response.status_code)
        self.assertEqual(2, len(response.data))
        self.assertEqual(wakiso_expected_message, response.data[0])
        self.assertEqual(bukoto_expected_message, response.data[1])
Exemplo n.º 5
0
    def test_editing_existing_message_is_valid(self):
        district = Location(
            **dict(name='Kampala', type='district', parent=None)).save()
        disaster_type = DisasterType(
            **dict(name="Fire", description="Fire")).save()

        disaster_attributes = dict(name=disaster_type,
                                   locations=[district],
                                   description="Big Flood",
                                   date="2014-12-01 00:00:00",
                                   status="Assessment")
        disaster = Disaster(**disaster_attributes).save()

        message = RapidProMessage(**self.serialized_data).save()

        data = self.serialized_data.copy()
        data['disaster'] = disaster.id

        serializer = RapidProMessageSerializer(message, data=data)

        self.assertTrue(serializer.is_valid())
        new_message = serializer.save()

        self.assertTrue(message.id, new_message.id)
        self.assertEqual(disaster, new_message.disaster)
Exemplo n.º 6
0
    def test_message_location_str_returns_the_to_str_of_its_location(self):
        message = self.message.copy()
        message['text'] = "NECOC.Bukoto. there are some serious fire over here"

        rapid_pro_message = RapidProMessage(**message).save()

        self.assertEqual("Kampala >> Bukoto", rapid_pro_message.location_str())
Exemplo n.º 7
0
    def test_message_defaults_to_userprofile_location_if_message_content_does_not_specify_location(
            self):
        message = self.message.copy()
        message['text'] = "NECOC hahaha Fire"
        rapid_pro_message = RapidProMessage(**message).save()

        self.assertEqual(self.mobile_user.location, rapid_pro_message.location)
Exemplo n.º 8
0
    def test_message_defaults_to_userprofile_location_if_message_is_empty(
            self):
        message = self.message.copy()
        message['text'] = ""
        rapid_pro_message = RapidProMessage(**message).save()

        self.assertEqual(self.mobile_user.location, rapid_pro_message.location)
Exemplo n.º 9
0
 def test_fields(self):
     expected_fields = [
         'text', 'created_at', 'phone_no', 'received_at', 'location',
         'disaster'
     ]
     rapidpro_message = RapidProMessage()
     for field in expected_fields:
         self.assertTrue(hasattr(rapidpro_message, field))
Exemplo n.º 10
0
    def setUp(self):
        self.location_name = 'Kampala'
        text = "NECOC.%s. fire baba fire" % self.location_name
        date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        phone_number = "+256775019449"
        self.message = dict(phone_no=phone_number,
                            text=text,
                            received_at=date_time,
                            relayer_id=234,
                            run_id=23243)
        self.district = Location(**dict(
            name=self.location_name, parent=None, type='district')).save()

        RapidProMessage(**self.message).save()
        message1 = self.message.copy()
        message1["text"] = "some message that is not coming from Kampala"
        RapidProMessage(**message1).save()
Exemplo n.º 11
0
    def test_message_gets_the_location_if_it_is_militarily_coded_and_matched(
            self):
        message = self.message.copy()
        message['text'] = "NECOC . Kampala. Fire"
        rapid_pro_message = RapidProMessage(**message).save()

        message_location = rapid_pro_message.location

        self.assertEqual(self.district, message_location)
Exemplo n.º 12
0
    def test_should_update_disaster_field_using_a_patch(self):
        message = RapidProMessage(**self.message).save()

        response = self.client.patch(self._api_url(message.id), data=dict(disaster=self.disaster.id))
        self.assertEqual(200, response.status_code)

        retrieved_message = RapidProMessage.objects(**self.message)
        self.assertEqual(1, retrieved_message.count())
        self.assertEqual(self.disaster, retrieved_message[0].disaster)
Exemplo n.º 13
0
    def test_get_messages_from_a_location(self):
        location_name = 'Abim'
        text = "NECOC.%s. fire baba fire" % location_name
        district = Location(
            **dict(name=location_name, parent=None, type='district')).save()
        message_attr = self.message.copy()
        message_attr['text'] = text
        message = RapidProMessage(**message_attr).save()

        message1_attr = message_attr.copy()
        message1_attr["text"] = " message without location"
        message1 = RapidProMessage(**message1_attr).save()

        location_messages = RapidProMessage.from_(district)

        self.assertEqual(1, location_messages.count())
        self.assertIn(message, location_messages)
        self.assertNotIn(message1, location_messages)
Exemplo n.º 14
0
    def test_should_update_disaster_field_with_post_is_also_supported_for_phantomjs_sake(self):
        message = RapidProMessage(**self.message).save()

        response = self.client.post(self._api_url(message.id), data=dict(disaster=self.disaster.id))
        self.assertEqual(200, response.status_code)

        retrieved_message = RapidProMessage.objects(**self.message)
        self.assertEqual(1, retrieved_message.count())
        self.assertEqual(self.disaster, retrieved_message[0].disaster)
Exemplo n.º 15
0
    def test_should_get_rapid_pro_message(self):
        RapidProMessage(**self.message).save()
        response = self.client.get(self.API_ENDPOINT, format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertDictContainsSubset(self.expected_message, response.data[0])
        self.assertEqual(self.mobile_user.name, response.data[0]['source'])
        self.assertEqual('Kampala >> Bukoto', response.data[0]['location'])
        self.assertIsNotNone(response.data[0]['id'])
Exemplo n.º 16
0
    def test_message_location_str_defaults_to_userprofile_location_str_if_no_location(
            self):
        message = self.message.copy()
        message[
            'text'] = "NECOC.UnknownLocation. there are some serious fire over here"

        rapid_pro_message = RapidProMessage(**message).save()

        self.assertEqual(self.mobile_user.location.__unicode__(),
                         rapid_pro_message.location_str())
Exemplo n.º 17
0
    def test_supplied_location_superseded_militarily_coded_matched_location(
            self):
        message = self.message.copy()
        message['text'] = "NECOC Fire Kampala"
        message['location'] = self.village
        rapid_pro_message = RapidProMessage(**message).save()

        message_location = rapid_pro_message.location

        self.assertEqual(self.village, message_location)
Exemplo n.º 18
0
 def test_should_auto_associate_message_to_disaster_when_posted_from_subcounty(self):
     disaster_type = DisasterType(**dict(name='Flood', description="Some flood")).save()
     disaster_attr = dict(name=disaster_type, locations=[self.district], description="Big Flood",
                               date=self.date_time,
                               status="Assessment")
     kampala_disaster = Disaster(**disaster_attr).save()
     text = "NECOC.%s. flood here and allover the place!" % self.village.name
     self.message['text'] = text
     saved_message = RapidProMessage(**self.message).save()
     self.assertEqual(saved_message.disaster, kampala_disaster)
    def test_filter_stats_in_subcounties_in_district_by_disaster_type(self):
        RapidProMessage(**self.message).save()

        bugolobi_name = 'Bugolobi'
        bugolobi = Location(**dict(
            name=bugolobi_name, parent=self.kampala, type='subcounty')).save()
        text = "NECOC.%s. flood" % bugolobi_name
        message_bugolobi = dict(phone_no='123444',
                                text=text,
                                received_at=self.date_time,
                                relayer_id=234,
                                run_id=23243)

        Disaster(**self.disaster_attr).save()
        disaster_attr_bugolobi = self.disaster_attr.copy()
        disaster_attr_bugolobi["locations"] = [bugolobi]
        disaster_bugolobi = Disaster(**disaster_attr_bugolobi).save()
        message_bugolobi['disaster'] = disaster_bugolobi

        RapidProMessage(**message_bugolobi).save()

        expected_serialized_data = {
            'bugolobi': {
                'messages': {
                    'count': 1,
                    'percentage': 100,
                    'reporter_ratio': 0
                },
                'disasters': {
                    'count': 1,
                    'percentage': 50,
                    'reporter_ratio': 0
                }
            }
        }

        url = self.API_ENDPOINT + '%s/?format=json&disaster_type=%s' % (str(
            self.kampala.name.lower()), str(self.disaster_type.id))
        response = self.client.get(url, format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)
Exemplo n.º 20
0
    def test_get_messages_from_children_are_also_added(self):
        location_name = 'Abim'
        text = "NECOC.%s. fire baba fire" % location_name
        district = Location(
            **dict(name=location_name, parent=None, type='district')).save()
        message_attr = self.message.copy()
        message_attr['text'] = text
        message = RapidProMessage(**message_attr).save()

        message1_attr = message_attr.copy()
        location_name = 'Wakiso'
        text = "NECOC.%s. fire baba fire" % location_name
        district_son = Location(**dict(
            name=location_name, parent=district, type='village')).save()
        message1_attr["text"] = text
        message1 = RapidProMessage(**message1_attr).save()

        location_messages = RapidProMessage.from_(district)

        self.assertEqual(2, location_messages.count())
        self.assertIn(message, location_messages)
        self.assertIn(message1, location_messages)
Exemplo n.º 21
0
    def test_should_update_disaster_field_with_put(self):
        message = RapidProMessage(**self.message).save()

        self.expected_message['time'] = self.date_time.replace(tzinfo=None)
        data = self.expected_message.copy()
        data['disaster'] = self.disaster.id

        response = self.client.put(self._api_url(message.id), data=data)
        self.assertEqual(200, response.status_code)

        retrieved_message = RapidProMessage.objects(**self.message)
        self.assertEqual(1, retrieved_message.count())
        self.assertEqual(self.disaster, retrieved_message[0].disaster)
Exemplo n.º 22
0
 def test_should_serialize_rapid_pro_message_object(self):
     rapid_pro_message = RapidProMessage(**self.message).save()
     serialized_object = RapidProMessageSerializer(rapid_pro_message)
     self.serialized_data['time'] = self.date_time.replace(tzinfo=pytz.utc)
     serialized_data_with_source = dict(
         self.serialized_data.items() + {
             'id': str(rapid_pro_message.id),
             'source': self.mobile_user.name,
             'disaster': None,
             'location': 'Kampala >> Bukoto',
             'profile_id': str(self.mobile_user.id),
             'auto_associated': False
         }.items())
     self.assertEqual(serialized_data_with_source, serialized_object.data)
Exemplo n.º 23
0
    def test_should_return_csv_when_csv_endpoint_is_called(self):
        disaster_type = DisasterType(**dict(name='Fire', description="Some fire")).save()
        disaster_attr = dict(name=disaster_type, locations=[self.district], description="Big Fire",
                                  date=self.date_time,
                                  status="Assessment")
        kampala_disaster = Disaster(**disaster_attr).save()

        msg = RapidProMessage(**self.message).save()
        expected_response = "phone,text,source,location,time,disaster\n" \
                            "+256775019449,NECOC.Bukoto. There is a fire,timothy,Kampala >> Bukoto,%s,Fire" % msg.received_at
        response = self.client.get(self.CSV_ENDPOINT, {'format': 'csv'})
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertTrue(isinstance(response.accepted_renderer, CSVRenderer))
    def test_should_retrieve_message_stats_in_all_locations(self):
        RapidProMessage(**self.message).save()
        RapidProMessage(**self.message_bukoto).save()
        Disaster(**self.disaster_attr).save()
        Disaster(**self.disaster_attr_bukoto).save()

        expected_serialized_data = {
            'kampala': {
                'messages': {
                    'count': 1,
                    'percentage': 50,
                    'reporter_ratio': 0
                },
                'disasters': {
                    'count': 1,
                    'percentage': 50,
                    'reporter_ratio': 0
                }
            },
            'bukoto': {
                'messages': {
                    'count': 1,
                    'percentage': 50,
                    'reporter_ratio': 0
                },
                'disasters': {
                    'count': 1,
                    'percentage': 50,
                    'reporter_ratio': 0
                }
            }
        }

        response = self.client.get(self.API_ENDPOINT, format='json')
        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)
Exemplo n.º 25
0
    def test_should_return_only_district_messages_for_CAO(self):
        self.mobile_user.user = self.cao_user
        self.mobile_user.save()

        masaka_district = Location(**dict(name='Masaka', parent=None, type='district')).save()
        masaka_village = Location(**dict(name='Bukasa', parent=masaka_district, type='village')).save()
        masaka_message = self.message.copy()
        masaka_message['location'] = masaka_village
        masaka_message['phone_no'] = "+256775019441"
        msk_msg = RapidProMessage(**masaka_message).save()
        kla_msg = RapidProMessage(**self.message).save()

        login_response = self.client.post(self.login_url, self.login_data)
        self.assertRedirects(login_response, '/')
        self.assertTrue('sessionid' in login_response.cookies)

        response = self.client.get(self.API_ENDPOINT, format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertDictContainsSubset(self.expected_message, response.data[0])
        self.assertEqual(self.mobile_user.name, response.data[0]['source'])
        self.assertEqual('Kampala >> Bukoto', response.data[0]['location'])
        self.assertIsNotNone(response.data[0]['id'])
Exemplo n.º 26
0
    def test_should_filter_messages_by_location(self):
        RapidProMessage(**self.message).save()
        wakiso = Location(**(dict(name='Wakiso', type='village'))).save()

        response = self.client.get(self.API_ENDPOINT, {"location": wakiso.id, "format": "json"})

        self.assertEqual(200, response.status_code)
        self.assertEqual(0, len(response.data))

        other_phone_number = '1234'
        other_message_options = dict(phone_no=other_phone_number, text=self.text_format % wakiso.name, received_at=self.date_time,
                                     relayer_id=234, run_id=23243)
        RapidProMessage(**other_message_options).save()

        response = self.client.get(self.API_ENDPOINT, {"location": wakiso.id, "format": "json"})

        expected_message = {'phone': other_phone_number, 'time': self.date_time, 'relayer': 234, 'run': 23243,
                            'text': self.text_format % wakiso.name}

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertDictContainsSubset(expected_message, response.data[0])
        self.assertEqual('NECOC Volunteer', response.data[0]['source'])
        self.assertEqual(wakiso.name, response.data[0]['location'])
Exemplo n.º 27
0
    def test_should_retrieve_message_stats_in_all_locations(self):
        RapidProMessage(**self.message).save()
        RapidProMessage(**self.message_bukoto).save()
        Disaster(**self.disaster_attr).save()

        multi_location_serializer = MultiLocationStatsSerializer(location=None)
        serialized_object = multi_location_serializer

        expected_serialized_data = {
            'kampala': {
                'messages': {
                    'count': 1,
                    'percentage': 50,
                    'reporter_ratio': 0
                },
                'disasters': {
                    'count': 1,
                    'percentage': 100,
                    'reporter_ratio': 0
                }
            },
            'bukoto': {
                'messages': {
                    'count': 1,
                    'percentage': 50,
                    'reporter_ratio': 0
                },
                'disasters': {
                    'count': 0,
                    'percentage': 0,
                    'reporter_ratio': 0
                }
            }
        }

        self.assertEqual(expected_serialized_data, serialized_object.data)
Exemplo n.º 28
0
    def test_save_rapid_pro_message(self):
        self.message['location'] = self.village
        disaster_type = DisasterType(
            **dict(name="Fire", description="Fire")).save()
        disaster_attributes = dict(name=disaster_type,
                                   locations=[self.village],
                                   description="Big Flood",
                                   date="2014-12-01 00:00:00",
                                   status="Assessment")
        disaster = Disaster(**disaster_attributes).save()
        self.message['disaster'] = disaster

        RapidProMessage(**self.message).save()

        rp_messages = RapidProMessage.objects(**self.message)
        self.assertEqual(1, rp_messages.count())
Exemplo n.º 29
0
    def test_get_user_profile_name_if_exists(self):
        self.message['location'] = self.village
        disaster_type = DisasterType(
            **dict(name="Fire", description="Fire")).save()
        disaster_attributes = dict(name=disaster_type,
                                   locations=[self.village],
                                   description="Big Flood",
                                   date="2014-12-01 00:00:00",
                                   status="Assessment")
        disaster = Disaster(**disaster_attributes).save()
        self.message['disaster'] = disaster

        RapidProMessage(**self.message).save()

        rp_messages = RapidProMessage.objects(**self.message)
        self.assertEqual(self.mobile_user.name, rp_messages[0].source())
Exemplo n.º 30
0
    def test_default_user_profile_name_if_profile_names_not_enabled(self):
        AdminSetting._set("enable_volunteer_profiles", False)
        self.message['location'] = self.village
        disaster_type = DisasterType(
            **dict(name="Fire", description="Fire")).save()
        disaster_attributes = dict(name=disaster_type,
                                   locations=[self.village],
                                   description="Big Flood",
                                   date="2014-12-01 00:00:00",
                                   status="Assessment")
        disaster = Disaster(**disaster_attributes).save()
        self.message['disaster'] = disaster

        RapidProMessage(**self.message).save()

        rp_messages = RapidProMessage.objects(**self.message)
        self.assertEqual("NECOC Volunteer", rp_messages[0].source())