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_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_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) 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), 1)
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_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_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_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_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_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_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 __create_multiple_objects_and_functions(self, number_of_functions): for i in range(number_of_functions): utils.add_function(self)