Skip to content

Commit

Permalink
feat(testing): refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SnyderMbishai committed Dec 11, 2018
1 parent f624a31 commit 46dad62
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 14 deletions.
10 changes: 7 additions & 3 deletions authors/apps/articles/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUp(self):
self.register_url = reverse("authentication:user-signup")
self.login_url = reverse("authentication:user-login")
self.articles_url = reverse("articles:articles")
self.article_url = reverse("articles:article", kwargs={'pk':1})
self.article_url = reverse("articles:get_article", kwargs={'pk':1})
self.comments_url = ''#reverse("articles:comments")
self.comment_url = ''#reverse("articles:comment")

Expand Down Expand Up @@ -44,6 +44,12 @@ def setUp(self):
"body": "You have to believe"
}
}
self.article_invalid_data = {
"article": {
"description": "Ever wonder how?",
"body": "You have to believe"
}
}

self.new_comment_data = { "comment": {
"body": "Awesome!!!"
Expand All @@ -59,7 +65,6 @@ def user_signup(self):
self.register_url,
self.register_data,
format="json")
print(res, "signup8888888888888")
return res


Expand All @@ -69,7 +74,6 @@ def user_login(self):
self.login_url,
self.login_data,
format='json')
print(res.data, "66666666666666666")
return res.data['token']

def post_article(self):
Expand Down
2 changes: 1 addition & 1 deletion authors/apps/articles/tests/test_comments.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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. """

Expand Down
10 changes: 10 additions & 0 deletions authors/apps/articles/tests/test_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework import status
from .base_test import BaseTestCase

class TestDeleteArticle(BaseTestCase):
def test_successful_article_deletion(self):
self.user_signup()
token='Token ' + self.user_login()
self.test_client.post(self.articles_url, self.article_data, format='json', HTTP_AUTHORIZATION=token)
response = self.test_client.delete(self.article_url, HTTP_AUTHORIZATION=token)
self.assertEqual(response.status_code, status.HTTP_200_OK)
36 changes: 36 additions & 0 deletions authors/apps/articles/tests/test_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.urls import reverse
from rest_framework import status
from .base_test import BaseTestCase

class TestPostArticle(BaseTestCase):

def test_successful_get_creation(self):
# valid data
self.user_signup()
token='Token ' + self.user_login()
self.test_client.post(self.articles_url, self.article_data, format='json', HTTP_AUTHORIZATION=token)
response = self.test_client.get(self.articles_url, HTTP_AUTHORIZATION=token)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_get_article(self):
self.user_signup()
token='Token ' + self.user_login()
# print('-----------------------{}---------------------------'.format(token))

self.test_client.post(self.articles_url, self.article_data, format='json', HTTP_AUTHORIZATION=token)
response = self.test_client.get(self.article_url, HTTP_AUTHORIZATION=token)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_get_non_existing_article(self):
self.user_signup()
token = 'Token '+ self.user_login()
self.test_client.post(self.articles_url, self.article_data, format='json', HTTP_AUTHORIZATION=token)
response = self.test_client.get("articles/10")
print("*"*20, response)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)






32 changes: 32 additions & 0 deletions authors/apps/articles/tests/test_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from rest_framework import status
from .base_test import BaseTestCase

class TestPostArticle(BaseTestCase):
def test_successful_article_creation(self):
# valid data
self.user_signup()
token='Token ' + self.user_login()
response = self.test_client.post(self.articles_url, self.article_data, format='json', HTTP_AUTHORIZATION=token)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_post_using_invalid_data(self):
self.user_signup()
token='Token ' + self.user_login()
response = self.test_client.post(self.articles_url, self.article_invalid_data, format='json', HTTP_AUTHORIZATION=token)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_without_authentication(self):
self.user_signup()
response = self.test_client.post(self.articles_url, self.article_invalid_data, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

# Post
# Test article creation with valid data
# Test article with invalid data
# Test article with
# Get
#
# Put
#
# Delete
#
Empty file.
2 changes: 1 addition & 1 deletion authors/apps/articles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

urlpatterns = [
path('articles/', ArticleAPIView.as_view(), name='articles'),
path('articles/<int:pk>', ArticleAPIView.as_view(), name='article')
path('articles/<int:pk>', ArticleAPIView.as_view(), name='get_article')
]
25 changes: 16 additions & 9 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.shortcuts import render
from rest_framework import permissions
from rest_framework.generics import RetrieveUpdateAPIView
from rest_framework.exceptions import PermissionDenied
from rest_framework import status
from rest_framework.views import APIView
Expand All @@ -16,7 +17,7 @@ class ArticleAPIView(APIView):
"""
Class for handling Article.
"""
permission_classes = (IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)
permission_classes = (IsAuthenticatedOrReadOnly,)

def post(self, request):
"""
Expand All @@ -35,17 +36,20 @@ def get(self, request, pk=None):
Method for getting one or all articles.
"""
# If the primary key is provided, a specific article should be returned.
if pk:
article = Article.objects.get(pk=pk)
serializer = ArticleAuthorSerializer(article)
message = {'message':"Article found.", 'article':serializer.data}
return Response(message, status=status.HTTP_200_OK)
# If no primary is provided, all articles should be returned.
else:
articles = Article.objects.all()
# if pk:
# article = Article.objects.get(pk=pk)
# serializer = ArticleAuthorSerializer(article)
# message = {'message':"Article found.", 'article':serializer.data}
# return Response(message, status=status.HTTP_200_OK)
# # If no primary is provided, all articles should be returned.
# else:
articles = Article.objects.all()
if articles:
serializer = ArticleAuthorSerializer(articles, many=True)
message = {'message':"Articles found.", 'articles': serializer.data}
return Response(message, status=status.HTTP_200_OK)
else:
return Response({'message':"Articles not found"})

def put(self, request, pk):

Expand Down Expand Up @@ -77,3 +81,6 @@ def delete(self, request, pk):
'message':"article '{}' has been successfully deleted.".format(article.title)
}
return Response(message, status=status.HTTP_200_OK)

class SpecificArticleAPIView(RetrieveUpdateAPIView):
permission_classes = (IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)

0 comments on commit 46dad62

Please sign in to comment.