Exemple #1
0
 def test_get_admin(self, testclient, fakeproduct, admin_login):
     expected = {
         "status_code": 200,
         "body":
         paginated_body([self.product_serializer(fakeproduct).data]),
     }
     testclient.get_request_test_helper(expected)
 def test_get_locations(self, testclient, fakelocation):
     """Get expected location"""
     expected = {
         "status_code": 200,
         "body": paginated_body([model_to_dict(fakelocation)]),
     }
     testclient.get_request_test_helper(expected)
Exemple #3
0
    def test_get_none(self, testclient):
        """Get an empty list of organizations"""
        expected = {
            "status_code": 200,
            "body": paginated_body([])
        }

        testclient.get_request_test_helper(expected)
Exemple #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)
Exemple #5
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)
Exemple #6
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)
Exemple #7
0
    def test_no_members(self, testclient, fake_org):
        """Organization has no members"""

        path_params = {
            "org_id": fake_org.id,
        }
        expected = {
            "status_code": 200,
            "body": paginated_body([])
        }
        testclient.get_request_test_helper(expected, path_params=path_params)
    def test_get_no_archived_data(self, fakedevice, testclient):
        path_params = {
            "device_id": fakedevice.id,
        }

        expected = {
            "status_code": 200,
            "body": paginated_body([])
        }

        testclient.get_request_test_helper(expected, path_params=path_params)
Exemple #9
0
    def test_no_members(self, testclient, fake_org):
        """Get an empty user list (paginated)"""

        path_params = {
            "org_id": fake_org.id,
        }
        expected = {
            "status_code": 200,
            "body": paginated_body([])
        }

        testclient.get_request_test_helper(expected, path_params=path_params)
    def test_filter_by_last_name(self, testclient, fredbloggs, wildcard):
        query_params = {
            "last_name": wildcard,
        }
        expected = {
            "status_code": 200,
            # Don't care about the serialization here, just as long as it
            # returns the right user
            "body": paginated_body([UserSerializer(instance=fredbloggs).data]),
        }

        testclient.get_request_test_helper(expected, query_params=query_params)
Exemple #11
0
    def test_nobody_no_devices(self, testclient, org_devices):
        """No devices for random user"""

        UserFactory(first_name="bart",
                    last_name="simpson",
                    username="******",
                    email="*****@*****.**",
                    password=make_password("test_password"))
        testclient.login("*****@*****.**", "test_password")

        expected = {"status_code": 200, "body": paginated_body([])}
        testclient.get_request_test_helper(expected)
    def test_get_all_users(self, testclient, joeseed, adminuser):
        user1 = model_to_dict(joeseed)
        user2 = model_to_dict(adminuser)
        del user1['password'], user2['password']
        del user1['user_permissions'], user2['user_permissions']
        expected = {
            "status_code": 200,
            "body": paginated_body([user2,
                                    user1]),  # Users are sorted by username
        }

        testclient.get_request_test_helper(expected)
Exemple #13
0
    def test_get_devices_with_login(self, testclient, joeseed):
        expected = {"status_code": 200, "body": paginated_body([])}
        testclient.login(joeseed.username, "test_password")
        testclient.get_request_test_helper(expected)
        testclient.logout()

        expected = {
            "status_code": 401,
            "body": {
                'detail': 'Authentication credentials were not provided.'
            }
        }
        testclient.get_request_test_helper(expected)
Exemple #14
0
    def test_get_stubbed_devices(self, testclient, fakedevice):
        """Stubbing should work on device endpoint"""

        returned = {"id": fakedevice.id, "name": fakedevice.name}

        query_params = {
            "stub": True,
        }
        expected = {
            "status_code": 200,
            "body": paginated_body([returned]),
        }
        testclient.get_request_test_helper(expected, query_params=query_params)
Exemple #15
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)
Exemple #16
0
    def test_get_one(self, testclient, fake_org):
        """Get a single organization in a paginated body"""

        dumped = model_to_dict(fake_org)
        dumped["logo"] = None
        del dumped["created"]
        del dumped["modified"]
        expected = {
            "status_code": 200,
            "body": paginated_body([dumped])
        }

        testclient.get_request_test_helper(expected)
Exemple #17
0
    def test_one_member(self, testclient, joeseed, fake_org):
        """Joeseed is a member of fake_org in the fixtures"""

        membership = OrganizationUser.objects.filter(user=joeseed).get()
        as_dict = membership_to_dict(membership, joeseed)
        path_params = {
            "org_id": fake_org.id,
        }
        expected = {
            "status_code": 200,
            "body": paginated_body([as_dict])
        }
        testclient.get_request_test_helper(expected, path_params=path_params)
