Repository: Cloud-CV/EvalAI

Issue:
Travis Failing Tests Because of bug in GetRemainingSubmissionTest
The Test `test_get_remaining_submission_when_submission_made_three_days_back` is failing for expected `remaining_submissions_this_month_count`


Discussion:

Resolving patch:
diff --git a/apps/jobs/views.py b/apps/jobs/views.py
--- a/apps/jobs/views.py
+++ b/apps/jobs/views.py
@@ -342,6 +342,11 @@ def leaderboard(request, challenge_phase_split_id):
 
     challenge_host_user = is_user_a_host_of_challenge(request.user, challenge_obj.pk)
 
+    # Check if challenge phase leaderboard is public for participant user or not
+    if challenge_phase_split.visibility != ChallengePhaseSplit.PUBLIC and not challenge_host_user:
+        response_data = {'error': 'Sorry, the leaderboard is not public!'}
+        return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
+
     leaderboard_data = LeaderboardData.objects.exclude(
         submission__created_by__email__in=challenge_hosts_emails)
 
@@ -356,7 +361,7 @@ def leaderboard(request, challenge_phase_split_id):
             'id', 'submission__participant_team__team_name',
             'challenge_phase_split', 'result', 'filtering_score', 'leaderboard__schema', 'submission__submitted_at')
 
-    if not challenge_host_user:
+    if challenge_phase_split.visibility == ChallengePhaseSplit.PUBLIC:
         leaderboard_data = leaderboard_data.filter(submission__is_public=True)
 
     sorted_leaderboard_data = sorted(leaderboard_data, key=lambda k: float(k['filtering_score']), reverse=True)
@@ -379,14 +384,8 @@ def leaderboard(request, challenge_phase_split_id):
                                                 distinct_sorted_leaderboard_data,
                                                 request,
                                                 pagination_class=StandardResultSetPagination())
-
-    # Check if challenge phase leaderboard is public for participant user or not
-    if challenge_phase_split.visibility != ChallengePhaseSplit.PUBLIC and not challenge_host_user:
-        response_data = {'error': 'Sorry, the leaderboard is not public!'}
-        return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
-    else:
-        response_data = result_page
-        return paginator.get_paginated_response(response_data)
+    response_data = result_page
+    return paginator.get_paginated_response(response_data)
 
 
 @throttle_classes([UserRateThrottle])


Test patch:
diff --git a/tests/unit/jobs/test_views.py b/tests/unit/jobs/test_views.py
--- a/tests/unit/jobs/test_views.py
+++ b/tests/unit/jobs/test_views.py
@@ -581,20 +581,23 @@ def test_get_remaining_submission_when_submission_made_three_days_back(self):
                                     'challenge_pk': self.challenge.pk
                                 })
         self.submission3.status = 'cancelled'
-        self.submission2.status = 'failed'
         self.submission3.save()
+        self.submission2.status = 'failed'
         self.submission2.save()
+        self.submission1.submitted_at = timezone.now() - timedelta(days=3)
+        self.submission1.save()
+        self.challenge.participant_teams.add(self.participant_team)
+        self.challenge.save()
+
+        monthly_count = 0 if self.submission1.submitted_at.month != timezone.now().month else 1
+        remaining_monthly_submissions = self.challenge_phase.max_submissions_per_month - monthly_count
 
         expected = {
             'remaining_submissions_today_count': 10,
-            'remaining_submissions_this_month_count': 19,
+            'remaining_submissions_this_month_count': remaining_monthly_submissions,
             'remaining_submissions': 99
         }
 
-        self.challenge.participant_teams.add(self.participant_team)
-        self.challenge.save()
-        self.submission1.submitted_at = timezone.now() - timedelta(days=3)
-        self.submission1.save()
         response = self.client.get(self.url, {})
         self.assertEqual(response.data, expected)
         self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -1278,26 +1281,44 @@ def setUp(self):
             method_name="Test Method",
             method_description="Test Description",
             project_url="http://testserver/",
-            publication_url="http://testserver/"
+            publication_url="http://testserver/",
+            is_public=True
+        )
+
+        self.submission_2 = Submission.objects.create(
+            participant_team=self.participant_team,
+            challenge_phase=self.challenge_phase,
+            created_by=self.user1,
+            status="submitted",
+            input_file=self.challenge_phase.test_annotation,
+            method_name="Test Method",
+            method_description="Test Description",
+            project_url="http://testserver/",
+            publication_url="http://testserver/",
+            is_public=False
         )
 
         self.private_submission = Submission.objects.create(
             participant_team=self.host_participant_team,
             challenge_phase=self.private_challenge_phase,
-            created_by=self.user,
+            created_by=self.user1,
             status="submitted",
             input_file=self.private_challenge_phase.test_annotation,
             method_name="Test Method",
             method_description="Test Description",
             project_url="http://testserver/",
-            publication_url="http://testserver/"
+            publication_url="http://testserver/",
         )
 
         self.submission.is_public = True
         self.submission.status = Submission.FINISHED
         self.submission.save()
 
-        self.private_submission.is_public = True
+        self.submission_2.is_public = False
+        self.submission_2.status = Submission.FINISHED
+        self.submission_2.save()
+
+        self.private_submission.is_public = False
         self.private_submission.status = Submission.FINISHED
         self.private_submission.save()
 
@@ -1306,6 +1327,11 @@ def setUp(self):
             'test-score': 75.0
         }
 
+        self.result_json_2 = {
+            'score': 10.0,
+            'test-score': 20.0
+        }
+
         self.expected_results = [self.result_json['score'], self.result_json['test-score']]
         self.filtering_score = self.result_json[self.leaderboard.schema['default_order_by']]
 
@@ -1316,6 +1342,13 @@ def setUp(self):
             result=self.result_json
         )
 
+        self.leaderboard_data_2 = LeaderboardData.objects.create(
+            challenge_phase_split=self.challenge_phase_split,
+            submission=self.submission,
+            leaderboard=self.leaderboard,
+            result=self.result_json_2
+        )
+
         self.private_leaderboard_data = LeaderboardData.objects.create(
             challenge_phase_split=self.private_challenge_phase_split,
             submission=self.private_submission,
@@ -1414,3 +1447,17 @@ def test_get_leaderboard_for_host_submissions_on_private_challenge_phase(self):
         self.assertEqual(response.data['previous'], expected['previous'])
         self.assertEqual(response.data['results'], expected['results'])
         self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+    def test_get_private_leaderboard_when_user_is_participant(self):
+        self.url = reverse_lazy('jobs:leaderboard',
+                                kwargs={'challenge_phase_split_id': self.private_challenge_phase_split.id})
+
+        expected = {
+            'error': 'Sorry, the leaderboard is not public!'
+        }
+
+        self.client.force_authenticate(user=self.user1)
+
+        response = self.client.get(self.url, {})
+        self.assertEqual(response.data, expected)
+        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)