def test_unauthorized_add_membership_no_member(self): """POST /memberships/ for a user that is not a member of the organization.""" # Need a different user for this test case. # User tomTester (pk=2) is OWNER of Test-Team (pk=1). user_id = 2 self.authenticate(username="******", password="******") request_data = { "data": { "type": format_resource_type("Membership"), "attributes": { "role": "A", }, "relationships": { # Make tomTester a Versuchsverbund (pk=2) ASSISTANT. "user": { "data": {"type": format_resource_type("User"), "id": user_id} }, "organization": { "data": { "type": format_resource_type("Organization"), "id": 2, } }, }, } } response = self.client.post(self.collection_url, data=request_data) self.assertEqual(response.status_code, 403)
def test_unauthorized_create_no_member(self): """POST /installations/ for an organization the user ist not a member of.""" node_id = "c727b2f8-8377-d4cb-0e95-ac03200b8c93" request_data = { "data": { "type": format_resource_type("Installation"), "attributes": { "from_timestamp_s": 1601500000, "to_timestamp_s": 2147483647, "description": "Testinstallation", "is_public": True, }, "relationships": { # Install Clairchen Rot in Testraum 1 (pk=1). # The currently authenticated user VeraVersuch is not a member of # the organization that owns Testraum 1 and Clairchen Rot. "node": { "data": { "type": format_resource_type("Node"), "id": node_id, } }, "room": { "data": { "type": format_resource_type("Room"), "id": 1, } }, }, } } response = self.client.post(self.collection_url, data=request_data) self.assertEqual(response.status_code, 403)
def test_patch_installation(self): """PATCH /installations/<installation_pk>/""" request_data = { "data": { "type": format_resource_type("Installation"), "id": self.inst_pk, "attributes": {"from_timestamp_s": 1601510000, "is_public": True}, "relationships": { # Node and room must always be provided, to make sure owners match. "node": { "data": { "type": format_resource_type("Node"), "id": self.node_id, } }, "room": { "data": { "type": format_resource_type("Room"), "id": 3, } }, }, } } response = self.client.patch(self.detail_url, data=request_data) self.assertEqual(response.status_code, 200) self.assertEqual(response.data["from_timestamp_s"], 1601510000) self.assertEqual(response.data["is_public"], True)
def test_node_room_owner_mismatch(self): """POST /installations/ where the owner of the room and the node differ.""" # Clairchen Rot belongs to Test-Team incorrect_node_id = "c727b2f8-8377-d4cb-0e95-ac03200b8c93" request_data = { "data": { "type": format_resource_type("Installation"), "attributes": { "from_timestamp_s": 1601500000, "to_timestamp_s": 2147483647, "description": "Testinstallation", "is_public": True, }, "relationships": { # Try to install Clairchen Rot in Prüfstube (id=4). "node": { "data": { "type": format_resource_type("Node"), "id": incorrect_node_id, } }, "room": { "data": { "type": format_resource_type("Room"), "id": self.room_pk, } }, }, } } response = self.client.post(self.collection_url, data=request_data) # Expect a HTTP 400 (Bad Request) error code, because the request data is # inconsistent. self.assertEqual(response.status_code, 400)
def test_unauthorized_create_no_owner(self): """POST /installations/ for an organization where the user is not an OWNER.""" # Need a different user for this test case. # User horstHilfsarbeiter is ASSISTANT in Versuchsverbung (pk=2). self.authenticate(username="******", password="******") request_data = { "data": { "type": format_resource_type("Installation"), "attributes": { "from_timestamp_s": 1601500000, "to_timestamp_s": 2147483647, "description": "Testinstallation", "is_public": False, }, "relationships": { # Install the ERS Test-Node (id=9d02faee-4260-1377-22ec-936428b572ee) in Prüfstube (id=4). "node": { "data": { "type": format_resource_type("Node"), "id": self.node2_id, } }, "room": { "data": { "type": format_resource_type("Room"), "id": self.room_pk, } }, }, } } response = self.client.post(self.collection_url, data=request_data) self.assertEqual(response.status_code, 403)
def test_get_blog_relationship_entry_set(self): response = self.client.get('/blogs/{}/relationships/entry_set'.format( self.blog.id)) expected_data = [{ 'type': format_resource_type('Entry'), 'id': str(self.first_entry.id) }, { 'type': format_resource_type('Entry'), 'id': str(self.second_entry.id) }] assert response.data == expected_data
def test_create_get_delete_installation(self): request_data = { "data": { "type": format_resource_type("Installation"), "attributes": { "from_timestamp_s": 1601500000, # Leave end timestamp open. "description": "Testinstallation", "is_public": False, }, "relationships": { # Install the ERS Test-Node (id=9d02faee-4260-1377-22ec-936428b572ee) in Prüfstube (id=4). "node": { "data": { "type": format_resource_type("Node"), "id": self.node2_id, } }, "room": { "data": { "type": format_resource_type("Room"), "id": self.room_pk, } }, }, } } # POST /memberships/ response1 = self.client.post(self.collection_url, data=request_data) self.assertEqual(response1.status_code, 201) self.assertEqual(response1.data["from_timestamp_s"], 1601500000) self.assertEqual(response1.data["to_timestamp_s"], 2147483647) self.assertEqual( response1.data["node"]["id"], "9d02faee-4260-1377-22ec-936428b572ee" ) self.assertEqual(response1.data["room"]["id"], "4") # Fetch the installation resource just created. response_url = response1.data["url"] # GET /installations/<installation_pk/> response2 = self.client.get(response_url) self.assertEqual(response2.status_code, 200) self.assertEqual(response2.data["from_timestamp_s"], 1601500000) self.assertEqual(response2.data["node"]["id"], self.node2_id) self.assertEqual(response2.data["room"]["id"], "4") # Delete the installation. # DELETE /installations/<installation_pk>/ response3 = self.client.delete(response_url) self.assertEqual(response3.status_code, 204) # Make sure it is gone. # GET /installations/<installation_pk>/ response4 = self.client.get(response_url) self.assertEqual(response4.status_code, 404)
def test_get_blog_relationship_entry_set(self): response = self.client.get("/blogs/{}/relationships/entry_set".format( self.blog.id)) expected_data = [ { "type": format_resource_type("Entry"), "id": str(self.first_entry.id) }, { "type": format_resource_type("Entry"), "id": str(self.second_entry.id) }, ] assert response.data == expected_data
def test_unauthorized_create_no_member(self): """POST /rooms/ for an organization the user ist not a member of.""" request_data = { "data": { "type": format_resource_type("Room"), "attributes": { "name": "Räumchen", "description": "Nur zum Test", "size_sqm": "5", "height_m": "2.6", "max_occupancy": 1, }, "relationships": { # The currently authenticated user VeraVersuch is not a member of # the organization Test-Team with Site Test-Site (pk=1). "site": { "data": { "type": "Site", "id": "1" } }, }, } } response = self.client.post(self.collection_url, data=request_data) self.assertEqual(response.status_code, 403)
def test_post_to_one_relationship_should_fail(self): url = '/entries/{}/relationships/blog'.format(self.first_entry.id) request_data = { 'data': {'type': format_resource_type('Blog'), 'id': str(self.other_blog.id)} } response = self.client.post(url, data=request_data) assert response.status_code == 405, response.content.decode()
def test_delete_to_many_relationship_with_change(self): url = '/authors/{}/relationships/comment_set'.format(self.author.id) request_data = { 'data': [{'type': format_resource_type('Comment'), 'id': str(self.second_comment.id)}, ] } response = self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json') assert response.status_code == 200, response.content.decode()
def test_unauthorized_create_no_member(self): """POST /sites/ for an organization where the user is not a member of.""" request_data = { "data": { "type": format_resource_type("Site"), "attributes": { "name": "Versuchsort 2", }, "relationships": { "address": { "data": { "type": "Address", "id": "2" } }, # The currently authenticated user VeraVersuch is not a member of # the organization Test-Team with pk=1. "operator": { "data": { "type": "Organization", "id": "1" } }, }, } } response1 = self.client.post(self.collection_url, data=request_data) self.assertEqual(response1.status_code, 403)
def test_delete_one_to_many_relationship_with_not_null_constraint(self): url = '/entries/{}/relationships/comment_set'.format(self.first_entry.id) request_data = { 'data': [{'type': format_resource_type('Comment'), 'id': str(self.first_comment.id)}, ] } response = self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json') assert response.status_code == 409, response.content.decode()
def test_unauthorized_create_no_owner(self): """POST /sites/ for an organization where the user is not an OWNER.""" # Need a different user for this test case. # User horstHilfsarbeiter is ASSISTANT in Versuchsverbung (pk=2). self.authenticate(username="******", password="******") request_data = { "data": { "type": format_resource_type("Site"), "attributes": { "name": "Versuchsort 2", }, "relationships": { "address": { "data": { "type": "Address", "id": "2" } }, # The currently logged-in user horstHilfsarbeiter is not an OWNER # of the organization Versuchsverbund with pk=2. "operator": { "data": { "type": "Organization", "id": "2" } }, }, } } response1 = self.client.post(self.collection_url, data=request_data) self.assertEqual(response1.status_code, 403)
def test_delete_to_one_relationship_should_fail(self): url = '/entries/{}/relationships/blog'.format(self.first_entry.id) request_data = { 'data': {'type': format_resource_type('Blog'), 'id': str(self.other_blog.id)} } response = self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json') assert response.status_code == 405, response.content.decode()
def test_unauthorized_create_no_owner(self): """POST /rooms/ for an organization where the user is not an OWNER.""" # Need a different user for this test case. # User horstHilfsarbeiter is ASSISTANT in Versuchsverbung (pk=2). self.authenticate(username="******", password="******") request_data = { "data": { "type": format_resource_type("Room"), "attributes": { "name": "Räumchen", "description": "Nur zum Test", "size_sqm": "5", "height_m": "2.6", "max_occupancy": 1, }, "relationships": { # The currently authenticated user horstHilfsarbeiter is not an # OWNER of the organization Versuchsverbund with Versuchs-Site # (pk=2). "site": {"data": {"type": "Site", "id": "2"}}, }, } } response = self.client.post(self.collection_url, data=request_data) self.assertEqual(response.status_code, 403)
def test_create_get_delete_organization(self): collection_url = reverse("organization-list") request_data = { "data": { "type": format_resource_type("Organization"), "attributes": { "name": "Temp-Testers", "description": "Vorübergehende Test-Organisation", }, } } # POST /organizations/ response1 = self.client.post(collection_url, data=request_data) self.assertEqual(response1.status_code, 201) self.assertEqual(response1.data["name"], "Temp-Testers") self.assertEqual( response1.data["description"], "Vorübergehende Test-Organisation" ) # Fetch the organization resource just created. # GET /organizations/<organization_id>/ response_url = response1.data["url"] response2 = self.client.get(response_url) self.assertEqual(response2.status_code, 200) self.assertEqual(response2.data["name"], "Temp-Testers") self.assertEqual( response2.data["description"], "Vorübergehende Test-Organisation" ) # Delete the organization. # DELETE /organizations/<organization_id>/ response3 = self.client.delete(response_url) self.assertEqual(response3.status_code, 204) # Make sure it is gone. # GET /organizations/<organization_id>/ response4 = self.client.get(response_url) self.assertEqual(response4.status_code, 404)
def test_post_to_many_relationship_with_no_change(self): url = '/entries/{}/relationships/comment_set'.format(self.first_entry.id) request_data = { 'data': [{'type': format_resource_type('Comment'), 'id': str(self.first_comment.id)}, ] } response = self.client.post(url, data=json.dumps(request_data), content_type='application/vnd.api+json') assert response.status_code == 204, response.content.decode() assert len(response.rendered_content) == 0, response.rendered_content.decode()
def test_get_entry_relationship_blog(self): url = reverse( 'entry-relationships', kwargs={'pk': self.first_entry.id, 'related_field': 'blog'} ) response = self.client.get(url) expected_data = {'type': format_resource_type('Blog'), 'id': str(self.first_entry.blog.id)} assert response.data == expected_data
def test_delete_to_many_relationship_with_no_change(self): url = '/entries/{}/relationships/comments'.format(self.first_entry.id) request_data = { 'data': [{'type': format_resource_type('Comment'), 'id': str(self.second_comment.id)}, ] } response = self.client.delete(url, data=request_data) assert response.status_code == 204, response.content.decode() assert len(response.rendered_content) == 0, response.rendered_content.decode()
def test_deserialize_primitive_data_blog(self): initial_data = {"type": format_resource_type("Blog"), "id": str(self.blog.id)} serializer = ResourceIdentifierObjectSerializer( data=initial_data, model_class=Blog ) self.assertTrue(serializer.is_valid(), msg=serializer.errors) assert serializer.validated_data == self.blog
def test_data_in_correct_format_when_instantiated_with_entry_object(self): serializer = ResourceIdentifierObjectSerializer(instance=self.entry) expected_data = { "type": format_resource_type("Entry"), "id": str(self.entry.id), } assert serializer.data == expected_data
def test_get_to_many_relationship_self_link(self): url = '/authors/{}/relationships/comment_set'.format(self.author.id) response = self.client.get(url) expected_data = { 'links': {'self': 'http://testserver/authors/1/relationships/comment_set'}, 'data': [{'id': str(self.second_comment.id), 'type': format_resource_type('Comment')}] } assert json.loads(response.content.decode('utf-8')) == expected_data
def test_deserialize_primitive_data_blog(self): initial_data = { 'type': format_resource_type('Blog'), 'id': str(self.blog.id) } serializer = ResourceIdentifierObjectSerializer(data=initial_data, model_class=Blog) self.assertTrue(serializer.is_valid(), msg=serializer.errors) assert serializer.validated_data == self.blog
def test_data_in_correct_format_when_instantiated_with_blog_object(self): serializer = ResourceIdentifierObjectSerializer(instance=self.blog) expected_data = { "type": format_resource_type("Blog"), "id": str(self.blog.id) } assert serializer.data == expected_data
def test_data_in_correct_format_when_instantiated_with_queryset(self): qs = Author.objects.all() serializer = ResourceIdentifierObjectSerializer(instance=qs, many=True) type_string = format_resource_type('Author') author_pks = Author.objects.values_list('pk', flat=True) expected_data = [{'type': type_string, 'id': str(pk)} for pk in author_pks] assert serializer.data == expected_data
def test_data_in_correct_format_when_instantiated_with_entry_object(self): serializer = ResourceIdentifierObjectSerializer(instance=self.entry) expected_data = { 'type': format_resource_type('Entry'), 'id': str(self.entry.id) } assert serializer.data == expected_data
def test_post_to_many_relationship_with_change(self): url = '/entries/{}/relationships/comments'.format(self.first_entry.id) request_data = { 'data': [{'type': format_resource_type('Comment'), 'id': str(self.second_comment.id)}, ] } response = self.client.post(url, data=request_data) assert response.status_code == 200, response.content.decode() assert request_data['data'][0] in response.data
def test_post_to_one_relationship_should_fail(self): url = '/entries/{}/relationships/blog'.format(self.first_entry.id) request_data = { 'data': { 'type': format_resource_type('Blog'), 'id': str(self.other_blog.id) } } response = self.client.post(url, data=request_data) assert response.status_code == 405, response.content.decode()
def test_patch_to_one_relationship(self): url = '/entries/{}/relationships/blog'.format(self.first_entry.id) request_data = { 'data': {'type': format_resource_type('Blog'), 'id': str(self.other_blog.id)} } response = self.client.patch(url, data=json.dumps(request_data), content_type='application/vnd.api+json') assert response.status_code == 200, response.content.decode() response = self.client.get(url) assert response.data == request_data['data']
def test_deserialize_many(self): type_string = format_resource_type('Author') author_pks = Author.objects.values_list('pk', flat=True) initial_data = [{'type': type_string, 'id': str(pk)} for pk in author_pks] serializer = ResourceIdentifierObjectSerializer(data=initial_data, model_class=Author, many=True) self.assertTrue(serializer.is_valid(), msg=serializer.errors) print(serializer.data)
def test_delete_to_one_relationship_should_fail(self): url = "/entries/{}/relationships/blog".format(self.first_entry.id) request_data = { "data": { "type": format_resource_type("Blog"), "id": str(self.other_blog.id), } } response = self.client.delete(url, data=request_data) assert response.status_code == 405, response.content.decode()
def test_serialize_many_to_many_relation(self): serializer = EntryModelSerializer(instance=self.entry) type_string = format_resource_type("Author") author_pks = Author.objects.values_list("pk", flat=True) expected_data = [{ "type": type_string, "id": str(pk) } for pk in author_pks] self.assertEqual(serializer.data["authors"], expected_data)
def test_patch_one_to_many_relationship(self): url = '/blogs/{}/relationships/entry_set'.format(self.first_entry.id) request_data = { 'data': [{'type': format_resource_type('Entry'), 'id': str(self.first_entry.id)}, ] } response = self.client.patch(url, data=json.dumps(request_data), content_type='application/vnd.api+json') assert response.status_code == 200, response.content.decode() assert response.data == request_data['data'] response = self.client.get(url) assert response.data == request_data['data']
def test_data_in_correct_format_when_instantiated_with_blog_object(self): serializer = BlogFKSerializer(instance={'blog': self.blog}) expected_data = { 'type': format_resource_type('Blog'), 'id': str(self.blog.id) } actual_data = serializer.data['blog'] self.assertEqual(actual_data, expected_data)
def test_deserialize_primitive_data_blog(self): serializer = BlogFKSerializer( data={ 'blog': { 'type': format_resource_type('Blog'), 'id': str(self.blog.id) } }) self.assertTrue(serializer.is_valid()) self.assertEqual(serializer.validated_data['blog'], self.blog)
def test_data_in_correct_format_when_instantiated_with_entry_object(self): serializer = EntryFKSerializer(instance={'entry': self.entry}) expected_data = { 'type': format_resource_type('Entry'), 'id': str(self.entry.id) } actual_data = serializer.data['entry'] self.assertEqual(actual_data, expected_data)
def test_serialize_many_to_many_relation(self): serializer = EntryModelSerializer(instance=self.entry) type_string = format_resource_type('Author') author_pks = Author.objects.values_list('pk', flat=True) expected_data = [{ 'type': type_string, 'id': str(pk) } for pk in author_pks] self.assertEqual(serializer.data['authors'], expected_data)
def test_deserialize_primitive_data_blog(self): serializer = BlogFKSerializer(data={ 'blog': { 'type': format_resource_type('Blog'), 'id': str(self.blog.id) } } ) self.assertTrue(serializer.is_valid()) self.assertEqual(serializer.validated_data['blog'], self.blog)
def test_serialize_many_to_many_relation(self): serializer = EntryModelSerializer(instance=self.entry) type_string = format_resource_type('Author') author_pks = Author.objects.values_list('pk', flat=True) expected_data = [{'type': type_string, 'id': str(pk)} for pk in author_pks] self.assertEqual( serializer.data['authors'], expected_data )
def test_deserialize_many_to_many_relation(self): type_string = format_resource_type('Author') author_pks = Author.objects.values_list('pk', flat=True) authors = [{'type': type_string, 'id': pk} for pk in author_pks] serializer = EntryModelSerializer(data={'authors': authors, 'comment_set': []}) self.assertTrue(serializer.is_valid()) self.assertEqual(len(serializer.validated_data['authors']), Author.objects.count()) for author in serializer.validated_data['authors']: self.assertIsInstance(author, Author)
def test_patch_many_to_many_relationship(self): url = '/entries/{}/relationships/authors'.format(self.first_entry.id) request_data = { 'data': [ { 'type': format_resource_type('Author'), 'id': str(self.author.id) }, ] } response = self.client.patch(url, data=request_data) assert response.status_code == 200, response.content.decode() assert response.data == request_data['data'] response = self.client.get(url) assert response.data == request_data['data']
def test_new_comment_data_patch_to_many_relationship(self): entry = EntryFactory(blog=self.blog, authors=(self.author,)) comment = CommentFactory(entry=entry) url = '/authors/{}/relationships/comments'.format(self.author.id) request_data = { 'data': [{'type': format_resource_type('Comment'), 'id': str(comment.id)}, ] } previous_response = { 'data': [ {'type': 'comments', 'id': str(self.second_comment.id) } ], 'links': { 'self': 'http://testserver/authors/{}/relationships/comments'.format( self.author.id ) } } response = self.client.get(url) assert response.status_code == 200 assert response.json() == previous_response new_patched_response = { 'data': [ {'type': 'comments', 'id': str(comment.id) } ], 'links': { 'self': 'http://testserver/authors/{}/relationships/comments'.format( self.author.id ) } } response = self.client.patch(url, data=request_data) assert response.status_code == 200 assert response.json() == new_patched_response assert Comment.objects.filter(id=self.second_comment.id).exists()
def test_format_resource_type(): assert utils.format_resource_type('first_name', 'capitalize') == 'FirstNames' assert utils.format_resource_type('first_name', 'camelize') == 'firstNames'
def test_get_blog_relationship_entry_set(self): response = self.client.get('/blogs/{}/relationships/entry_set'.format(self.blog.id)) expected_data = [{'type': format_resource_type('Entry'), 'id': str(self.first_entry.id)}, {'type': format_resource_type('Entry'), 'id': str(self.second_entry.id)}] assert response.data == expected_data
def test_data_in_correct_format_when_instantiated_with_entry_object(self): serializer = ResourceIdentifierObjectSerializer(instance=self.entry) expected_data = {'type': format_resource_type('Entry'), 'id': str(self.entry.id)} assert serializer.data == expected_data