Exemple #18
0
 def test_get_devices_with_data(self, testclient, fakedevice, fake_ts_data):
     device = model_to_dict(fakedevice)
     # Add latest sensor data to expected results
     sensor_data = model_to_dict(fake_ts_data[0])
     # 'id' and 'sensor' is not returned in serializer
     del sensor_data["id"]
     del sensor_data["sensor"]
     device.update({"sensors_current": {"power_sensor": sensor_data}})
     expected = {
         "status_code": 200,
         "body": paginated_body([device]),
     }
     testclient.get_request_test_helper(expected,
                                        expect_identical_values=False)
    def test_get_multiple_locations(self, testclient, fakelocation):
        """Get all locations

        This should work just because the second location was added after
        fakelocation and there's no ordering. If ordering is applied in future
        this just needs changing so the order is correct.
        """
        other_location = LocationFactory(locality="Secret lab")
        expected = {
            "status_code": 200,
            "body": paginated_body([
                model_to_dict(fakelocation),
                model_to_dict(other_location),
            ]),
        }
        testclient.get_request_test_helper(expected)
 def test_device_activity_stream_filtering(self, testclient, fakedevices):
     """ Test that activity stream is filtered by device """
     pp = {
         "device_id": fakedevices[0].id,
     }
     activity = self.activity
     # Create activity streams for multiple devices
     for fakedevice in fakedevices:
         device_activity(fakedevice, activity)
     activity["created_at"] = None
     # Should only return activity stream for device defined in path_params
     expected = {
         "status_code": 200,
         "body": paginated_body([activity]),
     }
     testclient.get_request_test_helper(expected, path_params=pp)
    def test_get_archived_data_no_filter(self, fakedevice, testclient, fake_ts_archive_data):
        """should return all data for this device"""
        path_params = {
            "device_id": fakedevice.id,
        }
        query_params = {
            # Make sure it's all returned at once so we can check easier
            "page_size": len(fake_ts_archive_data)
        }

        serialized = TimeSeriesDataArchiveSerializer(instance=fake_ts_archive_data, many=True).data

        expected = {
            "status_code": 200,
            "body": paginated_body(serialized),
        }

        testclient.get_request_test_helper(expected, path_params=path_params, query_params=query_params)
 def test_device_activity_stream(self, testclient, fakedevice):
     """ Test that multiple actions in a devices activity stream are
     returned"""
     pp = {
         "device_id": fakedevice.id,
     }
     activity = self.activity
     # Number of actions to create in devices activity stream
     num_actions = 5
     for i in range(num_actions):
         # Create device activity that is saved to database as an `Action`
         device_activity(fakedevice, activity)
     activity["created_at"] = None
     expected = {
         "status_code": 200,
         "body": paginated_body([activity] * num_actions),
     }
     testclient.get_request_test_helper(expected, path_params=pp)
    def test_get_archived_data_filter_by_aggregation_type_no_archive(self, fakedevice, testclient,
            fake_ts_archive_data):
        """Correct aggregation type, but no data"""

        aggregation_type = "min"

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

        expected = {
            "status_code": 200,
            "body": paginated_body([]),
        }

        testclient.get_request_test_helper(expected, path_params=path_params, query_params=query_params)
    def test_get_archived_data_filter_by_aggregation_type(self, fakedevice, testclient, fake_ts_archive_data,
            aggregation_type):
        """should return all data for this device"""

        trimmed_archive_data = [i for i in fake_ts_archive_data if i.aggregation_type == aggregation_type]

        path_params = {
            "device_id": fakedevice.id,
        }
        query_params = {
            # Make sure it's all returned at once so we can check easier
            "page_size": len(trimmed_archive_data),
            "aggregation_type": aggregation_type,
        }

        serialized = TimeSeriesDataArchiveSerializer(instance=trimmed_archive_data, many=True).data

        expected = {
            "status_code": 200,
            "body": paginated_body(serialized),
        }

        testclient.get_request_test_helper(expected, path_params=path_params, query_params=query_params)
Exemple #25
0
    def test_add_membership(self, testclient, fredbloggs, fake_org):
        """Fred bloggs is not a member - add him, check he's been added"""

        path_params = {
            "org_id": fake_org.id,
        }

        post_body = {
            "user": {
                "id": fredbloggs.id,
                "email": fredbloggs.email
            }
        }
        expected = {
            "status_code": 201,
            "body": {
                **post_body,
                "id": 1,
                "is_admin": False,
                "created": None,
            }
        }

        testclient.post_request_test_helper(post_body, expected,
                                            path_params=path_params)

        membership = OrganizationUser.objects.filter(user=fredbloggs).get()
        as_dict = membership_to_dict(membership, fredbloggs)

        expected = {
            "status_code": 200,
            "body": paginated_body([as_dict])
        }


        testclient.get_request_test_helper(expected, path_params=path_params)
    def test_filter_time_nothing(self, fakedevice, testclient, fake_ts_archive_data):
        """Try to get archive data from the distant past"""

        now = datetime.datetime.utcnow()
        start_date = now - datetime.timedelta(weeks=15)

        trimmed_archive_data = [i for i in fake_ts_archive_data if i.start <= start_date]

        path_params = {
            "device_id": fakedevice.id,
        }
        query_params = {
            # "page_size": len(trimmed_archive_data),
            "start__lt": start_date.isoformat(),
        }

        serialized = TimeSeriesDataArchiveSerializer(instance=trimmed_archive_data, many=True).data

        expected = {
            "status_code": 200,
            "body": paginated_body(serialized),
        }

        testclient.get_request_test_helper(expected, path_params=path_params, query_params=query_params)
Exemple #27
0
 def test_no_devices_200(self, testclient, joeseed):
     expected = {"status_code": 200, "body": paginated_body([])}
     testclient.login(joeseed.username, "test_password")
     testclient.get_request_test_helper(expected)