def test_get_queryset(self): view = PostViewSet() view.action = 'list' response = view.get_queryset() self.assertEquals(list(Post.objects.all()), list(response))
def test_get_queryset_owner(self): user = UserModel.objects.get(pk=2) request = APIRequestFactory() request.user = user view = PostViewSet() view.action = 'update' view.request = request response = view.get_queryset() self.assertEqual(response.first().created_by, user) self.assertEqual(response.last().created_by, user)
def vote_post_service(self, post_id, vote, vote_by): view = PostViewSet.as_view({'post': vote}) url = 'api/v1/posts' request = self.factory.post(url) force_authenticate(request, user=vote_by) return view(request, pk=post_id)
def create_post_service(self): view = PostViewSet.as_view({'post': 'create'}) url = 'api/v1/posts' request = self.factory.post(url, self.content_data) force_authenticate(request, user=self.user_first_poster) return view(request)
def test_get_posts_unauthorized_user(self): view = PostViewSet.as_view({'get': 'list'}) url = 'api/v1/posts' request = self.factory.get(url) response = view(request) error_detail = response.data.get('detail') error_code = 'not_authenticated' self.assertEqual(response.status_code, HTTP_401_UNAUTHORIZED) self.assertEqual(error_detail.code, error_code)
def test_deny_another_user_to_delete_post(self): response_create = self.create_post_service() view = PostViewSet.as_view({'post': 'destroy'}) url = 'api/v1/posts' request = self.factory.post(url) force_authenticate(request, user=self.user_second_poster) response_destroy = view(request, pk=response_create.data.get('id')) error_code = 'permission_denied' error_detail = response_destroy.data.get('detail') self.assertEqual(response_destroy.status_code, HTTP_403_FORBIDDEN) self.assertEqual(error_detail.code, error_code)
def test_retrieve(self): template_renderer = TemplateHTMLRenderer template_renderer.format = 'html' request = APIRequestFactory() request.accepted_renderer = template_renderer request.query_params = dict() view = PostViewSet() view.kwargs = {'pk': 1} view.action = 'retrieve' view.request = request view.format_kwarg = '' response = view.retrieve(request) self.assertEqual(response.template_name, 'posts/detail.html')
def test_fail_create_post_with_invalid_data(self): view = PostViewSet.as_view({'post': 'create'}) url = 'api/v1/posts' list_content_data = [{}, { 'title': 'Test Post' }, { 'content': 'This is post of testing' }, { 'title': '', 'content': '' }] for data in list_content_data: request = self.factory.post(url, data) force_authenticate(request, user=self.user_first_poster) response = view(request) self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST)
def test_delete_post(self): response_create = self.create_post_service() post = Post.objects.filter(title='Test Post').first() self.assertEqual(post.title, self.content_data.get('title')) self.assertEqual(post.content, self.content_data.get('content')) view = PostViewSet.as_view({'post': 'destroy'}) url = 'api/v1/posts' request = self.factory.post(url) force_authenticate(request, user=self.user_first_poster) response_destroy = view(request, pk=response_create.data.get('id')) self.assertEqual(response_destroy.status_code, HTTP_204_NO_CONTENT) post = Post.objects.filter(title='Test Post').first() self.assertIsNone(post)
def test_list(self): template_renderer = TemplateHTMLRenderer template_renderer.format = 'html' request = APIRequestFactory() request.accepted_renderer = template_renderer request.query_params = dict() request.GET = {} view = PostViewSet() view.action = 'list' view.request = request view.format_kwarg = '' response = view.list(request) self.assertEqual(response.template_name, 'posts/list.html')
from django.conf.urls import url from posts.views import PostViewSet post_detail = PostViewSet.as_view(actions={'get': 'retrieve', 'delete': 'destroy'}) post_list = PostViewSet.as_view(actions={'post': 'create', 'get': 'list'}) urlpatterns = [ url('^$', post_list), url('^/(?P<post_id>\w+)', post_detail) ]
from django.urls import path, include from rest_framework.urlpatterns import format_suffix_patterns from rest_framework import renderers from rest_framework.routers import DefaultRouter from rest_framework.authtoken.views import obtain_auth_token # <-- Here from posts import views from posts.views import PostViewSet, UserViewSet, CommentViewSet, api_root post_list = PostViewSet.as_view({'get': 'list', 'post': 'create'}) post_detail = PostViewSet.as_view({ 'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy' }) comment_list = CommentViewSet.as_view({'get': 'list', 'post': 'create'}) comment_detail = CommentViewSet.as_view({ 'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy' }) user_list = UserViewSet.as_view({'get': 'list', 'post': 'create'}) user_detail = UserViewSet.as_view({ 'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy' })
from django.conf.urls import url, include from rest_framework import routers from posts.views import PostViewSet, CommentViewSet router = routers.DefaultRouter() router.register(r'posts', PostViewSet, basename='post') urlpatterns = [ url(r'^', include(router.urls)), url(r'^author/posts$', PostViewSet.as_view({'get': 'visible_posts'})), url(r'^author/(?P<user_id>.+)/posts$', PostViewSet.as_view({'get': 'visible_user_posts'})), url(r'^posts/(?P<post_id>.+)/comments$', CommentViewSet.as_view({ 'get': 'list', 'post': 'create' })), ]