def test_get_relations(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object_1 = utils.create_mathematical_object(self) mathematical_object_2 = utils.create_mathematical_object(self) mathematical_object_3 = utils.create_mathematical_object(self) utils.add_relation(self, mathematical_object_1.id, mathematical_object_2.id) utils.add_relation(self, mathematical_object_1.id, mathematical_object_3.id) response = self.client.get(reverse( 'api:mathematical_object_relations', kwargs={'object_pk': mathematical_object_1.id}), format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) is_found_1 = False is_found_2 = False for relation in response.data: is_found_1 |= relation['id'] == mathematical_object_2.id is_found_2 |= relation['id'] == mathematical_object_3.id self.assertTrue(is_found_1) self.assertTrue(is_found_2)
def test_retrieve_two_function(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object_1 = utils.create_mathematical_object(self) function_object = utils.add_function(self, mathematical_object_1.id) mathematical_object_2 = utils.create_mathematical_object(self) mathematical_object_2.functions.add(function_object) response = self.client.get(reverse('api:function', kwargs={'pk': function_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 2)
def test_add_relation(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object_1 = utils.create_mathematical_object(self) mathematical_object_2 = utils.create_mathematical_object(self) utils.add_relation(self, mathematical_object_1.id, mathematical_object_2.id) mathematical_object_2.refresh_from_db() self.assertEqual(mathematical_object_1.related.count(), 1) self.assertEqual(mathematical_object_2.related.count(), 1)
def test_add_another_relation(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object_1 = utils.create_mathematical_object(self) mathematical_object_2 = utils.create_mathematical_object(self) mathematical_object_3 = utils.create_mathematical_object(self) utils.add_relation(self, mathematical_object_1.id, mathematical_object_2.id) utils.add_relation(self, mathematical_object_1.id, mathematical_object_3.id) self.assertEqual(mathematical_object_1.related.count(), 2) self.assertEqual(mathematical_object_2.related.count(), 1)
def test_add_tag(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) tag = 'test_add_tag' data = { 'tag': tag } response = self.client.post(reverse('api:mathematical_object_tags', kwargs={'object_pk': mathematical_object.id}), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) tag_object = Tag.objects.get(pk=response.data['id']) self.assertEqual(Tag.objects.count(), 1) self.assertEqual(tag_object.tag, tag) self.assertEqual(len(mathematical_object.tags.all()), 1) utils.log_as(self, utils.UserType.USER) response = self.client.post( reverse('api:mathematical_object_tags', kwargs={'object_pk': mathematical_object.id}), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) utils.log_as(self, utils.UserType.VISITOR) response = self.client.post( reverse('api:mathematical_object_tags', kwargs={'object_pk': mathematical_object.id}), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_retrieve_specific_function_as_user_or_visitor(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) function_object = utils.add_function(self, mathematical_object.id) utils.log_as(self, utils.UserType.USER) response = self.client.delete(reverse( 'api:mathematical_object_function', kwargs={ 'object_pk': mathematical_object.id, 'function_pk': function_object.id }), format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) utils.log_as(self, utils.UserType.VISITOR) response = self.client.delete(reverse( 'api:mathematical_object_function', kwargs={ 'object_pk': mathematical_object.id, 'function_pk': function_object.id }), format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_add_name(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) name = 'test_add_name' data = {'name': name} response = self.client.post(reverse( 'api:mathematical_object_names', kwargs={'object_pk': mathematical_object.id}), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) name_object = Name.objects.get(pk=response.data['id']) self.assertEqual(Name.objects.count(), 1) self.assertEqual(name_object.name, name) self.assertEqual(len(mathematical_object.names.all()), 1) utils.log_as(self, utils.UserType.USER) response = self.client.post(reverse( 'api:mathematical_object_names', kwargs={'object_pk': mathematical_object.id}), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) utils.log_as(self, utils.UserType.VISITOR) response = self.client.post(reverse( 'api:mathematical_object_names', kwargs={'object_pk': mathematical_object.id}), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_automatically_delete_description_of_mathematical_object(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self, description='Test') self.assertTrue(utils.is_file(mathematical_object.description.path)) mathematical_object.delete() self.assertFalse(utils.is_file(mathematical_object.description.path))
def test_add_another_tag(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self, with_tag=True) tag = 'test_add_another_tag' tag_object = utils.add_tag(self, mathematical_object.id, default_tag=tag) self.assertEqual(Tag.objects.count(), 2) self.assertEqual(tag_object.tag, tag) self.assertEqual(len(mathematical_object.tags.all()), 2)
def test_add_description_to_mathematical_object(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) self.assertFalse(bool(mathematical_object.description)) content = 'test_add_description_to_mathematical_object' mathematical_object.save_content(content) self.assertEqual(mathematical_object.get_content(), content)
def test_retrieve_tag(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) tag_object = utils.add_tag(self, mathematical_object.id) response = self.client.get(reverse('api:tag', kwargs={'pk': tag_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 1)
def test_delete_one_function(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) function_object_1 = utils.add_function(self, mathematical_object.id) function_object_2 = utils.add_function(self, mathematical_object.id) self.assertEqual(mathematical_object.functions.count(), 2) response = self.client.delete(reverse('api:function', kwargs={'pk': function_object_1.id}), format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(mathematical_object.functions.count(), 1) self.assertEqual(len(mathematical_object.functions.filter(pk=function_object_2.id)), 1)
def test_retrieve_tag(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) tag_object = utils.add_tag(self, mathematical_object.id, default_tag='test_retrieve_tag' + utils.get_random_characters()) response = self.client.get(reverse('api:tag-autocomplete'), format='json') self.assertContains(response, tag_object.tag)
def test_retrieve_specific_name(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) name_object = utils.add_name(self, mathematical_object.id) response = self.client.delete(reverse('api:mathematical_object_name', kwargs={'object_pk': mathematical_object.id, 'name_pk': name_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(mathematical_object.names.count(), 0) self.assertEqual(Name.objects.count(), 0)
def test_replace_description_of_mathematical_object(self): utils.log_as(self, utils.UserType.STAFF) old_content = 'old_content' mathematical_object = utils.create_mathematical_object(self, description=old_content) self.assertTrue(bool(mathematical_object.description)) self.assertEqual(mathematical_object.get_content(), old_content) content = 'test_replace_description_of_mathematical_object' mathematical_object.save_content(content) self.assertEqual(mathematical_object.get_content(), content)
def test_add_another_name(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self, with_name=True) name = 'test_add_another_name' name_object = utils.add_name(self, mathematical_object.id, default_name=name) self.assertEqual(Name.objects.count(), 2) self.assertEqual(name_object.name, name) self.assertEqual(len(mathematical_object.names.all()), 2)
def test_retrieve_function_as_visitor_and_user(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) function_object = utils.add_function(self, mathematical_object.id) utils.log_as(self, utils.UserType.VISITOR) response = self.client.get(reverse('api:function', kwargs={'pk': function_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) utils.log_as(self, utils.UserType.USER) response = self.client.get(reverse('api:function', kwargs={'pk': function_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_retrieve_name_not_connected(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) name_object = utils.add_name( self, mathematical_object.id, default_name='test_retrieve_name_not_connected' + utils.get_random_characters()) utils.log_as(self, utils.UserType.VISITOR) response = self.client.get(reverse('api:name-autocomplete'), format='json') self.assertNotContains(response, name_object.name)
def test_retrieve_function(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) function_object = utils.add_function( self, mathematical_object.id, function_name='test_retrieve_function' + utils.get_random_characters()) response = self.client.get(reverse('api:function-autocomplete'), format='json') self.assertContains(response, function_object.function)
def test_retrieve_specific_relation(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object_1 = utils.create_mathematical_object(self) mathematical_object_2 = utils.create_mathematical_object(self) utils.add_relation(self, mathematical_object_1.id, mathematical_object_2.id) self.assertEqual(mathematical_object_1.related.count(), 1) self.assertEqual(mathematical_object_2.related.count(), 1) response = self.client.delete(reverse( 'api:mathematical_object_relation', kwargs={ 'object_pk': mathematical_object_1.id, 'other_pk': mathematical_object_2.id }), format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(mathematical_object_1.related.count(), 0) self.assertEqual(mathematical_object_2.related.count(), 0) self.assertEqual(Name.objects.count(), 0)
def test_put_function(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) function_object = utils.add_function(self, mathematical_object.id) new_name = 'test_put_function' data = { 'function': new_name } response = self.client.put(reverse('api:function', kwargs={'pk': function_object.id}), data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) function_object.refresh_from_db() self.assertEqual(function_object.function, new_name)
def test_put_tag(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) tag_object = utils.add_tag(self, mathematical_object.id) new_tag = 'test_put_tag' data = {'tag': new_tag} response = self.client.put(reverse('api:tag', kwargs={'pk': tag_object.id}), data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) tag_object.refresh_from_db() self.assertEqual(tag_object.tag, new_tag)
def test_delete_tag_as_visitor_and_user(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) tag_object = utils.add_tag(self, mathematical_object.id) utils.log_as(self, utils.UserType.VISITOR) response = self.client.delete(reverse('api:tag', kwargs={'pk': tag_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) utils.log_as(self, utils.UserType.USER) response = self.client.delete(reverse('api:tag', kwargs={'pk': tag_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_get_tags(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self, with_tag=True) tag_object_1 = utils.add_tag(self, mathematical_object.id) tag_object_2 = utils.add_tag(self, mathematical_object.id) response = self.client.get(reverse('api:mathematical_object_tags', kwargs={'object_pk': mathematical_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) is_found_1 = False is_found_2 = False for tag in response.data: is_found_1 |= tag['id'] == tag_object_1.id is_found_2 |= tag['id'] == tag_object_2.id self.assertTrue(is_found_1) self.assertTrue(is_found_2)
def test_put_function_as_visitor_and_user(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) function_object = utils.add_function(self, mathematical_object.id) new_name = 'test_put_function_as_visitor_and_user' data = { 'function': new_name } utils.log_as(self, utils.UserType.VISITOR) response = self.client.put(reverse('api:function', kwargs={'pk': function_object.id}), data, format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) utils.log_as(self, utils.UserType.USER) response = self.client.put(reverse('api:function', kwargs={'pk': function_object.id}), data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_retrieve_multiple_tags(self): utils.log_as(self, utils.UserType.STAFF) number_of_tags = 3 mathematical_object = utils.create_mathematical_object(self) tag_objects = [ utils.add_tag(self, mathematical_object.id, default_tag='test_retrieve_multiple_tags' + utils.get_random_characters()) for _ in range(number_of_tags) ] response = self.client.get(reverse('api:tag-autocomplete'), format='json') for tag_object in tag_objects: self.assertContains(response, tag_object.tag)
def test_retrieve_multiple_names(self): utils.log_as(self, utils.UserType.STAFF) number_of_names = 3 mathematical_object = utils.create_mathematical_object(self) name_objects = [ utils.add_name(self, mathematical_object.id, default_name='test_retrieve_multiple_names' + utils.get_random_characters()) for _ in range(number_of_names) ] response = self.client.get(reverse('api:name-autocomplete'), format='json') for name_object in name_objects: self.assertContains(response, name_object.name)
def test_retrieve_multiple_functions(self): utils.log_as(self, utils.UserType.STAFF) number_of_functions = 3 mathematical_object = utils.create_mathematical_object(self) function_objects = [ utils.add_function( self, mathematical_object.id, function_name='test_retrieve_multiple_functions' + utils.get_random_characters()) for _ in range(number_of_functions) ] response = self.client.get(reverse('api:function-autocomplete'), format='json') for function_object in function_objects: self.assertContains(response, function_object.function)
def test_retrieve_only_part_of_functions(self): utils.log_as(self, utils.UserType.STAFF) number_of_functions = 3 mathematical_object = utils.create_mathematical_object(self) function_objects_without_bananas = [ utils.add_function( self, mathematical_object.id, function_name='test_retrieve_only_part_of_functions' + utils.get_random_characters()) for _ in range(number_of_functions) ] function_objects_with_bananas = [ utils.add_function(self, mathematical_object.id, function_name='bananas' + utils.get_random_characters()) for _ in range(number_of_functions) ] response = self.client.get(reverse('api:function-autocomplete') + "?q=test", format='json') for function_object in function_objects_without_bananas: self.assertContains(response, function_object.function) for function_object in function_objects_with_bananas: self.assertNotContains(response, function_object.function) response = self.client.get(reverse('api:function-autocomplete') + "?q=banana", format='json') for function_object in function_objects_without_bananas: self.assertNotContains(response, function_object.function) for function_object in function_objects_with_bananas: self.assertContains(response, function_object.function)
def test_retrieve_only_part_of_tags(self): utils.log_as(self, utils.UserType.STAFF) number_of_tags = 3 mathematical_object = utils.create_mathematical_object(self) tag_objects_without_bananas = [ utils.add_tag(self, mathematical_object.id, default_tag='test_retrieve_only_part_of_tags' + utils.get_random_characters()) for _ in range(number_of_tags) ] tag_objects_with_bananas = [ utils.add_tag(self, mathematical_object.id, default_tag='bananas' + utils.get_random_characters()) for _ in range(number_of_tags) ] response = self.client.get(reverse('api:tag-autocomplete') + "?q=test", format='json') for tag_object in tag_objects_without_bananas: self.assertContains(response, tag_object.tag) for tag_object in tag_objects_with_bananas: self.assertNotContains(response, tag_object.tag) response = self.client.get(reverse('api:tag-autocomplete') + "?q=banana", format='json') for tag_object in tag_objects_without_bananas: self.assertNotContains(response, tag_object.tag) for tag_object in tag_objects_with_bananas: self.assertContains(response, tag_object.tag)