Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/andela/ah-jarvis into ft…
Browse files Browse the repository at this point in the history
…-search-159054021
  • Loading branch information
dessHub committed Aug 15, 2018
2 parents f4ceae8 + b8c4073 commit b2d96ad
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 38 deletions.
41 changes: 20 additions & 21 deletions authors/apps/articles/tests/test_favorite.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,6 @@ def register_user(self, user):
user = json.loads(response.content)
return user

def test_user_get_favorite_non_existent_article(self):
"""
Check if user can favorite an article with a non-existent slug.
"""
user_details = self.register_user(TEST_USER).get("user")
token = user_details['token']
name = 'wrong slug :-('

url = reverse("articles:favorite", args=[name])

self.client.credentials(HTTP_AUTHORIZATION='Bearer '+token)

response = self.client.get(url)
response.render()
favorite = json.loads(response.content)
details = favorite["favorite"]["detail"]

self.assertEquals(details, "An article with this slug doesn't exist")
self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND)

def test_user_favorite_non_existent_article(self):
"""
Check if user can favorite an article with a non-existent slug.
Expand All @@ -72,7 +52,6 @@ def test_user_favorite_non_existent_article(self):
favorite = json.loads(response.content)
details = favorite["favorite"]["detail"]


self.assertEquals(details, "An article with this slug does not exist")
self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND)

Expand All @@ -96,6 +75,26 @@ def test_user_favorite_article(self):
self.assertEquals(count, 1)
self.assertEquals(is_favorited, True)

def test_user_unfavorite_non_existent_article(self):
"""
Check if user can favorite an article with a non-existent slug.
"""
user_details = self.register_user(TEST_USER).get("user")
token = user_details['token']
name = 'wrong slug :-('

url = reverse("articles:favorite", args=[name])

self.client.credentials(HTTP_AUTHORIZATION='Bearer '+token)

response = self.client.delete(url)
response.render()
favorite = json.loads(response.content)
details = favorite["favorite"]["detail"]

self.assertEquals(details, "An article with this slug does not exist")
self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND)

def test_user_unfavorite_article(self):
article = CreateArticle().create_article()

Expand Down
201 changes: 201 additions & 0 deletions authors/apps/articles/tests/test_likes_and_dislikes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import json
from django.urls import reverse
from rest_framework.test import APITestCase
from rest_framework import status
from authors.apps.authentication.models import User
from authors.apps.authentication.tests.utils import TEST_USER
from django.core import mail
from authors.apps.authentication.views import VerifyAccount
from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes
from authors.apps.authentication.utils import generate_token
from rest_framework.test import force_authenticate
from rest_framework.test import APIRequestFactory
from authors.apps.articles.models import Article

user = {
"user": {
"username": "test",
"email": "info@test.co",
"password": "Test123."
}
}


class ArticleCRUDTestCase(APITestCase):
"""Test Cases to test ratings feature"""

def setUp(self):
"""Initialize default data."""

self.user = {
"user": {
"username": "test",
"email": "info@test.co",
"password": "Test123."
}
}
self.user1 = {
"user": {
"username": "Jacob",
"email": "jake@jake.com",
"password": "Test123."
}
}
self.article1 = {
"article": {
"title": "How to train your dragon",
"description": "Ever wonder how?",
"body": "You have to believe",
}
}

self.article2 = {
"article": {
"title": "How to feed your dragon",
"description": "Wanna know how?",
"body": "You don't believe?",
}
}

def login_user(self, user=user):
"""
login user
"""
response = self.client.post(
reverse("authentication:login"),
user,
format='json')
response.render()
user = json.loads(response.content)
return user

def create_a_user(self, username='test', email='info@test.co',
password='Test123.'):
"""
Create a test user
"""
user = User.objects.create_user(username, email, password)
user.save()
return user

def create_article(self):
"""
Create a test article
"""
user = User.objects.get()
article = Article.objects.create(
title="How to train your dragon",
description="Ever wonder how?",
body="You have to believe",
author=user.profile)
article.save()
return article

def verify_user(self, user):
"""Verify user"""
token = generate_token.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk)).decode("utf-8")
request = APIRequestFactory().get(
reverse("authentication:verify", args=[uid, token]))
verify_account = VerifyAccount.as_view()
verify_account(request, uidb64=uid, token=token)
return user

def test_auth_user_can_like(self):
user = self.create_a_user()
self.verify_user(user)
auth_user = self.login_user()
user = User.objects.get()
article = self.create_article()

res = self.client.put('/api/articles/'+article.slug+'/like/',
HTTP_AUTHORIZATION='Bearer ' +
auth_user['user']['token'],
format='json'
)
self.assertEquals(res.status_code, 200)

def test_auth_user_can_dislike(self):
user = self.create_a_user()
self.verify_user(user)
auth_user = self.login_user()
user = User.objects.get()
article = self.create_article()

res = self.client.put('/api/articles/'+article.slug+'/dislike/',
HTTP_AUTHORIZATION='Bearer ' +
auth_user['user']['token'],
format='json'
)
self.assertEquals(res.status_code, 200)

def test_like_404_article(self):
user = self.create_a_user()
self.verify_user(user)
auth_user = self.login_user()
user = User.objects.get()
slug = 'fake-slug-13qedffd23'

res = self.client.put('/api/articles/'+slug+'/like/',
HTTP_AUTHORIZATION='Bearer ' +
auth_user['user']['token'],
format='json'
)
self.assertEquals(res.status_code, 404)

def test_dislike_404_article(self):
user = self.create_a_user()
self.verify_user(user)
auth_user = self.login_user()
user = User.objects.get()
slug = 'fake-slug-13qedffd23'

res = self.client.put('/api/articles/'+slug+'/dislike/',
HTTP_AUTHORIZATION='Bearer ' +
auth_user['user']['token'],
format='json'
)
self.assertEquals(res.status_code, 404)

def test_like_disliked_article(self):
user = self.create_a_user()
self.verify_user(user)
auth_user = self.login_user()
user = User.objects.get()
article = self.create_article()

self.client.put('/api/articles/'+article.slug+'/dislike/',
HTTP_AUTHORIZATION='Bearer ' +
auth_user['user']['token'],
format='json'
)

res = self.client.put('/api/articles/'+article.slug+'/like/',
HTTP_AUTHORIZATION='Bearer ' +
auth_user['user']['token'],
format='json'
)

self.assertEquals(res.status_code, 200)

def test_dislike_liked_article(self):
user = self.create_a_user()
self.verify_user(user)
auth_user = self.login_user()
user = User.objects.get()
article = self.create_article()

self.client.put('/api/articles/'+article.slug+'/like/',
HTTP_AUTHORIZATION='Bearer ' +
auth_user['user']['token'],
format='json'
)

res = self.client.put('/api/articles/'+article.slug+'/dislike/',
HTTP_AUTHORIZATION='Bearer ' +
auth_user['user']['token'],
format='json'
)

self.assertEquals(res.status_code, 200)
17 changes: 0 additions & 17 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,23 +301,6 @@ class FavoriteAPIView(APIView):
serializer_class = ArticleSerializer
queryset = Article.objects.all()

def get(self, request, slug):
"""
Override the retrieve method to get a article
"""
serializer_context = {'request': request}
try:
serializer_instance = self.queryset.get(slug=slug)
except Article.DoesNotExist:
raise NotFound("An article with this slug doesn't exist")

serializer = self.serializer_class(
serializer_instance,
context=serializer_context
)

return Response(serializer.data, status=status.HTTP_200_OK)

def post(self, request, slug):
"""
Method that favorites articles.
Expand Down

0 comments on commit b2d96ad

Please sign in to comment.