def test_delete_one_tag(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object = utils.create_mathematical_object(self) tag_object_1 = utils.add_tag(self, mathematical_object.id) tag_object_2 = utils.add_tag(self, mathematical_object.id) self.assertEqual(mathematical_object.tags.count(), 2) response = self.client.delete(reverse('api:tag', kwargs={'pk': tag_object_1.id}), format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(mathematical_object.tags.count(), 1) self.assertEqual( len(mathematical_object.tags.filter(pk=tag_object_2.id)), 1)
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_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_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_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_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.delete(reverse('api:mathematical_object_tag', kwargs={'object_pk': mathematical_object.id, 'tag_pk': tag_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(mathematical_object.tags.count(), 0) self.assertEqual(Tag.objects.count(), 0)
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_specific_tag_as_user_or_visitor(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.USER) response = self.client.delete(reverse('api:mathematical_object_tag', kwargs={'object_pk': mathematical_object.id, 'tag_pk': tag_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_tag', kwargs={'object_pk': mathematical_object.id, 'tag_pk': tag_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
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_retrieve_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.get(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.get(reverse('api:tag', kwargs={'pk': tag_object.id}), format='json') self.assertEqual(response.status_code, status.HTTP_200_OK)
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_put_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) new_tag = 'test_put_tag_as_visitor_and_user' data = {'function': new_tag} utils.log_as(self, utils.UserType.VISITOR) response = self.client.put(reverse('api:tag', kwargs={'pk': tag_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:tag', kwargs={'pk': tag_object.id}), data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def __create_multiple_objects_and_tags(self, number_of_tags): for i in range(number_of_tags): utils.add_tag(self)