From 0fec3d1e2c4ee1ffd36329a8ee032ab60b125f98 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Fri, 5 Apr 2019 16:35:48 +0100
Subject: [PATCH] WIP: add tests

---
 opentech/apply/api/v1/permissions.py          | 18 ------------
 opentech/apply/api/v1/serializers.py          |  5 ++--
 opentech/apply/api/v1/tests/__init__.py       |  0
 .../apply/api/v1/tests/test_serializers.py    | 28 +++++++++++++++++++
 .../v1/tests/test_views.py}                   |  4 +--
 opentech/apply/api/v1/urls.py                 |  2 ++
 opentech/apply/api/v1/views.py                |  4 +--
 opentech/apply/funds/permissions.py           | 16 +++++++++++
 opentech/apply/funds/urls.py                  | 27 ------------------
 9 files changed, 51 insertions(+), 53 deletions(-)
 create mode 100644 opentech/apply/api/v1/tests/__init__.py
 create mode 100644 opentech/apply/api/v1/tests/test_serializers.py
 rename opentech/apply/{funds/tests/test_api_views.py => api/v1/tests/test_views.py} (94%)
 create mode 100644 opentech/apply/funds/permissions.py

diff --git a/opentech/apply/api/v1/permissions.py b/opentech/apply/api/v1/permissions.py
index 3a623b13a..c87691666 100644
--- a/opentech/apply/api/v1/permissions.py
+++ b/opentech/apply/api/v1/permissions.py
@@ -16,21 +16,3 @@ class IsApplyStaffUser(permissions.BasePermission):
 
     def has_object_permission(self, request, view, obj):
         return request.user.is_apply_staff
-
-
-def is_user_has_access_to_view_submission(user, submission):
-    has_access = False
-
-    if not user.is_authenticated:
-        pass
-
-    elif user.is_apply_staff or submission.user == user or user.is_reviewer:
-        has_access = True
-
-    elif user.is_partner and submission.partners.filter(pk=user.pk).exists():
-        has_access = True
-
-    elif user.is_community_reviewer and submission.community_review:
-        has_access = True
-
-    return has_access
diff --git a/opentech/apply/api/v1/serializers.py b/opentech/apply/api/v1/serializers.py
index bc2f7e1e0..87886264c 100644
--- a/opentech/apply/api/v1/serializers.py
+++ b/opentech/apply/api/v1/serializers.py
@@ -11,7 +11,6 @@ from opentech.apply.funds.models import ApplicationSubmission, RoundsAndLabs
 from opentech.apply.review.models import Review, ReviewOpinion
 from opentech.apply.review.options import RECOMMENDATION_CHOICES
 from opentech.apply.users.groups import PARTNER_GROUP_NAME, STAFF_GROUP_NAME
-from .models import ApplicationSubmission, RoundsAndLabs
 
 User = get_user_model()
 
@@ -202,7 +201,7 @@ class RoundLabSerializer(serializers.ModelSerializer):
 class CommentSerializer(serializers.ModelSerializer):
     user = serializers.StringRelatedField()
     message = serializers.SerializerMethodField()
-    edit_url = serializers.HyperlinkedIdentityField(view_name='funds:api:comments:edit')
+    edit_url = serializers.HyperlinkedIdentityField(view_name='api:v1:comments:edit')
     editable = serializers.SerializerMethodField()
     timestamp = TimestampField(read_only=True)
     edited = TimestampField(read_only=True)
@@ -220,7 +219,7 @@ class CommentSerializer(serializers.ModelSerializer):
 
 class CommentCreateSerializer(serializers.ModelSerializer):
     user = serializers.StringRelatedField()
-    edit_url = serializers.HyperlinkedIdentityField(view_name='funds:api:comments:edit')
+    edit_url = serializers.HyperlinkedIdentityField(view_name='api:v1:comments:edit')
     editable = serializers.SerializerMethodField()
     timestamp = TimestampField(read_only=True)
     edited = TimestampField(read_only=True)
diff --git a/opentech/apply/api/v1/tests/__init__.py b/opentech/apply/api/v1/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/opentech/apply/api/v1/tests/test_serializers.py b/opentech/apply/api/v1/tests/test_serializers.py
new file mode 100644
index 000000000..0f5ea6c76
--- /dev/null
+++ b/opentech/apply/api/v1/tests/test_serializers.py
@@ -0,0 +1,28 @@
+from django.test import override_settings, TestCase
+
+from opentech.apply.funds.tests.factories import ApplicationSubmissionFactory
+from opentech.apply.review.tests.factories import ReviewFactory
+
+from ..serializers import ReviewSummarySerializer
+
+
+@override_settings(ROOT_URLCONF='opentech.apply.urls')
+class TestReviewSummarySerializer(TestCase):
+    def test_handles_no_reviews(self):
+        submission = ApplicationSubmissionFactory()
+        data = ReviewSummarySerializer(submission).data
+        self.assertEqual(data['count'], 0)
+        self.assertEqual(data['score'], None)
+        self.assertEqual(data['recommendation'], {'value': -1, 'display': None})
+        self.assertEqual(data['assigned'], [])
+        self.assertEqual(data['reviews'], [])
+
+    def test_handles_negative_reviews(self):
+        submission = ApplicationSubmissionFactory()
+        ReviewFactory(submission=submission)
+        data = ReviewSummarySerializer(submission).data
+        self.assertEqual(data['count'], 1)
+        self.assertEqual(data['score'], 0)
+        self.assertEqual(data['recommendation'], {'value': 0, 'display': 'No'})
+        self.assertEqual(len(data['assigned']), 1)
+        self.assertEqual(len(data['reviews']), 1)
diff --git a/opentech/apply/funds/tests/test_api_views.py b/opentech/apply/api/v1/tests/test_views.py
similarity index 94%
rename from opentech/apply/funds/tests/test_api_views.py
rename to opentech/apply/api/v1/tests/test_views.py
index 10083d15f..38858d56a 100644
--- a/opentech/apply/funds/tests/test_api_views.py
+++ b/opentech/apply/api/v1/tests/test_views.py
@@ -11,7 +11,7 @@ from opentech.apply.users.tests.factories import UserFactory
 class TestCommentEdit(TestCase):
     def post_to_edit(self, comment_pk, message='my message'):
         return self.client.post(
-            reverse_lazy('funds:api:comments:edit', kwargs={'pk': comment_pk}),
+            reverse_lazy('api:v1:comments:edit', kwargs={'pk': comment_pk}),
             secure=True,
             data={'message': message},
         )
