Пример #1
0
    def test_hospital_equipment_serializer(self):

        # test HospitalSerializer
        for he in (self.he1, self.he2, self.he3, self.he4):
            serializer = HospitalEquipmentSerializer(he)
            result = {
                'hospital_id': he.hospital.id,
                'hospital_name': he.hospital.name,
                'equipment_id': he.equipment.id,
                'equipment_name': he.equipment.name,
                'equipment_etype': he.equipment.etype,
                'value': he.value,
                'comment': he.comment,
                'updated_by': he.updated_by.id,
                'updated_on': date2iso(he.updated_on)
            }
            self.assertDictEqual(serializer.data, result)
Пример #2
0
    def on_hospital_equipment(self, client, userdata, msg):

        logger.debug("on_hospital_equipment: msg = '{}:{}'".format(
            msg.topic, msg.payload))

        # parse topic
        values = self.parse_topic(msg)
        if not values:
            return

        logger.debug("on_hospital_equipment: values = '{}'".format(values))

        try:

            # retrieve parsed values
            user, data, hospital_id, equipment_name = values

            # retrieve hospital equipment
            hospital_equipment = HospitalEquipment.objects.get(
                hospital=hospital_id, equipment__name=equipment_name)

        except ObjectDoesNotExist:

            # send error message to user
            self.send_error_message(
                user, msg.topic, msg.payload,
                "Hospital equipment with hospital id '{}' and name '{}' does not exist"
                .format(hospital_id, equipment_name))
            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_hospital_equipment: equipment = {}'.format(hospital_equipment))

        try:

            # update hospital equipment
            serializer = HospitalEquipmentSerializer(hospital_equipment,
                                                     data=data,
                                                     partial=True)
            if serializer.is_valid():

                logger.debug('on_hospital_equipment: valid serializer')

                # save to database
                serializer.save(updated_by=user)

            else:

                logger.debug('on_hospital_equipment: 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_hospital_equipment: serializer EXCEPTION')

            # send error message to user
            self.send_error_message(user, msg.topic, msg.payload, e)

        logger.debug('on_hospital_equipment: DONE')
Пример #3
0
    def test_hospital_equipment_update_viewset(self):

        # instantiate client
        client = Client()

        # login as admin
        client.login(username=settings.MQTT['USERNAME'],
                     password=settings.MQTT['PASSWORD'])

        # set equipment value
        value = 'True'
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id), str(self.e1.name)),
                                content_type='application/json',
                                data=json.dumps({'value': value}))
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        answer = HospitalEquipmentSerializer(
            HospitalEquipment.objects.get(hospital=self.h1.id,
                                          equipment=self.e1.id)).data
        self.assertDictEqual(result, answer)

        # retrieve equipment value
        response = client.get('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id), str(self.e1.name)),
                              follow=True)
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        self.assertEqual(result['value'], value)

        # set equipment comment
        comment = 'some comment'
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id), str(self.e1.name)),
                                content_type='application/json',
                                data=json.dumps({'comment': comment}))
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        answer = HospitalEquipmentSerializer(
            HospitalEquipment.objects.get(hospital=self.h1.id,
                                          equipment=self.e1.id)).data
        self.assertDictEqual(result, answer)

        # retrieve equipment comment
        response = client.get('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id), str(self.e1.name)),
                              follow=True)
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        self.assertEqual(result['value'], value)
        self.assertEqual(result['comment'], comment)

        # set inexistent equipment
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id), str(self.e3.name)),
                                content_type='application/json',
                                data=json.dumps({'comment': comment}))
        self.assertEqual(response.status_code, 404)

        # set wrong ambulance id
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id + 100), str(self.e1.name)),
                                content_type='application/json',
                                data=json.dumps({'comment': comment}))
        self.assertEqual(response.status_code, 404)

        # set wrong equipment name
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id), str(self.e1.name + 'wrong')),
                                content_type='application/json',
                                data=json.dumps({'comment': comment}))
        self.assertEqual(response.status_code, 404)

        # logout
        client.logout()

        # login as testuser1
        client.login(username='******', password='******')

        # set equipment value
        value = 'False'
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h2.id), str(self.e1.name)),
                                content_type='application/json',
                                data=json.dumps({'value': value}))
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        answer = HospitalEquipmentSerializer(
            HospitalEquipment.objects.get(hospital=self.h2.id,
                                          equipment=self.e1.id)).data
        self.assertDictEqual(result, answer)

        # retrieve equipment value
        response = client.get('/api/hospital/{}/equipment/{}/'.format(
            str(self.h2.id), str(self.e1.name)),
                              follow=True)
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        self.assertEqual(result['value'], value)

        # set equipment comment
        comment = 'some new comment'
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h2.id), str(self.e1.name)),
                                content_type='application/json',
                                data=json.dumps({'comment': comment}))
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        answer = HospitalEquipmentSerializer(
            HospitalEquipment.objects.get(hospital=self.h2.id,
                                          equipment=self.e1.id)).data
        self.assertDictEqual(result, answer)

        # retrieve equipment comment
        response = client.get('/api/hospital/{}/equipment/{}/'.format(
            str(self.h2.id), str(self.e1.name)),
                              follow=True)
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        self.assertEqual(result['value'], value)
        self.assertEqual(result['comment'], comment)

        # not permitted to write
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id), str(self.e1.name)),
                                content_type='application/json',
                                data=json.dumps({'value': value}))
        self.assertEqual(response.status_code, 404)

        # logout
        client.logout()

        # login as testuser2
        client.login(username='******', password='******')

        # set equipment value
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id), str(self.e1.name)),
                                content_type='application/json',
                                data=json.dumps({'value': value}))
        self.assertEqual(response.status_code, 404)

        # set equipment value
        response = client.patch('/api/hospital/{}/equipment/{}/'.format(
            str(self.h1.id), str(self.e2.name)),
                                content_type='application/json',
                                data=json.dumps({'value': value}))
        self.assertEqual(response.status_code, 404)

        # logout
        client.logout()
