def test_list_filtering(self): """ Users should be able to filter by the experiment_id and key fields. """ url = reverse('api_experiments:v0:data-list') user = UserFactory() self.client.login(username=user.username, password=UserFactory._DEFAULT_PASSWORD) experiment_id = 1 ExperimentDataFactory() ExperimentDataFactory(user=user) data = ExperimentDataFactory.create_batch(3, user=user, experiment_id=experiment_id) qs = urllib.urlencode({'experiment_id': experiment_id}) response = self.client.get('{url}?{qs}'.format(url=url, qs=qs)) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['results'], ExperimentDataSerializer(data, many=True).data) datum = data[0] qs = urllib.urlencode({'key': datum.key}) response = self.client.get('{url}?{qs}'.format(url=url, qs=qs)) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['results'], ExperimentDataSerializer([datum], many=True).data) qs = urllib.urlencode({ 'experiment_id': experiment_id, 'key': datum.key }) response = self.client.get('{url}?{qs}'.format(url=url, qs=qs)) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['results'], ExperimentDataSerializer([datum], many=True).data)
def test_list_permissions(self): """ Users should only be able to list their own data. """ url = reverse('api_experiments:v0:data-list') user = UserFactory() response = self.client.get(url) self.assertEqual(response.status_code, 401) ExperimentDataFactory() datum = ExperimentDataFactory(user=user) self.client.login(username=user.username, password=UserFactory._DEFAULT_PASSWORD) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['results'], ExperimentDataSerializer([datum], many=True).data)
def test_list_filtering(self): """ Users should be able to filter by the experiment_id and key fields. """ url = reverse('api_experiments:v0:data-list') user = UserFactory() self.client.login(username=user.username, password=UserFactory._DEFAULT_PASSWORD) experiment_id = 1 ExperimentDataFactory() ExperimentDataFactory(user=user) data = ExperimentDataFactory.create_batch(3, user=user, experiment_id=experiment_id) qs = six.moves.urllib.parse.urlencode({'experiment_id': experiment_id}) response = self.client.get('{url}?{qs}'.format(url=url, qs=qs)) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['results'], ExperimentDataSerializer(data, many=True).data) datum = data[0] qs = six.moves.urllib.parse.urlencode({'key': datum.key}) response = self.client.get('{url}?{qs}'.format(url=url, qs=qs)) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['results'], ExperimentDataSerializer([datum], many=True).data) qs = six.moves.urllib.parse.urlencode({'experiment_id': experiment_id, 'key': datum.key}) response = self.client.get('{url}?{qs}'.format(url=url, qs=qs)) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['results'], ExperimentDataSerializer([datum], many=True).data)
def test_read_permissions(self): """ Users should only be allowed to read their own data. """ user = UserFactory() datum = ExperimentDataFactory(user=user) url = reverse('api_experiments:v0:data-detail', kwargs={'pk': datum.id}) response = self.client.get(url) self.assertEqual(response.status_code, 401) self.client.login(username=user.username, password=UserFactory._DEFAULT_PASSWORD) response = self.client.get(url) self.assertEqual(response.status_code, 200) other_user = UserFactory() self.client.login(username=other_user.username, password=UserFactory._DEFAULT_PASSWORD) response = self.client.get(url) self.assertEqual(response.status_code, 404)