-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
323 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import unittest | ||
from rest_framework import status | ||
from .base_test import BaseTestCase | ||
import unittest | ||
|
||
|
||
@unittest.skip("Skip this class") | ||
@unittest.skip("Not implemented") | ||
class TestComments(BaseTestCase): | ||
""" Class for testing comments. """ | ||
|
||
# test post comment | ||
def test_comment_creation(self): | ||
""" Test comment posting. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.post_comment() | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
def test_comment_creation_with_invalid_data(self): | ||
""" Test creating a comment using invalid data. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.test_client.post(self.comment_url, | ||
self.invalid_comment_data, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_commenting_on_non_existing_article(self): | ||
""" Test commenting on a missing article. """ | ||
self.user_signup() | ||
self.user_login() | ||
response = self.test_client.post(self.comment_url, | ||
self.invalid_comment_data, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def test_commenting_by_a_non_user(self): | ||
""" Test a non-user cannot comment. """ | ||
response = self.test_client.post(self.comment_url, | ||
self.invalid_comment_data, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
# test getting comment | ||
def test_getting_a_comment(self): | ||
""" Test getting a single comment successfully. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.post_comment() | ||
response2 = self.test_client.get(self.comment_url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_getting_a_non_existing_comment(self): | ||
""" Test getting a missing comment. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.test_client.get(self.comment_url) | ||
self.assertEqual(response.status_code, status.HTTP_400_NOT_FOUND) | ||
|
||
def test_getting_comment_from_a_missing_article(self): | ||
""" Test getting comment from a non-existent article. """ | ||
self.user_signup() | ||
self.user_login() | ||
response2 = self.test_client.get(self.comment_url) | ||
self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_getting_all_comments(self): | ||
""" Test getting all comments to an article. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.post_comment() | ||
response2 = self.test_client.get(self.comments_url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_getting_all_comments_from_a_missing_article(self): | ||
""" Test getting all comments from a non-existent article. """ | ||
self.user_signup() | ||
self.user_login() | ||
response2 = self.test_client.get(self.comments_url) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
# test updating comment | ||
def test_updating_a_comment(self): | ||
""" Test editing an existing comment. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.post_comment() | ||
response2 = self.test_client.put(self.comment_url, | ||
self.new_comment_data, format='json') | ||
self.assertEqual(response2.status_code, status.HTTP_200_OK) | ||
|
||
def test_updating_with_invalid_data(self): | ||
""" Test updating comment using invalid data. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.post_comment() | ||
response2 = self.test_client.put(self.comment_url, | ||
self.invalid_comment_data, format='json') | ||
self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_updating_missing_comment(self): | ||
""" Test updating a non-existent comment. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.test_client.put(self.comment_url, | ||
self.new_comment_data, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def test_non_logged_in_user_cannot_update(self): | ||
""" Test a user has to login before updating. """ | ||
self.user_signup() | ||
self.post_article() | ||
response = self.test_client.put(self.comment_url, | ||
self.new_comment_data, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
# test deleting comment | ||
def test_deleting_an_existing_comment(self): | ||
""" Method for testing deleting an existing comment. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.post_comment() | ||
response2 = self.test_client.delete(self.comment_url) | ||
self.assertEqual(response2.status_code, status.HTTP_200_OK) | ||
|
||
def test_deleting_a_non_existing_comment(self): | ||
""" Method for testing deleting an existing comment. """ | ||
self.user_signup() | ||
self.user_login() | ||
self.post_article() | ||
response = self.test_client.delete(self.comment_url) | ||
self.assertEqual(response.status_code, status.HTTP_404_OK) | ||
|
||
def test_non_logged_in_user_deletting_comment(self): | ||
""" Test a user has to login before deleting. """ | ||
self.user_signup() | ||
self.post_article() | ||
response = self.post_comment() | ||
response2 = self.test_client.delete(self.comment_url) | ||
self.assertEqual(response2.status_code, status.HTTP_403_FORBIDDEN) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from rest_framework import status | ||
from .base_test import BaseTestCase | ||
from django.urls import reverse | ||
|
||
|
||
class TestArticles(BaseTestCase): | ||
"class to test liking and disliking of articles" | ||
|
||
def test_like(self): | ||
"""test for liking an article""" | ||
self.user_signup() | ||
token = self.user_login() | ||
response = self.client.put(self.like_article(), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'article': 'You have liked this article'}) | ||
|
||
def test_dislike(self): | ||
"""test for liking an article""" | ||
self.user_signup() | ||
token = self.user_login() | ||
response = self.client.put(self.dislike_article(), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'article': "You have disliked this article"}) | ||
|
||
def test_like_dislike(self): | ||
"""test for liking then disliking an article""" | ||
self.user_signup() | ||
token = self.user_login() | ||
response = self.client.put(self.like_article(), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.data, {'article': 'You have liked this article'}) | ||
response = self.client.put(self.dislike_article(), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'article': "You have disliked this article"}) | ||
|
||
def test_dislike_like(self): | ||
"""test for liking then disliking an article""" | ||
self.user_signup() | ||
token = self.user_login() | ||
response = self.client.put(self.dislike_article(), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'article': "You have disliked this article"}) | ||
response = self.client.put(self.like_article(), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.data, {'article': 'You have liked this article'}) | ||
|
||
def test_like_nonexisting_article(self): | ||
"""test for liking an article""" | ||
self.user_signup() | ||
token = self.user_login() | ||
response = self.client.put(self.likearticle_url, format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.data, {'Error': "Article does not exist"}) | ||
|
||
def test_dislike_nonexisting_article(self): | ||
"""test for liking an article""" | ||
self.user_signup() | ||
token = self.user_login() | ||
response = self.client.put(self.dislikearticle_url, format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.data, {'Error': "Article does not exist"}) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.