示例#1
0
    def test_get_devices_custom_pagination(self, testclient, fakedevices):
        # There are 10 fakedevices, the following custom pagination will return the 4th and 5th
        qp = {
            "page_size": "2",
            "page": "3",
        }
        additional_fields = {
            "sensors_current": {},
        }

        device4 = device_to_dict(fakedevices[4])
        device4.update(additional_fields)
        device5 = device_to_dict(fakedevices[5])
        device5.update(additional_fields)
        expected = {
            "status_code": 200,
            "body": {
                "results": [device4, device5],
                "count": 10,
                "previous":
                "http://testserver/api/v3/devices/?page=2&page_size=2",
                "next": "http://testserver/api/v3/devices/?page=4&page_size=2",
            }
        }

        testclient.get_request_test_helper(expected, query_params=qp)
示例#2
0
    def test_create_device(self, testclient, fakeproduct):
        device = DeviceFactory(product=fakeproduct)

        device_as_dict = device_to_dict(device)

        unwanted = [
            "created_at", "updated_at", "last_seen", "id", "product",
            "sensors_current"
        ]
        for key in unwanted:
            del device_as_dict[key]

        device_as_dict["product"] = fakeproduct.id

        body = device_as_dict.copy()

        device_as_dict.update({
            "id": 2,
            "created_at": None,
            "updated_at": None,
            "last_seen": None,
        })

        expected = {
            "status_code": 201,
            "body": device_as_dict,
        }

        testclient.post_request_test_helper(body, expected)
示例#3
0
    def test_admin_both_devices(self, testclient, fredbloggs, org_devices):
        """All devices"""

        expected = {
            "status_code": 200,
            "body": paginated_body([device_to_dict(i) for i in org_devices])
        }
        testclient.get_request_test_helper(expected)
示例#4
0
    def test_fred_one_device(self, testclient, fredbloggs, org_devices):
        """Make sure joe's device is returned"""

        _, device_fred, _ = org_devices
        device = device_to_dict(device_fred)

        expected = {"status_code": 200, "body": paginated_body([device])}
        testclient.get_request_test_helper(expected)
示例#5
0
    def test_joe_one_device(self, testclient, joeseed, org_devices):
        """Make sure joe's device is returned"""

        device_joe, _, _ = org_devices
        device = device_to_dict(device_joe)

        expected = {"status_code": 200, "body": paginated_body([device])}
        testclient.get_request_test_helper(expected)
示例#6
0
    def test_get_devices_by_user(self, testclient, fakedevice):
        device = device_to_dict(fakedevice)
        device.update({
            "sensors_current": {},
        })
        expected = {
            "status_code": 200,
            "body": paginated_body([device]),
        }

        qp = {"name": fakedevice.name}

        testclient.get_request_test_helper(expected, query_params=qp)
示例#7
0
    def test_get_device(self, testclient, fakedevice, fake_ts_data, joeseed):
        device = device_to_dict(fakedevice)
        # Add latest sensor data to expected results
        sensor_data = model_to_dict(fake_ts_data[0])
        del sensor_data["id"]
        del sensor_data["sensor"]

        device.update({
            "sensors_current": {
                "power_sensor": sensor_data
            },
        })
        expected = {
            "status_code": 200,
            "body": device,
        }

        path_params = {
            "device_id": fakedevice.id,
        }
        testclient.get_request_test_helper(expected, path_params=path_params)
示例#8
0
    def test_update_device(self, testclient, fakedevice, admin_login):
        """Update device owned by user"""
        post_body = {"name": "Joe's device"}

        device = device_to_dict(fakedevice)
        device.update({
            "sensors_current": {},
            "updated_at": None,
        })
        device.update(post_body)

        path_params = {
            "device_id": fakedevice.id,
        }

        expected = {
            "status_code": 200,
            "body": device,
        }
        testclient.patch_request_test_helper(post_body,
                                             expected,
                                             path_params=path_params)
示例#9
0
    def test_get_device_as_user_in_group(self, testclient, fakedevice,
                                         fredbloggs, fake_org):
        """Assign a device to a specific org and make sure that a user in that
        group who is not the owner of that device can access it
        """

        # Using fakedevice
        fakedevice.orgs.add(fake_org)
        fakedevice.save()
        path_params = {"device_id": fakedevice.id}

        # No auth - failure
        expected = {
            "status_code": 401,
            "body": {
                "detail": "Authentication credentials were not provided."
            }
        }
        testclient.get_request_test_helper(expected, path_params=path_params)

        # Authenticate as this user
        testclient.login(fredbloggs.username)
        # No permissions - failure
        expected = {"status_code": 404, "body": {"detail": "Not found."}}
        testclient.get_request_test_helper(expected, path_params=path_params)

        # Add fred to this org
        fredbloggs.add_org(fake_org)
        fredbloggs.save()
        # Object permission granted - success
        expected = {
            "status_code": 200,
            "body": {
                "sensors_current": {},
                **device_to_dict(fakedevice)
            }
        }
        testclient.get_request_test_helper(expected, path_params=path_params)