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_retrieve_specific_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) 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_204_NO_CONTENT) self.assertEqual(mathematical_object.functions.count(), 0) self.assertEqual(Function.objects.count(), 0)
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_small_mathematical_object(self): utils.log_as(self, utils.UserType.STAFF) representation = 'test' type = 'S' data = { 'latex': representation, 'type': type, } response = self.client.post(reverse('api:mathematical_objects'), data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) response = self.client.get(reverse('api:mathematical_object', kwargs={'pk': response.data['id']})) self.assertEqual(response.status_code, status.HTTP_200_OK) response_data = response.data self.assertEqual(representation, response_data['latex']) self.assertEqual(type, response_data['type'])
def test_create_invalid_type_mathematical_object(self): utils.log_as(self, utils.UserType.STAFF) representation = 'test' type = 'BUG' function = 'function' name = 'name' data = { 'latex': representation, 'type': type, 'functions': [{'function', function}], 'names': [{'name', name}] } response = self.client.post(reverse('api:mathematical_objects'), data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(MathematicalObject.objects.count(), 0)
def test_create_small_mathematical_object(self): utils.log_as(self, utils.UserType.STAFF) representation = 'test' type = 'S' data = {'latex': representation, 'type': type} response = self.client.post(reverse('api:mathematical_objects'), data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(MathematicalObject.objects.count(), 1) response_data = response.data self.assertEqual(representation, response_data['latex']) self.assertEqual(type, response_data['type']) mathematical_object = MathematicalObject.objects.get( pk=response_data['id']) self.assertEqual(mathematical_object.functions.count(), 0) self.assertEqual(mathematical_object.names.count(), 0)
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_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_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)
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_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_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)