Пример #4
0
    def test_hospital_equipment_list_viewset(self):

        # instantiate client
        client = Client()

        # login as admin
        client.login(username=settings.MQTT['USERNAME'],
                     password=settings.MQTT['PASSWORD'])

        # retrieve all hospital equipment
        response = client.get('/api/hospital/{}/equipment/'.format(
            str(self.h1.id)),
                              follow=True)
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        answer = [
            HospitalEquipmentSerializer(
                HospitalEquipment.objects.get(hospital=self.h1.id,
                                              equipment=self.e1.id)).data,
            HospitalEquipmentSerializer(
                HospitalEquipment.objects.get(hospital=self.h1.id,
                                              equipment=self.e2.id)).data
        ]

        self.assertCountEqual(result, answer)

        # retrieve all hospital equipment
        response = client.get('/api/hospital/{}/equipment/'.format(
            str(self.h2.id)),
                              follow=True)
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        answer = [
            HospitalEquipmentSerializer(
                HospitalEquipment.objects.get(hospital=self.h2.id,
                                              equipment=self.e1.id)).data,
            HospitalEquipmentSerializer(
                HospitalEquipment.objects.get(hospital=self.h2.id,
                                              equipment=self.e3.id)).data
        ]
        self.assertCountEqual(result, answer)

        # retrieve all hospital equipment
        response = client.get('/api/hospital/{}/equipment/'.format(
            str(self.h3.id)),
                              follow=True)
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        answer = [
            HospitalEquipmentSerializer(
                HospitalEquipment.objects.get(hospital=self.h3.id,
                                              equipment=self.e1.id)).data
        ]
        self.assertCountEqual(result, answer)

        # retrieve inexistent
        response = client.get('/api/hospital/{}/equipment/'.format(1000),
                              follow=True)
        self.assertEqual(response.status_code, 404)

        # logout
        client.logout()

        # login as testuser1
        client.login(username='******', password='******')

        # retrieve all hospital equipment
        response = client.get('/api/hospital/{}/equipment/'.format(
            str(self.h1.id)),
                              follow=True)
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        answer = [
            HospitalEquipmentSerializer(
                HospitalEquipment.objects.get(hospital=self.h1.id,
                                              equipment=self.e1.id)).data,
            HospitalEquipmentSerializer(
                HospitalEquipment.objects.get(hospital=self.h1.id,
                                              equipment=self.e2.id)).data
        ]

        self.assertCountEqual(result, answer)

        # retrieve all hospital equipment
        response = client.get('/api/hospital/{}/equipment/'.format(
            str(self.h2.id)),
                              follow=True)
        self.assertEqual(response.status_code, 200)
        result = JSONParser().parse(BytesIO(response.content))
        answer = [
            HospitalEquipmentSerializer(
                HospitalEquipment.objects.get(hospital=self.h2.id,
                                              equipment=self.e1.id)).data,
            HospitalEquipmentSerializer(
                HospitalEquipment.objects.get(hospital=self.h2.id,
                                              equipment=self.e3.id)).data
        ]
        self.assertCountEqual(result, answer)

        # retrieve all hospital equipment
        response = client.get('/api/hospital/{}/equipment/'.format(
            str(self.h3.id)),
                              follow=True)
        self.assertEqual(response.status_code, 404)

        # logout
        client.logout()

        # login as testuser2
        client.login(username='******', password='******')

        # retrieve all hospital equipment
        response = client.get('/api/hospital/{}/equipment/'.format(
            str(self.h1.id)),
                              follow=True)
        self.assertEqual(response.status_code, 404)

        # retrieve all hospital equipment
        response = client.get('/api/hospital/{}/equipment/'.format(
            str(self.h2.id)),
                              follow=True)
        self.assertEqual(response.status_code, 404)

        # retrieve all hospital equipment
        response = client.get('/api/hospital/{}/equipment/'.format(
            str(self.h3.id)),
                              follow=True)
        self.assertEqual(response.status_code, 404)

        # logout
        client.logout()
