def test_incomplete_form_data_post(self): self.client.credentials( HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) response = self.client.post(reverse('bookmark:bookmark')) data = u.get_json(response) self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) self.assertEqual(data, {'detail': "Field 'article_id' not provided."})
def test_invalid_article_pk_bookmark_creation(self): self.client.credentials( HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) response = self.client.post(reverse('bookmark:bookmark'), data={ 'article_id': Article.objects.first().id + random.randint(1, 10) }) data = u.get_json(response) self.assertEqual(response.status_code, 404) self.assertEqual(data, {'detail': 'Article does not exist.'})
def test_get_articles_ids_author_bookmarked_test(self): self.client.credentials( HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) response = self.client.get(reverse('bookmark:pk-list')) data = u.get_json(response) author_bookmarked_articles_ids = [ bookmark.article.id for bookmark in self.author.bookmarks.all() ] self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(data, author_bookmarked_articles_ids)
def test_authenticated_article_creation(self): """ Finally test proper creation. """ self.client.credentials(HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) response = self.client.post(reverse('article:create'), data=self.article_data) data = u.get_json(response) self.assertEqual(response.status_code, 201) self.assertEqual(data, ArticleDetailSerializer( Article.objects.first() ).data)
def test_authenticated_bookmark_deletion(self): self.client.credentials( HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) bookmark, _ = Bookmark.objects.get_or_create(author=self.author, article=random.choice( self.articles)) bookmarked_article_id = bookmark.article_id response = self.client.post(reverse('bookmark:bookmark'), data={'article_id': bookmarked_article_id}) data = u.get_json(response) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(data, {'detail': {'action': 'deleted'}})
def test_get_articles_author_bookmarked_test(self): self.client.credentials( HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) response = self.client.get(reverse('bookmark:list')) data = u.get_json(response) author_bookmarked_articles = [ bookmark.article for bookmark in self.author.bookmarks.all() ] serialized_data = ArticleListSerializer(author_bookmarked_articles, many=True).data self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(data['results'], serialized_data)
def test_authenticated_bookmark_creation(self): self.client.credentials( HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) article: Article = random.choice(self.articles) self.bookmarked_article_id = article.id response = self.client.post(reverse('bookmark:bookmark'), data={'article_id': article.id}) data = u.get_json(response) self.assertEqual(response.status_code, status.HTTP_201_CREATED, msg=data) self.assertEqual(data, {'detail': {'action': 'created'}})
def test_article_creation_with_invalid_topic_id(self): """ Pretty self explanatory - test with valid creds and data but for a Topic instance that doesn't exist - with an id. """ self.client.credentials(HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) article_data = self.article_data.copy() article_data['topic_id'] = Topic.objects.first().id + 10 response = self.client.post(reverse('article:create'), data=article_data) data = u.get_json(response) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(data, { 'detail': 'Topic not found.' })
def test_authenticated_article_creation_with_existing_name(self): """ Due to the order of how the tests run, the test_authenticated_article_creation should run before this test but for some reason the test database isn't populated with the article created in the former test case. Thus, a new article is created with the same data as self.article_data and """ # Create an Article Article.objects.create(**self.article_data) self.client.credentials(HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) response = self.client.post(reverse('article:create'), data=self.article_data) data = u.get_json(response) self.assertEqual(response.status_code, status.HTTP_409_CONFLICT) self.assertEqual(data, { 'detail': f"Article with title '{self.article_data['title']}' exists." })
def test_authenticated_article_creation_invalid_data(self): """ Make a valid authenticated request to /api/articles/create/ with incomplete (read: invalid) data and check for error messages. """ self.client.credentials(HTTP_AUTHORIZATION=u.auth_header(self.author.get_key())) for field in self.article_data: d = self.article_data.copy() del d[field] response = self.client.post(reverse('article:create'), data=d) data = u.get_json(response) self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) if field != 'thumbnail_url': self.assertEqual(data, { 'detail': f"Field '{field}' not provided." }) else: self.assertEqual(data, { 'detail': 'Either provide a url for an article thumbnail or an image upload.' })