def test_related_resource_authorization(self): resource = api.canonical_resource_for('notes') request = MockRequest() request.GET = {'format': 'json'} request.method = 'POST' request.body = '{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00", "author": null}' resp = resource.post_list(request) self.assertEqual(resp.status_code, 201) self.assertEqual( User.objects.get(id=self.user.id).username, 'testy_mctesterson') request = MockRequest() request.GET = {'format': 'json'} request.method = 'POST' request.body = '{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back-2", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00", "author": {"id": %s, "username": "******"}}' % self.user.id try: resource.post_list(request) except ImmediateHttpResponse as resp: self.assertEqual(resp.response.status_code, 400) else: self.fail("post_list should raise an ImmediateHttpResponse error") # self.assertEqual(resp.status_code, 201) self.assertEqual( User.objects.get(id=self.user.id).username, 'testy_mctesterson', "User resource is GET-only and so should not be updatable")
def test_one_to_many(self): # Sanity checks. self.assertEqual(Note.objects.count(), 2) self.assertEqual(MediaBit.objects.count(), 0) fnr = FreshNoteResource() data = { "title": "Create with related URIs", "slug": "create-with-related-uris", "content": "Some content here", "is_active": True, "media_bits": [{"title": "Picture #1"}], } request = MockRequest() request.GET = {"format": "json"} request.method = "POST" request.body = json.dumps(data) resp = fnr.post_list(request) self.assertEqual(resp.status_code, 201) self.assertEqual(Note.objects.count(), 3) note = Note.objects.latest("created") self.assertEqual(note.media_bits.count(), 1) self.assertEqual(note.media_bits.all()[0].title, u"Picture #1")
def patch_details(resource, pk, **kwargs): # Post the extradata element which is attached to a "reverse" OneToOne request = MockRequest() request.method = "PATCH" request.body = json.dumps(kwargs) response = resource.patch_detail(request, pk=pk) return response
def test_one_to_many(self): # Sanity checks. self.assertEqual(Note.objects.count(), 2) self.assertEqual(MediaBit.objects.count(), 0) fnr = FreshNoteResource() data = { 'title': 'Create with related URIs', 'slug': 'create-with-related-uris', 'content': 'Some content here', 'is_active': True, 'media_bits': [{ 'title': 'Picture #1' }] } request = MockRequest() request.GET = {'format': 'json'} request.method = 'POST' request.body = json.dumps(data) resp = fnr.post_list(request) self.assertEqual(resp.status_code, 201) self.assertEqual(Note.objects.count(), 3) note = Note.objects.latest('created') self.assertEqual(note.media_bits.count(), 1) self.assertEqual(note.media_bits.all()[0].title, u'Picture #1')
def test_cannot_access_user_resource(self): resource = api.canonical_resource_for("users") request = MockRequest() request.GET = {"format": "json"} request.method = "PUT" request.body = '{"username": "******"}' resp = resource.wrap_view("dispatch_detail")(request, pk=self.user.pk) self.assertEqual(resp.status_code, 405) self.assertEqual(User.objects.get(id=self.user.id).username, self.user.username)
def test_cannot_access_user_resource(self): resource = api.canonical_resource_for('users') request = MockRequest() request.GET = {'format': 'json'} request.method = 'PUT' request.body = '{"username": "******"}' resp = resource.wrap_view('dispatch_detail')(request, pk=self.user.pk) self.assertEqual(resp.status_code, 405) self.assertEqual( User.objects.get(id=self.user.id).username, self.user.username)
def test_related_resource_authorization(self): resource = api.canonical_resource_for('notes') request = MockRequest() request.GET = {'format': 'json'} request.method = 'POST' request.body = '{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00", "author": null}' resp = resource.post_list(request) self.assertEqual(resp.status_code, 201) self.assertEqual( User.objects.get(id=self.user.id).username, 'testy_mctesterson') request = MockRequest() request.GET = {'format': 'json'} request.method = 'POST' request.body = '{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back-2", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00", "author": {"id": %s, "username": "******"}}' % self.user.id resp = resource.post_list(request) self.assertEqual(resp.status_code, 201) self.assertEqual(User.objects.get(id=self.user.id).username, 'foobar')
def test_related_resource_authorization(self): resource = api.canonical_resource_for("notes") request = MockRequest() request.GET = {"format": "json"} request.method = "POST" request.body = '{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00", "author": null}' resp = resource.post_list(request) self.assertEqual(resp.status_code, 201) self.assertEqual(User.objects.get(id=self.user.id).username, "testy_mctesterson") request = MockRequest() request.GET = {"format": "json"} request.method = "POST" request.body = ( '{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back-2", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00", "author": {"id": %s, "username": "******"}}' % self.user.id ) resp = resource.post_list(request) self.assertEqual(resp.status_code, 201) self.assertEqual(User.objects.get(id=self.user.id).username, "foobar")
def test_put_null(self): resource = api.canonical_resource_for("category") request = MockRequest() request.GET = {"format": "json"} request.method = "PUT" request.body = '{"parent": null, "name": "Son"}' # Before the PUT, there should be a parent. self.assertEqual(Category.objects.get(pk=self.child_cat_1.pk).parent.pk, self.parent_cat_1.pk) # After the PUT, the parent should be ``None``. resp = resource.put_detail(request, pk=self.child_cat_1.pk) self.assertEqual(resp.status_code, 204) self.assertEqual(Category.objects.get(pk=self.child_cat_1.pk).name, "Son") self.assertEqual(Category.objects.get(pk=self.child_cat_1.pk).parent, None)
def test_reverse_one_to_one_post(self): ed = ExtraData.objects.create(name='ed_name') resource = TagResource() # Post the extradata element which is attached to a "reverse" OneToOne request = MockRequest() request.method = "POST" request.body = json.dumps({ "name": "tag_name", "tagged": [], "extradata": "/v1/extradata/%s/" % ed.pk }) resp = resource.post_list(request) # Assert that the status code is CREATED self.assertEqual(resp.status_code, 201) tag = Tag.objects.get(pk=int(resp['Location'].split("/")[-2])) self.assertEqual(tag.extradata, ed)
def test_put_null(self): resource = api.canonical_resource_for('category') request = MockRequest() request.GET = {'format': 'json'} request.method = 'PUT' request.body = '{"parent": null, "name": "Son"}' # Before the PUT, there should be a parent. self.assertEqual( Category.objects.get(pk=self.child_cat_1.pk).parent.pk, self.parent_cat_1.pk) # After the PUT, the parent should be ``None``. resp = resource.put_detail(request, pk=self.child_cat_1.pk) self.assertEqual(resp.status_code, 204) self.assertEqual( Category.objects.get(pk=self.child_cat_1.pk).name, 'Son') self.assertEqual( Category.objects.get(pk=self.child_cat_1.pk).parent, None)
def test_post_new_tag(self): resource = api.canonical_resource_for("tag") request = MockRequest() request.GET = {"format": "json"} request.method = "POST" request.body = '{"name": "school", "taggabletags": [ ]}' # Prior to the addition of ``blank=True``, this would # fail badly. resp = resource.wrap_view("dispatch_list")(request) self.assertEqual(resp.status_code, 201) # GET the created object (through its headers.location) self.assertTrue(resp.has_header("location")) location = resp["Location"] resp = self.client.get(location, data={"format": "json"}) self.assertEqual(resp.status_code, 200) deserialized = json.loads(resp.content) self.assertEqual(len(deserialized), 5) self.assertEqual(deserialized["name"], "school")
def test_post_new_tag(self): resource = api.canonical_resource_for('tag') request = MockRequest() request.GET = {'format': 'json'} request.method = 'POST' request.body = '{"name": "school", "taggabletags": [ ]}' # Prior to the addition of ``blank=True``, this would # fail badly. resp = resource.wrap_view('dispatch_list')(request) self.assertEqual(resp.status_code, 201) # GET the created object (through its headers.location) self.assertTrue(resp.has_header('location')) location = resp['Location'] resp = self.client.get(location, data={'format': 'json'}) self.assertEqual(resp.status_code, 200) deserialized = json.loads(resp.content) self.assertEqual(len(deserialized), 5) self.assertEqual(deserialized['name'], 'school')