@@ -59,7 +59,7 @@ class TestCommentEdit(TestCase):
         self.client.force_login(user)
 
         response = self.client.post(
-            reverse_lazy('funds:api:comments:edit', kwargs={'pk': comment.pk}),
+            reverse_lazy('api:v1:comments:edit', kwargs={'pk': comment.pk}),
             secure=True,
             data={
                 'message': 'the new message',
diff --git a/opentech/apply/api/v1/urls.py b/opentech/apply/api/v1/urls.py
index d1996f19c..df7f62c6d 100644
--- a/opentech/apply/api/v1/urls.py
+++ b/opentech/apply/api/v1/urls.py
@@ -1,6 +1,7 @@
 from django.urls import include, path
 
 from .views import (
+    CommentEdit,
     CommentList,
     CommentListCreate,
     RoundLabDetail,
@@ -25,5 +26,6 @@ urlpatterns = [
     ], 'rounds'))),
     path('comments/', include(([
         path('', CommentList.as_view(), name='list'),
+        path('<int:pk>/edit/', CommentEdit.as_view(), name='edit'),
     ], 'comments')))
 ]
diff --git a/opentech/apply/api/v1/views.py b/opentech/apply/api/v1/views.py
index 5bcf532da..1953515fd 100644
--- a/opentech/apply/api/v1/views.py
+++ b/opentech/apply/api/v1/views.py
@@ -22,7 +22,7 @@ from opentech.apply.review.models import Review
 from opentech.apply.funds.models import FundType, LabType
 
 from .pagination import StandardResultsSetPagination
-from .permissions import IsApplyStaffUser
+from .permissions import IsApplyStaffUser, IsAuthor
 from .serializers import (
     CommentSerializer,
     CommentCreateSerializer,
@@ -33,8 +33,6 @@ from .serializers import (
     SubmissionListSerializer,
     SubmissionDetailSerializer,
 )
-from .permissions import IsApplyStaffUser, IsAuthor
-from .workflow import PHASES
 
 
 class RoundLabFilter(filters.ModelChoiceFilter):
diff --git a/opentech/apply/funds/permissions.py b/opentech/apply/funds/permissions.py
new file mode 100644
index 000000000..816e1cf0b
--- /dev/null
+++ b/opentech/apply/funds/permissions.py
@@ -0,0 +1,16 @@
+def is_user_has_access_to_view_submission(user, submission):
+    has_access = False
+
+    if not user.is_authenticated:
+        pass
+
+    elif user.is_apply_staff or submission.user == user or user.is_reviewer:
+        has_access = True
+
+    elif user.is_partner and submission.partners.filter(pk=user.pk).exists():
+        has_access = True
+
+    elif user.is_community_reviewer and submission.community_review:
+        has_access = True
+
+    return has_access
diff --git a/opentech/apply/funds/urls.py b/opentech/apply/funds/urls.py
index d23951716..677f98cfd 100644
--- a/opentech/apply/funds/urls.py
+++ b/opentech/apply/funds/urls.py
@@ -20,16 +20,6 @@ from .views import (
     SubmissionUserFlaggedView,
     SubmissionStaffFlaggedView,
 )
-from .api_views import (
-    CommentEdit,
-    CommentList,
-    CommentListCreate,
-    RoundLabDetail,
-    RoundLabList,
-    SubmissionAction,
-    SubmissionList,
-    SubmissionDetail,
-)
 
 
 revision_urls = ([
@@ -68,23 +58,6 @@ submission_urls = ([
     path('<slug:status>/', SubmissionsByStatus.as_view(), name='status'),
 ], 'submissions')
 
-api_urls = ([
-    path('submissions/', include(([
-        path('', SubmissionList.as_view(), name='list'),
-        path('<int:pk>/', SubmissionDetail.as_view(), name='detail'),
-        path('<int:pk>/actions/', SubmissionAction.as_view(), name='actions'),
-        path('<int:pk>/comments/', CommentListCreate.as_view(), name='comments'),
-    ], 'submissions'))),
-    path('rounds/', include(([
-        path('', RoundLabList.as_view(), name='list'),
-        path('<int:pk>/', RoundLabDetail.as_view(), name='detail'),
-    ], 'rounds'))),
-    path('comments/', include(([
-        path('', CommentList.as_view(), name='list'),
-        path('<int:pk>/edit/', CommentEdit.as_view(), name='edit'),
-    ], 'comments')))
-], 'api')
-
 rounds_urls = ([
     path('', RoundListView.as_view(), name="list"),
     path('<int:pk>/', SubmissionsByRound.as_view(), name="detail"),
-- 
GitLab