-
Notifications
You must be signed in to change notification settings - Fork 5
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
10 changed files
with
384 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from rest_framework.test import APITestCase, APIClient | ||
from django.urls import reverse | ||
from .test_data import * | ||
from authors.apps.authentication.models import User | ||
from authors.apps.articles.models import Article | ||
|
||
|
||
class ArticlesBaseTest(APITestCase): | ||
"""Sets up tests for features related to articles""" | ||
|
||
def setUp(self): | ||
client = APIClient() | ||
self.user1 = User.objects.create_user( | ||
username='Moracha', email="jratcher@gmail.com", password='password') | ||
self.user1.is_verified = True | ||
self.user1.save() | ||
self.user2 = User.objects.create_user( | ||
username='Josh', email='joshmoracha@gmail.com', password='password') | ||
self.user2.is_verified = True | ||
self.user2.save() | ||
self.user1_credentials = client.post(reverse('authentication:login'), | ||
user1, format='json') | ||
self.user2_credentials = client.post(reverse('authentication:login'), | ||
user2, format='json') | ||
user1_token = self.user1_credentials.data.get('token') | ||
user2_token = self.user2_credentials.data.get('token') | ||
self.header_user1 = { | ||
'HTTP_AUTHORIZATION': f'Bearer {user1_token}' | ||
} | ||
self.header_user2 = { | ||
'HTTP_AUTHORIZATION': f'Bearer {user2_token}' | ||
} | ||
response = client.post(reverse('articles:create_article'), | ||
article, **self.header_user1, format='json') | ||
|
||
self.slug = response.data.get('slug') | ||
#Publish article | ||
retrieved_article = Article.objects.filter(slug=self.slug).first() | ||
retrieved_article.published = True | ||
retrieved_article.save() | ||
self.like_article_url = '/api/articles/{}/like/'.format(self.slug) | ||
|
||
|
||
|
||
|
||
|
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,29 @@ | ||
user2 = { | ||
"user": { | ||
"username": "Josh", | ||
"email": "joshmoracha@gmail.com", | ||
"password": "password", | ||
'callback_url': 'http://www.youtube.com' | ||
} | ||
} | ||
|
||
user1 = { | ||
"user": { | ||
"username": "Moracha", | ||
"email": "jratcher@gmail.com", | ||
"password": "password", | ||
'callback_url': 'http://www.youtube.com' | ||
} | ||
} | ||
article = { | ||
"article": { | ||
"title": "How to train your dragon", | ||
"description": "Ever wonder how?", | ||
"body": "You have to believe", | ||
"tagList": ["reactjs", "angularjs", "dragons"] | ||
} | ||
} | ||
|
||
like_data = { | ||
"is_like": True | ||
} |
107 changes: 107 additions & 0 deletions
107
authors/apps/articles/tests/test_like_dislike_article.py
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,107 @@ | ||
import os | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase, APIClient | ||
from .article_tests_base_class import ArticlesBaseTest | ||
from .test_data import * | ||
from authors.apps.articles.views import * | ||
|
||
|
||
class LikeDislikeArticleTestCase(ArticlesBaseTest): | ||
"""Test like and dislike of articles""" | ||
|
||
def test_like_article(self): | ||
"""Test like""" | ||
response = self.client.post(self.like_article_url, | ||
like_data, **self.header_user1, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
# Test invalid slug | ||
response = self.client.post('/api/articles/invalid-slug/like/', | ||
like_data, **self.header_user1, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
# Test already liked article | ||
response = self.client.post(self.like_article_url, | ||
like_data, **self.header_user1, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_get_like(self): | ||
"""Test like""" | ||
response = self.client.get(self.like_article_url, | ||
**self.header_user1) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
self.client.post(self.like_article_url, | ||
like_data, **self.header_user1, format='json') | ||
response1 = self.client.get( | ||
self.like_article_url, **self.header_user1) | ||
self.assertEqual(response1.status_code, status.HTTP_200_OK) | ||
# Test invalid slug | ||
response = self.client.get('/api/articles/invalid-slug/like/', | ||
**self.header_user1) | ||
detail = "This article has not been found." | ||
self.assertEqual(response.data.get('detail'), detail) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def test_update_like(self): | ||
"""Test update like to dislike or vice versa""" | ||
# test updating like that does not exist | ||
response1 = self.client.patch(self.like_article_url + str(3) + '/', | ||
like_data, **self.header_user1, format='json') | ||
self.assertEqual(response1.status_code, status.HTTP_404_NOT_FOUND) | ||
# Create like | ||
response = self.client.post(self.like_article_url, | ||
like_data, **self.header_user1, format='json') | ||
like_id = response.data.get('id') | ||
response1 = self.client.patch(self.like_article_url + str(like_id) + '/', | ||
like_data, **self.header_user1, format='json') | ||
self.assertContains(response1, 'is_like', status_code=200) | ||
# Trying to update while not an owner | ||
response = self.client.patch(self.like_article_url + str(like_id) + '/', | ||
like_data, **self.header_user2, format='json') | ||
detail = "This user does not own this like" | ||
self.assertEqual(response.data.get('detail'), detail) | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
def test_delete_like(self): | ||
"""Test update like to dislike or vice versa""" | ||
# test updating like that does not exist | ||
response1 = self.client.delete(self.like_article_url + str(3) + '/', | ||
like_data, **self.header_user1, format='json') | ||
self.assertEqual(response1.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
response = self.client.post(self.like_article_url, | ||
like_data, **self.header_user1, format='json') | ||
like_id = response.data.get('id') | ||
|
||
# Trying to update while not an owner | ||
response = self.client.delete(self.like_article_url + str(like_id) + '/', | ||
like_data, **self.header_user2, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
response1 = self.client.delete(self.like_article_url + str(like_id) + '/', | ||
like_data, **self.header_user1, format='json') | ||
self.assertEqual(response1.status_code, status.HTTP_204_NO_CONTENT) | ||
|
||
def test_method_not_allowed(self): | ||
"""Test module not found class""" | ||
response = self.client.put(self.like_article_url, | ||
like_data, **self.header_user1, format='json') | ||
self.assertEqual(response.status_code, | ||
status.HTTP_405_METHOD_NOT_ALLOWED) | ||
|
||
def test_get_likes(self): | ||
"""Test get likes count endpoint""" | ||
self.client.post(self.like_article_url, | ||
like_data, **self.header_user1, format='json') | ||
|
||
response = self.client.get('/api/articles/' + self.slug + '/likes/', | ||
**self.header_user1) | ||
self.assertEqual(response.data.get('likes'), 1) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
# Test invalid slug | ||
response = self.client.get('/api/articles/invalid-slug/likes/', | ||
**self.header_user1) | ||
detail = "This article has not been found." | ||
self.assertEqual(response.data.get('detail'), detail) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
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
Oops, something went wrong.