def test_ambulance_create_serializer(self): serializer = AmbulanceSerializer( data={ 'identifier': 'NEW-1897', 'capability': AmbulanceCapability.R.name, 'comment': 'no comments' }) serializer.is_valid() serializer.save(updated_by=self.u1) # test AmbulanceSerializer a = Ambulance.objects.get(identifier='NEW-1897') serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': 'NEW-1897', 'comment': 'no comments', 'capability': AmbulanceCapability.R.name, 'status': AmbulanceStatus.UK.name, 'orientation': a.orientation, 'location': point2str(a.location), 'timestamp': date2iso(a.timestamp), 'client_id': None, 'updated_by': self.u1.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result)
def on_ambulance(self, client, userdata, msg): logger.debug("on_ambulance: msg = '{}:{}'".format( msg.topic, msg.payload)) # parse topic values = self.parse_topic(msg) if not values: return logger.debug("on_ambulance: values = '{}'".format(values)) try: # retrieve parsed values user, data, ambulance_id = values logger.debug('ambulance_id = {}'.format(ambulance_id)) # retrieve ambulance ambulance = Ambulance.objects.get(id=ambulance_id) except ObjectDoesNotExist: # send error message to user self.send_error_message( user, msg.topic, msg.payload, "Ambulance with id '{}' does not exist".format(ambulance_id)) return except Exception as e: # send error message to user self.send_error_message(user, msg.topic, msg.payload, "Exception: '{}'".format(e)) return logger.debug('on_ambulance: ambulance = {}'.format(ambulance)) try: # update ambulance serializer = AmbulanceSerializer(ambulance, data=data, partial=True) if serializer.is_valid(): logger.debug('on_ambulance: valid serializer') # save to database serializer.save(updated_by=user) else: logger.debug('on_ambulance: INVALID serializer') # send error message to user self.send_error_message(user, msg.topic, msg.payload, serializer.errors) except Exception as e: logger.debug('on_ambulance: serializer EXCEPTION') # send error message to user self.send_error_message(user, msg.topic, msg.payload, e) logger.debug('on_ambulance: DONE')
def test(self): # Update ambulance a1 a = self.a1 user = self.u1 status = AmbulanceStatus.AH.name serializer = AmbulanceSerializer(Ambulance.objects.get(id=a.id), data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) timestamp = timezone.now() location = {'latitude': -2., 'longitude': 7.} serializer = AmbulanceSerializer(Ambulance.objects.get(id=a.id), data={ 'location': location, 'timestamp': timestamp }, partial=True) serializer.is_valid() serializer.save(updated_by=user) status = AmbulanceStatus.OS.name serializer = AmbulanceSerializer(Ambulance.objects.get(id=a.id), data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # This update does not go to AmbulanceUpdate! serializer = AmbulanceSerializer(Ambulance.objects.get(id=a.id), data={ 'identifier': 'someid', }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test AmbulanceUpdateSerializer queryset = AmbulanceUpdate.objects.filter(ambulance=a) answer1 = [] for u in queryset: serializer = AmbulanceUpdateSerializer(u) result = { 'id': u.id, 'ambulance_id': u.ambulance.id, 'ambulance_identifier': u.ambulance.identifier, 'comment': u.comment, 'capability': u.capability, 'status': u.status, 'orientation': u.orientation, 'location': point2str(u.location), 'timestamp': date2iso(u.timestamp), 'updated_by_username': u.updated_by.username, 'updated_on': date2iso(u.updated_on) } answer1.append(serializer.data) self.assertDictEqual(serializer.data, result) # Update ambulance a2 a = self.a3 user = self.u3 status = AmbulanceStatus.AH.name serializer = AmbulanceSerializer(a, data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) timestamp = timezone.now() location = {'latitude': -2., 'longitude': 7.} serializer = AmbulanceSerializer(a, data={ 'location': location, 'timestamp': timestamp }, partial=True) serializer.is_valid() serializer.save(updated_by=user) status = AmbulanceStatus.OS.name serializer = AmbulanceSerializer(a, data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test AmbulanceUpdateSerializer queryset = AmbulanceUpdate.objects.filter(ambulance=a) answer3 = [] for u in queryset: serializer = AmbulanceUpdateSerializer(u) result = { 'id': u.id, 'ambulance_id': u.ambulance.id, 'ambulance_identifier': a.identifier, 'comment': u.comment, 'capability': u.capability, 'status': u.status, 'orientation': u.orientation, 'location': point2str(u.location), 'timestamp': date2iso(u.timestamp), 'updated_by_username': u.updated_by.username, 'updated_on': date2iso(u.updated_on) } answer3.append(serializer.data) self.assertDictEqual(serializer.data, result) # Test api # instantiate client client = Client() # login as admin client.login(username=settings.MQTT['USERNAME'], password=settings.MQTT['PASSWORD']) # retrieve ambulances updates response = client.get('/en/api/ambulance/{}/updates/'.format( self.a1.id), follow=True) self.assertEqual(response.status_code, 200) result = JSONParser().parse(BytesIO(response.content)) self.assertCountEqual(result, answer1) self.assertEqual(len(result), 4) # retrieve ambulances updates response = client.get('/en/api/ambulance/{}/updates/'.format( self.a3.id), follow=True) self.assertEqual(response.status_code, 200) result = JSONParser().parse(BytesIO(response.content)) self.assertCountEqual(result, answer3) self.assertEqual(len(result), 4) # logout client.logout() # login as testuser1 client.login(username='******', password='******') # retrieve ambulances response = client.get('/en/api/ambulance/{}/updates/'.format( self.a1.id), follow=True) self.assertEqual(response.status_code, 404) # retrieve ambulances response = client.get('/en/api/ambulance/{}/updates/'.format( self.a3.id), follow=True) self.assertEqual(response.status_code, 404) # logout client.logout() # login as testuser2 client.login(username='******', password='******') # retrieve ambulances response = client.get('/en/api/ambulance/{}/updates/'.format( self.a1.id), follow=True) self.assertEqual(response.status_code, 404) # retrieve ambulances updates response = client.get('/en/api/ambulance/{}/updates/'.format( self.a3.id), follow=True) self.assertEqual(response.status_code, 200) result = JSONParser().parse(BytesIO(response.content)) self.assertCountEqual(result, answer3) self.assertEqual(len(result), 4) # logout client.logout()
def test_ambulance_update_serializer(self): # superuser first # Update ambulance status a = self.a1 user = self.u1 status = AmbulanceStatus.AH.name serializer = AmbulanceSerializer(a, data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': status, 'orientation': a.orientation, 'location': point2str(a.location), 'client_id': None, 'timestamp': date2iso(a.timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # Update ambulance location timestamp = timezone.now() location = {'latitude': -2., 'longitude': 7.} serializer = AmbulanceSerializer(a, data={ 'location': location, 'timestamp': timestamp, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'client_id': None, 'timestamp': date2iso(timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # error update timestamp without location or status serializer = AmbulanceSerializer(a, data={ 'timestamp': timestamp, }, partial=True) self.assertEqual(serializer.is_valid(), False) # regular authorized user # Update ambulance status a = self.a3 user = self.u3 status = AmbulanceStatus.AH.name serializer = AmbulanceSerializer(a, data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': status, 'orientation': a.orientation, 'location': point2str(a.location), 'client_id': None, 'timestamp': date2iso(a.timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # Update ambulance location timestamp = timezone.now() location = {'latitude': -2., 'longitude': 7.} serializer = AmbulanceSerializer(a, data={ 'location': location, 'timestamp': timestamp }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'client_id': None, 'timestamp': date2iso(timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # error update timestamp without location or status serializer = AmbulanceSerializer(a, data={ 'timestamp': timestamp, }, partial=True) self.assertEqual(serializer.is_valid(), False) # update client client1 = loginClient(client_id='client_id_1', user_id=self.u1.id, ambulance=a) client1.save() client2 = loginClient(client_id='client_id_2', user_id=self.u3.id) client2.save() self.assertEqual(client1.ambulance, a) self.assertEqual(client2.ambulance, None) self.assertEqual(a.client, client1) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'client_id': client1.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will not change client2.ambulance = self.a2 with self.assertRaises(Exception) as raised: with transaction.atomic(): client2.save() self.assertEqual(PermissionDenied, type(raised.exception)) # will not change client2.ambulance = a with self.assertRaises(Exception) as raised: with transaction.atomic(): client2.save() self.assertEqual(IntegrityError, type(raised.exception)) client2 = loginClient.objects.get(client_id='client_id_2') a = Ambulance.objects.get(id=a.id) self.assertEqual(client1.ambulance, a) self.assertEqual(client2.ambulance, None) self.assertEqual(a.client, client1) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'client_id': client1.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will reset client1.ambulance = None client1.save() a = Ambulance.objects.get(id=a.id) self.assertEqual(client1.ambulance, None) self.assertEqual(client2.ambulance, None) self.assertEqual(hasattr(a, 'client'), False) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'client_id': None, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will change client2.ambulance = a client2.save() self.assertEqual(client1.ambulance, None) self.assertEqual(client2.ambulance, a) self.assertEqual(a.client, client2) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'client_id': client2.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will not change in partial update serializer = AmbulanceSerializer( a, data={'status': AmbulanceStatus.OS.name}, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': AmbulanceStatus.OS.name, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'client_id': client2.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will not change in partial update serializer = AmbulanceSerializer(a, data={ 'status': AmbulanceStatus.PB.name, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': AmbulanceStatus.PB.name, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'client_id': client2.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result)
def test_ambulance_update_serializer(self): # superuser first # Update ambulance status a = self.a1 user = self.u1 status = AmbulanceStatus.AH.name serializer = AmbulanceSerializer(a, data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': status, 'orientation': a.orientation, 'location': point2str(a.location), 'location_timestamp': date2iso(a.location_timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # Update ambulance location location_timestamp = timezone.now() location = {'latitude': -2., 'longitude': 7.} serializer = AmbulanceSerializer(a, data={ 'location': location, 'location_timestamp': location_timestamp, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'location_timestamp': date2iso(location_timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # error update location with timestamp serializer = AmbulanceSerializer(a, data={ 'location': location, }, partial=True) self.assertEqual(serializer.is_valid(), False) # error update timestamp without location serializer = AmbulanceSerializer(a, data={ 'location_timestamp': location_timestamp, }, partial=True) self.assertEqual(serializer.is_valid(), False) # regular authorized user # Update ambulance status a = self.a3 user = self.u3 status = AmbulanceStatus.AH.name serializer = AmbulanceSerializer(a, data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': status, 'orientation': a.orientation, 'location': point2str(a.location), 'location_timestamp': date2iso(a.location_timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # Update ambulance location location_timestamp = timezone.now() location = {'latitude': -2., 'longitude': 7.} serializer = AmbulanceSerializer(a, data={ 'location': location, 'location_timestamp': location_timestamp }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'location_timestamp': date2iso(location_timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # error update location with timestamp serializer = AmbulanceSerializer(a, data={'location': location}, partial=True) self.assertEqual(serializer.is_valid(), False) # error update timestamp without location serializer = AmbulanceSerializer(a, data={ 'location_timestamp': location_timestamp, }, partial=True) self.assertEqual(serializer.is_valid(), False)
def test_ambulance_update_serializer(self): # superuser first # Update ambulance status a = self.a1 user = self.u1 status = AmbulanceStatus.AH.name serializer = AmbulanceSerializer(a, data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': status, 'orientation': a.orientation, 'location': point2str(a.location), 'location_client_id': None, 'timestamp': date2iso(a.timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # Update ambulance location timestamp = timezone.now() location = {'latitude': -2., 'longitude': 7.} serializer = AmbulanceSerializer(a, data={ 'location': location, 'timestamp': timestamp, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'location_client_id': None, 'timestamp': date2iso(timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # error update timestamp without location or status serializer = AmbulanceSerializer(a, data={ 'timestamp': timestamp, }, partial=True) self.assertEqual(serializer.is_valid(), False) # regular authorized user # Update ambulance status a = self.a3 user = self.u3 status = AmbulanceStatus.AH.name serializer = AmbulanceSerializer(a, data={ 'status': status, }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': status, 'orientation': a.orientation, 'location': point2str(a.location), 'location_client_id': None, 'timestamp': date2iso(a.timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # Update ambulance location timestamp = timezone.now() location = {'latitude': -2., 'longitude': 7.} serializer = AmbulanceSerializer(a, data={ 'location': location, 'timestamp': timestamp }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'location_client_id': None, 'timestamp': date2iso(timestamp), 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # error update timestamp without location or status serializer = AmbulanceSerializer(a, data={ 'timestamp': timestamp, }, partial=True) self.assertEqual(serializer.is_valid(), False) # update location_client client1 = loginClient(client_id='client_id_1', user_id=self.u1.id) client1.save() client2 = loginClient(client_id='client_id_2', user_id=self.u2.id) client2.save() serializer = AmbulanceSerializer( a, data={'location_client_id': client1.client_id}, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'location_client_id': client1.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will not change serializer = AmbulanceSerializer( a, data={'location_client_id': client2.client_id}, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'location_client_id': client1.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will reset serializer = AmbulanceSerializer(a, data={'location_client_id': None}, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'location_client_id': None, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will change serializer = AmbulanceSerializer( a, data={'location_client_id': client2.client_id}, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': a.status, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'location_client_id': client2.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will not change in partial update serializer = AmbulanceSerializer( a, data={'status': AmbulanceStatus.OS.name}, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': AmbulanceStatus.OS.name, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'location_client_id': client2.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # will not change in partial update serializer = AmbulanceSerializer(a, data={ 'status': AmbulanceStatus.PB.name, 'location_client_id': client1.client_id }, partial=True) serializer.is_valid() serializer.save(updated_by=user) # test serializer = AmbulanceSerializer(a) result = { 'id': a.id, 'identifier': a.identifier, 'comment': a.comment, 'capability': a.capability, 'status': AmbulanceStatus.PB.name, 'orientation': a.orientation, 'location': point2str(location), 'timestamp': date2iso(timestamp), 'location_client_id': client2.client_id, 'updated_by': user.id, 'updated_on': date2iso(a.updated_on) } self.assertDictEqual(serializer.data, result) # invalid client id serializer = AmbulanceSerializer(a, data={ 'status': AmbulanceStatus.PB.name, 'location_client_id': '__invalid__' }, partial=True) self.assertFalse(serializer.is_valid())