Skip to content

Commit

Permalink
Merge 8669253 into 99a25c5
Browse files Browse the repository at this point in the history
  • Loading branch information
AGMETEOR committed Sep 3, 2018
2 parents 99a25c5 + 8669253 commit 73bf408
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
50 changes: 50 additions & 0 deletions authors/apps/articles/tests/test_fetch_all_articles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from django.test import TestCase, RequestFactory
from authors.apps.articles.views import ListAllArticlesAPIView, ArticleCreateAPIView
from authors.apps.authentication.views import RegistrationAPIView, VerificationAPIView


class FetchAllArticlesTestCase(TestCase):
def setUp(self):

self.user = {
"user": {
"email": "test@gmail.com",
"username": "tester",
"password": "testpass@word"
}
}

self.obj = UtilClass()
registered_user = self.obj.get_reg_data(self.user)
self.obj.verify_user({"token": registered_user.data["token"]})
logged_in_user = self.obj.get_login_data(self.user)

self.headers = {
'HTTP_AUTHORIZATION': 'Token ' + logged_in_user.data["token"]
}

def test_fetch_all_existing_articles(self):
article_data = {
"title": "This is Postman",
"description": "Hello Postman",
"body": "Hahahaha",
"tags": ["post", "man"]
}

make_article_request = self.factory.post('api/articles', data=json.dumps(
article_data), **self.headers, content_type='application/json')

ArticleCreateAPIView.as_view()(make_article_request)

request = self.factory.get('api/all/articles/')
response = ListAllArticlesAPIView.as_view()(request)
self.assertEqual(response.data['results'][count], 1)
self.assertEqual(
response.data['results']['articles'][0]['title'], "This is Postman")

def test_fetch_all_from_empty_db(self):
request = self.factory.get('api/all/articles/')
response = ListAllArticlesAPIView.as_view()(request)
self.assertEqual(response.data['results'][count], 0)
self.assertEqual(
len(response.data['results']['articles']), 0)
2 changes: 2 additions & 0 deletions authors/apps/articles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
UpdateArticleAPIView,
SearchArticlesAPIView,
DeleteArticleAPIView,
ListAllArticlesAPIView,
)

app_name = "articles"
Expand All @@ -24,4 +25,5 @@
path('articles/<slug>/favorite', FavoriteArticleAPIView.as_view()),
path('articles/search', SearchArticlesAPIView.as_view()),
path('articles/<slug>/delete/', DeleteArticleAPIView.as_view()),
path('articles/all/', ListAllArticlesAPIView.as_view()),
]
11 changes: 10 additions & 1 deletion authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .serializers import CreateArticleSerializer
from authors.apps.authentication.models import User
from authors.apps.articles.models import Article, Comment
from rest_framework.permissions import IsAuthenticated
from rest_framework.permissions import IsAuthenticated, AllowAny
from authors.apps.authentication.backends import JWTAuthentication
from rest_framework.views import APIView
from rest_framework import status, exceptions
Expand Down Expand Up @@ -186,6 +186,15 @@ def get_queryset(self):
filtered_queryset.append(article)
return filtered_queryset

class ListAllArticlesAPIView(ListAPIView):
permission_classes = (AllowAny, )
serializer_class = CreateArticleSerializer
renderer_classes = (ListArticlesJSONRenderer,)
pagination_class = PageNumberPagination
def get_queryset(self):
articles = Article.objects.all()
return articles


class UpdateArticleAPIView(UpdateAPIView):
permission_classes = (IsAuthenticated, )
Expand Down

0 comments on commit 73bf408

Please sign in to comment.