def test_delete_one_name(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) name_object_1 = utils.add_name(self, mathematical_object.id) name_object_2 = utils.add_name(self, mathematical_object.id) self.assertEqual(mathematical_object.names.count(), 2) response = self.client.delete(reverse('api:name', kwargs={'pk': name_object_1.id}), format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(mathematical_object.names.count(), 1) self.assertEqual( len(mathematical_object.names.filter(pk=name_object_2.id)), 1)
def test_retrieve_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.get(reverse('api:name', kwargs={'pk': name_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 1)
def test_retrieve_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, default_name='test_retrieve_name' + utils.get_random_characters()) response = self.client.get(reverse('api:name-autocomplete'), format='json') self.assertContains(response, name_object.name)
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_get_names(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self, with_name=True) name_object_1 = utils.add_name(self, mathematical_object.id) name_object_2 = utils.add_name(self, mathematical_object.id) response = self.client.get(reverse( 'api:mathematical_object_names', 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 name in response.data: is_found_1 |= name['id'] == name_object_1.id is_found_2 |= name['id'] == name_object_2.id self.assertTrue(is_found_1) self.assertTrue(is_found_2)
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_only_part_of_names(self): utils.log_as(self, utils.UserType.STAFF) number_of_names = 3 mathematical_object = utils.create_mathematical_object(self) name_objects_without_bananas = [ utils.add_name(self, mathematical_object.id, default_name='test_retrieve_only_part_of_names' + utils.get_random_characters()) for _ in range(number_of_names) ] name_objects_with_bananas = [ utils.add_name(self, mathematical_object.id, default_name='bananas' + utils.get_random_characters()) for _ in range(number_of_names) ] response = self.client.get(reverse('api:name-autocomplete') + "?q=test", format='json') for name_object in name_objects_without_bananas: self.assertContains(response, name_object.name) for name_object in name_objects_with_bananas: self.assertNotContains(response, name_object.name) response = self.client.get(reverse('api:name-autocomplete') + "?q=banana", format='json') for name_object in name_objects_without_bananas: self.assertNotContains(response, name_object.name) for name_object in name_objects_with_bananas: self.assertContains(response, name_object.name)
def test_retrieve_specific_name_as_user_or_visitor(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) name_object = utils.add_name(self, mathematical_object.id) utils.log_as(self, utils.UserType.USER) 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_403_FORBIDDEN) utils.log_as(self, utils.UserType.VISITOR) 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_401_UNAUTHORIZED)
def test_put_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) new_name = 'test_put_name' data = {'name': new_name} response = self.client.put(reverse('api:name', kwargs={'pk': name_object.id}), data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) name_object.refresh_from_db() self.assertEqual(name_object.name, new_name)
def test_retrieve_name_as_visitor_and_user(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) name_object = utils.add_name(self, mathematical_object.id) utils.log_as(self, utils.UserType.VISITOR) response = self.client.get(reverse('api:name', kwargs={'pk': name_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:name', kwargs={'pk': name_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_put_name_as_visitor_and_user(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) name_object = utils.add_name(self, mathematical_object.id) new_name = 'test_put_name_as_visitor_and_user' data = {'function': new_name} utils.log_as(self, utils.UserType.VISITOR) response = self.client.put(reverse('api:name', kwargs={'pk': name_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:name', kwargs={'pk': name_object.id}), data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def __create_multiple_objects_and_names(self, number_of_names): for i in range(number_of_names): utils.add_name(self)