diff --git a/backend/secfit/comments/tests.py b/backend/secfit/comments/tests.py index 7ce503c2dd97ba78597f6ff6e4393132753573f6..367af6105c6903d2277578fdf964a57899709ff6 100644 --- a/backend/secfit/comments/tests.py +++ b/backend/secfit/comments/tests.py @@ -1,3 +1,38 @@ from django.test import TestCase +from rest_framework.test import APIClient +import json +from workouts.models import Workout +from users.models import User +from django.utils import timezone -# Create your tests here. +class CommentsTestCase(TestCase): + + def setUp(self): + User.objects.create(id="1",username="Bill",password="secret", email="hei") + self.user_1 = User.objects.get(id="1") + self.client = APIClient() + self.client.force_authenticate(user=self.user_1) + User.objects.create(id="2",username="Bill2",password="secret", email="hei1") + self.user_2 = User.objects.get(id="2") + self.client2 = APIClient() + self.client2.force_authenticate(user=self.user_2) + self.commentURL = "http://testserver/api/comments/" + self.workout1URL = "http://testserver/api/workouts/1/" + + def testPostComment(self): + Workout.objects.create(id="1",name="workout",date=timezone.now(),owner=self.user_1, visibility="PU") + post = self.client.post(self.commentURL,({"workout":self.workout1URL,"content":"asd"}),format='json') + self.assertEquals(post.status_code,201) + + def testGetComments(self): + Workout.objects.create(id="1",name="workout",date=timezone.now(),owner=self.user_1, visibility="PU") + Workout.objects.create(id="2",name="workout",date=timezone.now(),owner=self.user_2, visibility="PR") + post = self.client.post(self.commentURL,({"workout":self.workout1URL,"content":"asd"}),format='json') + self.assertEquals(post.status_code,201) + self.client2.post(self.commentURL,({"workout":"http://testserver/api/workouts/2/","content":"assdsdd"}),format='json') + user1get = self.client.get("http://testserver/api/comments/") + datadict = dict(user1get.data) + self.assertEquals(len(datadict["results"]), 1) + user2get = self.client2.get("http://testserver/api/comments/") + datadict2 = dict(user2get.data) + self.assertEquals(len(datadict2["results"]), 2) \ No newline at end of file diff --git a/backend/secfit/comments/views.py b/backend/secfit/comments/views.py index b74d0f208c9bcf06ee49817541d47742767f0b7d..a40e0a88c45f20d7e6521d3ebb64471f6a0b1c24 100644 --- a/backend/secfit/comments/views.py +++ b/backend/secfit/comments/views.py @@ -8,11 +8,10 @@ from comments.serializers import CommentSerializer, LikeSerializer from django.db.models import Q from rest_framework.filters import OrderingFilter -# Create your views here. + class CommentList( mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView ): - # queryset = Comment.objects.all() serializer_class = CommentSerializer permission_classes = [permissions.IsAuthenticated] filter_backends = [OrderingFilter] @@ -28,35 +27,25 @@ class CommentList( serializer.save(owner=self.request.user) def get_queryset(self): - workout_pk = self.kwargs.get("pk") - qs = Comment.objects.none() - - if workout_pk: - qs = Comment.objects.filter(workout=workout_pk) - elif self.request.user: - """A comment should be visible to the requesting user if any of the following hold: - - The comment is on a public visibility workout - - The comment was written by the user - - The comment is on a coach visibility workout and the user is the workout owner's coach - - The comment is on a workout owned by the user - """ - # The code below is kind of duplicate of the one in ./permissions.py - # We should replace it with a better solution. - # Or maybe not. - - qs = Comment.objects.filter( - Q(workout__visibility="PU") - | Q(owner=self.request.user) - | ( - Q(workout__visibility="CO") - & Q(workout__owner__coach=self.request.user) - ) - | Q(workout__owner=self.request.user) - ).distinct() + """A comment should be visible to the requesting user if any of the following hold: + - The comment is on a public visibility workout + - The comment was written by the user + - The comment is on a coach visibility workout and the user is the workout owner's coach + - The comment is on a workout owned by the user + """ + + qs = Comment.objects.filter( + Q(workout__visibility="PU") + | Q(owner=self.request.user) + | ( + Q(workout__visibility="CO") + & Q(workout__owner__coach=self.request.user) + ) + | Q(workout__owner=self.request.user) + ).distinct() return qs -# Details of comment class CommentDetail( mixins.RetrieveModelMixin, mixins.UpdateModelMixin, @@ -79,7 +68,6 @@ class CommentDetail( return self.destroy(request, *args, **kwargs) -# List of likes class LikeList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView): serializer_class = LikeSerializer permission_classes = [permissions.IsAuthenticated] @@ -97,7 +85,6 @@ class LikeList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericA return Like.objects.filter(owner=self.request.user) -# Details of like class LikeDetail( mixins.RetrieveModelMixin, mixins.UpdateModelMixin,