def test_list(self): """Validate we can list bundles.""" resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(0, len(resp.data)) user = create_user() bundle_public = create_bundle(public=True, owner=user) bundle_private = create_bundle(public=False, owner=user) # anonymous users # should only see the public bundle resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) bundle_rsp = resp.data[0] self.assertSerialized(bundle_public, bundle_rsp) # authenticated user # should see the public and private bundle self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(2, len(resp.data)) for bundle_rsp, bundle_obj in zip(resp.data, [bundle_public, bundle_private]): self.assertSerialized(bundle_obj, bundle_rsp)
def test_list_version_1_0(self): """Validate that newer fields are dropped for older API versions.""" create_bundle(public=True) resp = self.client.get(self.api_url(version='1.0')) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) self.assertIn('url', resp.data[0]) self.assertNotIn('web_url', resp.data[0])
def _create_bundles(self): user = create_user(username='******') project = create_project(linkname='myproject') bundle_public = create_bundle(public=True, owner=user, project=project) bundle_private = create_bundle( public=False, owner=user, project=project ) return user, project, bundle_public, bundle_private
def _create_bundles(self): user = create_user(username='******') project = create_project(linkname='myproject') bundle_public = create_bundle(public=True, owner=user, project=project) bundle_private = create_bundle(public=False, owner=user, project=project) return user, project, bundle_public, bundle_private
def test_update_no_patches(self): """Validate we handle updating only the name.""" user, project, patch_a, patch_b = self._test_create_update() bundle = create_bundle(owner=user, project=project) bundle.append_patch(patch_a) bundle.append_patch(patch_b) self.assertEqual(1, Bundle.objects.all().count()) self.assertEqual(2, len(Bundle.objects.first().patches.all())) resp = self.client.patch( self.api_url(bundle.id), { 'name': 'hello-bundle', 'public': True, }, ) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(2, len(resp.data['patches'])) self.assertEqual('hello-bundle', resp.data['name']) self.assertTrue(resp.data['public']) self.assertEqual(1, Bundle.objects.all().count()) self.assertEqual(2, len(Bundle.objects.first().patches.all())) self.assertEqual('hello-bundle', Bundle.objects.first().name) self.assertTrue(Bundle.objects.first().public)
def test_detail(self): """Validate we can get a specific bundle.""" bundle = create_bundle(public=True) resp = self.client.get(self.api_url(bundle.id)) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertSerialized(bundle, resp.data)
def setUp(self, count=3): self.user = create_user() self.client.login(username=self.user.username, password=self.user.username) self.bundle = create_bundle(owner=self.user) self.project = create_project() self.patches = create_patches(count, project=self.project)
def test_list(self): """Validate we can list bundles.""" resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(0, len(resp.data)) user = create_user(username='******') project = create_project(linkname='myproject') bundle_public = create_bundle(public=True, owner=user, project=project) bundle_private = create_bundle(public=False, owner=user, project=project) # anonymous users # should only see the public bundle resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) bundle_rsp = resp.data[0] self.assertSerialized(bundle_public, bundle_rsp) # authenticated user # should see the public and private bundle self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(2, len(resp.data)) for bundle_rsp, bundle_obj in zip( resp.data, [bundle_public, bundle_private]): self.assertSerialized(bundle_obj, bundle_rsp) # test filtering by project resp = self.client.get(self.api_url(), {'project': 'myproject'}) self.assertEqual([bundle_public.id, bundle_private.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) self.assertEqual(0, len(resp.data)) # test filtering by owner, both ID and username resp = self.client.get(self.api_url(), {'owner': user.id}) self.assertEqual([bundle_public.id, bundle_private.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'owner': 'myuser'}) self.assertEqual([bundle_public.id, bundle_private.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'owner': 'otheruser'}) self.assertEqual(0, len(resp.data))
def test_list(self): """Validate we can list bundles.""" resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(0, len(resp.data)) user = create_user(username='******') project = create_project(linkname='myproject') bundle_public = create_bundle(public=True, owner=user, project=project) bundle_private = create_bundle(public=False, owner=user, project=project) # anonymous users # should only see the public bundle resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) bundle_rsp = resp.data[0] self.assertSerialized(bundle_public, bundle_rsp) # authenticated user # should see the public and private bundle self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(2, len(resp.data)) for bundle_rsp, bundle_obj in zip(resp.data, [bundle_public, bundle_private]): self.assertSerialized(bundle_obj, bundle_rsp) # test filtering by project resp = self.client.get(self.api_url(), {'project': 'myproject'}) self.assertEqual([bundle_public.id, bundle_private.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) self.assertEqual(0, len(resp.data)) # test filtering by owner, both ID and username resp = self.client.get(self.api_url(), {'owner': user.id}) self.assertEqual([bundle_public.id, bundle_private.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'owner': 'myuser'}) self.assertEqual([bundle_public.id, bundle_private.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'owner': 'otheruser'}) self.assertEqual(0, len(resp.data))
def test_user_profile_bundles(self): bundle = create_bundle(owner=self.user) response = self.client.get(reverse('user-profile')) self.assertContains(response, 'Your Profile') self.assertContains(response, 'You have the following bundle') self.assertContains(response, bundle.get_absolute_url())
def test_delete(self): """Validate we can delete an existing bundle.""" user = create_user() bundle = create_bundle(owner=user) self.client.force_authenticate(user=user) resp = self.client.delete(self.api_url(bundle.id)) self.assertEqual(status.HTTP_204_NO_CONTENT, resp.status_code) self.assertEqual(0, Bundle.objects.all().count())
def test_delete_anonymous(self): """Delete a bundle when not signed in. Ensure deletions can only be performed when signed in. """ user, project, patch_a, patch_b = self._test_create_update( authenticate=False) bundle = create_bundle(owner=user, project=project) resp = self.client.delete(self.api_url(bundle.id)) self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
def test_update_anonymous(self): """Update an existing bundle when not signed in. Ensure updates can only be performed by signed in users. """ user, project, patch_a, patch_b = self._test_create_update( authenticate=False) bundle = create_bundle(owner=user, project=project) resp = self.client.patch(self.api_url(bundle.id), { 'name': 'hello-bundle', 'patches': [patch_a.id, patch_b.id] }) self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code)
def test_update(self): """Validate we can update an existing bundle.""" user, project, patch_a, patch_b = self._test_create_update() bundle = create_bundle(owner=user, project=project) self.assertEqual(1, Bundle.objects.all().count()) self.assertEqual(0, len(Bundle.objects.first().patches.all())) resp = self.client.patch(self.api_url(bundle.id), { 'name': 'hello-bundle', 'patches': [patch_a.id, patch_b.id] }) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(2, len(resp.data['patches'])) self.assertEqual('hello-bundle', resp.data['name']) self.assertEqual(1, Bundle.objects.all().count()) self.assertEqual(2, len(Bundle.objects.first().patches.all())) self.assertEqual('hello-bundle', Bundle.objects.first().name)
def test_single_bundle(self): bundle = create_bundle(owner=self.user) bundle.save() response = self.client.get(reverse('user-bundles')) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context['bundles']), 1)
def test_detail_version_1_0(self): bundle = create_bundle(public=True) resp = self.client.get(self.api_url(bundle.id, version='1.0')) self.assertIn('url', resp.data) self.assertNotIn('web_url', resp.data)