Пример #5
0
    def test_mqttseed(self):

        # seed
        from django.core import management

        management.call_command('mqttseed', verbosity=1)

        print('>> Processing messages...')

        # Start client as admin
        broker = {
            'HOST': 'localhost',
            'PORT': 1883,
            'KEEPALIVE': 60,
            'CLEAN_SESSION': True
        }
        broker.update(settings.MQTT)
        broker['CLIENT_ID'] = 'test_mqttseed_admin'

        client = MQTTTestClient(broker)
        self.is_connected(client)

        qos = 0

        # Expect settings
        client.expect('settings',
                      JSONRenderer().render(SettingsView.get_settings()), qos)

        # Expect all ambulances
        for ambulance in Ambulance.objects.all():
            client.expect(
                'ambulance/{}/data'.format(ambulance.id),
                JSONRenderer().render(AmbulanceSerializer(ambulance).data),
                qos)

        # Expect all hospitals
        for hospital in Hospital.objects.all():
            client.expect(
                'hospital/{}/data'.format(hospital.id),
                JSONRenderer().render(HospitalSerializer(hospital).data), qos)
            hospital_equipment = hospital.hospitalequipment_set.values(
                'equipment')
            equipment = Equipment.objects.filter(id__in=hospital_equipment)
            client.expect(
                'hospital/{}/metadata'.format(hospital.id),
                JSONRenderer().render(
                    EquipmentSerializer(equipment, many=True).data), qos)

        # Expect all hospital equipments
        for e in HospitalEquipment.objects.all():
            client.expect(
                'hospital/{}/equipment/{}/data'.format(e.hospital.id,
                                                       e.equipment.name),
                JSONRenderer().render(HospitalEquipmentSerializer(e).data),
                qos)

        # Expect all profiles
        for obj in Profile.objects.all():
            client.expect(
                'user/{}/profile'.format(obj.user.username),
                JSONRenderer().render(ExtendedProfileSerializer(obj).data),
                qos)

        # Subscribed?
        self.is_subscribed(client)

        # Done?
        self.loop(client)
        client.wait()

        # Repeat with same client

        client = MQTTTestClient(broker)
        self.is_connected(client)

        qos = 0

        # Expect settings
        client.expect('settings',
                      JSONRenderer().render(SettingsView.get_settings()), qos)

        # Expect all ambulances
        for ambulance in Ambulance.objects.all():
            client.expect(
                'ambulance/{}/data'.format(ambulance.id),
                JSONRenderer().render(AmbulanceSerializer(ambulance).data),
                qos)

        # Expect all hospitals
        for hospital in Hospital.objects.all():
            client.expect(
                'hospital/{}/data'.format(hospital.id),
                JSONRenderer().render(HospitalSerializer(hospital).data), qos)
            hospital_equipment = hospital.hospitalequipment_set.values(
                'equipment')
            equipment = Equipment.objects.filter(id__in=hospital_equipment)
            client.expect(
                'hospital/{}/metadata'.format(hospital.id),
                JSONRenderer().render(
                    EquipmentSerializer(equipment, many=True).data), qos)

        # Expect all hospital equipments
        for e in HospitalEquipment.objects.all():
            client.expect(
                'hospital/{}/equipment/{}/data'.format(e.hospital.id,
                                                       e.equipment.name),
                JSONRenderer().render(HospitalEquipmentSerializer(e).data),
                qos)

        # Expect all profiles
        for obj in Profile.objects.all():
            client.expect(
                'user/{}/profile'.format(obj.user.username),
                JSONRenderer().render(ExtendedProfileSerializer(obj).data),
                qos)

        # Subscribed?
        self.is_subscribed(client)

        # Done?
        self.loop(client)
        client.wait()

        # Repeat with same client and different qos

        client = MQTTTestClient(broker)
        self.is_connected(client)

        qos = 2

        # Expect settings
        client.expect('settings',
                      JSONRenderer().render(SettingsView.get_settings()), qos)

        # Expect all ambulances
        for ambulance in Ambulance.objects.all():
            client.expect(
                'ambulance/{}/data'.format(ambulance.id),
                JSONRenderer().render(AmbulanceSerializer(ambulance).data),
                qos)

        # Expect all hospitals
        for hospital in Hospital.objects.all():
            client.expect(
                'hospital/{}/data'.format(hospital.id),
                JSONRenderer().render(HospitalSerializer(hospital).data), qos)
            hospital_equipment = hospital.hospitalequipment_set.values(
                'equipment')
            equipment = Equipment.objects.filter(id__in=hospital_equipment)
            client.expect(
                'hospital/{}/metadata'.format(hospital.id),
                JSONRenderer().render(
                    EquipmentSerializer(equipment, many=True).data), qos)

        # Expect all hospital equipments
        for e in HospitalEquipment.objects.all():
            client.expect(
                'hospital/{}/equipment/{}/data'.format(e.hospital.id,
                                                       e.equipment.name),
                JSONRenderer().render(HospitalEquipmentSerializer(e).data),
                qos)

        # Expect all profiles
        for obj in Profile.objects.all():
            client.expect(
                'user/{}/profile'.format(obj.user.username),
                JSONRenderer().render(ExtendedProfileSerializer(obj).data),
                qos)

        # Subscribed?
        self.is_subscribed(client)

        # Done?
        self.loop(client)
        client.wait()

        # repeat with another user

        qos = 0

        # Start client as common user
        broker['USERNAME'] = '******'
        broker['PASSWORD'] = '******'
        broker['CLIENT_ID'] = 'test_mqttseed_testuser1'

        client = MQTTTestClient(broker)
        self.is_connected(client)

        # Expect settings
        client.expect('settings',
                      JSONRenderer().render(SettingsView.get_settings()), qos)

        # Expect user profile
        profile = Profile.objects.get(user__username='******')
        client.expect(
            'user/testuser1/profile',
            JSONRenderer().render(ExtendedProfileSerializer(profile).data),
            qos)

        # User Ambulances
        can_read = profile.ambulances.filter(
            can_read=True).values('ambulance_id')
        for ambulance in Ambulance.objects.filter(id__in=can_read):
            client.expect(
                'ambulance/{}/data'.format(ambulance.id),
                JSONRenderer().render(AmbulanceSerializer(ambulance).data),
                qos)

        # User Hospitals
        can_read = profile.hospitals.filter(
            can_read=True).values('hospital_id')
        for hospital in Hospital.objects.filter(id__in=can_read):
            client.expect(
                'hospital/{}/data'.format(hospital.id),
                JSONRenderer().render(HospitalSerializer(hospital).data), qos)

        # Expect all user hospital equipments
        for e in HospitalEquipment.objects.filter(hospital__id__in=can_read):
            client.expect(
                'hospital/{}/equipment/{}/data'.format(e.hospital.id,
                                                       e.equipment.name),
                JSONRenderer().render(HospitalEquipmentSerializer(e).data),
                qos)

        # Subscribed?
        self.is_subscribed(client)

        # Done?
        self.loop(client)
        client.wait()

        # repeat with another user

        qos = 0

        # Start client as common user
        broker['USERNAME'] = '******'
        broker['PASSWORD'] = '******'
        broker['CLIENT_ID'] = 'test_mqttseed_testuser2'

        client = MQTTTestClient(broker)
        self.is_connected(client)

        # Expect settings
        client.expect('settings',
                      JSONRenderer().render(SettingsView.get_settings()), qos)

        # Expect user profile
        profile = Profile.objects.get(user__username='******')
        client.expect(
            'user/testuser2/profile',
            JSONRenderer().render(ExtendedProfileSerializer(profile).data),
            qos)

        # User Ambulances
        can_read = profile.ambulances.filter(
            can_read=True).values('ambulance_id')
        for ambulance in Ambulance.objects.filter(id__in=can_read):
            client.expect(
                'ambulance/{}/data'.format(ambulance.id),
                JSONRenderer().render(AmbulanceSerializer(ambulance).data),
                qos)

        # User Hospitals
        can_read = profile.hospitals.filter(
            can_read=True).values('hospital_id')
        for hospital in Hospital.objects.filter(id__in=can_read):
            client.expect(
                'hospital/{}/data'.format(hospital.id),
                JSONRenderer().render(HospitalSerializer(hospital).data), qos)

        # Expect all user hospital equipments
        for e in HospitalEquipment.objects.filter(hospital__id__in=can_read):
            client.expect(
                'hospital/{}/equipment/{}/data'.format(e.hospital.id,
                                                       e.equipment.name),
                JSONRenderer().render(HospitalEquipmentSerializer(e).data),
                qos)

        # Subscribed?
        self.is_subscribed(client)

        # Done?
        self.loop(client)
        client.wait()
Пример #6
0
 def publish_hospital_equipment(self, equipment, qos=2, retain=True):
     self.publish_topic('hospital/{}/equipment/{}/data'.format(
         equipment.hospital.id, equipment.equipment.name),
                        HospitalEquipmentSerializer(equipment),
                        qos=qos,
                        retain=retain)