From 6eb77380ad7ae415dc68a15c3a71e2b70e9cb4c0 Mon Sep 17 00:00:00 2001 From: hj Date: Sat, 31 Aug 2024 22:36:08 +0530 Subject: [PATCH 01/36] Add feature to disable private submission --- ...13_challenge_disable_private_submission.py | 18 +++++++ apps/challenges/models.py | 1 + apps/challenges/serializers.py | 6 ++- apps/challenges/views.py | 1 + frontend/src/js/controllers/challengeCtrl.js | 51 ++++++++++++++++++- .../views/web/challenge/challenge-page.html | 12 +++++ .../src/views/web/challenge/submission.html | 2 +- 7 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 apps/challenges/migrations/0113_challenge_disable_private_submission.py diff --git a/apps/challenges/migrations/0113_challenge_disable_private_submission.py b/apps/challenges/migrations/0113_challenge_disable_private_submission.py new file mode 100644 index 0000000000..90d0c76807 --- /dev/null +++ b/apps/challenges/migrations/0113_challenge_disable_private_submission.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.20 on 2024-08-31 06:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('challenges', '0112_challenge_sqs_retention_period'), + ] + + operations = [ + migrations.AddField( + model_name='challenge', + name='disable_private_submission', + field=models.BooleanField(default=False), + ), + ] diff --git a/apps/challenges/models.py b/apps/challenges/models.py index 7539627878..ec660c04d4 100644 --- a/apps/challenges/models.py +++ b/apps/challenges/models.py @@ -222,6 +222,7 @@ def __init__(self, *args, **kwargs): ) worker_image_url = models.CharField(max_length=200, blank=True, null=True, default="") evaluation_module_error = models.TextField(null=True, blank=True) + disable_private_submission = models.BooleanField(default=False) class Meta: app_label = "challenges" diff --git a/apps/challenges/serializers.py b/apps/challenges/serializers.py index 6d81932c15..956c1c09a6 100644 --- a/apps/challenges/serializers.py +++ b/apps/challenges/serializers.py @@ -94,7 +94,8 @@ class Meta: "evaluation_module_error", "worker_image_url", "worker_instance_type", - "sqs_retention_period" + "sqs_retention_period", + "disable_private_submission" ) @@ -311,7 +312,8 @@ class Meta: "ephemeral_storage", "evaluation_module_error", "worker_image_url", - "sqs_retention_period" + "sqs_retention_period", + "disable_private_submission" ) diff --git a/apps/challenges/views.py b/apps/challenges/views.py index b79e2fae41..4ba18b03e0 100644 --- a/apps/challenges/views.py +++ b/apps/challenges/views.py @@ -4186,6 +4186,7 @@ def create_or_update_github_challenge(request, challenge_host_team_pk): "evaluation_script": files[ "challenge_evaluation_script_file" ], + "disable_private_submission": request.data.get('disable_private_submission') }, ) if serializer.is_valid(): diff --git a/frontend/src/js/controllers/challengeCtrl.js b/frontend/src/js/controllers/challengeCtrl.js index c8bf18a82b..28af4ac2ff 100644 --- a/frontend/src/js/controllers/challengeCtrl.js +++ b/frontend/src/js/controllers/challengeCtrl.js @@ -82,7 +82,7 @@ vm.currentPhaseLeaderboardPublic = false; vm.eligible_to_submit = false; vm.evaluation_module_error = null; - + vm.disable_private_submission = false; vm.filter_all_submission_by_team_name = ''; vm.filter_my_submission_by_team_name = ''; // show loader @@ -396,6 +396,7 @@ vm.has_sponsors = details.has_sponsors; vm.queueName = details.queue; vm.evaluation_module_error = details.evaluation_module_error; + vm.disable_private_submission = details.disable_private_submission; vm.getTeamName(vm.challengeId); if (vm.page.image === null) { vm.page.image = "dist/images/logo.png"; @@ -708,6 +709,9 @@ formData.append("project_url", vm.projectUrl); formData.append("publication_url", vm.publicationUrl); formData.append("submission_metadata", JSON.stringify(vm.metaAttributesforCurrentSubmission)); + if (vm.disable_private_submission) { + vm.isPublicSubmission = true; + } if (vm.isPublicSubmission !== null) { formData.append("is_public", vm.isPublicSubmission); } @@ -2836,7 +2840,50 @@ $mdDialog.hide(); } }; - + vm.toggleDisablePrivateSubmission = function(ev) { + ev.stopPropagation(); + vm.toggleSubmissionState = null; + vm.submissionDesc = null; + if (vm.disable_private_submission) + vm.toggleSubmissionState = "allowed"; + else + vm.toggleSubmissionState = "disabled"; + + var confirm = $mdDialog.confirm() + .title('Make private submissions ' + vm.toggleSubmissionState + '?') + .ariaLabel('') + .targetEvent(ev) + .ok('Yes') + .cancel('No'); + + $mdDialog.show(confirm).then(function() { + parameters.url = "challenges/challenge_host_team/" + vm.page.creator.id + "/challenge/" + vm.page.id; + parameters.method = 'PATCH'; + parameters.data = { + "disable_private_submission": !vm.disable_private_submission, + }; + vm.disable_private_submission = !vm.disable_private_submission; + parameters.callback = { + onSuccess: function(response) { + var status = response.status; + if (status === 200) { + $mdDialog.hide(); + $rootScope.notify("success", "Private submissions were successfully made " + vm.toggleSubmissionState); + } + }, + onError: function(response) { + $mdDialog.hide(); + var error = response.data; + $rootScope.notify("error", error); + } + }; + + console.log("Sending request with parameters:", parameters); + utilities.sendRequest(parameters); + }, function() { + // Nope + }); + }; vm.publishChallenge = function(ev) { ev.stopPropagation(); vm.toggleChallengeState = null; diff --git a/frontend/src/views/web/challenge/challenge-page.html b/frontend/src/views/web/challenge/challenge-page.html index dd6373771a..c9a5825eff 100644 --- a/frontend/src/views/web/challenge/challenge-page.html +++ b/frontend/src/views/web/challenge/challenge-page.html @@ -56,6 +56,18 @@

{{challenge.page.title}} Not Published +
+ + + Private Submissions Disabled + + + + + + Private Submissions Allowed + +
diff --git a/frontend/src/views/web/challenge/submission.html b/frontend/src/views/web/challenge/submission.html index 4011b7c65a..99b7fe0e9f 100644 --- a/frontend/src/views/web/challenge/submission.html +++ b/frontend/src/views/web/challenge/submission.html @@ -488,7 +488,7 @@

Make Submission
  • -
    Use   --private  or  --public   flag in the submission +
    Use   --private  or  --public   flag in the submission command to make the submission private or public respectively.
  • From 5e853183bdda179b485db381bec297c847a980a6 Mon Sep 17 00:00:00 2001 From: hj Date: Sat, 31 Aug 2024 23:37:09 +0530 Subject: [PATCH 02/36] add test case fix for private submission disable feature --- tests/unit/challenges/test_views.py | 19 +++++++++++++++++++ tests/unit/participants/test_views.py | 2 ++ 2 files changed, 21 insertions(+) diff --git a/tests/unit/challenges/test_views.py b/tests/unit/challenges/test_views.py index 5ea9939341..f452b1d72e 100644 --- a/tests/unit/challenges/test_views.py +++ b/tests/unit/challenges/test_views.py @@ -195,6 +195,7 @@ def test_get_challenge(self): "worker_image_url": self.challenge.worker_image_url, "worker_instance_type": self.challenge.worker_instance_type, "sqs_retention_period": self.challenge.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } ] @@ -550,6 +551,7 @@ def test_get_particular_challenge(self): "worker_image_url": self.challenge.worker_image_url, "worker_instance_type": self.challenge.worker_instance_type, "sqs_retention_period": self.challenge.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } response = self.client.get(self.url, {}) self.assertEqual(response.data, expected) @@ -652,6 +654,7 @@ def test_update_challenge_when_user_is_its_creator(self): "worker_image_url": self.challenge.worker_image_url, "worker_instance_type": self.challenge.worker_instance_type, "sqs_retention_period": self.challenge.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } response = self.client.put( self.url, {"title": new_title, "description": new_description} @@ -780,6 +783,7 @@ def test_particular_challenge_partial_update(self): "worker_image_url": self.challenge.worker_image_url, "worker_instance_type": self.challenge.worker_instance_type, "sqs_retention_period": self.challenge.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } response = self.client.patch(self.url, self.partial_update_data) self.assertEqual(response.data, expected) @@ -857,6 +861,7 @@ def test_particular_challenge_update(self): "worker_image_url": self.challenge.worker_image_url, "worker_instance_type": self.challenge.worker_instance_type, "sqs_retention_period": self.challenge.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } response = self.client.put(self.url, self.data) self.assertEqual(response.data, expected) @@ -1450,6 +1455,7 @@ def test_get_past_challenges(self): "worker_image_url": self.challenge3.worker_image_url, "worker_instance_type": self.challenge3.worker_instance_type, "sqs_retention_period": self.challenge3.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } ] response = self.client.get(self.url, {}, format="json") @@ -1533,6 +1539,7 @@ def test_get_present_challenges(self): "worker_image_url": self.challenge.worker_image_url, "worker_instance_type": self.challenge.worker_instance_type, "sqs_retention_period": self.challenge.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } ] response = self.client.get(self.url, {}, format="json") @@ -1616,6 +1623,7 @@ def test_get_future_challenges(self): "worker_image_url": self.challenge4.worker_image_url, "worker_instance_type": self.challenge4.worker_instance_type, "sqs_retention_period": self.challenge4.sqs_retention_period, + "disable_private_submission": self.challenge4.disable_private_submission, } ] response = self.client.get(self.url, {}, format="json") @@ -1699,6 +1707,7 @@ def test_get_all_challenges(self): "worker_image_url": self.challenge3.worker_image_url, "worker_instance_type": self.challenge3.worker_instance_type, "sqs_retention_period": self.challenge3.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, }, { "id": self.challenge3.pk, @@ -1766,6 +1775,7 @@ def test_get_all_challenges(self): "worker_image_url": self.challenge3.worker_image_url, "worker_instance_type": self.challenge3.worker_instance_type, "sqs_retention_period": self.challenge3.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, }, { "id": self.challenge2.pk, @@ -1833,6 +1843,7 @@ def test_get_all_challenges(self): "worker_image_url": self.challenge2.worker_image_url, "worker_instance_type": self.challenge2.worker_instance_type, "sqs_retention_period": self.challenge2.sqs_retention_period, + "disable_private_submission": self.challenge2.disable_private_submission, }, ] response = self.client.get(self.url, {}, format="json") @@ -1971,6 +1982,7 @@ def test_get_featured_challenges(self): "worker_image_url": self.challenge3.worker_image_url, "worker_instance_type": self.challenge3.worker_instance_type, "sqs_retention_period": self.challenge3.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } ] response = self.client.get(self.url, {}, format="json") @@ -2133,6 +2145,7 @@ def test_get_challenge_by_pk_when_user_is_challenge_host(self): "worker_image_url": self.challenge3.worker_image_url, "worker_instance_type": self.challenge3.worker_instance_type, "sqs_retention_period": self.challenge3.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } response = self.client.get(self.url, {}) @@ -2224,6 +2237,7 @@ def test_get_challenge_by_pk_when_user_is_participant(self): "worker_image_url": self.challenge4.worker_image_url, "worker_instance_type": self.challenge4.worker_instance_type, "sqs_retention_period": self.challenge4.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } self.client.force_authenticate(user=self.user1) @@ -2375,6 +2389,7 @@ def test_get_challenge_when_host_team_is_given(self): "worker_image_url": self.challenge2.worker_image_url, "worker_instance_type": self.challenge2.worker_instance_type, "sqs_retention_period": self.challenge2.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } ] @@ -2454,6 +2469,7 @@ def test_get_challenge_when_participant_team_is_given(self): "worker_image_url": self.challenge2.worker_image_url, "worker_instance_type": self.challenge2.worker_instance_type, "sqs_retention_period": self.challenge2.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } ] @@ -2533,6 +2549,7 @@ def test_get_challenge_when_mode_is_participant(self): "worker_image_url": self.challenge2.worker_image_url, "worker_instance_type": self.challenge2.worker_instance_type, "sqs_retention_period": self.challenge2.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, } ] @@ -2610,6 +2627,7 @@ def test_get_challenge_when_mode_is_host(self): "worker_image_url": self.challenge.worker_image_url, "worker_instance_type": self.challenge.worker_instance_type, "sqs_retention_period": self.challenge.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, }, { "id": self.challenge2.pk, @@ -2677,6 +2695,7 @@ def test_get_challenge_when_mode_is_host(self): "worker_image_url": self.challenge2.worker_image_url, "worker_instance_type": self.challenge2.worker_instance_type, "sqs_retention_period": self.challenge2.sqs_retention_period, + "disable_private_submission": self.challenge.disable_private_submission, }, ] diff --git a/tests/unit/participants/test_views.py b/tests/unit/participants/test_views.py index 6004451dd2..2324eb6d69 100644 --- a/tests/unit/participants/test_views.py +++ b/tests/unit/participants/test_views.py @@ -884,6 +884,7 @@ def test_get_teams_and_corresponding_challenges_for_a_participant(self): "worker_image_url": self.challenge1.worker_image_url, "worker_instance_type": self.challenge1.worker_instance_type, "sqs_retention_period": self.challenge1.sqs_retention_period, + "disable_private_submission": self.challenge1.disable_private_submission, }, "participant_team": { "id": self.participant_team.id, @@ -978,6 +979,7 @@ def test_get_participant_team_challenge_list(self): "worker_image_url": self.challenge1.worker_image_url, "worker_instance_type": self.challenge1.worker_instance_type, "sqs_retention_period": self.challenge1.sqs_retention_period, + "disable_private_submission": self.challenge1.disable_private_submission, } ] From 288f96d388b42824e95c5cfbf6bbe06d3ec9dbec Mon Sep 17 00:00:00 2001 From: hj Date: Sun, 1 Sep 2024 00:06:54 +0530 Subject: [PATCH 03/36] Removed console.log --- frontend/src/js/controllers/challengeCtrl.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/frontend/src/js/controllers/challengeCtrl.js b/frontend/src/js/controllers/challengeCtrl.js index 28af4ac2ff..1b156abf2e 100644 --- a/frontend/src/js/controllers/challengeCtrl.js +++ b/frontend/src/js/controllers/challengeCtrl.js @@ -2877,8 +2877,6 @@ $rootScope.notify("error", error); } }; - - console.log("Sending request with parameters:", parameters); utilities.sendRequest(parameters); }, function() { // Nope From 6647892ac900e0b62c8dcaa38874e9ecd0ff1e3e Mon Sep 17 00:00:00 2001 From: hj Date: Sun, 1 Sep 2024 11:22:18 +0530 Subject: [PATCH 04/36] add option disable changes & docs --- apps/challenges/views.py | 1 - docs/source/configuration.md | 2 ++ frontend/src/views/web/challenge/my-submission.html | 4 ++-- frontend/src/views/web/challenge/submission.html | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/apps/challenges/views.py b/apps/challenges/views.py index 4ba18b03e0..b79e2fae41 100644 --- a/apps/challenges/views.py +++ b/apps/challenges/views.py @@ -4186,7 +4186,6 @@ def create_or_update_github_challenge(request, challenge_host_team_pk): "evaluation_script": files[ "challenge_evaluation_script_file" ], - "disable_private_submission": request.data.get('disable_private_submission') }, ) if serializer.is_valid(): diff --git a/docs/source/configuration.md b/docs/source/configuration.md index 7ab0d0860d..f72e8645bf 100644 --- a/docs/source/configuration.md +++ b/docs/source/configuration.md @@ -51,6 +51,8 @@ Following fields are required (and can be customized) in the [`challenge_config. 3. `metadata`: This field defines additional information about the metrics that are used to evaluate the challenge submissions. +- **disable_private_submission**: True/False (a Boolean field that gives the flexibility to Challenge Hosts to either disable or enable private submissions. Default is `False`) + The leaderboard schema for the [sample challenge configuration](https://github.com/Cloud-CV/EvalAI-Starters/blob/master/challenge_config.yaml) looks like this: ```yaml diff --git a/frontend/src/views/web/challenge/my-submission.html b/frontend/src/views/web/challenge/my-submission.html index f7acdb1d7e..41d0f061c5 100644 --- a/frontend/src/views/web/challenge/my-submission.html +++ b/frontend/src/views/web/challenge/my-submission.html @@ -66,7 +66,7 @@
    My Participated Team: {{challenge.participated_team_name}}
    Environment Logs Submitted at - Show on leaderboard Baseline @@ -121,7 +121,7 @@
    My Participated Team: {{challenge.participated_team_name}}
    {{key.submitted_at | date:'medium'}} - + Make Submission
    -
    +

    Select submission visibility:

  • Date: Wed, 30 Apr 2025 03:21:03 +0530 Subject: [PATCH 05/36] feat: Add feature to disable private submission --- .../migrations/0113_challenge_disable_private_submission.py | 6 +++--- apps/challenges/serializers.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/challenges/migrations/0113_challenge_disable_private_submission.py b/apps/challenges/migrations/0113_challenge_disable_private_submission.py index 90d0c76807..0ddb7d8494 100644 --- a/apps/challenges/migrations/0113_challenge_disable_private_submission.py +++ b/apps/challenges/migrations/0113_challenge_disable_private_submission.py @@ -6,13 +6,13 @@ class Migration(migrations.Migration): dependencies = [ - ('challenges', '0112_challenge_sqs_retention_period'), + ("challenges", "0112_challenge_sqs_retention_period"), ] operations = [ migrations.AddField( - model_name='challenge', - name='disable_private_submission', + model_name="challenge", + name="disable_private_submission", field=models.BooleanField(default=False), ), ] diff --git a/apps/challenges/serializers.py b/apps/challenges/serializers.py index 98cbd3c966..f42568f0e2 100644 --- a/apps/challenges/serializers.py +++ b/apps/challenges/serializers.py @@ -94,7 +94,7 @@ class Meta: "worker_image_url", "worker_instance_type", "sqs_retention_period", - "disable_private_submission" + "disable_private_submission", ) @@ -312,7 +312,7 @@ class Meta: "evaluation_module_error", "worker_image_url", "sqs_retention_period", - "disable_private_submission" + "disable_private_submission", ) From b1f46ef171c4a942a7344f2ffd50d404226b79e4 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Fri, 2 May 2025 00:55:12 +0530 Subject: [PATCH 06/36] feat: added unit test for makeSubmission --- .../controllers-test/challengeCtrl.test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 1ebfa38326..75a8eb335c 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -752,6 +752,24 @@ describe('Unit tests for challenge controller', function () { expect(vm.stopLoader).toHaveBeenCalled(); }); + it('should set isPublicSubmission to true when disable_private_submission is true', function () { + success = true; + vm.isParticipated = true; + vm.eligible_to_submit = true; + vm.disable_private_submission = true; + + vm.isSubmissionUsingUrl = false; + vm.input_file = new Blob(['dummy'], { type: 'text/plain' }); + + // Stub validation to pass + vm.metaAttributesforCurrentSubmission = {}; + spyOn(vm, 'isCurrentSubmissionMetaAttributeValid').and.returnValue(true); + + vm.makeSubmission(); + + expect(vm.isPublicSubmission).toBe(true); + }); + it('404 backend error', function () { success = false; status = 404; From 8b97728a1adf72f2e0beb7d6bb2e7d248f234739 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Fri, 2 May 2025 01:13:29 +0530 Subject: [PATCH 07/36] feat: added unit test for makeSubmission --- .../controllers-test/challengeCtrl.test.js | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 75a8eb335c..ef6796553c 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -757,18 +757,25 @@ describe('Unit tests for challenge controller', function () { vm.isParticipated = true; vm.eligible_to_submit = true; vm.disable_private_submission = true; - + vm.phaseId = 1; + vm.input_file = new Blob(['dummy content'], { type: 'application/zip' }); vm.isSubmissionUsingUrl = false; - vm.input_file = new Blob(['dummy'], { type: 'text/plain' }); - - // Stub validation to pass - vm.metaAttributesforCurrentSubmission = {}; + + + vm.fileVal = 'submission.zip'; + + vm.methodName = 'My Method'; + vm.methodDesc = 'Description'; + vm.projectUrl = 'http://example.com/project'; + vm.publicationUrl = 'http://example.com/publication'; + spyOn(vm, 'isCurrentSubmissionMetaAttributeValid').and.returnValue(true); - + vm.makeSubmission(); - + expect(vm.isPublicSubmission).toBe(true); }); + it('404 backend error', function () { success = false; From 827be7446455a73e9c78446d64e7cbf4aac3c55e Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Fri, 2 May 2025 02:56:26 +0530 Subject: [PATCH 08/36] feat: added unit test for makeSubmission --- .../controllers-test/challengeCtrl.test.js | 210 +++++++++++------- 1 file changed, 124 insertions(+), 86 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index ef6796553c..b83d0102ef 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -5,7 +5,7 @@ describe('Unit tests for challenge controller', function () { var $controller, createController, $injector, $rootScope, $state, $scope, utilities, $http, $interval, $mdDialog, moment, vm; - beforeEach(inject(function (_$controller_, _$injector_, _$rootScope_, _$state_, _utilities_, _$http_, _$interval_, _$mdDialog_, _moment_) { + beforeEach(inject(function (_$controller_, _$injector_, _$rootScope_, _$state_, _utilities_, _$http_, _$interval_, _$mdDialog_, _moment_) { $controller = _$controller_; $injector = _$injector_; $rootScope = _$rootScope_; @@ -15,10 +15,10 @@ describe('Unit tests for challenge controller', function () { $interval = _$interval_; $mdDialog = _$mdDialog_; moment = _moment_; - + $scope = $rootScope.$new(); createController = function () { - return $controller('ChallengeCtrl', {$scope: $scope}); + return $controller('ChallengeCtrl', { $scope: $scope }); }; vm = $controller('ChallengeCtrl', { $scope: $scope }); })); @@ -99,22 +99,22 @@ describe('Unit tests for challenge controller', function () { } if ((challengePhaseSuccess == true && parameters.url == 'challenges/challenge/undefined/challenge_phase') || - (challengeSuccess == true && parameters.url == 'challenges/challenge/undefined/') || - (challengePhaseSplitSuccess == true && parameters.url == 'challenges/undefined/challenge_phase_split') || - (participantTeamChallengeSuccess == true && parameters.url == 'participants/participant_teams/challenges/undefined/user') || - (participantTeamSuccess == true && parameters.url == 'participants/participant_team') || - (selectExistTeamSuccess == true && parameters.url == 'challenges/challenge/undefined/participant_team/null')) { + (challengeSuccess == true && parameters.url == 'challenges/challenge/undefined/') || + (challengePhaseSplitSuccess == true && parameters.url == 'challenges/undefined/challenge_phase_split') || + (participantTeamChallengeSuccess == true && parameters.url == 'participants/participant_teams/challenges/undefined/user') || + (participantTeamSuccess == true && parameters.url == 'participants/participant_team') || + (selectExistTeamSuccess == true && parameters.url == 'challenges/challenge/undefined/participant_team/null')) { parameters.callback.onSuccess({ status: 200, data: successResponse }); } else if ((challengePhaseSuccess == false && parameters.url == 'challenges/challenge/undefined/challenge_phase') || - (challengeSuccess == false && parameters.url == 'challenges/challenge/undefined/') || - (challengePhaseSplitSuccess == false && parameters.url == 'challenges/undefined/challenge_phase_split') || - (participantTeamChallengeSuccess == false && parameters.url == 'participants/participant_teams/challenges/undefined/user') || - (participantTeamSuccess == false && parameters.url == 'participants/participant_team') || - (selectExistTeamSuccess == false && parameters.url == 'challenges/challenge/undefined/participant_team/null')){ + (challengeSuccess == false && parameters.url == 'challenges/challenge/undefined/') || + (challengePhaseSplitSuccess == false && parameters.url == 'challenges/undefined/challenge_phase_split') || + (participantTeamChallengeSuccess == false && parameters.url == 'participants/participant_teams/challenges/undefined/user') || + (participantTeamSuccess == false && parameters.url == 'participants/participant_team') || + (selectExistTeamSuccess == false && parameters.url == 'challenges/challenge/undefined/participant_team/null')) { parameters.callback.onError({ data: errorResponse, @@ -213,8 +213,9 @@ describe('Unit tests for challenge controller', function () { team_list.forEach(response => { it('pagination next is ' + response.next + ' and previous is ' + response.previous + '\ - `participants/participant_team`', function () {; - challengeSuccess = true; + `participants/participant_team`', function () { + ; + challengeSuccess = true; participantTeamChallengeSuccess = true; participantTeamSuccess = true; selectExistTeamSuccess = null; @@ -446,7 +447,7 @@ describe('Unit tests for challenge controller', function () { } }; utilities.storeData('userKey', 'encrypted key'); - + vm = createController(); spyOn(vm, 'startLoader'); spyOn($http, 'get').and.callFake(function () { @@ -460,7 +461,7 @@ describe('Unit tests for challenge controller', function () { var headers = { 'Authorization': "Token " + utilities.getData('userKey') }; - expect($http.get).toHaveBeenCalledWith(url, {headers: headers}); + expect($http.get).toHaveBeenCalledWith(url, { headers: headers }); }); it('backend error of the particular challenge `challenges/challenge//', function () { @@ -511,7 +512,7 @@ describe('Unit tests for challenge controller', function () { expect(vm.phases.results[i].showPrivate).toBeTruthy(); } - for(var i = 0; i < successResponse.results.length; i++){ + for (var i = 0; i < successResponse.results.length; i++) { var offset = new Date(successResponse.results[i].start_date).getTimezoneOffset(); expect(vm.phases.results[i].time_zone).toEqual(moment.tz.zone(timezone).abbr(offset)); } @@ -564,7 +565,7 @@ describe('Unit tests for challenge controller', function () { vm.isParticipated = true; vm = createController(); expect(vm.phaseSplits).toEqual(successResponse); - for(var i = 0; i < successResponse.length; i++) { + for (var i = 0; i < successResponse.length; i++) { if (successResponse[i].visibility != challengePhaseVisibility.public) { expect(vm.phaseSplits[i].showPrivate).toBeTruthy(); } @@ -627,7 +628,7 @@ describe('Unit tests for challenge controller', function () { limits: { submission_limit_exceeded: true, remaining_submissions_today_count: 12, - remaining_time: 12/12/12, + remaining_time: 12 / 12 / 12, } }, ] @@ -637,7 +638,7 @@ describe('Unit tests for challenge controller', function () { vm.displayDockerSubmissionInstructions(true, true); expect(vm.phaseRemainingSubmissions).toEqual(successResponse); var details = vm.phaseRemainingSubmissions.phases; - for (var i=0; i < details.length; i++) { + for (var i = 0; i < details.length; i++) { expect(vm.phaseRemainingSubmissionsFlags[details[i].id]).toEqual('maxExceeded'); } expect(utilities.hideLoader).toHaveBeenCalled(); @@ -651,7 +652,7 @@ describe('Unit tests for challenge controller', function () { limits: { submission_limit_exceeded: false, remaining_submissions_today_count: 12, - remaining_time: 12/12/12, + remaining_time: 12 / 12 / 12, } }, ] @@ -661,7 +662,7 @@ describe('Unit tests for challenge controller', function () { vm.displayDockerSubmissionInstructions(true, true); expect(vm.phaseRemainingSubmissions).toEqual(successResponse); var details = vm.phaseRemainingSubmissions.phases; - for (var i=0; i < details.length; i++) { + for (var i = 0; i < details.length; i++) { expect(vm.phaseRemainingSubmissionsFlags[details[i].id]).toEqual('showSubmissionNumbers'); } expect(utilities.hideLoader).toHaveBeenCalled(); @@ -675,7 +676,7 @@ describe('Unit tests for challenge controller', function () { limits: { submission_limit_exceeded: false, remaining_submissions_today_count: 0, - remaining_time: 12/12/12, + remaining_time: 12 / 12 / 12, } }, ] @@ -685,7 +686,7 @@ describe('Unit tests for challenge controller', function () { vm.displayDockerSubmissionInstructions(true, true); expect(vm.phaseRemainingSubmissions).toEqual(successResponse); var details = vm.phaseRemainingSubmissions.phases; - for (var i=0; i < details.length; i++) { + for (var i = 0; i < details.length; i++) { expect(vm.eachPhase).toEqual(details[i]); expect(vm.phaseRemainingSubmissionsFlags[details[i].id]).toEqual('showClock'); @@ -722,7 +723,7 @@ describe('Unit tests for challenge controller', function () { success: 'success', }; - beforeEach(function() { + beforeEach(function () { spyOn(vm, 'startLoader'); spyOn(vm, 'stopLoader'); spyOn($rootScope, 'notify'); @@ -752,30 +753,67 @@ describe('Unit tests for challenge controller', function () { expect(vm.stopLoader).toHaveBeenCalled(); }); - it('should set isPublicSubmission to true when disable_private_submission is true', function () { + it('forces isPublicSubmission=true when disable_private_submission is true', function () { success = true; vm.isParticipated = true; vm.eligible_to_submit = true; vm.disable_private_submission = true; - vm.phaseId = 1; - vm.input_file = new Blob(['dummy content'], { type: 'application/zip' }); + // simulate file‐upload path so it doesn’t short‐circuit on URL logic vm.isSubmissionUsingUrl = false; - - - vm.fileVal = 'submission.zip'; - - vm.methodName = 'My Method'; - vm.methodDesc = 'Description'; - vm.projectUrl = 'http://example.com/project'; - vm.publicationUrl = 'http://example.com/publication'; - - spyOn(vm, 'isCurrentSubmissionMetaAttributeValid').and.returnValue(true); - + vm.input_file = new Blob(['x'], { type: 'text/plain' }); + vm.currentPhaseAllowedSubmissionFileTypes = ['txt']; + + // capture the outgoing FormData + var originalSend = utilities.sendRequest; + var captured; + spyOn(window, 'FormData').and.callFake(function () { + var f = { store: {} }; + f.append = function (k, v) { f.store[k] = v; }; + return f; + }); + utilities.sendRequest = function (params) { + captured = params.data.store; + // call success path so post‐submission cleanup happens + params.callback.onSuccess({ status: 200, data: successResponse }); + }; + vm.makeSubmission(); - - expect(vm.isPublicSubmission).toBe(true); + + expect(captured.is_public).toBe(true, 'should set is_public to true'); + + // restore + utilities.sendRequest = originalSend; }); - + + it('respects vm.isPublicSubmission when disable_private_submission is false', function () { + success = true; + vm.isParticipated = true; + vm.eligible_to_submit = true; + vm.disable_private_submission = false; + vm.isPublicSubmission = false; // developer explicitly said “private” + vm.isSubmissionUsingUrl = false; + vm.input_file = new Blob(['x'], { type: 'text/plain' }); + vm.currentPhaseAllowedSubmissionFileTypes = ['txt']; + + var originalSend = utilities.sendRequest; + var captured; + spyOn(window, 'FormData').and.callFake(function () { + var f = { store: {} }; + f.append = function (k, v) { f.store[k] = v; }; + return f; + }); + utilities.sendRequest = function (params) { + captured = params.data.store; + params.callback.onSuccess({ status: 200, data: successResponse }); + }; + + vm.makeSubmission(); + + expect(captured.is_public).toBe(false, 'should use the vm.isPublicSubmission value'); + + utilities.sendRequest = originalSend; + }); + it('404 backend error', function () { success = false; @@ -880,10 +918,10 @@ describe('Unit tests for challenge controller', function () { { id: 1, leaderboard__schema: - { - labels: ['label1', 'label2'], - default_order_by: 'default_order_by', - }, + { + labels: ['label1', 'label2'], + default_order_by: 'default_order_by', + }, submission__submitted_at: (new Date() - new Date().setFullYear(new Date().getFullYear() - 1)), }, ] @@ -963,12 +1001,12 @@ describe('Unit tests for challenge controller', function () { utilities.sendRequest = function (parameters) { if ((submissionCountSuccess == true && parameters.url == "analytics/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/count") || - (submissionListSuccess == true && parameters.url == "jobs/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/submission/")) { + (submissionListSuccess == true && parameters.url == "jobs/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/submission/")) { parameters.callback.onSuccess({ data: successResponse }); } else if ((submissionCountSuccess == false && parameters.url == "analytics/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/count") || - (submissionListSuccess == false && parameters.url == "jobs/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/submission/")){ + (submissionListSuccess == false && parameters.url == "jobs/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/submission/")) { parameters.callback.onError({ data: errorResponse }); @@ -1031,7 +1069,7 @@ describe('Unit tests for challenge controller', function () { vm.getResults(phaseId); expect(vm.isExistLoader).toBeTruthy(); expect(vm.startLoader).toHaveBeenCalledWith("Loading Submissions"); - for (var i = 0; i < successResponse.results.length; i++){ + for (var i = 0; i < successResponse.results.length; i++) { expect(vm.submissionVisibility[successResponse.results[i].id]).toEqual(successResponse.results[i].is_public); expect(vm.baselineStatus[successResponse.results[i].id] = successResponse.results[i].is_baseline); } @@ -1090,7 +1128,7 @@ describe('Unit tests for challenge controller', function () { // get submissions response next: "page=4", previous: "page=2", - }; + }; vm.getResults(phaseId); spyOn($http, 'get').and.callFake(function () { var deferred = $injector.get('$q').defer(); @@ -1103,7 +1141,7 @@ describe('Unit tests for challenge controller', function () { var headers = { 'Authorization': "Token " + utilities.getData('userKey') }; - expect($http.get).toHaveBeenCalledWith(url, {headers: headers}); + expect($http.get).toHaveBeenCalledWith(url, { headers: headers }); }); }); @@ -1368,7 +1406,7 @@ describe('Unit tests for challenge controller', function () { }); describe('Unit tests for getAllSubmissionResults function \ - `challenges//challenge_phase//submissions`', function() { + `challenges//challenge_phase//submissions`', function () { var success, successResponse; var errorResponse = { detail: 'error' @@ -1423,7 +1461,7 @@ describe('Unit tests for challenge controller', function () { }); submission_list.forEach(response => { - it('submission list have count' + response.count + ', next ' + response.next + 'and previous ' + response.previous, function() { + it('submission list have count' + response.count + ', next ' + response.next + 'and previous ' + response.previous, function () { success = true; successResponse = response; var phaseId = 1 @@ -1507,43 +1545,43 @@ describe('Unit tests for challenge controller', function () { describe('Unit tests for showapprovalparticipantteamDialog function', function () { var $mdDialog; - + beforeEach(function () { $mdDialog = $injector.get('$mdDialog'); }); - + it('should open dialog when approved_status is true', function () { var $mdDialogOpened = false; $mdDialog.show = jasmine.createSpy().and.callFake(function () { $mdDialogOpened = true; }); - + var challengeId = '123'; var participant_team_id = '456'; var approved_status = true; - + vm.showapprovalparticipantteamDialog(challengeId, participant_team_id, approved_status); - + expect($mdDialog.show).toHaveBeenCalled(); expect($mdDialogOpened).toEqual(true); }); - + it('should call check_approval_status when approved_status is false', function () { vm.check_approval_status = jasmine.createSpy(); - + var challengeId = '123'; var participant_team_id = '456'; var approved_status = false; - + vm.showapprovalparticipantteamDialog(challengeId, participant_team_id, approved_status); - + expect(vm.check_approval_status).toHaveBeenCalledWith(challengeId, participant_team_id, approved_status, false); }); }); describe('Unit tests for check_approval_status function', function () { var success, errorResponse, secondfunction; - + beforeEach(function () { spyOn($rootScope, 'notify'); spyOn($state, 'reload'); @@ -1564,38 +1602,38 @@ describe('Unit tests for challenge controller', function () { } }; }); - + it('should handle successful approval of participant team', function () { success = true; - + var challengeId = '123'; var participant_team_id = '456'; var approved_status = true; var formvalid = true; - + vm.check_approval_status(challengeId, participant_team_id, approved_status, formvalid); - + expect($rootScope.notify).toHaveBeenCalledWith('success', 'Participant Team Approved successfully.'); expect($mdDialog.hide).toHaveBeenCalled(); }); - + it('should handle error during approval of participant team', function () { success = false; secondfunction = false; errorResponse = { - error: 'Approval failed' + error: 'Approval failed' }; - + var challengeId = '123'; var participant_team_id = '456'; var approved_status = true; var formvalid = true; - + vm.check_approval_status(challengeId, participant_team_id, approved_status, formvalid); - + expect($rootScope.notify).toHaveBeenCalledWith('error', 'Approval failed'); }); - + it('should handle disapproval of participant team', function () { success = false; secondfunction = true; @@ -1603,9 +1641,9 @@ describe('Unit tests for challenge controller', function () { var participant_team_id = '456'; var approved_status = false; var formvalid = false; - + vm.check_approval_status(challengeId, participant_team_id, approved_status, formvalid); - + expect($rootScope.notify).toHaveBeenCalledWith('success', 'Participant Team Disapproved successfully.'); expect($state.reload).not.toHaveBeenCalled(); }); @@ -1801,7 +1839,7 @@ describe('Unit tests for challenge controller', function () { vm.project_url = 'project url'; vm.publication_url = 'publication url'; vm.submissionMetaData = { - submission_metadata : null + submission_metadata: null }; vm.currentSubmissionMetaData = [ { @@ -1815,7 +1853,7 @@ describe('Unit tests for challenge controller', function () { name: 'SingleOptionAttribute', type: 'radio', value: null, - options: ['A','B','C'], + options: ['A', 'B', 'C'], $$hashKey: 'object:43', description: 'Sample', }, @@ -1823,7 +1861,7 @@ describe('Unit tests for challenge controller', function () { name: 'MultipleChoiceAttribute', type: 'checkbox', values: [], - options: ['alpha','beta','gamma'], + options: ['alpha', 'beta', 'gamma'], $$hashKey: 'object:44', description: 'Sample', }, @@ -1849,7 +1887,7 @@ describe('Unit tests for challenge controller', function () { vm.project_url = 'project url'; vm.publication_url = 'publication url'; vm.submissionMetaData = { - submission_metadata : null + submission_metadata: null }; vm.currentSubmissionMetaData = [ { @@ -1863,7 +1901,7 @@ describe('Unit tests for challenge controller', function () { name: 'SingleOptionAttribute', type: 'radio', value: null, - options: ['A','B','C'], + options: ['A', 'B', 'C'], $$hashKey: 'object:43', description: 'Sample', }, @@ -1871,7 +1909,7 @@ describe('Unit tests for challenge controller', function () { name: 'MultipleChoiceAttribute', type: 'checkbox', values: [], - options: ['alpha','beta','gamma'], + options: ['alpha', 'beta', 'gamma'], $$hashKey: 'object:44', description: 'Sample', }, @@ -2620,7 +2658,7 @@ describe('Unit tests for challenge controller', function () { it('valid `edit challenge phase` form & successfull edit', function () { var editChallengePhaseForm = true; success = true; - vm.page.challenge_phase ={ + vm.page.challenge_phase = { id: 1, name: "challenge phase name", description: "challenge phase description", @@ -2641,7 +2679,7 @@ describe('Unit tests for challenge controller', function () { it('valid `edit challenge phase` form & backend error', function () { var editChallengePhaseForm = true; success = false; - vm.page.challenge_phase ={ + vm.page.challenge_phase = { id: 1, name: "challenge phase name", description: "challenge phase description", @@ -2691,7 +2729,7 @@ describe('Unit tests for challenge controller', function () { }); it('change challenge state from `public` to `private`', function () { - vm.isPublished = true; + vm.isPublished = true; vm.publishChallenge(ev); expect(vm.publishDesc).toEqual(null); expect(ev.stopPropagation).toHaveBeenCalled(); @@ -2699,7 +2737,7 @@ describe('Unit tests for challenge controller', function () { }); it('change challenge state from `private` to `public`', function () { - vm.isPublished = false; + vm.isPublished = false; vm.publishChallenge(ev); expect(vm.publishDesc).toEqual(null); expect(ev.stopPropagation).toHaveBeenCalled(); From f296f18f02b0cde1cb358cce2992b27cf66e1461 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Fri, 2 May 2025 03:19:31 +0530 Subject: [PATCH 09/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index b83d0102ef..68a3fb244c 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1145,6 +1145,101 @@ describe('Unit tests for challenge controller', function () { }); }); + describe('vm.toggleDisablePrivateSubmission(ev)', function () { + var ev, confirmDeferred; + + beforeEach(inject(function ($q) { + // fake click event + ev = { stopPropagation: jasmine.createSpy('stopPropagation') }; + + // stub out the confirm dialog + confirmDeferred = $q.defer(); + spyOn($mdDialog, 'confirm').and.callThrough(); + spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); + + spyOn($mdDialog, 'hide'); + spyOn($rootScope, 'notify'); + + // set up page IDs for URL building + vm.page = { + creator: { id: 321 }, + id: 654 + }; + })); + + it('on cancel (reject) should just stopPropagation and set toggleSubmissionState', function () { + vm.disable_private_submission = false; + vm.toggleDisablePrivateSubmission(ev); + + expect(ev.stopPropagation).toHaveBeenCalled(); + expect(vm.toggleSubmissionState).toBe('disabled'); + + // simulate user clicking “No” + confirmDeferred.reject(); + // flush $q + $rootScope.$apply(); + + // confirm dialog was shown, but no request, no notify + expect($mdDialog.show).toHaveBeenCalled(); + expect(utilities.sendRequest).toBeUndefined(); + expect($rootScope.notify).not.toHaveBeenCalled(); + }); + + it('on OK + success should PATCH, toggle flag, hide dialog and notify success', function () { + vm.disable_private_submission = false; + vm.toggleDisablePrivateSubmission(ev); + + expect(vm.toggleSubmissionState).toBe('disabled'); + + // spy and capture parameters + spyOn(utilities, 'sendRequest').and.callFake(function (params) { + // should have flipped the flag + expect(vm.disable_private_submission).toBe(true); + // correct URL + expect(params.url) + .toBe('challenges/challenge_host_team/321/challenge/654'); + expect(params.method).toBe('PATCH'); + expect(params.data) + .toEqual({ disable_private_submission: true }); + + // invoke success callback + params.callback.onSuccess({ status: 200, data: {} }); + }); + + // simulate user clicking “Yes” + confirmDeferred.resolve(); + $rootScope.$apply(); + + expect(utilities.sendRequest).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify) + .toHaveBeenCalledWith('success', + 'Private submissions were successfully made disabled'); + }); + + it('on OK + error should hide dialog and notify error', function () { + vm.disable_private_submission = true; + vm.toggleDisablePrivateSubmission(ev); + + expect(vm.toggleSubmissionState).toBe('allowed'); + + spyOn(utilities, 'sendRequest').and.callFake(function (params) { + // flip back + expect(vm.disable_private_submission).toBe(false); + // simulate error response + params.callback.onError({ data: 'oops!' }); + }); + + confirmDeferred.resolve(); + $rootScope.$apply(); + + expect(utilities.sendRequest).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify) + .toHaveBeenCalledWith('error', 'oops!'); + }); + }); + describe('Unit tests for refreshSubmissionData function \ `jobs/challenge//challenge_phase//submission/?page=`', function () { var success, successResponse; From 370c4e6b2aa16aaa0acf73aacc25db02292f6169 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 11:03:00 +0530 Subject: [PATCH 10/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 173 +++++++++--------- 1 file changed, 86 insertions(+), 87 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 68a3fb244c..5780850bd2 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1147,98 +1147,97 @@ describe('Unit tests for challenge controller', function () { describe('vm.toggleDisablePrivateSubmission(ev)', function () { var ev, confirmDeferred; - + beforeEach(inject(function ($q) { - // fake click event - ev = { stopPropagation: jasmine.createSpy('stopPropagation') }; - - // stub out the confirm dialog - confirmDeferred = $q.defer(); - spyOn($mdDialog, 'confirm').and.callThrough(); - spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); - - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - // set up page IDs for URL building - vm.page = { - creator: { id: 321 }, - id: 654 - }; + // prevent any real HTTP/get_auth_token calls + spyOn(utilities, 'sendRequest').and.callFake(function () {}); + + // fake click event + ev = { stopPropagation: jasmine.createSpy('stopPropagation') }; + + // stub out the confirm dialog + confirmDeferred = $q.defer(); + spyOn($mdDialog, 'confirm').and.callThrough(); + spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); + + spyOn($mdDialog, 'hide'); + spyOn($rootScope, 'notify'); + + // set up page IDs for URL building + vm.page = { + creator: { id: 321 }, + id: 654 + }; })); - + it('on cancel (reject) should just stopPropagation and set toggleSubmissionState', function () { - vm.disable_private_submission = false; - vm.toggleDisablePrivateSubmission(ev); - - expect(ev.stopPropagation).toHaveBeenCalled(); - expect(vm.toggleSubmissionState).toBe('disabled'); - - // simulate user clicking “No” - confirmDeferred.reject(); - // flush $q - $rootScope.$apply(); - - // confirm dialog was shown, but no request, no notify - expect($mdDialog.show).toHaveBeenCalled(); - expect(utilities.sendRequest).toBeUndefined(); - expect($rootScope.notify).not.toHaveBeenCalled(); - }); - + // allow us to assert no request is sent + spyOn(utilities, 'sendRequest'); + + vm.disable_private_submission = false; + vm.toggleDisablePrivateSubmission(ev); + + expect(ev.stopPropagation).toHaveBeenCalled(); + expect(vm.toggleSubmissionState).toBe('disabled'); + expect($mdDialog.show).toHaveBeenCalled(); + + // simulate user clicking “No” + confirmDeferred.reject(); + $rootScope.$apply(); + + // no PATCH should be sent, no notification + expect(utilities.sendRequest).not.toHaveBeenCalled(); + expect($rootScope.notify).not.toHaveBeenCalled(); + }); + it('on OK + success should PATCH, toggle flag, hide dialog and notify success', function () { - vm.disable_private_submission = false; - vm.toggleDisablePrivateSubmission(ev); - - expect(vm.toggleSubmissionState).toBe('disabled'); - - // spy and capture parameters - spyOn(utilities, 'sendRequest').and.callFake(function (params) { - // should have flipped the flag - expect(vm.disable_private_submission).toBe(true); - // correct URL - expect(params.url) - .toBe('challenges/challenge_host_team/321/challenge/654'); - expect(params.method).toBe('PATCH'); - expect(params.data) - .toEqual({ disable_private_submission: true }); - - // invoke success callback - params.callback.onSuccess({ status: 200, data: {} }); - }); - - // simulate user clicking “Yes” - confirmDeferred.resolve(); - $rootScope.$apply(); - - expect(utilities.sendRequest).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify) - .toHaveBeenCalledWith('success', - 'Private submissions were successfully made disabled'); - }); - + vm.disable_private_submission = false; + + // capture & verify the PATCH parameters + spyOn(utilities, 'sendRequest').and.callFake(function (params) { + // flag flips before callback + expect(vm.disable_private_submission).toBe(true); + expect(params.url).toBe('challenges/challenge_host_team/321/challenge/654'); + expect(params.method).toBe('PATCH'); + expect(params.data).toEqual({ disable_private_submission: true }); + // simulate success + params.callback.onSuccess({ status: 200, data: {} }); + }); + + vm.toggleDisablePrivateSubmission(ev); + expect(vm.toggleSubmissionState).toBe('disabled'); + + // simulate user clicking “Yes” + confirmDeferred.resolve(); + $rootScope.$apply(); + + expect(utilities.sendRequest).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify) + .toHaveBeenCalledWith('success', + 'Private submissions were successfully made disabled'); + }); + it('on OK + error should hide dialog and notify error', function () { - vm.disable_private_submission = true; - vm.toggleDisablePrivateSubmission(ev); - - expect(vm.toggleSubmissionState).toBe('allowed'); - - spyOn(utilities, 'sendRequest').and.callFake(function (params) { - // flip back - expect(vm.disable_private_submission).toBe(false); - // simulate error response - params.callback.onError({ data: 'oops!' }); - }); - - confirmDeferred.resolve(); - $rootScope.$apply(); - - expect(utilities.sendRequest).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify) - .toHaveBeenCalledWith('error', 'oops!'); - }); - }); + vm.disable_private_submission = true; + + spyOn(utilities, 'sendRequest').and.callFake(function (params) { + // flag flips before error callback + expect(vm.disable_private_submission).toBe(false); + params.callback.onError({ data: 'oops!' }); + }); + + vm.toggleDisablePrivateSubmission(ev); + expect(vm.toggleSubmissionState).toBe('allowed'); + + confirmDeferred.resolve(); + $rootScope.$apply(); + + expect(utilities.sendRequest).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith('error', 'oops!'); + }); + }); describe('Unit tests for refreshSubmissionData function \ `jobs/challenge//challenge_phase//submission/?page=`', function () { From fc887652060c00e44700f4a3f20834b676aaa003 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 11:21:39 +0530 Subject: [PATCH 11/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 5780850bd2..78fcc2cdea 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1149,7 +1149,7 @@ describe('Unit tests for challenge controller', function () { var ev, confirmDeferred; beforeEach(inject(function ($q) { - // prevent any real HTTP/get_auth_token calls + // Prevent ANY real HTTP from utilities.sendRequest during controller init spyOn(utilities, 'sendRequest').and.callFake(function () {}); // fake click event @@ -1159,20 +1159,15 @@ describe('Unit tests for challenge controller', function () { confirmDeferred = $q.defer(); spyOn($mdDialog, 'confirm').and.callThrough(); spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); - spyOn($mdDialog, 'hide'); spyOn($rootScope, 'notify'); // set up page IDs for URL building - vm.page = { - creator: { id: 321 }, - id: 654 - }; + vm.page = { creator: { id: 321 }, id: 654 }; })); it('on cancel (reject) should just stopPropagation and set toggleSubmissionState', function () { - // allow us to assert no request is sent - spyOn(utilities, 'sendRequest'); + utilities.sendRequest.calls.reset(); // start fresh for this test vm.disable_private_submission = false; vm.toggleDisablePrivateSubmission(ev); @@ -1185,25 +1180,27 @@ describe('Unit tests for challenge controller', function () { confirmDeferred.reject(); $rootScope.$apply(); - // no PATCH should be sent, no notification - expect(utilities.sendRequest).not.toHaveBeenCalled(); + expect(utilities.sendRequest).not.toHaveBeenCalled(); // no PATCH expect($rootScope.notify).not.toHaveBeenCalled(); }); it('on OK + success should PATCH, toggle flag, hide dialog and notify success', function () { - vm.disable_private_submission = false; - - // capture & verify the PATCH parameters - spyOn(utilities, 'sendRequest').and.callFake(function (params) { + // fresh spy state + utilities.sendRequest.calls.reset(); + utilities.sendRequest.and.callFake(function (params) { // flag flips before callback expect(vm.disable_private_submission).toBe(true); - expect(params.url).toBe('challenges/challenge_host_team/321/challenge/654'); + expect(params.url) + .toBe('challenges/challenge_host_team/321/challenge/654'); expect(params.method).toBe('PATCH'); - expect(params.data).toEqual({ disable_private_submission: true }); + expect(params.data) + .toEqual({ disable_private_submission: true }); + // simulate success params.callback.onSuccess({ status: 200, data: {} }); }); + vm.disable_private_submission = false; vm.toggleDisablePrivateSubmission(ev); expect(vm.toggleSubmissionState).toBe('disabled'); @@ -1219,14 +1216,15 @@ describe('Unit tests for challenge controller', function () { }); it('on OK + error should hide dialog and notify error', function () { - vm.disable_private_submission = true; - - spyOn(utilities, 'sendRequest').and.callFake(function (params) { + // fresh spy state + utilities.sendRequest.calls.reset(); + utilities.sendRequest.and.callFake(function (params) { // flag flips before error callback expect(vm.disable_private_submission).toBe(false); params.callback.onError({ data: 'oops!' }); }); + vm.disable_private_submission = true; vm.toggleDisablePrivateSubmission(ev); expect(vm.toggleSubmissionState).toBe('allowed'); From 1032bb850f642650ce0e2f6ef468a6689127f70c Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 11:46:58 +0530 Subject: [PATCH 12/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 6044 +++++++++-------- 1 file changed, 3201 insertions(+), 2843 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 78fcc2cdea..2d77cad4c6 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1,2872 +1,3230 @@ -'use strict'; - -describe('Unit tests for challenge controller', function () { - beforeEach(angular.mock.module('evalai')); - - var $controller, createController, $injector, $rootScope, $state, $scope, utilities, $http, $interval, $mdDialog, moment, vm; - - beforeEach(inject(function (_$controller_, _$injector_, _$rootScope_, _$state_, _utilities_, _$http_, _$interval_, _$mdDialog_, _moment_) { - $controller = _$controller_; - $injector = _$injector_; - $rootScope = _$rootScope_; - $state = _$state_; - utilities = _utilities_; - $http = _$http_; - $interval = _$interval_; - $mdDialog = _$mdDialog_; - moment = _moment_; - - $scope = $rootScope.$new(); - createController = function () { - return $controller('ChallengeCtrl', { $scope: $scope }); - }; - vm = $controller('ChallengeCtrl', { $scope: $scope }); - })); +"use strict"; + +describe("Unit tests for challenge controller", function () { + beforeEach(angular.mock.module("evalai")); + + var $controller, + createController, + $injector, + $rootScope, + $state, + $scope, + utilities, + $http, + $interval, + $mdDialog, + moment, + vm; + + beforeEach(inject(function ( + _$controller_, + _$injector_, + _$rootScope_, + _$state_, + _utilities_, + _$http_, + _$interval_, + _$mdDialog_, + _moment_ + ) { + $controller = _$controller_; + $injector = _$injector_; + $rootScope = _$rootScope_; + $state = _$state_; + utilities = _utilities_; + $http = _$http_; + $interval = _$interval_; + $mdDialog = _$mdDialog_; + moment = _moment_; + + $scope = $rootScope.$new(); + createController = function () { + return $controller("ChallengeCtrl", { $scope: $scope }); + }; + vm = $controller("ChallengeCtrl", { $scope: $scope }); + })); + + describe("Global variables", function () { + it("has default values", function () { + expect(vm.phaseId).toEqual(null); + expect(vm.input_file).toEqual(null); + expect(vm.methodName).toEqual(""); + expect(vm.methodDesc).toEqual(""); + expect(vm.projectUrl).toEqual(""); + expect(vm.publicationUrl).toEqual(""); + expect(vm.page).toEqual({}); + expect(vm.isParticipated).toEqual(false); + expect(vm.isActive).toEqual(false); + expect(vm.phaseRemainingSubmissions).toEqual({}); + expect(vm.phaseRemainingSubmissionsFlags).toEqual({}); + expect(vm.phaseRemainingSubmissionsCountdown).toEqual({}); + expect(vm.subErrors).toEqual({}); + expect(vm.isExistLoader).toEqual(false); + expect(vm.loaderTitle).toEqual(""); + expect(vm.termsAndConditions).toEqual(false); + expect(vm.lastKey).toEqual(null); + expect(vm.sortColumn).toEqual("rank"); + expect(vm.columnIndexSort).toEqual(0); + expect(vm.reverseSort).toEqual(false); + expect(vm.poller).toEqual(null); + expect(vm.isResult).toEqual(false); + expect(vm.initial_ranking).toEqual({}); + expect(vm.submissionVisibility).toEqual({}); + expect(vm.baselineStatus).toEqual({}); + expect(vm.team).toEqual({}); + expect(vm.isPublished).toEqual(false); + + utilities.storeData("userKey", "encrypted key"); + }); + }); + + describe("Unit tests for all the global backend calls", function () { + var challengeSuccess, + successResponse, + errorResponse, + status, + challengePhaseSuccess; + var challengePhaseSplitSuccess, + participantTeamChallengeSuccess, + participantTeamSuccess, + selectExistTeamSuccess; + var challengeSuccessResponse, + participantTeamSuccessResponse, + participantTeamChallengeSuccessResponse; + var challengePhaseSplit = false; + var team_list = [ + { + next: null, + previous: null, + }, + { + next: null, + previous: null, + }, + { + next: "page=5", + previous: null, + }, + { + next: null, + previous: "page=3", + }, + { + next: "page=4", + previous: "page=2", + }, + ]; + + beforeEach(function () { + spyOn(utilities, "deleteData"); + + utilities.sendRequest = function (parameters) { + // set successResponse according to the requested url + if ( + participantTeamSuccess == true && + parameters.url == "participants/participant_team" + ) { + successResponse = participantTeamSuccessResponse; + } else if ( + challengeSuccess == true && + parameters.url == "challenges/challenge/undefined/" + ) { + successResponse = challengeSuccessResponse; + } else if ( + participantTeamChallengeSuccess == true && + parameters.url == + "participants/participant_teams/challenges/undefined/user" + ) { + successResponse = participantTeamChallengeSuccessResponse; + } - describe('Global variables', function () { - it('has default values', function () { - expect(vm.phaseId).toEqual(null); - expect(vm.input_file).toEqual(null); - expect(vm.methodName).toEqual(""); - expect(vm.methodDesc).toEqual(""); - expect(vm.projectUrl).toEqual(""); - expect(vm.publicationUrl).toEqual(""); - expect(vm.page).toEqual({}); - expect(vm.isParticipated).toEqual(false); - expect(vm.isActive).toEqual(false); - expect(vm.phaseRemainingSubmissions).toEqual({}); - expect(vm.phaseRemainingSubmissionsFlags).toEqual({}); - expect(vm.phaseRemainingSubmissionsCountdown).toEqual({}); - expect(vm.subErrors).toEqual({}); - expect(vm.isExistLoader).toEqual(false); - expect(vm.loaderTitle).toEqual(''); - expect(vm.termsAndConditions).toEqual(false); - expect(vm.lastKey).toEqual(null); - expect(vm.sortColumn).toEqual('rank'); - expect(vm.columnIndexSort).toEqual(0); - expect(vm.reverseSort).toEqual(false); - expect(vm.poller).toEqual(null); - expect(vm.isResult).toEqual(false); - expect(vm.initial_ranking).toEqual({}); - expect(vm.submissionVisibility).toEqual({}); - expect(vm.baselineStatus).toEqual({}); - expect(vm.team).toEqual({}); - expect(vm.isPublished).toEqual(false); - - utilities.storeData('userKey', 'encrypted key'); - }); - }); - - describe('Unit tests for all the global backend calls', function () { - var challengeSuccess, successResponse, errorResponse, status, challengePhaseSuccess; - var challengePhaseSplitSuccess, participantTeamChallengeSuccess, participantTeamSuccess, selectExistTeamSuccess; - var challengeSuccessResponse, participantTeamSuccessResponse, participantTeamChallengeSuccessResponse - var challengePhaseSplit = false; - var team_list = [ - { - next: null, - previous: null, - }, - { - next: null, - previous: null, + if ( + (challengePhaseSuccess == true && + parameters.url == + "challenges/challenge/undefined/challenge_phase") || + (challengeSuccess == true && + parameters.url == "challenges/challenge/undefined/") || + (challengePhaseSplitSuccess == true && + parameters.url == "challenges/undefined/challenge_phase_split") || + (participantTeamChallengeSuccess == true && + parameters.url == + "participants/participant_teams/challenges/undefined/user") || + (participantTeamSuccess == true && + parameters.url == "participants/participant_team") || + (selectExistTeamSuccess == true && + parameters.url == + "challenges/challenge/undefined/participant_team/null") + ) { + parameters.callback.onSuccess({ + status: 200, + data: successResponse, + }); + } else if ( + (challengePhaseSuccess == false && + parameters.url == + "challenges/challenge/undefined/challenge_phase") || + (challengeSuccess == false && + parameters.url == "challenges/challenge/undefined/") || + (challengePhaseSplitSuccess == false && + parameters.url == "challenges/undefined/challenge_phase_split") || + (participantTeamChallengeSuccess == false && + parameters.url == + "participants/participant_teams/challenges/undefined/user") || + (participantTeamSuccess == false && + parameters.url == "participants/participant_team") || + (selectExistTeamSuccess == false && + parameters.url == + "challenges/challenge/undefined/participant_team/null") + ) { + parameters.callback.onError({ + data: errorResponse, + status: status, + }); + } + }; + }); + + it("get the details of the particular challenge \ + `challenges/challenge//`", function () { + challengeSuccess = true; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = null; + participantTeamChallengeSuccess = null; + participantTeamSuccess = null; + selectExistTeamSuccess = null; + + challengeSuccessResponse = { + is_active: true, + published: false, + enable_forum: true, + forum_url: "http://example.com", + cli_version: "evalai-cli version", + image: "logo.png", + }; + vm = createController(); + expect(vm.page).toEqual(challengeSuccessResponse); + expect(vm.isActive).toEqual(challengeSuccessResponse.is_active); + expect(vm.isPublished).toEqual(challengeSuccessResponse.published); + expect(vm.isForumEnabled).toEqual(challengeSuccessResponse.enable_forum); + expect(vm.forumURL).toEqual(challengeSuccessResponse.forum_url); + expect(vm.cliVersion).toEqual(challengeSuccessResponse.cli_version); + }); + + it("when challenge logo image is null", function () { + challengeSuccess = true; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = null; + participantTeamChallengeSuccess = null; + participantTeamSuccess = null; + selectExistTeamSuccess = null; + + challengeSuccessResponse = { + is_active: true, + published: false, + enable_forum: true, + forum_url: "http://example.com", + cli_version: "evalai-cli version", + image: null, + }; + vm = createController(); + expect(vm.page.image).toEqual("dist/images/logo.png"); + }); + + it("get details of challenges corresponding to participant teams of that user \ + `participants/participant_teams/challenges//user`", function () { + challengeSuccess = true; + participantTeamChallengeSuccess = true; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = null; + participantTeamSuccess = null; + selectExistTeamSuccess = null; + utilities.storeData("userKey", "encrypted key"); + + // get challenge details response + challengeSuccessResponse = { + is_active: true, + published: false, + enable_forum: true, + forum_url: "http://example.com", + cli_version: "evalai-cli version", + image: "logo.png", + }; + + // get details of challenges corresponding to participant teams response + participantTeamChallengeSuccessResponse = { + challenge_participant_team_list: { + first_object: { + challenge: { + id: undefined, + title: "Challenge title", + description: "Challenge description", }, - { - next: 'page=5', - previous: null, + }, + }, + datetime_now: "timezone.now", + is_challenge_host: true, + }; + + vm = createController(); + expect(vm.currentDate).toEqual( + participantTeamChallengeSuccessResponse.datetime_now + ); + expect(vm.isParticipated).toBeTruthy(); + expect(vm.isChallengeHost).toBeTruthy(); + }); + + team_list.forEach((response) => { + it( + "pagination next is " + + response.next + + " and previous is " + + response.previous + + "\ + `participants/participant_team`", + function () { + challengeSuccess = true; + participantTeamChallengeSuccess = true; + participantTeamSuccess = true; + selectExistTeamSuccess = null; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = null; + + // get challenge details response + challengeSuccessResponse = { + is_active: true, + published: false, + enable_forum: true, + forum_url: "http://example.com", + cli_version: "evalai-cli version", + image: "logo.png", + }; + + // get details of challenges corresponding to participant teams response + participantTeamChallengeSuccessResponse = { + challenge_participant_team_list: { + first_object: { + challenge: { + id: 3, + title: "Challenge title", + description: "Challenge description", + }, + }, }, + datetime_now: "timezone.now", + is_challenge_host: true, + }; + + // participant team details response + participantTeamSuccessResponse = response; + participantTeamSuccessResponse.results = [ { - next: null, - previous: 'page=3', + is_public: false, + start_date: "Fri June 12 2018 22:41:51 GMT+0530", + end_date: "Fri June 12 2099 22:41:51 GMT+0530", }, - { - next: 'page=4', - previous: 'page=2', - } - ]; - - beforeEach(function () { - spyOn(utilities, 'deleteData'); - - utilities.sendRequest = function (parameters) { - // set successResponse according to the requested url - if (participantTeamSuccess == true && parameters.url == 'participants/participant_team') { - successResponse = participantTeamSuccessResponse; - } else if (challengeSuccess == true && parameters.url == 'challenges/challenge/undefined/') { - successResponse = challengeSuccessResponse; - } else if (participantTeamChallengeSuccess == true && parameters.url == 'participants/participant_teams/challenges/undefined/user') { - successResponse = participantTeamChallengeSuccessResponse; - } - - if ((challengePhaseSuccess == true && parameters.url == 'challenges/challenge/undefined/challenge_phase') || - (challengeSuccess == true && parameters.url == 'challenges/challenge/undefined/') || - (challengePhaseSplitSuccess == true && parameters.url == 'challenges/undefined/challenge_phase_split') || - (participantTeamChallengeSuccess == true && parameters.url == 'participants/participant_teams/challenges/undefined/user') || - (participantTeamSuccess == true && parameters.url == 'participants/participant_team') || - (selectExistTeamSuccess == true && parameters.url == 'challenges/challenge/undefined/participant_team/null')) { - - parameters.callback.onSuccess({ - status: 200, - data: successResponse - }); - } else if ((challengePhaseSuccess == false && parameters.url == 'challenges/challenge/undefined/challenge_phase') || - (challengeSuccess == false && parameters.url == 'challenges/challenge/undefined/') || - (challengePhaseSplitSuccess == false && parameters.url == 'challenges/undefined/challenge_phase_split') || - (participantTeamChallengeSuccess == false && parameters.url == 'participants/participant_teams/challenges/undefined/user') || - (participantTeamSuccess == false && parameters.url == 'participants/participant_team') || - (selectExistTeamSuccess == false && parameters.url == 'challenges/challenge/undefined/participant_team/null')) { - - parameters.callback.onError({ - data: errorResponse, - status: status - }); - } - }; - }); - - it('get the details of the particular challenge \ - `challenges/challenge//`', function () { - challengeSuccess = true; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = null; - participantTeamChallengeSuccess = null; - participantTeamSuccess = null; - selectExistTeamSuccess = null; - - challengeSuccessResponse = { - is_active: true, - published: false, - enable_forum: true, - forum_url: "http://example.com", - cli_version: "evalai-cli version", - image: 'logo.png', - }; - vm = createController(); - expect(vm.page).toEqual(challengeSuccessResponse); - expect(vm.isActive).toEqual(challengeSuccessResponse.is_active); - expect(vm.isPublished).toEqual(challengeSuccessResponse.published); - expect(vm.isForumEnabled).toEqual(challengeSuccessResponse.enable_forum); - expect(vm.forumURL).toEqual(challengeSuccessResponse.forum_url); - expect(vm.cliVersion).toEqual(challengeSuccessResponse.cli_version); - }); - - it('when challenge logo image is null', function () { - challengeSuccess = true; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = null; - participantTeamChallengeSuccess = null; - participantTeamSuccess = null; - selectExistTeamSuccess = null; - - challengeSuccessResponse = { - is_active: true, - published: false, - enable_forum: true, - forum_url: "http://example.com", - cli_version: "evalai-cli version", - image: null, - }; - vm = createController(); - expect(vm.page.image).toEqual("dist/images/logo.png"); - }); - - it('get details of challenges corresponding to participant teams of that user \ - `participants/participant_teams/challenges//user`', function () { - challengeSuccess = true; - participantTeamChallengeSuccess = true; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = null; - participantTeamSuccess = null; - selectExistTeamSuccess = null; - utilities.storeData('userKey', 'encrypted key'); - - // get challenge details response - challengeSuccessResponse = { - is_active: true, - published: false, - enable_forum: true, - forum_url: "http://example.com", - cli_version: "evalai-cli version", - image: 'logo.png', - }; - - // get details of challenges corresponding to participant teams response - participantTeamChallengeSuccessResponse = { - challenge_participant_team_list: { - first_object: { - challenge: { - id: undefined, - title: "Challenge title", - description: "Challenge description" - }, - } - }, - datetime_now: "timezone.now", - is_challenge_host: true, - }; - - vm = createController(); - expect(vm.currentDate).toEqual(participantTeamChallengeSuccessResponse.datetime_now); - expect(vm.isParticipated).toBeTruthy(); - expect(vm.isChallengeHost).toBeTruthy(); - }); - - team_list.forEach(response => { - it('pagination next is ' + response.next + ' and previous is ' + response.previous + '\ - `participants/participant_team`', function () { - ; - challengeSuccess = true; - participantTeamChallengeSuccess = true; - participantTeamSuccess = true; - selectExistTeamSuccess = null; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = null; - - // get challenge details response - challengeSuccessResponse = { - is_active: true, - published: false, - enable_forum: true, - forum_url: "http://example.com", - cli_version: "evalai-cli version", - image: 'logo.png', - }; - - // get details of challenges corresponding to participant teams response - participantTeamChallengeSuccessResponse = { - challenge_participant_team_list: { - first_object: { - challenge: { - id: 3, - title: "Challenge title", - description: "Challenge description" - }, - } - }, - datetime_now: "timezone.now", - is_challenge_host: true, - }; - - // participant team details response - participantTeamSuccessResponse = response; - participantTeamSuccessResponse.results = [ - { - is_public: false, - start_date: "Fri June 12 2018 22:41:51 GMT+0530", - end_date: "Fri June 12 2099 22:41:51 GMT+0530" - }, - ]; - utilities.storeData('userKey', 'encrypted key'); - - vm = createController(); - expect(vm.existTeam).toEqual(participantTeamSuccessResponse); - expect(vm.showPagination).toBeTruthy(); - expect(vm.paginationMsg).toEqual(''); - expect(utilities.deleteData).toHaveBeenCalledWith('emailError'); - - if (participantTeamSuccessResponse.next == null) { - expect(vm.isNext).toEqual('disabled'); - expect(vm.currentPage).toEqual(1); - } else { - expect(vm.isNext).toEqual(''); - expect(vm.currentPage).toEqual(participantTeamSuccessResponse.next.split('page=')[1] - 1); - } - - if (participantTeamSuccessResponse.previous == null) { - expect(vm.isPrev).toEqual('disabled'); - } else { - expect(vm.isPrev).toEqual(''); - } - - if (participantTeamSuccessResponse.next !== null) { - expect(vm.currentPage).toEqual(participantTeamSuccessResponse.next.split('page=')[1] - 1); - } else { - expect(vm.currentPage).toEqual(1); - } - }); - }); - - it('success of selectExistTeam function \ - `challenges/challenge//participant_team/`', function () { - challengeSuccess = true; - participantTeamChallengeSuccess = true; - participantTeamSuccess = true; - selectExistTeamSuccess = true; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = null; - - // get challenge details response - challengeSuccessResponse = { - is_active: true, - published: false, - enable_forum: true, - forum_url: "http://example.com", - cli_version: "evalai-cli version", - image: 'logo.png', - }; - - // get details of challenges corresponding to participant teams response - participantTeamChallengeSuccessResponse = { - challenge_participant_team_list: { - first_object: { - challenge: null - } - }, - datetime_now: "timezone.now", - is_challenge_host: true, - }; - - // get participant team details response - participantTeamSuccessResponse = { - next: 'page=4', - previous: 'page=2', - results: { - is_public: false, - start_date: "Fri June 12 2018 22:41:51 GMT+0530", - end_date: "Fri June 12 2099 22:41:51 GMT+0530" - } - }; - utilities.storeData('userKey', 'encrypted key'); - - vm = createController(); - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - spyOn($state, 'go'); - spyOn(angular, 'element'); - vm.selectExistTeam(); - expect(vm.loaderTitle).toEqual(''); - expect(angular.element).toHaveBeenCalledWith('.exist-team-card'); - expect(vm.startLoader).toHaveBeenCalledWith("Loading Teams"); - expect(vm.isParticipated).toBeTruthy(); - expect($state.go).toHaveBeenCalledWith('web.challenge-main.challenge-page.submission'); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - - it('404 backend error of selectExistTeam function \ - `challenges/challenge//participant_team/`', function () { - challengeSuccess = true; - participantTeamChallengeSuccess = true; - participantTeamSuccess = true; - selectExistTeamSuccess = null; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = null; - - // get challenge details response - challengeSuccessResponse = { - is_active: true, - published: false, - enable_forum: true, - forum_url: "http://example.com", - cli_version: "evalai-cli version", - image: 'logo.png', - }; - - // get details of challenges corresponding to participant teams response - participantTeamChallengeSuccessResponse = { - challenge_participant_team_list: { - first_object: { - challenge: null - } - }, - datetime_now: "timezone.now", - is_challenge_host: true, - }; - - // get participant team details response - participantTeamSuccessResponse = { - next: 'page=4', - previous: 'page=2', - results: { - is_public: false, - start_date: "Fri June 12 2018 22:41:51 GMT+0530", - end_date: "Fri June 12 2099 22:41:51 GMT+0530" - } - }; - utilities.storeData('userKey', 'encrypted key'); - - status = !404 - errorResponse = { - error: 'error' - }; - vm = createController(); - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - spyOn($rootScope, 'notify'); - spyOn(angular, 'element'); - - selectExistTeamSuccess = false; - vm.selectExistTeam(); - expect(vm.loaderTitle).toEqual(''); - expect(angular.element).toHaveBeenCalledWith('.exist-team-card'); - expect(vm.startLoader).toHaveBeenCalledWith("Loading Teams"); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse.error); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - - it('to load data with pagination', function () { - challengeSuccess = true; - participantTeamChallengeSuccess = true; - participantTeamSuccess = true; - selectExistTeamSuccess = null; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = null; - - // get challenge details response - challengeSuccessResponse = { - is_active: true, - published: false, - enable_forum: true, - forum_url: "http://example.com", - cli_version: "evalai-cli version", - image: 'logo.png', - }; - - // get details of challenges corresponding to participant teams response - participantTeamChallengeSuccessResponse = { - challenge_participant_team_list: { - first_object: { - challenge: { - id: 1, - title: "Challenge title", - description: "Challenge description" - }, - } - }, - datetime_now: "timezone.now", - is_challenge_host: true, - }; - - // get participant team details response - participantTeamSuccessResponse = { - next: 'page=4', - previous: 'page=2', - results: { - is_public: false, - start_date: "Fri June 12 2018 22:41:51 GMT+0530", - end_date: "Fri June 12 2099 22:41:51 GMT+0530" - } - }; - utilities.storeData('userKey', 'encrypted key'); - - vm = createController(); - spyOn(vm, 'startLoader'); - spyOn($http, 'get').and.callFake(function () { - var deferred = $injector.get('$q').defer(); - return deferred.promise; - }); - var url = 'participants/participant_team/page=2'; - vm.load(url); - expect(vm.isExistLoader).toBeTruthy(); - expect(vm.startLoader).toHaveBeenCalledWith("Loading Teams"); - var headers = { - 'Authorization': "Token " + utilities.getData('userKey') - }; - expect($http.get).toHaveBeenCalledWith(url, { headers: headers }); - }); - - it('backend error of the particular challenge `challenges/challenge//', function () { - challengeSuccess = false; - participantTeamChallengeSuccess = null; - participantTeamSuccess = null; - selectExistTeamSuccess = null; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = null; - - status = 404; - errorResponse = { - error: 'email error' - }; - spyOn($state, 'go'); - spyOn($rootScope, 'notify'); - spyOn(utilities, 'hideLoader'); - vm = createController(); - expect($rootScope.notify).toHaveBeenCalledWith('error', errorResponse.error); - expect($state.go).toHaveBeenCalledWith('web.dashboard'); - expect(utilities.hideLoader).toHaveBeenCalled(); - }); - - it('get details of the particular challenge phase `challenges/challenge//challenge_phase`', function () { - challengeSuccess = null; - participantTeamChallengeSuccess = null; - participantTeamSuccess = null; - selectExistTeamSuccess = null; - challengePhaseSuccess = true; - challengePhaseSplitSuccess = null; - - // get challenge phase details response - successResponse = { - results: [ - { - is_public: false, - start_date: "Fri June 12 2018 22:41:51 GMT+0530", - end_date: "Fri June 12 2099 22:41:51 GMT+0530" - }, - ], - }; - spyOn(utilities, 'hideLoader'); - vm = createController(); - expect(vm.phases).toEqual(successResponse); - var timezone = moment.tz.guess(); - for (var i = 0; i < successResponse.count; i++) { - expect(successResponse.results[i].is_public).toBeFalsy(); - expect(vm.phases.results[i].showPrivate).toBeTruthy(); - } - - for (var i = 0; i < successResponse.results.length; i++) { - var offset = new Date(successResponse.results[i].start_date).getTimezoneOffset(); - expect(vm.phases.results[i].time_zone).toEqual(moment.tz.zone(timezone).abbr(offset)); - } - expect(utilities.hideLoader).toHaveBeenCalled(); - }); - - it('backend error of particular challenge phase `challenges/challenge//challenge_phase`', function () { - challengeSuccess = null; - participantTeamChallengeSuccess = null; - participantTeamSuccess = null; - selectExistTeamSuccess = null; - challengePhaseSuccess = false; - challengePhaseSplitSuccess = null; - - status = 404; - errorResponse = { - detail: 'error' - }; - spyOn(utilities, 'storeData'); - spyOn($state, 'go'); - spyOn(utilities, 'hideLoader'); - vm = createController(); - expect(utilities.storeData).toHaveBeenCalledWith('emailError', errorResponse.detail); - expect($state.go).toHaveBeenCalledWith('web.permission-denied'); - expect(utilities.hideLoader).toHaveBeenCalled(); - }); - - it('get details of the particular challenge phase split `challenges//challenge_phase_split`', function () { - challengeSuccess = null; - participantTeamChallengeSuccess = null; - participantTeamSuccess = null; - selectExistTeamSuccess = null; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = true; - - challengePhaseSplit = true; - // get challenge phase split details response - successResponse = [ - { - visibility: 2, - host: 1 - } - ]; - var challengePhaseVisibility = { - owner_and_host: 1, - host: 2, - public: 3, - }; - spyOn(utilities, 'hideLoader'); - vm.isParticipated = true; - vm = createController(); - expect(vm.phaseSplits).toEqual(successResponse); - for (var i = 0; i < successResponse.length; i++) { - if (successResponse[i].visibility != challengePhaseVisibility.public) { - expect(vm.phaseSplits[i].showPrivate).toBeTruthy(); - } - } - expect(utilities.hideLoader).toHaveBeenCalled(); - }); - - it('backend error of particular challenge phase split `challenges//challenge_phase_split`', function () { - challengeSuccess = null; - participantTeamChallengeSuccess = null; - participantTeamSuccess = null; - selectExistTeamSuccess = null; - challengePhaseSuccess = null; - challengePhaseSplitSuccess = false; - - status = 404; - errorResponse = { - detail: 'error' - }; - spyOn(utilities, 'storeData'); - spyOn($state, 'go'); - spyOn(utilities, 'hideLoader'); - vm = createController(); - expect(utilities.storeData).toHaveBeenCalledWith('emailError', errorResponse.detail); - expect($state.go).toHaveBeenCalledWith('web.permission-denied'); - expect(utilities.hideLoader).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for displayDockerSubmissionInstructions function \ - `jobs//remaining_submissions`', function () { - var success, successResponse; - var errorResponse = { - detail: 'Email Error' - }; + ]; + utilities.storeData("userKey", "encrypted key"); + + vm = createController(); + expect(vm.existTeam).toEqual(participantTeamSuccessResponse); + expect(vm.showPagination).toBeTruthy(); + expect(vm.paginationMsg).toEqual(""); + expect(utilities.deleteData).toHaveBeenCalledWith("emailError"); + + if (participantTeamSuccessResponse.next == null) { + expect(vm.isNext).toEqual("disabled"); + expect(vm.currentPage).toEqual(1); + } else { + expect(vm.isNext).toEqual(""); + expect(vm.currentPage).toEqual( + participantTeamSuccessResponse.next.split("page=")[1] - 1 + ); + } + + if (participantTeamSuccessResponse.previous == null) { + expect(vm.isPrev).toEqual("disabled"); + } else { + expect(vm.isPrev).toEqual(""); + } + + if (participantTeamSuccessResponse.next !== null) { + expect(vm.currentPage).toEqual( + participantTeamSuccessResponse.next.split("page=")[1] - 1 + ); + } else { + expect(vm.currentPage).toEqual(1); + } + } + ); + }); - beforeEach(function () { - spyOn(utilities, 'hideLoader'); - spyOn(utilities, 'storeData'); - spyOn($state, 'go'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: successResponse, - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('when submission limit exceeded', function () { - successResponse = { - phases: [ - { - id: 1, - limits: { - submission_limit_exceeded: true, - remaining_submissions_today_count: 12, - remaining_time: 12 / 12 / 12, - } - }, - ] - }; - success = true; - vm.eligible_to_submit = true; - vm.displayDockerSubmissionInstructions(true, true); - expect(vm.phaseRemainingSubmissions).toEqual(successResponse); - var details = vm.phaseRemainingSubmissions.phases; - for (var i = 0; i < details.length; i++) { - expect(vm.phaseRemainingSubmissionsFlags[details[i].id]).toEqual('maxExceeded'); - } - expect(utilities.hideLoader).toHaveBeenCalled(); - }); - - it('when todays remaining submission count greater than 0', function () { - successResponse = { - phases: [ - { - id: 1, - limits: { - submission_limit_exceeded: false, - remaining_submissions_today_count: 12, - remaining_time: 12 / 12 / 12, - } - }, - ] - }; - success = true; - vm.eligible_to_submit = true; - vm.displayDockerSubmissionInstructions(true, true); - expect(vm.phaseRemainingSubmissions).toEqual(successResponse); - var details = vm.phaseRemainingSubmissions.phases; - for (var i = 0; i < details.length; i++) { - expect(vm.phaseRemainingSubmissionsFlags[details[i].id]).toEqual('showSubmissionNumbers'); - } - expect(utilities.hideLoader).toHaveBeenCalled(); - }); - - it('when submission limit exceeded & todays remaining submission count less than 0', function () { - successResponse = { - phases: [ - { - id: 1, - limits: { - submission_limit_exceeded: false, - remaining_submissions_today_count: 0, - remaining_time: 12 / 12 / 12, - } - }, - ] - }; - success = true; - vm.eligible_to_submit = true; - vm.displayDockerSubmissionInstructions(true, true); - expect(vm.phaseRemainingSubmissions).toEqual(successResponse); - var details = vm.phaseRemainingSubmissions.phases; - for (var i = 0; i < details.length; i++) { - expect(vm.eachPhase).toEqual(details[i]); - expect(vm.phaseRemainingSubmissionsFlags[details[i].id]).toEqual('showClock'); - - // Unit tests for `countDownTimer` function - expect(vm.remainingTime).toEqual(vm.eachPhase.limits.remaining_time); - expect(vm.days).toEqual(Math.floor(vm.remainingTime / 24 / 60 / 60)); - expect(vm.hoursLeft).toEqual(Math.floor((vm.remainingTime) - (vm.days * 86400))); - expect(vm.hours).toEqual(Math.floor(vm.hoursLeft / 3600)); - expect(vm.minutesLeft).toEqual(Math.floor((vm.hoursLeft) - (vm.hours * 3600))); - expect(vm.minutes).toEqual(Math.floor(vm.minutesLeft / 60)); - if (vm.remainingTime === 0) { - expect(vm.phaseRemainingSubmissionsFlags[vm.eachPhase.id]).toEqual('showSubmissionNumbers'); - } - } - expect(utilities.hideLoader).toHaveBeenCalled(); - }); - - it('backend error', function () { - success = false; - vm.eligible_to_submit = true; - vm.displayDockerSubmissionInstructions(true, true); - expect(utilities.storeData).toHaveBeenCalledWith('emailError', errorResponse.detail); - expect($state.go).toHaveBeenCalledWith('web.permission-denied'); - }); - }); - - describe('Unit tests for makeSubmission function \ - `jobs/challenge//challenge_phase//submission/`', function () { - var success, status; - var errorResponse = { - error: 'error', - }; - var successResponse = { - success: 'success', - }; + it("success of selectExistTeam function \ + `challenges/challenge//participant_team/`", function () { + challengeSuccess = true; + participantTeamChallengeSuccess = true; + participantTeamSuccess = true; + selectExistTeamSuccess = true; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = null; + + // get challenge details response + challengeSuccessResponse = { + is_active: true, + published: false, + enable_forum: true, + forum_url: "http://example.com", + cli_version: "evalai-cli version", + image: "logo.png", + }; + + // get details of challenges corresponding to participant teams response + participantTeamChallengeSuccessResponse = { + challenge_participant_team_list: { + first_object: { + challenge: null, + }, + }, + datetime_now: "timezone.now", + is_challenge_host: true, + }; + + // get participant team details response + participantTeamSuccessResponse = { + next: "page=4", + previous: "page=2", + results: { + is_public: false, + start_date: "Fri June 12 2018 22:41:51 GMT+0530", + end_date: "Fri June 12 2099 22:41:51 GMT+0530", + }, + }; + utilities.storeData("userKey", "encrypted key"); + + vm = createController(); + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + spyOn($state, "go"); + spyOn(angular, "element"); + vm.selectExistTeam(); + expect(vm.loaderTitle).toEqual(""); + expect(angular.element).toHaveBeenCalledWith(".exist-team-card"); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Teams"); + expect(vm.isParticipated).toBeTruthy(); + expect($state.go).toHaveBeenCalledWith( + "web.challenge-main.challenge-page.submission" + ); + expect(vm.stopLoader).toHaveBeenCalled(); + }); - beforeEach(function () { - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 200, - data: successResponse - }); - } else { - parameters.callback.onError({ - status: status, - data: errorResponse - }); - } - }; - }); - - it('succesfully submission', function () { - success = true; - vm.isParticipated = true; - vm.eligible_to_submit = true; - vm.makeSubmission(); - expect(vm.startLoader).toHaveBeenCalledWith('Making Submission'); - expect($rootScope.notify).toHaveBeenCalledWith('success', 'Your submission has been recorded succesfully!'); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - - it('forces isPublicSubmission=true when disable_private_submission is true', function () { - success = true; - vm.isParticipated = true; - vm.eligible_to_submit = true; - vm.disable_private_submission = true; - // simulate file‐upload path so it doesn’t short‐circuit on URL logic - vm.isSubmissionUsingUrl = false; - vm.input_file = new Blob(['x'], { type: 'text/plain' }); - vm.currentPhaseAllowedSubmissionFileTypes = ['txt']; - - // capture the outgoing FormData - var originalSend = utilities.sendRequest; - var captured; - spyOn(window, 'FormData').and.callFake(function () { - var f = { store: {} }; - f.append = function (k, v) { f.store[k] = v; }; - return f; - }); - utilities.sendRequest = function (params) { - captured = params.data.store; - // call success path so post‐submission cleanup happens - params.callback.onSuccess({ status: 200, data: successResponse }); - }; - - vm.makeSubmission(); - - expect(captured.is_public).toBe(true, 'should set is_public to true'); - - // restore - utilities.sendRequest = originalSend; - }); - - it('respects vm.isPublicSubmission when disable_private_submission is false', function () { - success = true; - vm.isParticipated = true; - vm.eligible_to_submit = true; - vm.disable_private_submission = false; - vm.isPublicSubmission = false; // developer explicitly said “private” - vm.isSubmissionUsingUrl = false; - vm.input_file = new Blob(['x'], { type: 'text/plain' }); - vm.currentPhaseAllowedSubmissionFileTypes = ['txt']; - - var originalSend = utilities.sendRequest; - var captured; - spyOn(window, 'FormData').and.callFake(function () { - var f = { store: {} }; - f.append = function (k, v) { f.store[k] = v; }; - return f; - }); - utilities.sendRequest = function (params) { - captured = params.data.store; - params.callback.onSuccess({ status: 200, data: successResponse }); - }; - - vm.makeSubmission(); - - expect(captured.is_public).toBe(false, 'should use the vm.isPublicSubmission value'); - - utilities.sendRequest = originalSend; - }); - - - it('404 backend error', function () { - success = false; - status = 404; - vm.isParticipated = true; - vm.isSubmissionUsingUrl = false; - vm.eligible_to_submit = true; - vm.makeSubmission(); - vm.fileVal = 'submission.zip'; - expect(vm.phaseId).toEqual(null); - expect(vm.methodName).toEqual(null); - expect(vm.methodDesc).toEqual(null); - expect(vm.projectUrl).toEqual(null); - expect(vm.publicationUrl).toEqual(null); - expect(vm.subErrors.msg).toEqual('Please select phase!'); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - - it('other backend error', function () { - success = false; - status = 403; - vm.isParticipated = true; - vm.isSubmissionUsingUrl = false; - vm.eligible_to_submit = true; - vm.makeSubmission('submission.zip'); - expect(vm.phaseId).toEqual(null); - expect(vm.methodName).toEqual(null); - expect(vm.methodDesc).toEqual(null); - expect(vm.projectUrl).toEqual(null); - expect(vm.publicationUrl).toEqual(null); - expect(vm.subErrors.msg).toEqual(errorResponse.error); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for sortFunction function', function () { - it('sort column by is known', function () { - var return_value = vm.sortFunction({}); - expect(return_value).not.toEqual(0); - }); - - it('sort column by is not known', function () { - vm.sortColumn = 'notKnown'; - var return_value = vm.sortFunction({}); - expect(return_value).toEqual(0); - }); - }); - - describe('Unit tests for sortLeaderboard function', function () { - it('column index is null and passed `column` not same with the current `sortColumn`', function () { - vm.sortLeaderboard(vm, 'number', null); - expect(vm.reverseSort).toEqual(false); - }); - - it('column index is null and passed `column` same with the current `sortColumn`', function () { - vm.sortLeaderboard(vm, 'rank', null); - expect(vm.reverseSort).toEqual(true); - }); - - it('column index is not null and passed `column` and `index` not same with the current `sortColumn` and `columnIndexSort`', function () { - vm.sortLeaderboard(vm, 'number', 1); - expect(vm.reverseSort).toEqual(false); - expect(vm.columnIndexSort).toEqual(1); - }); - - it('column index is not null and passed `column` and `index` same with the current `sortColumn` and `columnIndexSort`', function () { - vm.sortLeaderboard(vm, 'rank', 0); - expect(vm.reverseSort).toEqual(true); - expect(vm.columnIndexSort).toEqual(0); - }); - }); - - describe('Unit tests for getLeaderboard function \ - `jobs/challenge_phase_split//leaderboard/?page_size=1000`', function () { - var success, successResponse, errorResponse; - - beforeEach(function () { - spyOn($interval, 'cancel'); - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - spyOn(vm, 'startLeaderboard'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: successResponse - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('successfully get the leaderboard', function () { - success = true; - successResponse = { - results: { - duration: 'year', - results: [ - { - id: 1, - leaderboard__schema: - { - labels: ['label1', 'label2'], - default_order_by: 'default_order_by', - }, - submission__submitted_at: (new Date() - new Date().setFullYear(new Date().getFullYear() - 1)), - }, - ] - }, - }; - var phaseSplitId = 1; - vm.getLeaderboard(phaseSplitId); - vm.stopLeaderboard(); - expect($interval.cancel).toHaveBeenCalled(); - expect(vm.isResult).toEqual(true); - expect(vm.startLoader).toHaveBeenCalledWith('Loading Leaderboard Items'); - expect(vm.leaderboard).toEqual(successResponse.results); - - expect(vm.phaseName).toEqual(vm.phaseSplitId); - expect(vm.startLeaderboard).toHaveBeenCalled(); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - - it('backend error', function () { - success = false; - errorResponse = 'error'; - var phaseSplitId = 1; - vm.getLeaderboard(phaseSplitId); - vm.stopLeaderboard(); - expect($interval.cancel).toHaveBeenCalled(); - expect(vm.isResult).toEqual(true); - expect(vm.startLoader).toHaveBeenCalledWith('Loading Leaderboard Items'); - expect(vm.leaderboard.error).toEqual(errorResponse); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for getResults function', function () { - var submissionCountSuccess, submissionListSuccess, successResponse, errorResponse; - var submission_list = [ - { - next: null, - previous: null, + it("404 backend error of selectExistTeam function \ + `challenges/challenge//participant_team/`", function () { + challengeSuccess = true; + participantTeamChallengeSuccess = true; + participantTeamSuccess = true; + selectExistTeamSuccess = null; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = null; + + // get challenge details response + challengeSuccessResponse = { + is_active: true, + published: false, + enable_forum: true, + forum_url: "http://example.com", + cli_version: "evalai-cli version", + image: "logo.png", + }; + + // get details of challenges corresponding to participant teams response + participantTeamChallengeSuccessResponse = { + challenge_participant_team_list: { + first_object: { + challenge: null, + }, + }, + datetime_now: "timezone.now", + is_challenge_host: true, + }; + + // get participant team details response + participantTeamSuccessResponse = { + next: "page=4", + previous: "page=2", + results: { + is_public: false, + start_date: "Fri June 12 2018 22:41:51 GMT+0530", + end_date: "Fri June 12 2099 22:41:51 GMT+0530", + }, + }; + utilities.storeData("userKey", "encrypted key"); + + status = !404; + errorResponse = { + error: "error", + }; + vm = createController(); + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + spyOn($rootScope, "notify"); + spyOn(angular, "element"); + + selectExistTeamSuccess = false; + vm.selectExistTeam(); + expect(vm.loaderTitle).toEqual(""); + expect(angular.element).toHaveBeenCalledWith(".exist-team-card"); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Teams"); + expect($rootScope.notify).toHaveBeenCalledWith( + "error", + errorResponse.error + ); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + + it("to load data with pagination", function () { + challengeSuccess = true; + participantTeamChallengeSuccess = true; + participantTeamSuccess = true; + selectExistTeamSuccess = null; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = null; + + // get challenge details response + challengeSuccessResponse = { + is_active: true, + published: false, + enable_forum: true, + forum_url: "http://example.com", + cli_version: "evalai-cli version", + image: "logo.png", + }; + + // get details of challenges corresponding to participant teams response + participantTeamChallengeSuccessResponse = { + challenge_participant_team_list: { + first_object: { + challenge: { + id: 1, + title: "Challenge title", + description: "Challenge description", }, - { - next: null, - previous: null, + }, + }, + datetime_now: "timezone.now", + is_challenge_host: true, + }; + + // get participant team details response + participantTeamSuccessResponse = { + next: "page=4", + previous: "page=2", + results: { + is_public: false, + start_date: "Fri June 12 2018 22:41:51 GMT+0530", + end_date: "Fri June 12 2099 22:41:51 GMT+0530", + }, + }; + utilities.storeData("userKey", "encrypted key"); + + vm = createController(); + spyOn(vm, "startLoader"); + spyOn($http, "get").and.callFake(function () { + var deferred = $injector.get("$q").defer(); + return deferred.promise; + }); + var url = "participants/participant_team/page=2"; + vm.load(url); + expect(vm.isExistLoader).toBeTruthy(); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Teams"); + var headers = { + Authorization: "Token " + utilities.getData("userKey"), + }; + expect($http.get).toHaveBeenCalledWith(url, { headers: headers }); + }); + + it("backend error of the particular challenge `challenges/challenge//", function () { + challengeSuccess = false; + participantTeamChallengeSuccess = null; + participantTeamSuccess = null; + selectExistTeamSuccess = null; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = null; + + status = 404; + errorResponse = { + error: "email error", + }; + spyOn($state, "go"); + spyOn($rootScope, "notify"); + spyOn(utilities, "hideLoader"); + vm = createController(); + expect($rootScope.notify).toHaveBeenCalledWith( + "error", + errorResponse.error + ); + expect($state.go).toHaveBeenCalledWith("web.dashboard"); + expect(utilities.hideLoader).toHaveBeenCalled(); + }); + + it("get details of the particular challenge phase `challenges/challenge//challenge_phase`", function () { + challengeSuccess = null; + participantTeamChallengeSuccess = null; + participantTeamSuccess = null; + selectExistTeamSuccess = null; + challengePhaseSuccess = true; + challengePhaseSplitSuccess = null; + + // get challenge phase details response + successResponse = { + results: [ + { + is_public: false, + start_date: "Fri June 12 2018 22:41:51 GMT+0530", + end_date: "Fri June 12 2099 22:41:51 GMT+0530", + }, + ], + }; + spyOn(utilities, "hideLoader"); + vm = createController(); + expect(vm.phases).toEqual(successResponse); + var timezone = moment.tz.guess(); + for (var i = 0; i < successResponse.count; i++) { + expect(successResponse.results[i].is_public).toBeFalsy(); + expect(vm.phases.results[i].showPrivate).toBeTruthy(); + } + + for (var i = 0; i < successResponse.results.length; i++) { + var offset = new Date( + successResponse.results[i].start_date + ).getTimezoneOffset(); + expect(vm.phases.results[i].time_zone).toEqual( + moment.tz.zone(timezone).abbr(offset) + ); + } + expect(utilities.hideLoader).toHaveBeenCalled(); + }); + + it("backend error of particular challenge phase `challenges/challenge//challenge_phase`", function () { + challengeSuccess = null; + participantTeamChallengeSuccess = null; + participantTeamSuccess = null; + selectExistTeamSuccess = null; + challengePhaseSuccess = false; + challengePhaseSplitSuccess = null; + + status = 404; + errorResponse = { + detail: "error", + }; + spyOn(utilities, "storeData"); + spyOn($state, "go"); + spyOn(utilities, "hideLoader"); + vm = createController(); + expect(utilities.storeData).toHaveBeenCalledWith( + "emailError", + errorResponse.detail + ); + expect($state.go).toHaveBeenCalledWith("web.permission-denied"); + expect(utilities.hideLoader).toHaveBeenCalled(); + }); + + it("get details of the particular challenge phase split `challenges//challenge_phase_split`", function () { + challengeSuccess = null; + participantTeamChallengeSuccess = null; + participantTeamSuccess = null; + selectExistTeamSuccess = null; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = true; + + challengePhaseSplit = true; + // get challenge phase split details response + successResponse = [ + { + visibility: 2, + host: 1, + }, + ]; + var challengePhaseVisibility = { + owner_and_host: 1, + host: 2, + public: 3, + }; + spyOn(utilities, "hideLoader"); + vm.isParticipated = true; + vm = createController(); + expect(vm.phaseSplits).toEqual(successResponse); + for (var i = 0; i < successResponse.length; i++) { + if (successResponse[i].visibility != challengePhaseVisibility.public) { + expect(vm.phaseSplits[i].showPrivate).toBeTruthy(); + } + } + expect(utilities.hideLoader).toHaveBeenCalled(); + }); + + it("backend error of particular challenge phase split `challenges//challenge_phase_split`", function () { + challengeSuccess = null; + participantTeamChallengeSuccess = null; + participantTeamSuccess = null; + selectExistTeamSuccess = null; + challengePhaseSuccess = null; + challengePhaseSplitSuccess = false; + + status = 404; + errorResponse = { + detail: "error", + }; + spyOn(utilities, "storeData"); + spyOn($state, "go"); + spyOn(utilities, "hideLoader"); + vm = createController(); + expect(utilities.storeData).toHaveBeenCalledWith( + "emailError", + errorResponse.detail + ); + expect($state.go).toHaveBeenCalledWith("web.permission-denied"); + expect(utilities.hideLoader).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for displayDockerSubmissionInstructions function \ + `jobs//remaining_submissions`", function () { + var success, successResponse; + var errorResponse = { + detail: "Email Error", + }; + + beforeEach(function () { + spyOn(utilities, "hideLoader"); + spyOn(utilities, "storeData"); + spyOn($state, "go"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: successResponse, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("when submission limit exceeded", function () { + successResponse = { + phases: [ + { + id: 1, + limits: { + submission_limit_exceeded: true, + remaining_submissions_today_count: 12, + remaining_time: 12 / 12 / 12, }, - { - next: 'page=5', - previous: null, + }, + ], + }; + success = true; + vm.eligible_to_submit = true; + vm.displayDockerSubmissionInstructions(true, true); + expect(vm.phaseRemainingSubmissions).toEqual(successResponse); + var details = vm.phaseRemainingSubmissions.phases; + for (var i = 0; i < details.length; i++) { + expect(vm.phaseRemainingSubmissionsFlags[details[i].id]).toEqual( + "maxExceeded" + ); + } + expect(utilities.hideLoader).toHaveBeenCalled(); + }); + + it("when todays remaining submission count greater than 0", function () { + successResponse = { + phases: [ + { + id: 1, + limits: { + submission_limit_exceeded: false, + remaining_submissions_today_count: 12, + remaining_time: 12 / 12 / 12, }, - { - next: null, - previous: 'page=3', + }, + ], + }; + success = true; + vm.eligible_to_submit = true; + vm.displayDockerSubmissionInstructions(true, true); + expect(vm.phaseRemainingSubmissions).toEqual(successResponse); + var details = vm.phaseRemainingSubmissions.phases; + for (var i = 0; i < details.length; i++) { + expect(vm.phaseRemainingSubmissionsFlags[details[i].id]).toEqual( + "showSubmissionNumbers" + ); + } + expect(utilities.hideLoader).toHaveBeenCalled(); + }); + + it("when submission limit exceeded & todays remaining submission count less than 0", function () { + successResponse = { + phases: [ + { + id: 1, + limits: { + submission_limit_exceeded: false, + remaining_submissions_today_count: 0, + remaining_time: 12 / 12 / 12, }, - { - next: 'page=4', - previous: 'page=2', - } - ]; - - beforeEach(function () { - spyOn($interval, 'cancel'); - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - spyOn($rootScope, 'notify'); - spyOn($state, 'go'); - spyOn(utilities, 'storeData'); - - vm.challengeId = 1; - vm.phases = { - results: [ - { - id: 1, - name: "Challenge phase name", - description: "Challenge phase description", - leaderboard_public: true - }, - ] - }; - - utilities.sendRequest = function (parameters) { - if ((submissionCountSuccess == true && parameters.url == "analytics/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/count") || - (submissionListSuccess == true && parameters.url == "jobs/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/submission/")) { - parameters.callback.onSuccess({ - data: successResponse - }); - } else if ((submissionCountSuccess == false && parameters.url == "analytics/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/count") || - (submissionListSuccess == false && parameters.url == "jobs/challenge/" + vm.challengeId + "/challenge_phase/" + vm.phaseId + "/submission/")) { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('get the leaderboard of the current phase', function () { - submissionCountSuccess = null; - submissionListSuccess = null; - var phaseId = 1; - vm.getResults(phaseId); - vm.stopFetchingSubmissions(); - expect($interval.cancel).toHaveBeenCalled(); - expect(vm.isResult).toEqual(true); - expect(vm.phaseId).toEqual(phaseId); - - expect(vm.currentPhaseLeaderboardPublic).toEqual(true); - }); - - it('get the submission count \ - `analytics/challenge//challenge_phase//count`', function () { - submissionCountSuccess = true; - submissionListSuccess = null; - var phaseId = 1; - successResponse = { - challenge_phase: 1, - participant_team_submission_count: 200 - }; - vm.getResults(phaseId); - expect(vm.submissionCount).toEqual(successResponse.participant_team_submission_count); - }); - - it('backend error on getting submission count \ - `analytics/challenge//challenge_phase//count`', function () { - submissionCountSuccess = false; - submissionListSuccess = null; - var phaseId = 1; - errorResponse = 'error'; - vm.getResults(phaseId); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - }); - - submission_list.forEach(response => { - it('get submissions of a particular challenge phase when pagination next is ' + response.next + ' \ - and previous is ' + response.previous + '`jobs/challenge//challenge_phase//submission/`', function () { - submissionListSuccess = true; - var phaseId = 1; - successResponse = response; - successResponse.results = [ - { - id: 1, - participant_team: "Participant team", - challenge_phase: "Challenge phase", - is_public: true, - is_baseline: true - } - ]; - - vm.getResults(phaseId); - expect(vm.isExistLoader).toBeTruthy(); - expect(vm.startLoader).toHaveBeenCalledWith("Loading Submissions"); - for (var i = 0; i < successResponse.results.length; i++) { - expect(vm.submissionVisibility[successResponse.results[i].id]).toEqual(successResponse.results[i].is_public); - expect(vm.baselineStatus[successResponse.results[i].id] = successResponse.results[i].is_baseline); - } - expect(vm.submissionResult).toEqual(successResponse); - - if (vm.submissionResult.next == null) { - expect(vm.isNext).toEqual('disabled'); - expect(vm.currentPage).toEqual(1); - } else { - expect(vm.isNext).toEqual(''); - expect(vm.currentPage).toEqual(vm.submissionResult.next.split('page=')[1] - 1); - } - - if (vm.submissionResult.previous == null) { - expect(vm.isPrev).toEqual('disabled'); - } else { - expect(vm.isPrev).toEqual(''); - } - - if (vm.submissionResult.next !== null) { - expect(vm.currentPage).toEqual(vm.submissionResult.next.split('page=')[1] - 1); - } else { - expect(vm.currentPage).toEqual(1); - } - }); - }); - - it('backend error on getting submissions of a particular challenge \ - `jobs/challenge//challenge_phase//submission/`', function () { - submissionListSuccess = false; - submissionCountSuccess = null; - var phaseId = 1; - errorResponse = { - detail: 'error' - }; - vm.getResults(phaseId); - expect(utilities.storeData).toHaveBeenCalledWith("emailError", errorResponse.detail); - expect($state.go).toHaveBeenCalledWith('web.permission-denied'); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - - it('to load data with pagination `load` function', function () { - submissionListSuccess = true; - submissionCountSuccess = null; - var phaseId = 1; - successResponse = { - results: [ - { - id: 1, - participant_team: "Participant team", - challenge_phase: "Challenge phase", - is_public: true, - is_baseline: true - } - ], - // get submissions response - next: "page=4", - previous: "page=2", - }; - vm.getResults(phaseId); - spyOn($http, 'get').and.callFake(function () { - var deferred = $injector.get('$q').defer(); - return deferred.promise; - }); - var url = 'challenge//submission/page=2'; - vm.load(url); - expect(vm.isExistLoader).toBeTruthy(); - expect(vm.startLoader).toHaveBeenCalledWith("Loading Submissions"); - var headers = { - 'Authorization': "Token " + utilities.getData('userKey') - }; - expect($http.get).toHaveBeenCalledWith(url, { headers: headers }); - }); - }); - - describe('vm.toggleDisablePrivateSubmission(ev)', function () { - var ev, confirmDeferred; - - beforeEach(inject(function ($q) { - // Prevent ANY real HTTP from utilities.sendRequest during controller init - spyOn(utilities, 'sendRequest').and.callFake(function () {}); - - // fake click event - ev = { stopPropagation: jasmine.createSpy('stopPropagation') }; - - // stub out the confirm dialog - confirmDeferred = $q.defer(); - spyOn($mdDialog, 'confirm').and.callThrough(); - spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - // set up page IDs for URL building - vm.page = { creator: { id: 321 }, id: 654 }; - })); - - it('on cancel (reject) should just stopPropagation and set toggleSubmissionState', function () { - utilities.sendRequest.calls.reset(); // start fresh for this test - - vm.disable_private_submission = false; - vm.toggleDisablePrivateSubmission(ev); - - expect(ev.stopPropagation).toHaveBeenCalled(); - expect(vm.toggleSubmissionState).toBe('disabled'); - expect($mdDialog.show).toHaveBeenCalled(); - - // simulate user clicking “No” - confirmDeferred.reject(); - $rootScope.$apply(); - - expect(utilities.sendRequest).not.toHaveBeenCalled(); // no PATCH - expect($rootScope.notify).not.toHaveBeenCalled(); - }); - - it('on OK + success should PATCH, toggle flag, hide dialog and notify success', function () { - // fresh spy state - utilities.sendRequest.calls.reset(); - utilities.sendRequest.and.callFake(function (params) { - // flag flips before callback - expect(vm.disable_private_submission).toBe(true); - expect(params.url) - .toBe('challenges/challenge_host_team/321/challenge/654'); - expect(params.method).toBe('PATCH'); - expect(params.data) - .toEqual({ disable_private_submission: true }); - - // simulate success - params.callback.onSuccess({ status: 200, data: {} }); + }, + ], + }; + success = true; + vm.eligible_to_submit = true; + vm.displayDockerSubmissionInstructions(true, true); + expect(vm.phaseRemainingSubmissions).toEqual(successResponse); + var details = vm.phaseRemainingSubmissions.phases; + for (var i = 0; i < details.length; i++) { + expect(vm.eachPhase).toEqual(details[i]); + expect(vm.phaseRemainingSubmissionsFlags[details[i].id]).toEqual( + "showClock" + ); + + // Unit tests for `countDownTimer` function + expect(vm.remainingTime).toEqual(vm.eachPhase.limits.remaining_time); + expect(vm.days).toEqual(Math.floor(vm.remainingTime / 24 / 60 / 60)); + expect(vm.hoursLeft).toEqual( + Math.floor(vm.remainingTime - vm.days * 86400) + ); + expect(vm.hours).toEqual(Math.floor(vm.hoursLeft / 3600)); + expect(vm.minutesLeft).toEqual( + Math.floor(vm.hoursLeft - vm.hours * 3600) + ); + expect(vm.minutes).toEqual(Math.floor(vm.minutesLeft / 60)); + if (vm.remainingTime === 0) { + expect(vm.phaseRemainingSubmissionsFlags[vm.eachPhase.id]).toEqual( + "showSubmissionNumbers" + ); + } + } + expect(utilities.hideLoader).toHaveBeenCalled(); + }); + + it("backend error", function () { + success = false; + vm.eligible_to_submit = true; + vm.displayDockerSubmissionInstructions(true, true); + expect(utilities.storeData).toHaveBeenCalledWith( + "emailError", + errorResponse.detail + ); + expect($state.go).toHaveBeenCalledWith("web.permission-denied"); + }); + }); + + describe("Unit tests for makeSubmission function \ + `jobs/challenge//challenge_phase//submission/`", function () { + var success, status; + var errorResponse = { + error: "error", + }; + var successResponse = { + success: "success", + }; + + beforeEach(function () { + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 200, + data: successResponse, }); - - vm.disable_private_submission = false; - vm.toggleDisablePrivateSubmission(ev); - expect(vm.toggleSubmissionState).toBe('disabled'); - - // simulate user clicking “Yes” - confirmDeferred.resolve(); - $rootScope.$apply(); - - expect(utilities.sendRequest).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify) - .toHaveBeenCalledWith('success', - 'Private submissions were successfully made disabled'); - }); - - it('on OK + error should hide dialog and notify error', function () { - // fresh spy state - utilities.sendRequest.calls.reset(); - utilities.sendRequest.and.callFake(function (params) { - // flag flips before error callback - expect(vm.disable_private_submission).toBe(false); - params.callback.onError({ data: 'oops!' }); + } else { + parameters.callback.onError({ + status: status, + data: errorResponse, }); - - vm.disable_private_submission = true; - vm.toggleDisablePrivateSubmission(ev); - expect(vm.toggleSubmissionState).toBe('allowed'); - - confirmDeferred.resolve(); - $rootScope.$apply(); - - expect(utilities.sendRequest).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith('error', 'oops!'); - }); + } + }; + }); + + it("succesfully submission", function () { + success = true; + vm.isParticipated = true; + vm.eligible_to_submit = true; + vm.makeSubmission(); + expect(vm.startLoader).toHaveBeenCalledWith("Making Submission"); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "Your submission has been recorded succesfully!" + ); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + + it("forces isPublicSubmission=true when disable_private_submission is true", function () { + success = true; + vm.isParticipated = true; + vm.eligible_to_submit = true; + vm.disable_private_submission = true; + // simulate file‐upload path so it doesn’t short‐circuit on URL logic + vm.isSubmissionUsingUrl = false; + vm.input_file = new Blob(["x"], { type: "text/plain" }); + vm.currentPhaseAllowedSubmissionFileTypes = ["txt"]; + + // capture the outgoing FormData + var originalSend = utilities.sendRequest; + var captured; + spyOn(window, "FormData").and.callFake(function () { + var f = { store: {} }; + f.append = function (k, v) { + f.store[k] = v; + }; + return f; }); + utilities.sendRequest = function (params) { + captured = params.data.store; + // call success path so post‐submission cleanup happens + params.callback.onSuccess({ status: 200, data: successResponse }); + }; - describe('Unit tests for refreshSubmissionData function \ - `jobs/challenge//challenge_phase//submission/?page=`', function () { - var success, successResponse; - var errorResponse = 'error'; - var results = [ - { - id: 1, - is_public: true, - is_baseline: true - }, - { - id: 2, - is_public: false, - is_baseline: true - } - ]; - var paginated_list = [ - { - count: 0, - next: null, - previous: null, - results: results - }, - { - count: 2, - next: null, - previous: null, - results: results - }, - { - count: 30, - next: 'page=5', - previous: null, - results: results - }, - { - count: 30, - next: null, - previous: 'page=3', - results: results - }, - { - count: 30, - next: 'page=4', - previous: 'page=2', - results: results - } - ]; - - beforeEach(function () { - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: successResponse - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - paginated_list.forEach(response => { - it('submission data list have count' + response.count + ', next ' + response.next + 'and previous ' + response.previous, function () { - success = true; - successResponse = response; - vm.refreshSubmissionData(); - expect(vm.isResult).toEqual(false); - expect(vm.startLoader).toHaveBeenCalledWith("Loading Submissions"); - expect(vm.submissionResult).toEqual(successResponse); - - if (response.count == 0) { - expect(vm.showPagination).toEqual(false); - expect(vm.paginationMsg).toEqual("No results found"); - } else { - expect(vm.showPagination).toEqual(true); - expect(vm.paginationMsg).toEqual(""); - } - - if (response.next == null) { - expect(vm.isNext).toEqual('disabled'); - } else { - expect(vm.isNext).toEqual(''); - } - - if (response.previous == null) { - expect(vm.isPrev).toEqual('disabled'); - } else { - expect(vm.isPrev).toEqual(''); - } - - if (response.next != null) { - expect(vm.currentPage).toEqual(response.next.split('page=')[1] - 1); - } else { - expect(vm.currentPage).toEqual(1); - } - - for (var i = 0; i < response.results.length; i++) { - expect(vm.submissionVisibility[response.results[i].id]).toEqual(response.results[i].is_public); - expect(vm.baselineStatus[response.results[i].id]).toEqual(response.results[i].is_baseline); - } - - expect(vm.showUpdate).toEqual(false); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - }); - - it('backend error', function () { - success = false; - vm.refreshSubmissionData(); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for refreshLeaderboard function \ - `jobs/challenge_phase_split//leaderboard/?page_size=1000`', function () { - var success, successResponse, errorResponse; - - beforeEach(function () { - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: successResponse - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('on success of the function', function () { - success = true; - successResponse = { - results: 'success' - }; - vm.refreshLeaderboard(); - expect(vm.startLoader).toHaveBeenCalledWith("Loading Leaderboard Items"); - expect(vm.leaderboard).toEqual(successResponse.results); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - - it('backend error', function () { - success = false; - errorResponse = 'error'; - vm.refreshLeaderboard(); - expect(vm.startLoader).toHaveBeenCalledWith("Loading Leaderboard Items"); - expect(vm.leaderboard.error).toEqual(errorResponse); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for createNewTeam function \ - `participants/participant_team`', function () { - var success, successResponse; - var errorResponse = { - team_name: ['error'] + vm.makeSubmission(); + + expect(captured.is_public).toBe(true, "should set is_public to true"); + + // restore + utilities.sendRequest = originalSend; + }); + + it("respects vm.isPublicSubmission when disable_private_submission is false", function () { + success = true; + vm.isParticipated = true; + vm.eligible_to_submit = true; + vm.disable_private_submission = false; + vm.isPublicSubmission = false; // developer explicitly said “private” + vm.isSubmissionUsingUrl = false; + vm.input_file = new Blob(["x"], { type: "text/plain" }); + vm.currentPhaseAllowedSubmissionFileTypes = ["txt"]; + + var originalSend = utilities.sendRequest; + var captured; + spyOn(window, "FormData").and.callFake(function () { + var f = { store: {} }; + f.append = function (k, v) { + f.store[k] = v; }; - var team_name_list = [ - { - next: null, - previous: null, - }, - { - next: null, - previous: null, - }, - { - next: 'page=5', - previous: null, - }, + return f; + }); + utilities.sendRequest = function (params) { + captured = params.data.store; + params.callback.onSuccess({ status: 200, data: successResponse }); + }; + + vm.makeSubmission(); + + expect(captured.is_public).toBe( + false, + "should use the vm.isPublicSubmission value" + ); + + utilities.sendRequest = originalSend; + }); + + it("404 backend error", function () { + success = false; + status = 404; + vm.isParticipated = true; + vm.isSubmissionUsingUrl = false; + vm.eligible_to_submit = true; + vm.makeSubmission(); + vm.fileVal = "submission.zip"; + expect(vm.phaseId).toEqual(null); + expect(vm.methodName).toEqual(null); + expect(vm.methodDesc).toEqual(null); + expect(vm.projectUrl).toEqual(null); + expect(vm.publicationUrl).toEqual(null); + expect(vm.subErrors.msg).toEqual("Please select phase!"); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + + it("other backend error", function () { + success = false; + status = 403; + vm.isParticipated = true; + vm.isSubmissionUsingUrl = false; + vm.eligible_to_submit = true; + vm.makeSubmission("submission.zip"); + expect(vm.phaseId).toEqual(null); + expect(vm.methodName).toEqual(null); + expect(vm.methodDesc).toEqual(null); + expect(vm.projectUrl).toEqual(null); + expect(vm.publicationUrl).toEqual(null); + expect(vm.subErrors.msg).toEqual(errorResponse.error); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for sortFunction function", function () { + it("sort column by is known", function () { + var return_value = vm.sortFunction({}); + expect(return_value).not.toEqual(0); + }); + + it("sort column by is not known", function () { + vm.sortColumn = "notKnown"; + var return_value = vm.sortFunction({}); + expect(return_value).toEqual(0); + }); + }); + + describe("Unit tests for sortLeaderboard function", function () { + it("column index is null and passed `column` not same with the current `sortColumn`", function () { + vm.sortLeaderboard(vm, "number", null); + expect(vm.reverseSort).toEqual(false); + }); + + it("column index is null and passed `column` same with the current `sortColumn`", function () { + vm.sortLeaderboard(vm, "rank", null); + expect(vm.reverseSort).toEqual(true); + }); + + it("column index is not null and passed `column` and `index` not same with the current `sortColumn` and `columnIndexSort`", function () { + vm.sortLeaderboard(vm, "number", 1); + expect(vm.reverseSort).toEqual(false); + expect(vm.columnIndexSort).toEqual(1); + }); + + it("column index is not null and passed `column` and `index` same with the current `sortColumn` and `columnIndexSort`", function () { + vm.sortLeaderboard(vm, "rank", 0); + expect(vm.reverseSort).toEqual(true); + expect(vm.columnIndexSort).toEqual(0); + }); + }); + + describe("Unit tests for getLeaderboard function \ + `jobs/challenge_phase_split//leaderboard/?page_size=1000`", function () { + var success, successResponse, errorResponse; + + beforeEach(function () { + spyOn($interval, "cancel"); + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + spyOn(vm, "startLeaderboard"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: successResponse, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("successfully get the leaderboard", function () { + success = true; + successResponse = { + results: { + duration: "year", + results: [ { - next: null, - previous: 'page=3', + id: 1, + leaderboard__schema: { + labels: ["label1", "label2"], + default_order_by: "default_order_by", + }, + submission__submitted_at: + new Date() - + new Date().setFullYear(new Date().getFullYear() - 1), }, + ], + }, + }; + var phaseSplitId = 1; + vm.getLeaderboard(phaseSplitId); + vm.stopLeaderboard(); + expect($interval.cancel).toHaveBeenCalled(); + expect(vm.isResult).toEqual(true); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Leaderboard Items"); + expect(vm.leaderboard).toEqual(successResponse.results); + + expect(vm.phaseName).toEqual(vm.phaseSplitId); + expect(vm.startLeaderboard).toHaveBeenCalled(); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + + it("backend error", function () { + success = false; + errorResponse = "error"; + var phaseSplitId = 1; + vm.getLeaderboard(phaseSplitId); + vm.stopLeaderboard(); + expect($interval.cancel).toHaveBeenCalled(); + expect(vm.isResult).toEqual(true); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Leaderboard Items"); + expect(vm.leaderboard.error).toEqual(errorResponse); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for getResults function", function () { + var submissionCountSuccess, + submissionListSuccess, + successResponse, + errorResponse; + var submission_list = [ + { + next: null, + previous: null, + }, + { + next: null, + previous: null, + }, + { + next: "page=5", + previous: null, + }, + { + next: null, + previous: "page=3", + }, + { + next: "page=4", + previous: "page=2", + }, + ]; + + beforeEach(function () { + spyOn($interval, "cancel"); + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + spyOn($rootScope, "notify"); + spyOn($state, "go"); + spyOn(utilities, "storeData"); + + vm.challengeId = 1; + vm.phases = { + results: [ + { + id: 1, + name: "Challenge phase name", + description: "Challenge phase description", + leaderboard_public: true, + }, + ], + }; + + utilities.sendRequest = function (parameters) { + if ( + (submissionCountSuccess == true && + parameters.url == + "analytics/challenge/" + + vm.challengeId + + "/challenge_phase/" + + vm.phaseId + + "/count") || + (submissionListSuccess == true && + parameters.url == + "jobs/challenge/" + + vm.challengeId + + "/challenge_phase/" + + vm.phaseId + + "/submission/") + ) { + parameters.callback.onSuccess({ + data: successResponse, + }); + } else if ( + (submissionCountSuccess == false && + parameters.url == + "analytics/challenge/" + + vm.challengeId + + "/challenge_phase/" + + vm.phaseId + + "/count") || + (submissionListSuccess == false && + parameters.url == + "jobs/challenge/" + + vm.challengeId + + "/challenge_phase/" + + vm.phaseId + + "/submission/") + ) { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("get the leaderboard of the current phase", function () { + submissionCountSuccess = null; + submissionListSuccess = null; + var phaseId = 1; + vm.getResults(phaseId); + vm.stopFetchingSubmissions(); + expect($interval.cancel).toHaveBeenCalled(); + expect(vm.isResult).toEqual(true); + expect(vm.phaseId).toEqual(phaseId); + + expect(vm.currentPhaseLeaderboardPublic).toEqual(true); + }); + + it("get the submission count \ + `analytics/challenge//challenge_phase//count`", function () { + submissionCountSuccess = true; + submissionListSuccess = null; + var phaseId = 1; + successResponse = { + challenge_phase: 1, + participant_team_submission_count: 200, + }; + vm.getResults(phaseId); + expect(vm.submissionCount).toEqual( + successResponse.participant_team_submission_count + ); + }); + + it("backend error on getting submission count \ + `analytics/challenge//challenge_phase//count`", function () { + submissionCountSuccess = false; + submissionListSuccess = null; + var phaseId = 1; + errorResponse = "error"; + vm.getResults(phaseId); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + + submission_list.forEach((response) => { + it( + "get submissions of a particular challenge phase when pagination next is " + + response.next + + " \ + and previous is " + + response.previous + + "`jobs/challenge//challenge_phase//submission/`", + function () { + submissionListSuccess = true; + var phaseId = 1; + successResponse = response; + successResponse.results = [ { - next: 'page=4', - previous: 'page=2', - } - ]; - - beforeEach(function () { - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - spyOn(angular, 'element'); - spyOn($rootScope, 'notify'); - vm.team.name = "team_name"; - vm.team.url = "team_url"; - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: successResponse, - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - team_name_list.forEach(response => { - it('when pagination next is ' + response.next + 'and previous is ' + response.previous, function () { - success = true; - successResponse = response; - vm.createNewTeam(); - expect(vm.isLoader).toEqual(true); - expect(vm.loaderTitle).toEqual(''); - expect(angular.element).toHaveBeenCalledWith('.new-team-card'); - expect(vm.startLoader("Loading Teams")); - expect($rootScope.notify).toHaveBeenCalled(); - expect(vm.stopLoader).toHaveBeenCalled(); - expect(vm.team).toEqual({}); - - expect(vm.startLoader).toHaveBeenCalledWith("Loading Teams"); - expect(vm.existTeam).toEqual(successResponse); - expect(vm.showPagination).toEqual(true); - expect(vm.paginationMsg).toEqual(''); - - if (vm.existTeam.next == null) { - expect(vm.isNext).toEqual('disabled'); - expect(vm.currentPage).toEqual(1); - } else { - expect(vm.isNext).toEqual(''); - expect(vm.currentPage).toEqual(vm.existTeam.next.split('page=')[1] - 1); - } - - if (vm.existTeam.previous == null) { - expect(vm.isPrev).toEqual('disabled'); - } else { - expect(vm.isPrev).toEqual(''); - } - - expect(vm.stopLoader).toHaveBeenCalled(); - }); - }); - - it('backend error', function () { - success = false; - vm.createNewTeam(); - expect(vm.isLoader).toEqual(true); - expect(vm.loaderTitle).toEqual(''); - expect(angular.element).toHaveBeenCalledWith('.new-team-card'); - expect(vm.startLoader("Loading Teams")); - expect(vm.team.error).toEqual(errorResponse.team_name[0]); - expect(vm.stopLoader).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("error", "New team couldn't be created."); - }); - }); - - describe('Unit tests for getAllSubmissionResults function \ - `challenges//challenge_phase//submissions`', function () { - var success, successResponse; - var errorResponse = { - detail: 'error' - }; - var submission_list = [ - { - count: 0, - next: null, - previous: null, + id: 1, + participant_team: "Participant team", + challenge_phase: "Challenge phase", + is_public: true, + is_baseline: true, }, - { - count: 2, - next: null, - previous: null, + ]; + + vm.getResults(phaseId); + expect(vm.isExistLoader).toBeTruthy(); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Submissions"); + for (var i = 0; i < successResponse.results.length; i++) { + expect( + vm.submissionVisibility[successResponse.results[i].id] + ).toEqual(successResponse.results[i].is_public); + expect( + (vm.baselineStatus[successResponse.results[i].id] = + successResponse.results[i].is_baseline) + ); + } + expect(vm.submissionResult).toEqual(successResponse); + + if (vm.submissionResult.next == null) { + expect(vm.isNext).toEqual("disabled"); + expect(vm.currentPage).toEqual(1); + } else { + expect(vm.isNext).toEqual(""); + expect(vm.currentPage).toEqual( + vm.submissionResult.next.split("page=")[1] - 1 + ); + } + + if (vm.submissionResult.previous == null) { + expect(vm.isPrev).toEqual("disabled"); + } else { + expect(vm.isPrev).toEqual(""); + } + + if (vm.submissionResult.next !== null) { + expect(vm.currentPage).toEqual( + vm.submissionResult.next.split("page=")[1] - 1 + ); + } else { + expect(vm.currentPage).toEqual(1); + } + } + ); + }); + + it("backend error on getting submissions of a particular challenge \ + `jobs/challenge//challenge_phase//submission/`", function () { + submissionListSuccess = false; + submissionCountSuccess = null; + var phaseId = 1; + errorResponse = { + detail: "error", + }; + vm.getResults(phaseId); + expect(utilities.storeData).toHaveBeenCalledWith( + "emailError", + errorResponse.detail + ); + expect($state.go).toHaveBeenCalledWith("web.permission-denied"); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + + it("to load data with pagination `load` function", function () { + submissionListSuccess = true; + submissionCountSuccess = null; + var phaseId = 1; + successResponse = { + results: [ + { + id: 1, + participant_team: "Participant team", + challenge_phase: "Challenge phase", + is_public: true, + is_baseline: true, + }, + ], + // get submissions response + next: "page=4", + previous: "page=2", + }; + vm.getResults(phaseId); + spyOn($http, "get").and.callFake(function () { + var deferred = $injector.get("$q").defer(); + return deferred.promise; + }); + var url = "challenge//submission/page=2"; + vm.load(url); + expect(vm.isExistLoader).toBeTruthy(); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Submissions"); + var headers = { + Authorization: "Token " + utilities.getData("userKey"), + }; + expect($http.get).toHaveBeenCalledWith(url, { headers: headers }); + }); + }); + + describe("vm.toggleDisablePrivateSubmission(ev)", function () { + var $rootScope, + $controller, + $q, + $httpBackend, + $mdDialog, + utilities, + vm, + ev, + confirmDeferred; + + beforeEach(angular.mock.module("evalai")); + + /* ---- suite-level beforeEach: create ctrl, stub HTTP, basic spies ---- */ + beforeEach(inject(function ( + _$rootScope_, + _$controller_, + _$q_, + _$httpBackend_, + _$mdDialog_, + _utilities_ + ) { + $rootScope = _$rootScope_; + $controller = _$controller_; + $q = _$q_; + $httpBackend = _$httpBackend_; + $mdDialog = _$mdDialog_; + utilities = _utilities_; + + /* 1️⃣ swallow the GET /accounts/user/get_auth_token fired at ctrl init */ + $httpBackend + .whenGET(/accounts\/user\/get_auth_token/) + .respond(200, { token: "dummy" }); + + /* 2️⃣ spy on sendRequest ONCE for the whole suite */ + spyOn(utilities, "sendRequest").and.callFake(function () { + /* noop by default */ + }); + + /* instantiate controller */ + var $scope = $rootScope.$new(); + vm = $controller("ChallengeCtrl", { $scope: $scope }); + + /* flush that initial GET */ + $httpBackend.flush(); + + /* generic stubs for $mdDialog */ + spyOn($mdDialog, "confirm").and.callThrough(); // keep original, only spy + spyOn($mdDialog, "show").and.callFake(function () { + return confirmDeferred.promise; + }); + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + /* fake click-event for every spec */ + ev = { stopPropagation: jasmine.createSpy("stopPropagation") }; + + /* static page IDs used in URL-building */ + vm.page = { creator: { id: 321 }, id: 654 }; + })); + + /* ---- reset spy-call history and build fresh deferred before every spec ---- */ + beforeEach(function () { + utilities.sendRequest.calls.reset(); + confirmDeferred = $q.defer(); + /* make $mdDialog.show return the NEW promise */ + $mdDialog.show.and.returnValue(confirmDeferred.promise); + ev.stopPropagation.calls.reset(); + $mdDialog.hide.calls.reset(); + $rootScope.notify.calls.reset(); + }); + + /* ---- afterEach: make sure we didn’t leave pending requests ---- */ + afterEach(function () { + $httpBackend.verifyNoOutstandingExpectation(); + $httpBackend.verifyNoOutstandingRequest(); + }); + + /* --------------------------------------------------------------------- */ + it("on cancel (reject) only stops propagation and sets toggleSubmissionState", function () { + vm.disable_private_submission = false; + vm.toggleDisablePrivateSubmission(ev); + + expect(ev.stopPropagation).toHaveBeenCalled(); + expect(vm.toggleSubmissionState).toBe("disabled"); + expect($mdDialog.show).toHaveBeenCalled(); + + /* simulate “No” click */ + confirmDeferred.reject(); + $rootScope.$apply(); + + expect(utilities.sendRequest).not.toHaveBeenCalled(); + expect($rootScope.notify).not.toHaveBeenCalled(); + }); + + /* --------------------------------------------------------------------- */ + it("on OK + success sends PATCH, flips flag, hides dialog, notifies success", function () { + /* override sendRequest for this spec */ + utilities.sendRequest.and.callFake(function (params) { + /* flag should already be toggled */ + expect(vm.disable_private_submission).toBe(true); + expect(params.url).toBe( + "challenges/challenge_host_team/321/challenge/654" + ); + expect(params.method).toBe("PATCH"); + expect(params.data).toEqual({ disable_private_submission: true }); + + /* pretend backend success */ + params.callback.onSuccess({ status: 200, data: {} }); + }); + + vm.disable_private_submission = false; + vm.toggleDisablePrivateSubmission(ev); + expect(vm.toggleSubmissionState).toBe("disabled"); + + confirmDeferred.resolve(); // user clicks “Yes” + $rootScope.$apply(); + + expect(utilities.sendRequest).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "Private submissions were successfully made disabled" + ); + }); + + /* --------------------------------------------------------------------- */ + it("on OK + error hides dialog and notifies error", function () { + utilities.sendRequest.and.callFake(function (params) { + /* flag flipped back before error */ + expect(vm.disable_private_submission).toBe(false); + params.callback.onError({ data: "oops!" }); + }); + + vm.disable_private_submission = true; + vm.toggleDisablePrivateSubmission(ev); + expect(vm.toggleSubmissionState).toBe("allowed"); + + confirmDeferred.resolve(); // user clicks “Yes” + $rootScope.$apply(); + + expect(utilities.sendRequest).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith("error", "oops!"); + }); + }); + + describe("Unit tests for refreshSubmissionData function \ + `jobs/challenge//challenge_phase//submission/?page=`", function () { + var success, successResponse; + var errorResponse = "error"; + var results = [ + { + id: 1, + is_public: true, + is_baseline: true, + }, + { + id: 2, + is_public: false, + is_baseline: true, + }, + ]; + var paginated_list = [ + { + count: 0, + next: null, + previous: null, + results: results, + }, + { + count: 2, + next: null, + previous: null, + results: results, + }, + { + count: 30, + next: "page=5", + previous: null, + results: results, + }, + { + count: 30, + next: null, + previous: "page=3", + results: results, + }, + { + count: 30, + next: "page=4", + previous: "page=2", + results: results, + }, + ]; + + beforeEach(function () { + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: successResponse, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + paginated_list.forEach((response) => { + it( + "submission data list have count" + + response.count + + ", next " + + response.next + + "and previous " + + response.previous, + function () { + success = true; + successResponse = response; + vm.refreshSubmissionData(); + expect(vm.isResult).toEqual(false); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Submissions"); + expect(vm.submissionResult).toEqual(successResponse); + + if (response.count == 0) { + expect(vm.showPagination).toEqual(false); + expect(vm.paginationMsg).toEqual("No results found"); + } else { + expect(vm.showPagination).toEqual(true); + expect(vm.paginationMsg).toEqual(""); + } + + if (response.next == null) { + expect(vm.isNext).toEqual("disabled"); + } else { + expect(vm.isNext).toEqual(""); + } + + if (response.previous == null) { + expect(vm.isPrev).toEqual("disabled"); + } else { + expect(vm.isPrev).toEqual(""); + } + + if (response.next != null) { + expect(vm.currentPage).toEqual(response.next.split("page=")[1] - 1); + } else { + expect(vm.currentPage).toEqual(1); + } + + for (var i = 0; i < response.results.length; i++) { + expect(vm.submissionVisibility[response.results[i].id]).toEqual( + response.results[i].is_public + ); + expect(vm.baselineStatus[response.results[i].id]).toEqual( + response.results[i].is_baseline + ); + } + + expect(vm.showUpdate).toEqual(false); + expect(vm.stopLoader).toHaveBeenCalled(); + } + ); + }); + + it("backend error", function () { + success = false; + vm.refreshSubmissionData(); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for refreshLeaderboard function \ + `jobs/challenge_phase_split//leaderboard/?page_size=1000`", function () { + var success, successResponse, errorResponse; + + beforeEach(function () { + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: successResponse, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("on success of the function", function () { + success = true; + successResponse = { + results: "success", + }; + vm.refreshLeaderboard(); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Leaderboard Items"); + expect(vm.leaderboard).toEqual(successResponse.results); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + + it("backend error", function () { + success = false; + errorResponse = "error"; + vm.refreshLeaderboard(); + expect(vm.startLoader).toHaveBeenCalledWith("Loading Leaderboard Items"); + expect(vm.leaderboard.error).toEqual(errorResponse); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for createNewTeam function \ + `participants/participant_team`", function () { + var success, successResponse; + var errorResponse = { + team_name: ["error"], + }; + var team_name_list = [ + { + next: null, + previous: null, + }, + { + next: null, + previous: null, + }, + { + next: "page=5", + previous: null, + }, + { + next: null, + previous: "page=3", + }, + { + next: "page=4", + previous: "page=2", + }, + ]; + + beforeEach(function () { + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + spyOn(angular, "element"); + spyOn($rootScope, "notify"); + vm.team.name = "team_name"; + vm.team.url = "team_url"; + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: successResponse, + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + team_name_list.forEach((response) => { + it( + "when pagination next is " + + response.next + + "and previous is " + + response.previous, + function () { + success = true; + successResponse = response; + vm.createNewTeam(); + expect(vm.isLoader).toEqual(true); + expect(vm.loaderTitle).toEqual(""); + expect(angular.element).toHaveBeenCalledWith(".new-team-card"); + expect(vm.startLoader("Loading Teams")); + expect($rootScope.notify).toHaveBeenCalled(); + expect(vm.stopLoader).toHaveBeenCalled(); + expect(vm.team).toEqual({}); + + expect(vm.startLoader).toHaveBeenCalledWith("Loading Teams"); + expect(vm.existTeam).toEqual(successResponse); + expect(vm.showPagination).toEqual(true); + expect(vm.paginationMsg).toEqual(""); + + if (vm.existTeam.next == null) { + expect(vm.isNext).toEqual("disabled"); + expect(vm.currentPage).toEqual(1); + } else { + expect(vm.isNext).toEqual(""); + expect(vm.currentPage).toEqual( + vm.existTeam.next.split("page=")[1] - 1 + ); + } + + if (vm.existTeam.previous == null) { + expect(vm.isPrev).toEqual("disabled"); + } else { + expect(vm.isPrev).toEqual(""); + } + + expect(vm.stopLoader).toHaveBeenCalled(); + } + ); + }); + + it("backend error", function () { + success = false; + vm.createNewTeam(); + expect(vm.isLoader).toEqual(true); + expect(vm.loaderTitle).toEqual(""); + expect(angular.element).toHaveBeenCalledWith(".new-team-card"); + expect(vm.startLoader("Loading Teams")); + expect(vm.team.error).toEqual(errorResponse.team_name[0]); + expect(vm.stopLoader).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "error", + "New team couldn't be created." + ); + }); + }); + + describe("Unit tests for getAllSubmissionResults function \ + `challenges//challenge_phase//submissions`", function () { + var success, successResponse; + var errorResponse = { + detail: "error", + }; + var submission_list = [ + { + count: 0, + next: null, + previous: null, + }, + { + count: 2, + next: null, + previous: null, + }, + { + count: 30, + next: "page=5", + previous: null, + }, + { + count: 30, + next: null, + previous: "page=3", + }, + { + count: 30, + next: "page=4", + previous: "page=2", + }, + ]; + + beforeEach(function () { + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + spyOn(angular, "element"); + spyOn($interval, "cancel"); + spyOn(utilities, "storeData"); + spyOn($state, "go"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: successResponse, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + submission_list.forEach((response) => { + it( + "submission list have count" + + response.count + + ", next " + + response.next + + "and previous " + + response.previous, + function () { + success = true; + successResponse = response; + var phaseId = 1; + vm.getAllSubmissionResults(phaseId); + vm.stopFetchingSubmissions(); + expect($interval.cancel).toHaveBeenCalled(); + expect(vm.isResult).toEqual(true); + expect(vm.phaseId).toEqual(phaseId); + + expect(vm.submissionResult).toEqual(response); + if (vm.submissionResult.count == 0) { + expect(vm.showPagination).toEqual(false); + expect(vm.paginationMsg).toEqual("No results found"); + } else { + expect(vm.showPagination).toEqual(true); + expect(vm.paginationMsg).toEqual(""); + } + + if (vm.submissionResult.next == null) { + expect(vm.isNext).toEqual("disabled"); + } else { + expect(vm.isNext).toEqual(""); + } + if (vm.submissionResult.previous == null) { + expect(vm.isPrev).toEqual("disabled"); + } else { + expect(vm.isPrev).toEqual(""); + } + if (vm.submissionResult.next != null) { + expect(vm.currentPage).toEqual( + vm.submissionResult.next.split("page=")[1] - 1 + ); + } else { + expect(vm.currentPage).toEqual(1); + } + + expect(vm.stopLoader).toHaveBeenCalled(); + } + ); + }); + + it("backend error", function () { + success = false; + var phaseId = 1; + vm.getAllSubmissionResults(phaseId); + vm.stopFetchingSubmissions(); + expect($interval.cancel).toHaveBeenCalled(); + expect(vm.isResult).toEqual(true); + expect(vm.phaseId).toEqual(phaseId); + + expect(utilities.storeData).toHaveBeenCalledWith( + "emailError", + errorResponse.detail + ); + expect($state.go).toHaveBeenCalledWith("web.permission-denied"); + expect(vm.stopLoader).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for changeSubmissionVisibility function \ + `jobs/challenge//challenge_phase//submission/`", function () { + var success; + + beforeEach(function () { + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: "success", + }); + } else { + parameters.callback.onError({ + data: "error", + }); + } + }; + }); + + it("change submission visibility", function () { + var submissionId = 1; + expect(vm.submissionVisibility).toEqual({}); + vm.submissionVisibility[1] = true; + vm.changeSubmissionVisibility(submissionId); + expect(vm.submissionVisibility[submissionId]).toEqual(true); + }); + }); + + describe("Unit tests for showapprovalparticipantteamDialog function", function () { + var $mdDialog; + + beforeEach(function () { + $mdDialog = $injector.get("$mdDialog"); + }); + + it("should open dialog when approved_status is true", function () { + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + var challengeId = "123"; + var participant_team_id = "456"; + var approved_status = true; + + vm.showapprovalparticipantteamDialog( + challengeId, + participant_team_id, + approved_status + ); + + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + + it("should call check_approval_status when approved_status is false", function () { + vm.check_approval_status = jasmine.createSpy(); + + var challengeId = "123"; + var participant_team_id = "456"; + var approved_status = false; + + vm.showapprovalparticipantteamDialog( + challengeId, + participant_team_id, + approved_status + ); + + expect(vm.check_approval_status).toHaveBeenCalledWith( + challengeId, + participant_team_id, + approved_status, + false + ); + }); + }); + + describe("Unit tests for check_approval_status function", function () { + var success, errorResponse, secondfunction; + + beforeEach(function () { + spyOn($rootScope, "notify"); + spyOn($state, "reload"); + spyOn($mdDialog, "hide"); + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 201, + }); + } else if (secondfunction) { + parameters.callback.onSuccess({ + status: 204, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("should handle successful approval of participant team", function () { + success = true; + + var challengeId = "123"; + var participant_team_id = "456"; + var approved_status = true; + var formvalid = true; + + vm.check_approval_status( + challengeId, + participant_team_id, + approved_status, + formvalid + ); + + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "Participant Team Approved successfully." + ); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + + it("should handle error during approval of participant team", function () { + success = false; + secondfunction = false; + errorResponse = { + error: "Approval failed", + }; + + var challengeId = "123"; + var participant_team_id = "456"; + var approved_status = true; + var formvalid = true; + + vm.check_approval_status( + challengeId, + participant_team_id, + approved_status, + formvalid + ); + + expect($rootScope.notify).toHaveBeenCalledWith( + "error", + "Approval failed" + ); + }); + + it("should handle disapproval of participant team", function () { + success = false; + secondfunction = true; + var challengeId = "123"; + var participant_team_id = "456"; + var approved_status = false; + var formvalid = false; + + vm.check_approval_status( + challengeId, + participant_team_id, + approved_status, + formvalid + ); + + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "Participant Team Disapproved successfully." + ); + expect($state.reload).not.toHaveBeenCalled(); + }); + }); + + describe("Unit tests for changeBaselineStatus function \ + `jobs/challenge//challenge_phase//submission/`", function () { + var success; + + beforeEach(function () { + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: "success", + }); + } else { + parameters.callback.onError({ + data: "error", + }); + } + }; + }); + + it("change Baseline Status", function () { + var submissionId = 1; + expect(vm.baselineStatus).toEqual({}); + vm.baselineStatus[1] = true; + vm.changeBaselineStatus(submissionId); + expect(vm.baselineStatus[submissionId]).toEqual(true); + }); + }); + + describe("Unit tests for showRemainingSubmissions function \ + `jobs//remaining_submissions`", function () { + var success, successResponse; + var errorResponse = { + error: "error", + }; + + beforeEach(function () { + jasmine.clock().install; + spyOn(vm, "startLoader"); + spyOn(vm, "stopLoader"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: successResponse, + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + afterEach(function () { + jasmine.clock().uninstall(); + }); + + it("when submission limit exceeded", function () { + success = true; + successResponse = { + phases: { + first_object: { + id: undefined, + limits: { + submission_limit_exceeded: true, + remaining_submissions_today_count: 0, + remaining_time: 0, + message: "Max Limit Exceeded", }, - { - count: 30, - next: 'page=5', - previous: null, + }, + }, + }; + vm.showRemainingSubmissions(); + expect(vm.maxExceeded).toEqual(true); + expect(vm.maxExceededMessage).toEqual( + successResponse.phases.first_object.limits.message + ); + }); + + it("when today remaining submission greater than 0", function () { + success = true; + successResponse = { + phases: { + first_object: { + id: undefined, + limits: { + submission_limit_exceeded: false, + remaining_submissions_today_count: 10, + remaining_time: 0, }, - { - count: 30, - next: null, - previous: 'page=3', + }, + }, + }; + vm.showRemainingSubmissions(); + expect(vm.remainingSubmissions).toEqual( + successResponse.phases.first_object.limits + ); + expect(vm.showSubmissionNumbers).toEqual(true); + }); + + it("countdown for the remaining timer", function () { + success = true; + successResponse = { + phases: { + first_object: { + id: undefined, + limits: { + submission_limit_exceeded: false, + remaining_submissions_today_count: 0, + remaining_time: 36000, }, - { - count: 30, - next: 'page=4', - previous: 'page=2', - } - ]; - - beforeEach(function () { - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - spyOn(angular, 'element'); - spyOn($interval, 'cancel'); - spyOn(utilities, 'storeData'); - spyOn($state, 'go'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: successResponse, - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - submission_list.forEach(response => { - it('submission list have count' + response.count + ', next ' + response.next + 'and previous ' + response.previous, function () { - success = true; - successResponse = response; - var phaseId = 1 - vm.getAllSubmissionResults(phaseId); - vm.stopFetchingSubmissions(); - expect($interval.cancel).toHaveBeenCalled(); - expect(vm.isResult).toEqual(true); - expect(vm.phaseId).toEqual(phaseId); - - expect(vm.submissionResult).toEqual(response); - if (vm.submissionResult.count == 0) { - expect(vm.showPagination).toEqual(false); - expect(vm.paginationMsg).toEqual("No results found"); - } else { - expect(vm.showPagination).toEqual(true); - expect(vm.paginationMsg).toEqual(""); - } - - if (vm.submissionResult.next == null) { - expect(vm.isNext).toEqual('disabled'); - } else { - expect(vm.isNext).toEqual(''); - - } - if (vm.submissionResult.previous == null) { - expect(vm.isPrev).toEqual('disabled'); - } else { - expect(vm.isPrev).toEqual(''); - } - if (vm.submissionResult.next != null) { - expect(vm.currentPage).toEqual(vm.submissionResult.next.split('page=')[1] - 1); - } else { - expect(vm.currentPage).toEqual(1); - } - - expect(vm.stopLoader).toHaveBeenCalled(); - }); - }); - - it('backend error', function () { - success = false; - var phaseId = 1 - vm.getAllSubmissionResults(phaseId); - vm.stopFetchingSubmissions(); - expect($interval.cancel).toHaveBeenCalled(); - expect(vm.isResult).toEqual(true); - expect(vm.phaseId).toEqual(phaseId); - - expect(utilities.storeData).toHaveBeenCalledWith('emailError', errorResponse.detail); - expect($state.go).toHaveBeenCalledWith('web.permission-denied'); - expect(vm.stopLoader).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for changeSubmissionVisibility function \ - `jobs/challenge//challenge_phase//submission/`', function () { - var success; - - beforeEach(function () { - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: 'success', - }); - } else { - parameters.callback.onError({ - data: 'error' - }); - } - }; - }); - - it('change submission visibility', function () { - var submissionId = 1; - expect(vm.submissionVisibility).toEqual({}); - vm.submissionVisibility[1] = true; - vm.changeSubmissionVisibility(submissionId); - expect(vm.submissionVisibility[submissionId]).toEqual(true); - }); - }); - - describe('Unit tests for showapprovalparticipantteamDialog function', function () { - var $mdDialog; - - beforeEach(function () { - $mdDialog = $injector.get('$mdDialog'); - }); - - it('should open dialog when approved_status is true', function () { - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - var challengeId = '123'; - var participant_team_id = '456'; - var approved_status = true; - - vm.showapprovalparticipantteamDialog(challengeId, participant_team_id, approved_status); - - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - - it('should call check_approval_status when approved_status is false', function () { - vm.check_approval_status = jasmine.createSpy(); - - var challengeId = '123'; - var participant_team_id = '456'; - var approved_status = false; - - vm.showapprovalparticipantteamDialog(challengeId, participant_team_id, approved_status); - - expect(vm.check_approval_status).toHaveBeenCalledWith(challengeId, participant_team_id, approved_status, false); - }); - }); - - describe('Unit tests for check_approval_status function', function () { - var success, errorResponse, secondfunction; - - beforeEach(function () { - spyOn($rootScope, 'notify'); - spyOn($state, 'reload'); - spyOn($mdDialog, 'hide'); - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 201 - }); - } else if (secondfunction) { - parameters.callback.onSuccess({ - status: 204 - }); - } else { - parameters.callback.onError({ - data: errorResponse, - }); - } - }; - }); - - it('should handle successful approval of participant team', function () { - success = true; - - var challengeId = '123'; - var participant_team_id = '456'; - var approved_status = true; - var formvalid = true; - - vm.check_approval_status(challengeId, participant_team_id, approved_status, formvalid); - - expect($rootScope.notify).toHaveBeenCalledWith('success', 'Participant Team Approved successfully.'); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - - it('should handle error during approval of participant team', function () { - success = false; - secondfunction = false; - errorResponse = { - error: 'Approval failed' - }; - - var challengeId = '123'; - var participant_team_id = '456'; - var approved_status = true; - var formvalid = true; - - vm.check_approval_status(challengeId, participant_team_id, approved_status, formvalid); - - expect($rootScope.notify).toHaveBeenCalledWith('error', 'Approval failed'); - }); - - it('should handle disapproval of participant team', function () { - success = false; - secondfunction = true; - var challengeId = '123'; - var participant_team_id = '456'; - var approved_status = false; - var formvalid = false; - - vm.check_approval_status(challengeId, participant_team_id, approved_status, formvalid); - - expect($rootScope.notify).toHaveBeenCalledWith('success', 'Participant Team Disapproved successfully.'); - expect($state.reload).not.toHaveBeenCalled(); - }); - }); - - describe('Unit tests for changeBaselineStatus function \ - `jobs/challenge//challenge_phase//submission/`', function () { - var success; - - beforeEach(function () { - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: 'success', - }); - } else { - parameters.callback.onError({ - data: 'error' - }); - } - }; - }); - - it('change Baseline Status', function () { - var submissionId = 1; - expect(vm.baselineStatus).toEqual({}); - vm.baselineStatus[1] = true; - vm.changeBaselineStatus(submissionId); - expect(vm.baselineStatus[submissionId]).toEqual(true); - }); - }); - - describe('Unit tests for showRemainingSubmissions function \ - `jobs//remaining_submissions`', function () { - var success, successResponse; - var errorResponse = { - error: 'error' + }, + }, + }; + vm.showRemainingSubmissions(); + expect(vm.message).toEqual(successResponse.phases.first_object.limits); + expect(vm.showClock).toEqual(true); + + expect(vm.remainingTime).toEqual( + successResponse.phases.first_object.limits.remaining_time + ); + expect(vm.days).toEqual(Math.floor(vm.remainingTime / 24 / 60 / 60)); + expect(vm.hoursLeft).toEqual( + Math.floor(vm.remainingTime - vm.days * 86400) + ); + expect(vm.hours).toEqual(Math.floor(vm.hoursLeft / 3600)); + expect(vm.minutesLeft).toEqual( + Math.floor(vm.hoursLeft - vm.hours * 3600) + ); + expect(vm.minutes).toEqual(Math.floor(vm.minutesLeft / 60)); + }); + + it("backend error", function () { + success = false; + vm.showRemainingSubmissions(); + expect(vm.stopLoader).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "error", + errorResponse.error + ); + }); + }); + + describe("Unit tests for showMdDialog", function () { + it("Variables initialized", function () { + vm.submissionResult = []; + vm.submissionMetaData = { + id: 1, + method_name: "method name", + method_description: "method description", + project_url: "project url", + publication_url: "publication url", + }; + var submissionId = 1; + var ev = new Event("click"); + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + vm.showMdDialog(ev, submissionId); + expect(vm.method_name).toEqual(vm.submissionMetaData.method_name); + expect(vm.method_description).toEqual( + vm.submissionMetaData.method_description + ); + expect(vm.project_url).toEqual(vm.submissionMetaData.project_url); + expect(vm.publication_url).toEqual(vm.submissionMetaData.publication_url); + expect(vm.submissionId).toEqual(submissionId); + + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toBe(true); + }); + }); + + describe("Unit tests for updateSubmissionMetaData function \ + `jobs/challenge//challenge_phase//submission/`", function () { + var success; + var errorResponse = "error"; + + beforeEach(function () { + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: "success", + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); } + }; + }); - beforeEach(function () { - jasmine.clock().install; - spyOn(vm, 'startLoader'); - spyOn(vm, 'stopLoader'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: successResponse, - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - afterEach(function () { - jasmine.clock().uninstall(); - }); - - it('when submission limit exceeded', function () { - success = true; - successResponse = { - phases: { - first_object: { - id: undefined, - limits: { - submission_limit_exceeded: true, - remaining_submissions_today_count: 0, - remaining_time: 0, - message: "Max Limit Exceeded", - } - }, - } - }; - vm.showRemainingSubmissions(); - expect(vm.maxExceeded).toEqual(true); - expect(vm.maxExceededMessage).toEqual(successResponse.phases.first_object.limits.message); - }); - - it('when today remaining submission greater than 0', function () { - success = true; - successResponse = { - phases: { - first_object: { - id: undefined, - limits: { - submission_limit_exceeded: false, - remaining_submissions_today_count: 10, - remaining_time: 0, - } - } - } - }; - vm.showRemainingSubmissions(); - expect(vm.remainingSubmissions).toEqual(successResponse.phases.first_object.limits); - expect(vm.showSubmissionNumbers).toEqual(true); - }); - - it('countdown for the remaining timer', function () { - success = true; - successResponse = { - phases: { - first_object: { - id: undefined, - limits: { - submission_limit_exceeded: false, - remaining_submissions_today_count: 0, - remaining_time: 36000, - } - } - } - }; - vm.showRemainingSubmissions(); - expect(vm.message).toEqual(successResponse.phases.first_object.limits); - expect(vm.showClock).toEqual(true); - - expect(vm.remainingTime).toEqual(successResponse.phases.first_object.limits.remaining_time); - expect(vm.days).toEqual(Math.floor(vm.remainingTime / 24 / 60 / 60)); - expect(vm.hoursLeft).toEqual(Math.floor((vm.remainingTime) - (vm.days * 86400))); - expect(vm.hours).toEqual(Math.floor(vm.hoursLeft / 3600)); - expect(vm.minutesLeft).toEqual(Math.floor((vm.hoursLeft) - (vm.hours * 3600))); - expect(vm.minutes).toEqual(Math.floor(vm.minutesLeft / 60)); - }); - - it('backend error', function () { - success = false; - vm.showRemainingSubmissions(); - expect(vm.stopLoader).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse.error); - }); - }); - - describe('Unit tests for showMdDialog', function () { - it('Variables initialized', function () { - vm.submissionResult = []; - vm.submissionMetaData = { - id: 1, - method_name: 'method name', - method_description: 'method description', - project_url: 'project url', - publication_url: 'publication url' - }; - var submissionId = 1; - var ev = new Event('click'); - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.showMdDialog(ev, submissionId); - expect(vm.method_name).toEqual(vm.submissionMetaData.method_name); - expect(vm.method_description).toEqual(vm.submissionMetaData.method_description); - expect(vm.project_url).toEqual(vm.submissionMetaData.project_url); - expect(vm.publication_url).toEqual(vm.submissionMetaData.publication_url); - expect(vm.submissionId).toEqual(submissionId); - - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toBe(true); - }); - }); - - describe('Unit tests for updateSubmissionMetaData function \ - `jobs/challenge//challenge_phase//submission/`', function () { - var success; - var errorResponse = 'error'; - - beforeEach(function () { - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: 'success', - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid form submission and succesfully updated form', function () { - vm.method_name = 'method name'; - vm.method_description = 'method description'; - vm.project_url = 'project url'; - vm.publication_url = 'publication url'; - vm.submissionMetaData = { - submission_metadata: null - }; - vm.currentSubmissionMetaData = [ - { - name: 'TextAttribute', - type: 'text', - value: null, - $$hashKey: 'object:42', - description: 'Sample', - }, - { - name: 'SingleOptionAttribute', - type: 'radio', - value: null, - options: ['A', 'B', 'C'], - $$hashKey: 'object:43', - description: 'Sample', - }, - { - name: 'MultipleChoiceAttribute', - type: 'checkbox', - values: [], - options: ['alpha', 'beta', 'gamma'], - $$hashKey: 'object:44', - description: 'Sample', - }, - { - name: 'TrueFalseField', - type: 'boolean', - value: null, - $$hashKey: 'object:45', - description: 'Sample', - }, - ]; - success = true; - var updateSubmissionMetaDataForm = true; - vm.updateSubmissionMetaData(updateSubmissionMetaDataForm); - expect(status).toBeDefined(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The data is successfully updated!"); - }); - - it('valid form submission and backend error', function () { - vm.method_name = 'method name'; - vm.method_description = 'method description'; - vm.project_url = 'project url'; - vm.publication_url = 'publication url'; - vm.submissionMetaData = { - submission_metadata: null - }; - vm.currentSubmissionMetaData = [ - { - name: 'TextAttribute', - type: 'text', - value: null, - $$hashKey: 'object:42', - description: 'Sample', - }, - { - name: 'SingleOptionAttribute', - type: 'radio', - value: null, - options: ['A', 'B', 'C'], - $$hashKey: 'object:43', - description: 'Sample', - }, - { - name: 'MultipleChoiceAttribute', - type: 'checkbox', - values: [], - options: ['alpha', 'beta', 'gamma'], - $$hashKey: 'object:44', - description: 'Sample', - }, - { - name: 'TrueFalseField', - type: 'boolean', - value: null, - $$hashKey: 'object:45', - description: 'Sample', - }, - ]; - success = false; - var updateSubmissionMetaDataForm = true; - vm.updateSubmissionMetaData(updateSubmissionMetaDataForm); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - }); - - it('invalid form submission', function () { - var updateSubmissionMetaDataForm = false; - vm.updateSubmissionMetaData(updateSubmissionMetaDataForm); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for isStarred function \ - `challenges//`', function () { - var success, successResponse; - - beforeEach(function () { - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: successResponse - }); - } else { - parameters.callback.onError({ - data: 'error' - }); - } - }; - }); - - it('get the stars count and user specific unstarred', function () { - success = true; - successResponse = { - count: 1, - is_starred: false - }; - vm.isStarred(); - expect(vm.count).toEqual(successResponse.count); - expect(vm.is_starred).toEqual(successResponse.is_starred); - expect(vm.caption).toEqual('Star'); - }); - - it('get the stars count and user specific starred', function () { - success = true; - successResponse = { - count: 1, - is_starred: true - }; - vm.isStarred(); - expect(vm.count).toEqual(successResponse.count); - expect(vm.is_starred).toEqual(successResponse.is_starred); - expect(vm.caption).toEqual('Unstar'); - }); - }); - - describe('Unit tests for starChallenge function \ - `challenges//`', function () { - var success, successResponse; - var errorResponse = 'error'; - - beforeEach(function () { - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: successResponse - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('Unstar the challenge', function () { - success = true; - successResponse = { - count: 10, - is_starred: true - }; - vm.starChallenge(); - expect(vm.count).toEqual(successResponse.count); - expect(vm.is_starred).toEqual(successResponse.is_starred); - expect(vm.caption).toEqual('Unstar'); - }); - - it('Star the challenge', function () { - success = true; - successResponse = { - count: 10, - is_starred: false - }; - vm.starChallenge(); - expect(vm.count).toEqual(successResponse.count); - expect(vm.is_starred).toEqual(successResponse.is_starred); - expect(vm.caption).toEqual('Star'); - }); - - it('backend error', function () { - success = false; - vm.starChallenge(); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - }); - }); - - describe('Unit tests for overviewDialog function', function () { - it('open dialog for edit challenge overview', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.overviewDialog(); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - }); - - describe('Unit tests for editChallengeOverview function \ - `challenges/challenge_host_team//challenge/`', function () { - var success; - var errorResponse = 'error'; - - beforeEach(function () { - spyOn(utilities, 'getData'); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid editchallengeoverview form and succesfull edit', function () { - var editChallengeOverviewForm = true; - success = true; - vm.page.description = "description"; - vm.editChallengeOverview(editChallengeOverviewForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The description is successfully updated!"); - }); - - it('valid editchallengeoverview form and backend error', function () { - var editChallengeOverviewForm = true; - success = false; - vm.tempDesc = "temp description"; - vm.page.description = "description"; - vm.editChallengeOverview(editChallengeOverviewForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect(vm.page.description).toEqual(vm.tempDesc); - expect($rootScope.notify).toHaveBeenCalledWith('error', errorResponse); - }); - - it('invalid editchallengeoverview form', function () { - var editChallengeOverviewForm = false; - vm.tempDesc = "No description"; - vm.page.description = "description"; - vm.editChallengeOverview(editChallengeOverviewForm); - expect(vm.page.description).toEqual(vm.tempDesc); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for editchallengeTagDialog function', function () { - it('open dialog for edit challenge tag', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - vm.page.list_tags = ['tag1', 'tag2']; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.editchallengeTagDialog(); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - }); - - describe('Unit test for editChallengeTag function', function () { - var success; - var errorResponse = 'error'; - beforeEach(function () { - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid edit challenge tag', function () { - var editChallengeTagDomainForm = true; - success = true; - vm.tags = "tag1, tag2"; - vm.domain = 'CV'; - spyOn($state, 'reload'); - vm.editChallengeTag(editChallengeTagDomainForm); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The challenge tags and domain is successfully updated!"); - expect($state.reload).toHaveBeenCalled(); - }); - - it('invalid edit challenge tag', function () { - var editChallengeTagDomainForm = false; - success = true; - vm.tags = "tag1, tag2"; - vm.domain = 'CV'; - vm.editChallengeTag(editChallengeTagDomainForm); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - - it('invalid edit challenge domain and backend error', function () { - var editChallengeTagDomainForm = true; - success = false; - vm.tags = "tag1, tag2"; - vm.domain = 'domain'; - vm.editChallengeTag(editChallengeTagDomainForm); - expect($rootScope.notify).toHaveBeenCalledWith("error", "error"); - }); - - it('valid edit challenge more than 4 error', function () { - var editChallengeTagDomainForm = true; - success = true; - vm.tags = "tag1, tag2, tag3, tag4, tag5WithMorethan15Charactersd"; - vm.domain = 'CV'; - vm.editChallengeTag(editChallengeTagDomainForm); - expect($rootScope.notify).toHaveBeenCalledWith("error", "Invalid tags! Maximum 4 tags are allowed, and each tag must be 15 characters or less."); - }); - }); - - describe('Unit tests for deleteChallengeDialog function', function () { - it('open dialog for delete challenge', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.deleteChallengeDialog(); - expect(vm.titleInput).toEqual(""); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - }); - - describe('Unit tests for deleteChallenge function \ - `challenges/challenge//disable`', function () { - var success; - var errorResponse = 'error'; - - beforeEach(function () { - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 204 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid delete challenge form & successfully delete challenge', function () { - var deleteChallengeForm = true; - success = true; - vm.deleteChallenge(deleteChallengeForm); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The Challenge is successfully deleted!"); - }); - - it('valid delete challenge form & backend error', function () { - var deleteChallengeForm = true; - success = false; - vm.deleteChallenge(deleteChallengeForm); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - }); - - it('invalid delete challenge form', function () { - var deleteChallengeForm = false; - vm.deleteChallenge(deleteChallengeForm); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for submissionGuidelinesDialog function', function () { - it('open dialog to `edit submission guidelines`', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.page.submission_guidelines = "submission guidelines"; - vm.submissionGuidelinesDialog(); - expect(vm.tempSubmissionGuidelines).toEqual(vm.page.submission_guidelines); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - }); - - describe('Unit tests for editSubmissionGuideline function \ - `challenges/challenge_host_team//challenge/`', function () { - var success; - var errorResponse = 'error'; - - beforeEach(function () { - spyOn(utilities, 'getData'); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid edit submission guidelines form & successfull edit', function () { - var editSubmissionGuidelinesForm = true; - success = true; - vm.page.submission_guidelines = "submission guidelines"; - vm.editSubmissionGuideline(editSubmissionGuidelinesForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The submission guidelines is successfully updated!"); - }); - - it('valid edit submission guidelines form & backend error', function () { - var editSubmissionGuidelinesForm = true; - success = false; - vm.tempSubmissionGuidelines = "temp submission guidelines"; - vm.page.submission_guidelines = "submission guidelines"; - vm.editSubmissionGuideline(editSubmissionGuidelinesForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect(vm.page.submission_guidelines).toEqual(vm.tempSubmissionGuidelines); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - }); - - it('invalid edit submission guidelines form', function () { - var editSubmissionGuidelinesForm = false; - vm.tempSubmissionGuidelines = "temp submission guidelines"; - vm.page.submission_guidelines = "submission guidelines"; - vm.editSubmissionGuideline(editSubmissionGuidelinesForm); - expect(vm.page.submission_guidelines).toEqual(vm.tempSubmissionGuidelines); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for evaluationCriteriaDialog function', function () { - it('open dialog to `edit evaluation criteria`', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.page.evaluation_details = "evaluation details"; - vm.evaluationCriteriaDialog(); - expect(vm.tempEvaluationCriteria).toEqual(vm.page.evaluation_details); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - }); - - describe('Unit tests for editEvaluationCriteria function \ - `challenges/challenge_host_team//challenge/`', function () { - var success; - var errorResponse = 'error'; - - beforeEach(function () { - spyOn(utilities, 'getData'); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid `edit evaluation criteria` form & successfull edit', function () { - var editEvaluationCriteriaForm = true; - success = true; - vm.page.evaluation_details = "evaluation details"; - vm.editEvaluationCriteria(editEvaluationCriteriaForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The evaluation details is successfully updated!"); - }); - - it('valid `edit evaluation criteria` form & backend error', function () { - var editEvaluationCriteriaForm = true; - success = false; - vm.tempEvaluationCriteria = "temp evaluation details"; - vm.page.evaluation_details = "evaluation details"; - vm.editEvaluationCriteria(editEvaluationCriteriaForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect(vm.page.evaluation_details).toEqual(vm.tempEvaluationCriteria); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - }); - - it('invalid `edit evaluation criteria` form', function () { - var editEvaluationCriteriaForm = false; - vm.tempEvaluationCriteria = "temp evaluation details"; - vm.page.evaluation_details = "evaluation details"; - vm.editEvaluationCriteria(editEvaluationCriteriaForm); - expect(vm.page.evaluation_details).toEqual(vm.tempEvaluationCriteria); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for evaluationScriptDialog function', function () { - it('open dialog to `edit evaluation script`', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.evaluationScriptDialog(); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - }); - - describe('Unit tests for editEvalScript function \ - `challenges/challenge_host_team//challenge/`', function () { - var success; - var errorResponse = 'error'; - - beforeEach(function () { - spyOn(utilities, 'getData'); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid `edit evaluation script` form & successfull edit', function () { - var editEvaluationScriptForm = true; - vm.editEvaluationScript = "evaluation_script.zip"; - success = true; - vm.page.evaluation_details = "evaluation details"; - vm.editEvalScript(editEvaluationScriptForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The evaluation script is successfully updated!"); - }); - - it('invalid `edit evaluation script` form & frontend error', function () { - var editEvaluationScriptForm = true; - success = true; - vm.page.evaluation_details = "evaluation details"; - vm.editEvalScript(editEvaluationScriptForm); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("error", "Please upload a valid evaluation script!"); - }); - - it('valid `edit evaluation script` form & backend error', function () { - var editEvaluationScriptForm = true; - vm.editEvaluationScript = "evaluation_script.zip"; - success = false; - vm.tempEvaluationCriteria = "temp evaluation details"; - vm.page.evaluation_details = "evaluation details"; - vm.editEvalScript(editEvaluationScriptForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect(vm.page.evaluation_details).toEqual(vm.tempEvaluationCriteria); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - }); - - it('invalid `edit evaluation script` form', function () { - var editEvaluationScriptForm = false; - vm.tempEvaluationCriteria = "temp evaluation details"; - vm.page.evaluation_details = "evaluation details"; - vm.editEvalScript(editEvaluationScriptForm); - expect(vm.page.evaluation_details).toEqual(vm.tempEvaluationCriteria); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for termsAndConditionsDialog function', function () { - it('open dialog to `terms and conditions`', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.page.terms_and_conditions = "terms and conditions"; - vm.termsAndConditionsDialog(); - expect(vm.tempTermsAndConditions).toEqual(vm.page.terms_and_conditions); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - }); - - describe('Unit tests for editTermsAndConditions function \ - `challenges/challenge_host_team//challenge/`', function () { - var success; - var errorResponse = 'error'; - - beforeEach(function () { - spyOn(utilities, 'getData'); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid `edit terms and conditions` form & successfull edit', function () { - var editTermsAndConditionsForm = true; - success = true; - vm.page.terms_and_conditions = "terms and conditions"; - vm.editTermsAndConditions(editTermsAndConditionsForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The terms and conditions are successfully updated!"); - }); - - it('valid `edit terms and conditions` form & backend error', function () { - var editTermsAndConditionsForm = true; - success = false; - vm.tempTermsAndConditions = "temp terms and conditions"; - vm.page.terms_and_conditions = "terms and conditions"; - vm.editTermsAndConditions(editTermsAndConditionsForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect(vm.page.terms_and_conditions).toEqual(vm.tempTermsAndConditions); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - }); - - it('invalid `edit terms and conditions` form', function () { - var editTermsAndConditionsForm = false; - vm.tempTermsAndConditions = "temp evaluation details"; - vm.page.terms_and_conditions = "terms and conditions"; - vm.editTermsAndConditions(editTermsAndConditionsForm); - expect(vm.page.terms_and_conditions).toEqual(vm.tempTermsAndConditions); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for challengeTitleDialog function', function () { - it('open dialog to `edit challenge title`', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.page.title = "challenge title"; - vm.challengeTitleDialog(); - expect(vm.tempChallengeTitle).toEqual(vm.page.title); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - }); - - describe('Unit tests for editChallengeTitle function \ - `challenges/challenge_host_team//challenge/`', function () { - var success; - var errorResponse = 'error'; - - beforeEach(function () { - spyOn(utilities, 'getData'); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid `edit terms and conditions` form & successfull edit', function () { - var editChallengeTitleForm = true; - success = true; - vm.page.title = "challenge title"; - vm.editChallengeTitle(editChallengeTitleForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The challenge title is successfully updated!"); - }); - - it('valid `edit terms and conditions` form & backend error', function () { - var editChallengeTitleForm = true; - success = false; - vm.tempChallengeTitle = "temp challenge title"; - vm.page.title = "challenge title"; - vm.editChallengeTitle(editChallengeTitleForm); - expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); - expect($mdDialog.hide).toHaveBeenCalled(); - expect(vm.page.title).toEqual(vm.tempChallengeTitle); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - }); - - it('invalid `edit terms and conditions` form', function () { - var editChallengeTitleForm = false; - vm.tempChallengeTitle = "temp challenge title"; - vm.page.title = "challenge title"; - vm.editChallengeTitle(editChallengeTitleForm); - expect(vm.page.title).toEqual(vm.tempChallengeTitle); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for challengePhaseDialog function', function () { - it('open dialog to `edit challenge phase`', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - var phase = { - start_date: new Date('Fri June 12 2018 22:41:51 GMT+0530'), - end_date: new Date('Fri June 12 2099 22:41:51 GMT+0530'), - max_submissions_per_day: 1500 - }; - var ev = new Event('click'); - vm.challengePhaseDialog(ev, phase); - expect(vm.page.challenge_phase).toEqual(phase); - expect(vm.page.max_submissions_per_day).toEqual(phase.max_submissions_per_day); - expect(vm.page.max_submissions_per_month).toEqual(phase.max_submissions_per_month); - expect(vm.phaseStartDate).toEqual(moment(phase.start_date)); - expect(vm.phaseEndDate).toEqual(moment(phase.end_date)); - expect(vm.testAnnotationFile).toEqual(null); - expect(vm.sanityCheckPass).toEqual(true); - expect(vm.sanityCheck).toEqual(""); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toEqual(true); - }); - }); - - describe('Unit tests for editChallengePhase function \ - `challenges/challenge//challenge_phase/`', function () { - var success; - var errorResponse = { - detail: 'error' + it("valid form submission and succesfully updated form", function () { + vm.method_name = "method name"; + vm.method_description = "method description"; + vm.project_url = "project url"; + vm.publication_url = "publication url"; + vm.submissionMetaData = { + submission_metadata: null, + }; + vm.currentSubmissionMetaData = [ + { + name: "TextAttribute", + type: "text", + value: null, + $$hashKey: "object:42", + description: "Sample", + }, + { + name: "SingleOptionAttribute", + type: "radio", + value: null, + options: ["A", "B", "C"], + $$hashKey: "object:43", + description: "Sample", + }, + { + name: "MultipleChoiceAttribute", + type: "checkbox", + values: [], + options: ["alpha", "beta", "gamma"], + $$hashKey: "object:44", + description: "Sample", + }, + { + name: "TrueFalseField", + type: "boolean", + value: null, + $$hashKey: "object:45", + description: "Sample", + }, + ]; + success = true; + var updateSubmissionMetaDataForm = true; + vm.updateSubmissionMetaData(updateSubmissionMetaDataForm); + expect(status).toBeDefined(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The data is successfully updated!" + ); + }); + + it("valid form submission and backend error", function () { + vm.method_name = "method name"; + vm.method_description = "method description"; + vm.project_url = "project url"; + vm.publication_url = "publication url"; + vm.submissionMetaData = { + submission_metadata: null, + }; + vm.currentSubmissionMetaData = [ + { + name: "TextAttribute", + type: "text", + value: null, + $$hashKey: "object:42", + description: "Sample", + }, + { + name: "SingleOptionAttribute", + type: "radio", + value: null, + options: ["A", "B", "C"], + $$hashKey: "object:43", + description: "Sample", + }, + { + name: "MultipleChoiceAttribute", + type: "checkbox", + values: [], + options: ["alpha", "beta", "gamma"], + $$hashKey: "object:44", + description: "Sample", + }, + { + name: "TrueFalseField", + type: "boolean", + value: null, + $$hashKey: "object:45", + description: "Sample", + }, + ]; + success = false; + var updateSubmissionMetaDataForm = true; + vm.updateSubmissionMetaData(updateSubmissionMetaDataForm); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + + it("invalid form submission", function () { + var updateSubmissionMetaDataForm = false; + vm.updateSubmissionMetaData(updateSubmissionMetaDataForm); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for isStarred function \ + `challenges//`", function () { + var success, successResponse; + + beforeEach(function () { + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: successResponse, + }); + } else { + parameters.callback.onError({ + data: "error", + }); + } + }; + }); + + it("get the stars count and user specific unstarred", function () { + success = true; + successResponse = { + count: 1, + is_starred: false, + }; + vm.isStarred(); + expect(vm.count).toEqual(successResponse.count); + expect(vm.is_starred).toEqual(successResponse.is_starred); + expect(vm.caption).toEqual("Star"); + }); + + it("get the stars count and user specific starred", function () { + success = true; + successResponse = { + count: 1, + is_starred: true, + }; + vm.isStarred(); + expect(vm.count).toEqual(successResponse.count); + expect(vm.is_starred).toEqual(successResponse.is_starred); + expect(vm.caption).toEqual("Unstar"); + }); + }); + + describe("Unit tests for starChallenge function \ + `challenges//`", function () { + var success, successResponse; + var errorResponse = "error"; + + beforeEach(function () { + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: successResponse, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("Unstar the challenge", function () { + success = true; + successResponse = { + count: 10, + is_starred: true, + }; + vm.starChallenge(); + expect(vm.count).toEqual(successResponse.count); + expect(vm.is_starred).toEqual(successResponse.is_starred); + expect(vm.caption).toEqual("Unstar"); + }); + + it("Star the challenge", function () { + success = true; + successResponse = { + count: 10, + is_starred: false, + }; + vm.starChallenge(); + expect(vm.count).toEqual(successResponse.count); + expect(vm.is_starred).toEqual(successResponse.is_starred); + expect(vm.caption).toEqual("Star"); + }); + + it("backend error", function () { + success = false; + vm.starChallenge(); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + }); + + describe("Unit tests for overviewDialog function", function () { + it("open dialog for edit challenge overview", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + vm.overviewDialog(); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + }); + + describe("Unit tests for editChallengeOverview function \ + `challenges/challenge_host_team//challenge/`", function () { + var success; + var errorResponse = "error"; + + beforeEach(function () { + spyOn(utilities, "getData"); + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("valid editchallengeoverview form and succesfull edit", function () { + var editChallengeOverviewForm = true; + success = true; + vm.page.description = "description"; + vm.editChallengeOverview(editChallengeOverviewForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The description is successfully updated!" + ); + }); + + it("valid editchallengeoverview form and backend error", function () { + var editChallengeOverviewForm = true; + success = false; + vm.tempDesc = "temp description"; + vm.page.description = "description"; + vm.editChallengeOverview(editChallengeOverviewForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect(vm.page.description).toEqual(vm.tempDesc); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + + it("invalid editchallengeoverview form", function () { + var editChallengeOverviewForm = false; + vm.tempDesc = "No description"; + vm.page.description = "description"; + vm.editChallengeOverview(editChallengeOverviewForm); + expect(vm.page.description).toEqual(vm.tempDesc); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for editchallengeTagDialog function", function () { + it("open dialog for edit challenge tag", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + vm.page.list_tags = ["tag1", "tag2"]; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + vm.editchallengeTagDialog(); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + }); + + describe("Unit test for editChallengeTag function", function () { + var success; + var errorResponse = "error"; + beforeEach(function () { + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); } + }; + }); + + it("valid edit challenge tag", function () { + var editChallengeTagDomainForm = true; + success = true; + vm.tags = "tag1, tag2"; + vm.domain = "CV"; + spyOn($state, "reload"); + vm.editChallengeTag(editChallengeTagDomainForm); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The challenge tags and domain is successfully updated!" + ); + expect($state.reload).toHaveBeenCalled(); + }); + + it("invalid edit challenge tag", function () { + var editChallengeTagDomainForm = false; + success = true; + vm.tags = "tag1, tag2"; + vm.domain = "CV"; + vm.editChallengeTag(editChallengeTagDomainForm); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + + it("invalid edit challenge domain and backend error", function () { + var editChallengeTagDomainForm = true; + success = false; + vm.tags = "tag1, tag2"; + vm.domain = "domain"; + vm.editChallengeTag(editChallengeTagDomainForm); + expect($rootScope.notify).toHaveBeenCalledWith("error", "error"); + }); + + it("valid edit challenge more than 4 error", function () { + var editChallengeTagDomainForm = true; + success = true; + vm.tags = "tag1, tag2, tag3, tag4, tag5WithMorethan15Charactersd"; + vm.domain = "CV"; + vm.editChallengeTag(editChallengeTagDomainForm); + expect($rootScope.notify).toHaveBeenCalledWith( + "error", + "Invalid tags! Maximum 4 tags are allowed, and each tag must be 15 characters or less." + ); + }); + }); + + describe("Unit tests for deleteChallengeDialog function", function () { + it("open dialog for delete challenge", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + vm.deleteChallengeDialog(); + expect(vm.titleInput).toEqual(""); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + }); + + describe("Unit tests for deleteChallenge function \ + `challenges/challenge//disable`", function () { + var success; + var errorResponse = "error"; + + beforeEach(function () { + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 204, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("valid delete challenge form & successfully delete challenge", function () { + var deleteChallengeForm = true; + success = true; + vm.deleteChallenge(deleteChallengeForm); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The Challenge is successfully deleted!" + ); + }); + + it("valid delete challenge form & backend error", function () { + var deleteChallengeForm = true; + success = false; + vm.deleteChallenge(deleteChallengeForm); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + + it("invalid delete challenge form", function () { + var deleteChallengeForm = false; + vm.deleteChallenge(deleteChallengeForm); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for submissionGuidelinesDialog function", function () { + it("open dialog to `edit submission guidelines`", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + vm.page.submission_guidelines = "submission guidelines"; + vm.submissionGuidelinesDialog(); + expect(vm.tempSubmissionGuidelines).toEqual( + vm.page.submission_guidelines + ); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + }); + + describe("Unit tests for editSubmissionGuideline function \ + `challenges/challenge_host_team//challenge/`", function () { + var success; + var errorResponse = "error"; + + beforeEach(function () { + spyOn(utilities, "getData"); + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("valid edit submission guidelines form & successfull edit", function () { + var editSubmissionGuidelinesForm = true; + success = true; + vm.page.submission_guidelines = "submission guidelines"; + vm.editSubmissionGuideline(editSubmissionGuidelinesForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The submission guidelines is successfully updated!" + ); + }); + + it("valid edit submission guidelines form & backend error", function () { + var editSubmissionGuidelinesForm = true; + success = false; + vm.tempSubmissionGuidelines = "temp submission guidelines"; + vm.page.submission_guidelines = "submission guidelines"; + vm.editSubmissionGuideline(editSubmissionGuidelinesForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect(vm.page.submission_guidelines).toEqual( + vm.tempSubmissionGuidelines + ); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + + it("invalid edit submission guidelines form", function () { + var editSubmissionGuidelinesForm = false; + vm.tempSubmissionGuidelines = "temp submission guidelines"; + vm.page.submission_guidelines = "submission guidelines"; + vm.editSubmissionGuideline(editSubmissionGuidelinesForm); + expect(vm.page.submission_guidelines).toEqual( + vm.tempSubmissionGuidelines + ); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for evaluationCriteriaDialog function", function () { + it("open dialog to `edit evaluation criteria`", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + vm.page.evaluation_details = "evaluation details"; + vm.evaluationCriteriaDialog(); + expect(vm.tempEvaluationCriteria).toEqual(vm.page.evaluation_details); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + }); + + describe("Unit tests for editEvaluationCriteria function \ + `challenges/challenge_host_team//challenge/`", function () { + var success; + var errorResponse = "error"; + + beforeEach(function () { + spyOn(utilities, "getData"); + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("valid `edit evaluation criteria` form & successfull edit", function () { + var editEvaluationCriteriaForm = true; + success = true; + vm.page.evaluation_details = "evaluation details"; + vm.editEvaluationCriteria(editEvaluationCriteriaForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The evaluation details is successfully updated!" + ); + }); + + it("valid `edit evaluation criteria` form & backend error", function () { + var editEvaluationCriteriaForm = true; + success = false; + vm.tempEvaluationCriteria = "temp evaluation details"; + vm.page.evaluation_details = "evaluation details"; + vm.editEvaluationCriteria(editEvaluationCriteriaForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect(vm.page.evaluation_details).toEqual(vm.tempEvaluationCriteria); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + + it("invalid `edit evaluation criteria` form", function () { + var editEvaluationCriteriaForm = false; + vm.tempEvaluationCriteria = "temp evaluation details"; + vm.page.evaluation_details = "evaluation details"; + vm.editEvaluationCriteria(editEvaluationCriteriaForm); + expect(vm.page.evaluation_details).toEqual(vm.tempEvaluationCriteria); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for evaluationScriptDialog function", function () { + it("open dialog to `edit evaluation script`", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + vm.evaluationScriptDialog(); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + }); + + describe("Unit tests for editEvalScript function \ + `challenges/challenge_host_team//challenge/`", function () { + var success; + var errorResponse = "error"; + + beforeEach(function () { + spyOn(utilities, "getData"); + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("valid `edit evaluation script` form & successfull edit", function () { + var editEvaluationScriptForm = true; + vm.editEvaluationScript = "evaluation_script.zip"; + success = true; + vm.page.evaluation_details = "evaluation details"; + vm.editEvalScript(editEvaluationScriptForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The evaluation script is successfully updated!" + ); + }); + + it("invalid `edit evaluation script` form & frontend error", function () { + var editEvaluationScriptForm = true; + success = true; + vm.page.evaluation_details = "evaluation details"; + vm.editEvalScript(editEvaluationScriptForm); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "error", + "Please upload a valid evaluation script!" + ); + }); + + it("valid `edit evaluation script` form & backend error", function () { + var editEvaluationScriptForm = true; + vm.editEvaluationScript = "evaluation_script.zip"; + success = false; + vm.tempEvaluationCriteria = "temp evaluation details"; + vm.page.evaluation_details = "evaluation details"; + vm.editEvalScript(editEvaluationScriptForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect(vm.page.evaluation_details).toEqual(vm.tempEvaluationCriteria); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + + it("invalid `edit evaluation script` form", function () { + var editEvaluationScriptForm = false; + vm.tempEvaluationCriteria = "temp evaluation details"; + vm.page.evaluation_details = "evaluation details"; + vm.editEvalScript(editEvaluationScriptForm); + expect(vm.page.evaluation_details).toEqual(vm.tempEvaluationCriteria); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for termsAndConditionsDialog function", function () { + it("open dialog to `terms and conditions`", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + vm.page.terms_and_conditions = "terms and conditions"; + vm.termsAndConditionsDialog(); + expect(vm.tempTermsAndConditions).toEqual(vm.page.terms_and_conditions); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + }); + + describe("Unit tests for editTermsAndConditions function \ + `challenges/challenge_host_team//challenge/`", function () { + var success; + var errorResponse = "error"; + + beforeEach(function () { + spyOn(utilities, "getData"); + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("valid `edit terms and conditions` form & successfull edit", function () { + var editTermsAndConditionsForm = true; + success = true; + vm.page.terms_and_conditions = "terms and conditions"; + vm.editTermsAndConditions(editTermsAndConditionsForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The terms and conditions are successfully updated!" + ); + }); + + it("valid `edit terms and conditions` form & backend error", function () { + var editTermsAndConditionsForm = true; + success = false; + vm.tempTermsAndConditions = "temp terms and conditions"; + vm.page.terms_and_conditions = "terms and conditions"; + vm.editTermsAndConditions(editTermsAndConditionsForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect(vm.page.terms_and_conditions).toEqual(vm.tempTermsAndConditions); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + + it("invalid `edit terms and conditions` form", function () { + var editTermsAndConditionsForm = false; + vm.tempTermsAndConditions = "temp evaluation details"; + vm.page.terms_and_conditions = "terms and conditions"; + vm.editTermsAndConditions(editTermsAndConditionsForm); + expect(vm.page.terms_and_conditions).toEqual(vm.tempTermsAndConditions); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for challengeTitleDialog function", function () { + it("open dialog to `edit challenge title`", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + vm.page.title = "challenge title"; + vm.challengeTitleDialog(); + expect(vm.tempChallengeTitle).toEqual(vm.page.title); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + }); + + describe("Unit tests for editChallengeTitle function \ + `challenges/challenge_host_team//challenge/`", function () { + var success; + var errorResponse = "error"; + + beforeEach(function () { + spyOn(utilities, "getData"); + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("valid `edit terms and conditions` form & successfull edit", function () { + var editChallengeTitleForm = true; + success = true; + vm.page.title = "challenge title"; + vm.editChallengeTitle(editChallengeTitleForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The challenge title is successfully updated!" + ); + }); + + it("valid `edit terms and conditions` form & backend error", function () { + var editChallengeTitleForm = true; + success = false; + vm.tempChallengeTitle = "temp challenge title"; + vm.page.title = "challenge title"; + vm.editChallengeTitle(editChallengeTitleForm); + expect(utilities.getData).toHaveBeenCalledWith("challengeCreator"); + expect($mdDialog.hide).toHaveBeenCalled(); + expect(vm.page.title).toEqual(vm.tempChallengeTitle); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + }); + + it("invalid `edit terms and conditions` form", function () { + var editChallengeTitleForm = false; + vm.tempChallengeTitle = "temp challenge title"; + vm.page.title = "challenge title"; + vm.editChallengeTitle(editChallengeTitleForm); + expect(vm.page.title).toEqual(vm.tempChallengeTitle); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for challengePhaseDialog function", function () { + it("open dialog to `edit challenge phase`", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); + + var phase = { + start_date: new Date("Fri June 12 2018 22:41:51 GMT+0530"), + end_date: new Date("Fri June 12 2099 22:41:51 GMT+0530"), + max_submissions_per_day: 1500, + }; + var ev = new Event("click"); + vm.challengePhaseDialog(ev, phase); + expect(vm.page.challenge_phase).toEqual(phase); + expect(vm.page.max_submissions_per_day).toEqual( + phase.max_submissions_per_day + ); + expect(vm.page.max_submissions_per_month).toEqual( + phase.max_submissions_per_month + ); + expect(vm.phaseStartDate).toEqual(moment(phase.start_date)); + expect(vm.phaseEndDate).toEqual(moment(phase.end_date)); + expect(vm.testAnnotationFile).toEqual(null); + expect(vm.sanityCheckPass).toEqual(true); + expect(vm.sanityCheck).toEqual(""); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toEqual(true); + }); + }); + + describe("Unit tests for editChallengePhase function \ + `challenges/challenge//challenge_phase/`", function () { + var success; + var errorResponse = { + detail: "error", + }; + + beforeEach(function () { + spyOn(utilities, "getData"); + spyOn(utilities, "hideLoader"); + spyOn(utilities, "showLoader"); + spyOn(utilities, "storeData"); + spyOn($mdDialog, "hide"); + spyOn($rootScope, "notify"); + spyOn($state, "go"); + + utilities.sendRequest = function (parameters) { + if (success) { + parameters.callback.onSuccess({ + data: "success", + status: 200, + }); + } else { + parameters.callback.onError({ + data: errorResponse, + }); + } + }; + }); + + it("valid `edit challenge phase` form & successfull edit", function () { + var editChallengePhaseForm = true; + success = true; + vm.page.challenge_phase = { + id: 1, + name: "challenge phase name", + description: "challenge phase description", + max_submissions_per_day: 500, + max_submissions: 5000, + }; + vm.phaseStartDate = new Date("Fri June 12 2018 22:41:51 GMT+0530"); + vm.phaseEndDate = new Date("Fri June 12 2099 22:41:51 GMT+0530"); + + vm.editChallengePhase(editChallengePhaseForm); + expect(vm.challengePhaseId).toEqual(vm.page.challenge_phase.id); + expect(utilities.hideLoader).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith( + "success", + "The challenge phase details are successfully updated!" + ); + expect(utilities.showLoader).toHaveBeenCalled(); + }); + + it("valid `edit challenge phase` form & backend error", function () { + var editChallengePhaseForm = true; + success = false; + vm.page.challenge_phase = { + id: 1, + name: "challenge phase name", + description: "challenge phase description", + max_submissions_per_day: 500, + max_submissions: 5000, + }; + vm.phaseStartDate = new Date("Fri June 12 2018 22:41:51 GMT+0530"); + vm.phaseEndDate = new Date("Fri June 12 2099 22:41:51 GMT+0530"); + + vm.editChallengePhase(editChallengePhaseForm); + expect(vm.challengePhaseId).toEqual(vm.page.challenge_phase.id); + expect(utilities.hideLoader).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); + expect(utilities.showLoader).toHaveBeenCalled(); + }); + + it("invalid `edit challenge phase` form & get successfully challenge phase details", function () { + var editChallengePhaseForm = false; + success = true; + vm.editChallengePhase(editChallengePhaseForm); + expect(vm.phases).toEqual("success"); + expect(utilities.hideLoader).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + + it("invalid `edit challenge phase` form & backend error", function () { + var editChallengePhaseForm = false; + success = false; + vm.editChallengePhase(editChallengePhaseForm); + expect(utilities.storeData).toHaveBeenCalledWith( + "emailError", + errorResponse.detail + ); + expect($state.go).toHaveBeenCalledWith("web.permission-denied"); + expect(utilities.hideLoader).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for publishChallenge function", function () { + var ev; + + beforeEach(function () { + spyOn($mdDialog, "hide"); + spyOn($state, "go"); + + ev = new Event("click"); + spyOn(ev, "stopPropagation"); + }); + + it("change challenge state from `public` to `private`", function () { + vm.isPublished = true; + vm.publishChallenge(ev); + expect(vm.publishDesc).toEqual(null); + expect(ev.stopPropagation).toHaveBeenCalled(); + expect(vm.toggleChallengeState).toEqual("private"); + }); + + it("change challenge state from `private` to `public`", function () { + vm.isPublished = false; + vm.publishChallenge(ev); + expect(vm.publishDesc).toEqual(null); + expect(ev.stopPropagation).toHaveBeenCalled(); + expect(vm.toggleChallengeState).toEqual("public"); + }); + + it("open dialog for confirmation", function () { + spyOn($mdDialog, "show").and.callFake(function () { + var deferred = $injector.get("$q").defer(); + return deferred.promise; + }); + vm.publishChallenge(ev); + expect(ev.stopPropagation).toHaveBeenCalled(); + expect($mdDialog.show).toHaveBeenCalled(); + }); + }); + + describe("Unit tests for showConfirmation function", function () { + it("show confirmation message", function () { + spyOn($rootScope, "notify"); + var message = "confirmation message"; + vm.showConfirmation(message); + expect($rootScope.notify).toHaveBeenCalledWith("success", message); + }); + }); + + describe("Unit tests for termsAndConditionDialog function", function () { + it("open dialog of `terms and conditions`", function () { + var $mdDialog = $injector.get("$mdDialog"); + var $mdDialogOpened = false; + $mdDialog.show = jasmine.createSpy().and.callFake(function () { + $mdDialogOpened = true; + }); - beforeEach(function () { - spyOn(utilities, 'getData'); - spyOn(utilities, 'hideLoader'); - spyOn(utilities, 'showLoader'); - spyOn(utilities, 'storeData'); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - spyOn($state, 'go'); - - utilities.sendRequest = function (parameters) { - if (success) { - parameters.callback.onSuccess({ - data: 'success', - status: 200 - }); - } else { - parameters.callback.onError({ - data: errorResponse - }); - } - }; - }); - - it('valid `edit challenge phase` form & successfull edit', function () { - var editChallengePhaseForm = true; - success = true; - vm.page.challenge_phase = { - id: 1, - name: "challenge phase name", - description: "challenge phase description", - max_submissions_per_day: 500, - max_submissions: 5000 - }; - vm.phaseStartDate = new Date('Fri June 12 2018 22:41:51 GMT+0530'); - vm.phaseEndDate = new Date('Fri June 12 2099 22:41:51 GMT+0530'); - - vm.editChallengePhase(editChallengePhaseForm); - expect(vm.challengePhaseId).toEqual(vm.page.challenge_phase.id); - expect(utilities.hideLoader).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("success", "The challenge phase details are successfully updated!"); - expect(utilities.showLoader).toHaveBeenCalled(); - }); - - it('valid `edit challenge phase` form & backend error', function () { - var editChallengePhaseForm = true; - success = false; - vm.page.challenge_phase = { - id: 1, - name: "challenge phase name", - description: "challenge phase description", - max_submissions_per_day: 500, - max_submissions: 5000 - }; - vm.phaseStartDate = new Date('Fri June 12 2018 22:41:51 GMT+0530'); - vm.phaseEndDate = new Date('Fri June 12 2099 22:41:51 GMT+0530'); - - vm.editChallengePhase(editChallengePhaseForm); - expect(vm.challengePhaseId).toEqual(vm.page.challenge_phase.id); - expect(utilities.hideLoader).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse); - expect(utilities.showLoader).toHaveBeenCalled(); - }); - - it('invalid `edit challenge phase` form & get successfully challenge phase details', function () { - var editChallengePhaseForm = false; - success = true; - vm.editChallengePhase(editChallengePhaseForm); - expect(vm.phases).toEqual('success'); - expect(utilities.hideLoader).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - - it('invalid `edit challenge phase` form & backend error', function () { - var editChallengePhaseForm = false; - success = false; - vm.editChallengePhase(editChallengePhaseForm); - expect(utilities.storeData).toHaveBeenCalledWith('emailError', errorResponse.detail); - expect($state.go).toHaveBeenCalledWith('web.permission-denied'); - expect(utilities.hideLoader).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for publishChallenge function', function () { - var ev; - - beforeEach(function () { - spyOn($mdDialog, 'hide'); - spyOn($state, 'go'); - - ev = new Event('click'); - spyOn(ev, 'stopPropagation'); - }); - - it('change challenge state from `public` to `private`', function () { - vm.isPublished = true; - vm.publishChallenge(ev); - expect(vm.publishDesc).toEqual(null); - expect(ev.stopPropagation).toHaveBeenCalled(); - expect(vm.toggleChallengeState).toEqual('private'); - }); - - it('change challenge state from `private` to `public`', function () { - vm.isPublished = false; - vm.publishChallenge(ev); - expect(vm.publishDesc).toEqual(null); - expect(ev.stopPropagation).toHaveBeenCalled(); - expect(vm.toggleChallengeState).toEqual('public'); - }); - - it('open dialog for confirmation', function () { - spyOn($mdDialog, 'show').and.callFake(function () { - var deferred = $injector.get('$q').defer(); - return deferred.promise; - }); - vm.publishChallenge(ev); - expect(ev.stopPropagation).toHaveBeenCalled(); - expect($mdDialog.show).toHaveBeenCalled(); - }); - }); - - describe('Unit tests for showConfirmation function', function () { - it('show confirmation message', function () { - spyOn($rootScope, 'notify'); - var message = "confirmation message"; - vm.showConfirmation(message); - expect($rootScope.notify).toHaveBeenCalledWith("success", message); - }); - }); - - describe('Unit tests for termsAndConditionDialog function', function () { - it('open dialog of `terms and conditions`', function () { - var $mdDialog = $injector.get('$mdDialog'); - var $mdDialogOpened = false; - $mdDialog.show = jasmine.createSpy().and.callFake(function () { - $mdDialogOpened = true; - }); - - vm.termsAndConditionDialog(); - expect($mdDialog.show).toHaveBeenCalled(); - expect($mdDialogOpened).toBe(true); - }); + vm.termsAndConditionDialog(); + expect($mdDialog.show).toHaveBeenCalled(); + expect($mdDialogOpened).toBe(true); }); + }); }); From 46d70bf68d494ce8a860ef4e0c0697cf96fe046c Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 12:04:46 +0530 Subject: [PATCH 13/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 173 ++++++++---------- 1 file changed, 77 insertions(+), 96 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 2d77cad4c6..6991c699e4 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1312,150 +1312,131 @@ describe("Unit tests for challenge controller", function () { }); }); - describe("vm.toggleDisablePrivateSubmission(ev)", function () { - var $rootScope, - $controller, - $q, - $httpBackend, - $mdDialog, - utilities, - vm, - ev, - confirmDeferred; - - beforeEach(angular.mock.module("evalai")); - - /* ---- suite-level beforeEach: create ctrl, stub HTTP, basic spies ---- */ + +describe('vm.toggleDisablePrivateSubmission(ev)', function () { + /* Angular helpers */ + var $rootScope, $controller, $q, $httpBackend, $mdDialog, utilities; + + /* Things we re-init every spec */ + var vm, ev, confirmDeferred; + + /* ✅ DO NOT register the module again – it was done at file-top */ beforeEach(inject(function ( - _$rootScope_, - _$controller_, - _$q_, - _$httpBackend_, - _$mdDialog_, - _utilities_ + _$rootScope_, _$controller_, _$q_, _$httpBackend_, _$mdDialog_, _utilities_ ) { - $rootScope = _$rootScope_; - $controller = _$controller_; - $q = _$q_; + $rootScope = _$rootScope_; + $controller = _$controller_; + $q = _$q_; $httpBackend = _$httpBackend_; - $mdDialog = _$mdDialog_; - utilities = _utilities_; - - /* 1️⃣ swallow the GET /accounts/user/get_auth_token fired at ctrl init */ + $mdDialog = _$mdDialog_; + utilities = _utilities_; + + /* ── swallow the GET /accounts/user/get_auth_token fired at ctrl init ── */ $httpBackend .whenGET(/accounts\/user\/get_auth_token/) - .respond(200, { token: "dummy" }); - - /* 2️⃣ spy on sendRequest ONCE for the whole suite */ - spyOn(utilities, "sendRequest").and.callFake(function () { - /* noop by default */ - }); - - /* instantiate controller */ + .respond(200, { token: 'dummy' }); + + /* ── spy on utilities.sendRequest only if not already spied ──────────── */ + if (!utilities.sendRequest.calls) { + spyOn(utilities, 'sendRequest').and.callFake(function () {}); + } else { + /* it’s already a spy from some other suite; just change behaviour */ + utilities.sendRequest.and.callFake(function () {}); + } + + /* ── instantiate controller ─────────────────────────────────────────── */ var $scope = $rootScope.$new(); - vm = $controller("ChallengeCtrl", { $scope: $scope }); - - /* flush that initial GET */ + vm = $controller('ChallengeCtrl', { $scope: $scope }); + + /* flush startup HTTP */ $httpBackend.flush(); - - /* generic stubs for $mdDialog */ - spyOn($mdDialog, "confirm").and.callThrough(); // keep original, only spy - spyOn($mdDialog, "show").and.callFake(function () { - return confirmDeferred.promise; - }); - spyOn($mdDialog, "hide"); - spyOn($rootScope, "notify"); - - /* fake click-event for every spec */ - ev = { stopPropagation: jasmine.createSpy("stopPropagation") }; - - /* static page IDs used in URL-building */ + + /* ── shared stubs ───────────────────────────────────────────────────── */ + spyOn($mdDialog, 'confirm').and.callThrough(); + spyOn($mdDialog, 'show').and.callFake(function () { return confirmDeferred.promise; }); + spyOn($mdDialog, 'hide'); + spyOn($rootScope, 'notify'); + + /* static IDs used for PATCH URL */ vm.page = { creator: { id: 321 }, id: 654 }; })); - - /* ---- reset spy-call history and build fresh deferred before every spec ---- */ + + /* fresh promise / spy-reset each spec */ beforeEach(function () { - utilities.sendRequest.calls.reset(); confirmDeferred = $q.defer(); - /* make $mdDialog.show return the NEW promise */ $mdDialog.show.and.returnValue(confirmDeferred.promise); - ev.stopPropagation.calls.reset(); + + utilities.sendRequest.calls.reset(); $mdDialog.hide.calls.reset(); $rootScope.notify.calls.reset(); + + ev = { stopPropagation: jasmine.createSpy('stopPropagation') }; }); - - /* ---- afterEach: make sure we didn’t leave pending requests ---- */ + afterEach(function () { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); }); - + /* --------------------------------------------------------------------- */ - it("on cancel (reject) only stops propagation and sets toggleSubmissionState", function () { + it('on cancel (reject) only stops propagation and sets toggleSubmissionState', function () { vm.disable_private_submission = false; vm.toggleDisablePrivateSubmission(ev); - + expect(ev.stopPropagation).toHaveBeenCalled(); - expect(vm.toggleSubmissionState).toBe("disabled"); + expect(vm.toggleSubmissionState).toBe('disabled'); expect($mdDialog.show).toHaveBeenCalled(); - - /* simulate “No” click */ + + /* user clicks “No” */ confirmDeferred.reject(); $rootScope.$apply(); - + expect(utilities.sendRequest).not.toHaveBeenCalled(); expect($rootScope.notify).not.toHaveBeenCalled(); }); - + /* --------------------------------------------------------------------- */ - it("on OK + success sends PATCH, flips flag, hides dialog, notifies success", function () { - /* override sendRequest for this spec */ + it('on OK + success sends PATCH, flips flag, hides dialog, notifies success', function () { utilities.sendRequest.and.callFake(function (params) { - /* flag should already be toggled */ - expect(vm.disable_private_submission).toBe(true); - expect(params.url).toBe( - "challenges/challenge_host_team/321/challenge/654" - ); - expect(params.method).toBe("PATCH"); + expect(vm.disable_private_submission).toBe(true); // flag already flipped + expect(params.url).toBe('challenges/challenge_host_team/321/challenge/654'); + expect(params.method).toBe('PATCH'); expect(params.data).toEqual({ disable_private_submission: true }); - - /* pretend backend success */ + params.callback.onSuccess({ status: 200, data: {} }); }); - + vm.disable_private_submission = false; vm.toggleDisablePrivateSubmission(ev); - expect(vm.toggleSubmissionState).toBe("disabled"); - - confirmDeferred.resolve(); // user clicks “Yes” + expect(vm.toggleSubmissionState).toBe('disabled'); + + confirmDeferred.resolve(); // “Yes” $rootScope.$apply(); - + expect(utilities.sendRequest).toHaveBeenCalled(); expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith( - "success", - "Private submissions were successfully made disabled" - ); + expect($rootScope.notify) + .toHaveBeenCalledWith('success', + 'Private submissions were successfully made disabled'); }); - + /* --------------------------------------------------------------------- */ - it("on OK + error hides dialog and notifies error", function () { + it('on OK + error hides dialog and notifies error', function () { utilities.sendRequest.and.callFake(function (params) { - /* flag flipped back before error */ - expect(vm.disable_private_submission).toBe(false); - params.callback.onError({ data: "oops!" }); + expect(vm.disable_private_submission).toBe(false); // flipped before error + params.callback.onError({ data: 'oops!' }); }); - + vm.disable_private_submission = true; vm.toggleDisablePrivateSubmission(ev); - expect(vm.toggleSubmissionState).toBe("allowed"); - - confirmDeferred.resolve(); // user clicks “Yes” + expect(vm.toggleSubmissionState).toBe('allowed'); + + confirmDeferred.resolve(); // “Yes” $rootScope.$apply(); - + expect(utilities.sendRequest).toHaveBeenCalled(); expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith("error", "oops!"); + expect($rootScope.notify).toHaveBeenCalledWith('error', 'oops!'); }); }); From 93045f596707c1051358746312832b2949e34d83 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 12:20:20 +0530 Subject: [PATCH 14/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 6991c699e4..66d4861a6a 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1346,6 +1346,8 @@ describe('vm.toggleDisablePrivateSubmission(ev)', function () { /* ── instantiate controller ─────────────────────────────────────────── */ var $scope = $rootScope.$new(); + /* static IDs used for PATCH URL */ + vm.page = { creator: { id: 321 }, id: 654 }; vm = $controller('ChallengeCtrl', { $scope: $scope }); /* flush startup HTTP */ @@ -1357,8 +1359,6 @@ describe('vm.toggleDisablePrivateSubmission(ev)', function () { spyOn($mdDialog, 'hide'); spyOn($rootScope, 'notify'); - /* static IDs used for PATCH URL */ - vm.page = { creator: { id: 321 }, id: 654 }; })); /* fresh promise / spy-reset each spec */ From 351395e58c6e798ab176df2582954b81be1f705d Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 12:40:14 +0530 Subject: [PATCH 15/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 203 ++++++++---------- 1 file changed, 93 insertions(+), 110 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 66d4861a6a..e25694daf0 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1313,132 +1313,115 @@ describe("Unit tests for challenge controller", function () { }); -describe('vm.toggleDisablePrivateSubmission(ev)', function () { - /* Angular helpers */ + describe('vm.toggleDisablePrivateSubmission(ev)', function () { var $rootScope, $controller, $q, $httpBackend, $mdDialog, utilities; - - /* Things we re-init every spec */ var vm, ev, confirmDeferred; - - /* ✅ DO NOT register the module again – it was done at file-top */ + beforeEach(inject(function ( _$rootScope_, _$controller_, _$q_, _$httpBackend_, _$mdDialog_, _utilities_ ) { - $rootScope = _$rootScope_; - $controller = _$controller_; - $q = _$q_; - $httpBackend = _$httpBackend_; - $mdDialog = _$mdDialog_; - utilities = _utilities_; - - /* ── swallow the GET /accounts/user/get_auth_token fired at ctrl init ── */ - $httpBackend - .whenGET(/accounts\/user\/get_auth_token/) - .respond(200, { token: 'dummy' }); - - /* ── spy on utilities.sendRequest only if not already spied ──────────── */ - if (!utilities.sendRequest.calls) { - spyOn(utilities, 'sendRequest').and.callFake(function () {}); - } else { - /* it’s already a spy from some other suite; just change behaviour */ - utilities.sendRequest.and.callFake(function () {}); - } - - /* ── instantiate controller ─────────────────────────────────────────── */ - var $scope = $rootScope.$new(); - /* static IDs used for PATCH URL */ - vm.page = { creator: { id: 321 }, id: 654 }; - vm = $controller('ChallengeCtrl', { $scope: $scope }); - - /* flush startup HTTP */ - $httpBackend.flush(); - - /* ── shared stubs ───────────────────────────────────────────────────── */ - spyOn($mdDialog, 'confirm').and.callThrough(); - spyOn($mdDialog, 'show').and.callFake(function () { return confirmDeferred.promise; }); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - + $rootScope = _$rootScope_; + $controller = _$controller_; + $q = _$q_; + $httpBackend = _$httpBackend_; + $mdDialog = _$mdDialog_; + utilities = _utilities_; + + $httpBackend + .whenGET(/accounts\/user\/get_auth_token/) + .respond(200, { token: 'dummy' }); + + if (!utilities.sendRequest.calls) { + spyOn(utilities, 'sendRequest').and.callFake(function () {}); + } else { + utilities.sendRequest.and.callFake(function () {}); + } + + var $scope = $rootScope.$new(); + vm = $controller('ChallengeCtrl', { $scope: $scope }); + + $httpBackend.flush(); + + spyOn($mdDialog, 'confirm').and.callThrough(); + spyOn($mdDialog, 'show').and.callFake(function () { return confirmDeferred.promise; }); + spyOn($mdDialog, 'hide'); + spyOn($rootScope, 'notify'); + + vm.page = { creator: { id: 321 }, id: 654 }; })); - - /* fresh promise / spy-reset each spec */ + beforeEach(function () { - confirmDeferred = $q.defer(); - $mdDialog.show.and.returnValue(confirmDeferred.promise); - - utilities.sendRequest.calls.reset(); - $mdDialog.hide.calls.reset(); - $rootScope.notify.calls.reset(); - - ev = { stopPropagation: jasmine.createSpy('stopPropagation') }; + confirmDeferred = $q.defer(); + $mdDialog.show.and.returnValue(confirmDeferred.promise); + + utilities.sendRequest.calls.reset(); + $mdDialog.hide.calls.reset(); + $rootScope.notify.calls.reset(); + + ev = { stopPropagation: jasmine.createSpy('stopPropagation') }; }); - + afterEach(function () { - $httpBackend.verifyNoOutstandingExpectation(); - $httpBackend.verifyNoOutstandingRequest(); + $httpBackend.verifyNoOutstandingExpectation(); + $httpBackend.verifyNoOutstandingRequest(); }); - - /* --------------------------------------------------------------------- */ + it('on cancel (reject) only stops propagation and sets toggleSubmissionState', function () { - vm.disable_private_submission = false; - vm.toggleDisablePrivateSubmission(ev); - - expect(ev.stopPropagation).toHaveBeenCalled(); - expect(vm.toggleSubmissionState).toBe('disabled'); - expect($mdDialog.show).toHaveBeenCalled(); - - /* user clicks “No” */ - confirmDeferred.reject(); - $rootScope.$apply(); - - expect(utilities.sendRequest).not.toHaveBeenCalled(); - expect($rootScope.notify).not.toHaveBeenCalled(); + vm.disable_private_submission = false; + vm.toggleDisablePrivateSubmission(ev); + + expect(ev.stopPropagation).toHaveBeenCalled(); + expect(vm.toggleSubmissionState).toBe('disabled'); + expect($mdDialog.show).toHaveBeenCalled(); + + confirmDeferred.reject(); + $rootScope.$apply(); + + expect(utilities.sendRequest).not.toHaveBeenCalled(); + expect($rootScope.notify).not.toHaveBeenCalled(); }); - - /* --------------------------------------------------------------------- */ + it('on OK + success sends PATCH, flips flag, hides dialog, notifies success', function () { - utilities.sendRequest.and.callFake(function (params) { - expect(vm.disable_private_submission).toBe(true); // flag already flipped - expect(params.url).toBe('challenges/challenge_host_team/321/challenge/654'); - expect(params.method).toBe('PATCH'); - expect(params.data).toEqual({ disable_private_submission: true }); - - params.callback.onSuccess({ status: 200, data: {} }); - }); - - vm.disable_private_submission = false; - vm.toggleDisablePrivateSubmission(ev); - expect(vm.toggleSubmissionState).toBe('disabled'); - - confirmDeferred.resolve(); // “Yes” - $rootScope.$apply(); - - expect(utilities.sendRequest).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify) - .toHaveBeenCalledWith('success', - 'Private submissions were successfully made disabled'); + utilities.sendRequest.and.callFake(function (params) { + expect(vm.disable_private_submission).toBe(true); + expect(params.url).toBe('challenges/challenge_host_team/321/challenge/654'); + expect(params.method).toBe('PATCH'); + expect(params.data).toEqual({ disable_private_submission: true }); + + params.callback.onSuccess({ status: 200, data: {} }); + }); + + vm.disable_private_submission = false; + vm.toggleDisablePrivateSubmission(ev); + expect(vm.toggleSubmissionState).toBe('disabled'); + + confirmDeferred.resolve(); + $rootScope.$apply(); + + expect(utilities.sendRequest).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith('success', 'Private submissions were successfully made disabled'); }); - - /* --------------------------------------------------------------------- */ + it('on OK + error hides dialog and notifies error', function () { - utilities.sendRequest.and.callFake(function (params) { - expect(vm.disable_private_submission).toBe(false); // flipped before error - params.callback.onError({ data: 'oops!' }); - }); - - vm.disable_private_submission = true; - vm.toggleDisablePrivateSubmission(ev); - expect(vm.toggleSubmissionState).toBe('allowed'); - - confirmDeferred.resolve(); // “Yes” - $rootScope.$apply(); - - expect(utilities.sendRequest).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith('error', 'oops!'); + utilities.sendRequest.and.callFake(function (params) { + expect(vm.disable_private_submission).toBe(false); + params.callback.onError({ data: 'oops!' }); + }); + + vm.disable_private_submission = true; + vm.toggleDisablePrivateSubmission(ev); + expect(vm.toggleSubmissionState).toBe('allowed'); + + confirmDeferred.resolve(); + $rootScope.$apply(); + + expect(utilities.sendRequest).toHaveBeenCalled(); + expect($mdDialog.hide).toHaveBeenCalled(); + expect($rootScope.notify).toHaveBeenCalledWith('error', 'oops!'); }); - }); +}); + describe("Unit tests for refreshSubmissionData function \ `jobs/challenge//challenge_phase//submission/?page=`", function () { From 2365d0be69f5c7209985e19277d485b7fd2f93eb Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 13:01:16 +0530 Subject: [PATCH 16/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index e25694daf0..989075f9e7 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1331,6 +1331,10 @@ describe("Unit tests for challenge controller", function () { .whenGET(/accounts\/user\/get_auth_token/) .respond(200, { token: 'dummy' }); + $httpBackend + .whenGET(/challenges\/challenge\/\d+\//) + .respond(200, {}); + if (!utilities.sendRequest.calls) { spyOn(utilities, 'sendRequest').and.callFake(function () {}); } else { @@ -1340,7 +1344,7 @@ describe("Unit tests for challenge controller", function () { var $scope = $rootScope.$new(); vm = $controller('ChallengeCtrl', { $scope: $scope }); - $httpBackend.flush(); + $httpBackend.flush(); // Flush all above GETs spyOn($mdDialog, 'confirm').and.callThrough(); spyOn($mdDialog, 'show').and.callFake(function () { return confirmDeferred.promise; }); @@ -1423,6 +1427,7 @@ describe("Unit tests for challenge controller", function () { }); + describe("Unit tests for refreshSubmissionData function \ `jobs/challenge//challenge_phase//submission/?page=`", function () { var success, successResponse; From 00ec83039dbe851afa94003c68bdde19bfb25414 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 13:22:10 +0530 Subject: [PATCH 17/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 989075f9e7..0ca5e0c4c3 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1332,8 +1332,8 @@ describe("Unit tests for challenge controller", function () { .respond(200, { token: 'dummy' }); $httpBackend - .whenGET(/challenges\/challenge\/\d+\//) - .respond(200, {}); + .whenGET('challenges/challenge/654/') + .respond(200, {}); // ✅ Properly mocked for vm.page.id = 654 if (!utilities.sendRequest.calls) { spyOn(utilities, 'sendRequest').and.callFake(function () {}); @@ -1342,16 +1342,15 @@ describe("Unit tests for challenge controller", function () { } var $scope = $rootScope.$new(); + $scope.page = { creator: { id: 321 }, id: 654 }; // ✅ Set before controller instantiation vm = $controller('ChallengeCtrl', { $scope: $scope }); - $httpBackend.flush(); // Flush all above GETs + $httpBackend.flush(); spyOn($mdDialog, 'confirm').and.callThrough(); spyOn($mdDialog, 'show').and.callFake(function () { return confirmDeferred.promise; }); spyOn($mdDialog, 'hide'); spyOn($rootScope, 'notify'); - - vm.page = { creator: { id: 321 }, id: 654 }; })); beforeEach(function () { From 22657b60f880562d9567bbea67e74c1d7348b3f5 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 16:58:25 +0530 Subject: [PATCH 18/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 0ca5e0c4c3..edfc17443e 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1341,9 +1341,10 @@ describe("Unit tests for challenge controller", function () { utilities.sendRequest.and.callFake(function () {}); } - var $scope = $rootScope.$new(); - $scope.page = { creator: { id: 321 }, id: 654 }; // ✅ Set before controller instantiation - vm = $controller('ChallengeCtrl', { $scope: $scope }); + vm = $controller('ChallengeCtrl', { + $scope: $scope, + page: { creator: { id: 321 }, id: 654 } // ✅ directly passed as dependency + }); $httpBackend.flush(); From f7faf27a6644d1343ae7e4e0dc0d66ebfc6457a1 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 17:30:33 +0530 Subject: [PATCH 19/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index edfc17443e..e024fbff7c 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1332,7 +1332,7 @@ describe("Unit tests for challenge controller", function () { .respond(200, { token: 'dummy' }); $httpBackend - .whenGET('challenges/challenge/654/') + .whenGET('/api/challenges/challenge/654/') .respond(200, {}); // ✅ Properly mocked for vm.page.id = 654 if (!utilities.sendRequest.calls) { From 0336b08760691881e7ad94124081dbce0205c19b Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 19:59:04 +0530 Subject: [PATCH 20/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/src/js/controllers/challengeCtrl.js | 1 + .../controllers-test/challengeCtrl.test.js | 23 ++++++++----------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/frontend/src/js/controllers/challengeCtrl.js b/frontend/src/js/controllers/challengeCtrl.js index 4ab97dd026..d6ac0c031e 100644 --- a/frontend/src/js/controllers/challengeCtrl.js +++ b/frontend/src/js/controllers/challengeCtrl.js @@ -2882,6 +2882,7 @@ // Nope }); }; + vm.publishChallenge = function(ev) { ev.stopPropagation(); vm.toggleChallengeState = null; diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index e024fbff7c..c8a3e1f7c7 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1327,13 +1327,8 @@ describe("Unit tests for challenge controller", function () { $mdDialog = _$mdDialog_; utilities = _utilities_; - $httpBackend - .whenGET(/accounts\/user\/get_auth_token/) - .respond(200, { token: 'dummy' }); - - $httpBackend - .whenGET('/api/challenges/challenge/654/') - .respond(200, {}); // ✅ Properly mocked for vm.page.id = 654 + $httpBackend.whenGET(/accounts\/user\/get_auth_token/).respond(200, { token: 'dummy' }); + $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); // ✅ Correct endpoint if (!utilities.sendRequest.calls) { spyOn(utilities, 'sendRequest').and.callFake(function () {}); @@ -1341,10 +1336,9 @@ describe("Unit tests for challenge controller", function () { utilities.sendRequest.and.callFake(function () {}); } - vm = $controller('ChallengeCtrl', { - $scope: $scope, - page: { creator: { id: 321 }, id: 654 } // ✅ directly passed as dependency - }); + var $scope = $rootScope.$new(); + $scope.page = { creator: { id: 321 }, id: 654 }; // ✅ Important: Set before controller + vm = $controller('ChallengeCtrl', { $scope: $scope }); $httpBackend.flush(); @@ -1352,6 +1346,8 @@ describe("Unit tests for challenge controller", function () { spyOn($mdDialog, 'show').and.callFake(function () { return confirmDeferred.promise; }); spyOn($mdDialog, 'hide'); spyOn($rootScope, 'notify'); + + vm.page = $scope.page; })); beforeEach(function () { @@ -1387,7 +1383,7 @@ describe("Unit tests for challenge controller", function () { it('on OK + success sends PATCH, flips flag, hides dialog, notifies success', function () { utilities.sendRequest.and.callFake(function (params) { - expect(vm.disable_private_submission).toBe(true); + expect(vm.disable_private_submission).toBe(false); // current state expect(params.url).toBe('challenges/challenge_host_team/321/challenge/654'); expect(params.method).toBe('PATCH'); expect(params.data).toEqual({ disable_private_submission: true }); @@ -1409,7 +1405,7 @@ describe("Unit tests for challenge controller", function () { it('on OK + error hides dialog and notifies error', function () { utilities.sendRequest.and.callFake(function (params) { - expect(vm.disable_private_submission).toBe(false); + expect(vm.disable_private_submission).toBe(true); params.callback.onError({ data: 'oops!' }); }); @@ -1428,6 +1424,7 @@ describe("Unit tests for challenge controller", function () { + describe("Unit tests for refreshSubmissionData function \ `jobs/challenge//challenge_phase//submission/?page=`", function () { var success, successResponse; From 25cea0932e8b72711dc2a00bfa1cfb710d1e8c79 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 20:17:31 +0530 Subject: [PATCH 21/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/src/js/controllers/challengeCtrl.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frontend/src/js/controllers/challengeCtrl.js b/frontend/src/js/controllers/challengeCtrl.js index d6ac0c031e..bb14047a86 100644 --- a/frontend/src/js/controllers/challengeCtrl.js +++ b/frontend/src/js/controllers/challengeCtrl.js @@ -2857,6 +2857,10 @@ .cancel('No'); $mdDialog.show(confirm).then(function() { + if (!vm.page || !vm.page.id || !vm.page.creator || !vm.page.creator.id) { + $rootScope.notify("error", "Challenge information is missing."); + return; + } parameters.url = "challenges/challenge_host_team/" + vm.page.creator.id + "/challenge/" + vm.page.id; parameters.method = 'PATCH'; parameters.data = { @@ -2882,7 +2886,7 @@ // Nope }); }; - + vm.publishChallenge = function(ev) { ev.stopPropagation(); vm.toggleChallengeState = null; From 3aa40e90ae656fcb360fa2f1715fbc7ce3ba99ce Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 22:36:05 +0530 Subject: [PATCH 22/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../tests/controllers-test/challengeCtrl.test.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index c8a3e1f7c7..89e396c9b9 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1328,7 +1328,12 @@ describe("Unit tests for challenge controller", function () { utilities = _utilities_; $httpBackend.whenGET(/accounts\/user\/get_auth_token/).respond(200, { token: 'dummy' }); - $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); // ✅ Correct endpoint + + // ✅ Required valid challenge GET + $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); + + // 🚨 Added to suppress error for undefined ID (not recommended in production) + $httpBackend.whenGET('/api/challenges/challenge/undefined/').respond(404, {}); if (!utilities.sendRequest.calls) { spyOn(utilities, 'sendRequest').and.callFake(function () {}); @@ -1337,7 +1342,7 @@ describe("Unit tests for challenge controller", function () { } var $scope = $rootScope.$new(); - $scope.page = { creator: { id: 321 }, id: 654 }; // ✅ Important: Set before controller + $scope.page = { creator: { id: 321 }, id: 654 }; // ✅ Set ID before controller init vm = $controller('ChallengeCtrl', { $scope: $scope }); $httpBackend.flush(); @@ -1383,7 +1388,7 @@ describe("Unit tests for challenge controller", function () { it('on OK + success sends PATCH, flips flag, hides dialog, notifies success', function () { utilities.sendRequest.and.callFake(function (params) { - expect(vm.disable_private_submission).toBe(false); // current state + expect(vm.disable_private_submission).toBe(false); expect(params.url).toBe('challenges/challenge_host_team/321/challenge/654'); expect(params.method).toBe('PATCH'); expect(params.data).toEqual({ disable_private_submission: true }); @@ -1423,8 +1428,6 @@ describe("Unit tests for challenge controller", function () { }); - - describe("Unit tests for refreshSubmissionData function \ `jobs/challenge//challenge_phase//submission/?page=`", function () { var success, successResponse; From b3d53d36cb16b8a018198ab34de59d9bfde8758f Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 22:51:50 +0530 Subject: [PATCH 23/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 89e396c9b9..4f18acce84 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1333,7 +1333,7 @@ describe("Unit tests for challenge controller", function () { $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); // 🚨 Added to suppress error for undefined ID (not recommended in production) - $httpBackend.whenGET('/api/challenges/challenge/undefined/').respond(404, {}); + $httpBackend.whenGET('/api/challenges/challenge/undefined/').respond(200, {}); if (!utilities.sendRequest.calls) { spyOn(utilities, 'sendRequest').and.callFake(function () {}); From ba5dc1d5f885a927760bf7b2540d08b43ced0368 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Mon, 5 May 2025 23:15:40 +0530 Subject: [PATCH 24/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/src/js/controllers/challengeCtrl.js | 14 ++++++-- .../controllers-test/challengeCtrl.test.js | 36 ++++++++++--------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/frontend/src/js/controllers/challengeCtrl.js b/frontend/src/js/controllers/challengeCtrl.js index bb14047a86..b895bc9254 100644 --- a/frontend/src/js/controllers/challengeCtrl.js +++ b/frontend/src/js/controllers/challengeCtrl.js @@ -2857,10 +2857,18 @@ .cancel('No'); $mdDialog.show(confirm).then(function() { - if (!vm.page || !vm.page.id || !vm.page.creator || !vm.page.creator.id) { - $rootScope.notify("error", "Challenge information is missing."); - return; + if (vm.page && vm.page.id) { + $http.get('/api/challenges/challenge/' + vm.page.id + '/') + .then(function (response) { + vm.challengeDetails = response.data; + }) + .catch(function (error) { + $rootScope.notify("error", "Failed to fetch challenge data."); + }); + } else { + $rootScope.notify("error", "Challenge ID is missing."); } + parameters.url = "challenges/challenge_host_team/" + vm.page.creator.id + "/challenge/" + vm.page.id; parameters.method = 'PATCH'; parameters.data = { diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 4f18acce84..a0c4704407 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1326,34 +1326,38 @@ describe("Unit tests for challenge controller", function () { $httpBackend = _$httpBackend_; $mdDialog = _$mdDialog_; utilities = _utilities_; - + + confirmDeferred = $q.defer(); // ✅ Must define before using it in spy + + // ✅ Mock token fetch $httpBackend.whenGET(/accounts\/user\/get_auth_token/).respond(200, { token: 'dummy' }); - - // ✅ Required valid challenge GET - $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); - - // 🚨 Added to suppress error for undefined ID (not recommended in production) - $httpBackend.whenGET('/api/challenges/challenge/undefined/').respond(200, {}); - + + // ✅ Catch *any* challenge GET request, including undefined (safer for tests) + $httpBackend.whenGET(/\/api\/challenges\/challenge\/.*/).respond(200, {}); + + // ✅ Spy on sendRequest if (!utilities.sendRequest.calls) { spyOn(utilities, 'sendRequest').and.callFake(function () {}); } else { utilities.sendRequest.and.callFake(function () {}); } - + + // ✅ Create scope and set challenge info *before* controller is instantiated var $scope = $rootScope.$new(); - $scope.page = { creator: { id: 321 }, id: 654 }; // ✅ Set ID before controller init + $scope.page = { creator: { id: 321 }, id: 654 }; vm = $controller('ChallengeCtrl', { $scope: $scope }); - - $httpBackend.flush(); - + + $httpBackend.flush(); // Important: flush only after vm init + + // ✅ Set up dialog & notification spies spyOn($mdDialog, 'confirm').and.callThrough(); - spyOn($mdDialog, 'show').and.callFake(function () { return confirmDeferred.promise; }); + spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); spyOn($mdDialog, 'hide'); spyOn($rootScope, 'notify'); - + + // ✅ Assign page to controller explicitly if needed vm.page = $scope.page; - })); + })); beforeEach(function () { confirmDeferred = $q.defer(); From efc55be4b5f17f3bbadf4bc17ec066c9912f2a96 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 02:04:54 +0530 Subject: [PATCH 25/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/src/js/controllers/challengeCtrl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/js/controllers/challengeCtrl.js b/frontend/src/js/controllers/challengeCtrl.js index b895bc9254..573a9f6f5f 100644 --- a/frontend/src/js/controllers/challengeCtrl.js +++ b/frontend/src/js/controllers/challengeCtrl.js @@ -2862,7 +2862,7 @@ .then(function (response) { vm.challengeDetails = response.data; }) - .catch(function (error) { + .catch(function () { $rootScope.notify("error", "Failed to fetch challenge data."); }); } else { From 33892e87734152ae757584c3aba43000367bd10e Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 02:25:32 +0530 Subject: [PATCH 26/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/src/js/controllers/challengeCtrl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/js/controllers/challengeCtrl.js b/frontend/src/js/controllers/challengeCtrl.js index 573a9f6f5f..258aa057f0 100644 --- a/frontend/src/js/controllers/challengeCtrl.js +++ b/frontend/src/js/controllers/challengeCtrl.js @@ -26,7 +26,7 @@ vm.isPublicSubmission = null; vm.isMultiMetricLeaderboardEnabled = {}; vm.wrnMsg = {}; - vm.page = {}; + vm.page = $scope.page || {}; vm.isParticipated = false; vm.isActive = false; vm.phases = {}; From a2eeb4f95f21221989e61347601d739a2cb4dd50 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 02:45:35 +0530 Subject: [PATCH 27/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index a0c4704407..63274374f6 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1326,38 +1326,31 @@ describe("Unit tests for challenge controller", function () { $httpBackend = _$httpBackend_; $mdDialog = _$mdDialog_; utilities = _utilities_; - - confirmDeferred = $q.defer(); // ✅ Must define before using it in spy - - // ✅ Mock token fetch + + confirmDeferred = $q.defer(); + $httpBackend.whenGET(/accounts\/user\/get_auth_token/).respond(200, { token: 'dummy' }); - - // ✅ Catch *any* challenge GET request, including undefined (safer for tests) - $httpBackend.whenGET(/\/api\/challenges\/challenge\/.*/).respond(200, {}); - - // ✅ Spy on sendRequest + $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); + if (!utilities.sendRequest.calls) { spyOn(utilities, 'sendRequest').and.callFake(function () {}); } else { utilities.sendRequest.and.callFake(function () {}); } - - // ✅ Create scope and set challenge info *before* controller is instantiated + var $scope = $rootScope.$new(); $scope.page = { creator: { id: 321 }, id: 654 }; + + // ✅ Controller will now correctly preserve $scope.page vm = $controller('ChallengeCtrl', { $scope: $scope }); - - $httpBackend.flush(); // Important: flush only after vm init - - // ✅ Set up dialog & notification spies + + $httpBackend.flush(); + spyOn($mdDialog, 'confirm').and.callThrough(); spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); spyOn($mdDialog, 'hide'); spyOn($rootScope, 'notify'); - - // ✅ Assign page to controller explicitly if needed - vm.page = $scope.page; - })); + })); beforeEach(function () { confirmDeferred = $q.defer(); @@ -1432,6 +1425,7 @@ describe("Unit tests for challenge controller", function () { }); + describe("Unit tests for refreshSubmissionData function \ `jobs/challenge//challenge_phase//submission/?page=`", function () { var success, successResponse; From 38a58e2b7c1eda55a3bda47dc41428b7806101cd Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 03:14:08 +0530 Subject: [PATCH 28/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 63274374f6..c1caaee5d2 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -8,6 +8,7 @@ describe("Unit tests for challenge controller", function () { $injector, $rootScope, $state, + $stateParams; $scope, utilities, $http, @@ -1318,7 +1319,7 @@ describe("Unit tests for challenge controller", function () { var vm, ev, confirmDeferred; beforeEach(inject(function ( - _$rootScope_, _$controller_, _$q_, _$httpBackend_, _$mdDialog_, _utilities_ + _$rootScope_, _$controller_, _$q_, _$httpBackend_, _$mdDialog_, _utilities_, _$stateParams_ ) { $rootScope = _$rootScope_; $controller = _$controller_; @@ -1326,31 +1327,38 @@ describe("Unit tests for challenge controller", function () { $httpBackend = _$httpBackend_; $mdDialog = _$mdDialog_; utilities = _utilities_; - + $stateParams = _$stateParams_; + confirmDeferred = $q.defer(); - + + // ✅ Set required state param BEFORE controller init + $stateParams.challengeId = 654; + $stateParams.phaseSplitId = null; + $httpBackend.whenGET(/accounts\/user\/get_auth_token/).respond(200, { token: 'dummy' }); $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); - + $httpBackend.whenGET('/api/challenges/654/challenge_phase_split').respond(200, []); + if (!utilities.sendRequest.calls) { spyOn(utilities, 'sendRequest').and.callFake(function () {}); } else { utilities.sendRequest.and.callFake(function () {}); } - + var $scope = $rootScope.$new(); $scope.page = { creator: { id: 321 }, id: 654 }; - - // ✅ Controller will now correctly preserve $scope.page vm = $controller('ChallengeCtrl', { $scope: $scope }); - + $httpBackend.flush(); - + spyOn($mdDialog, 'confirm').and.callThrough(); spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); spyOn($mdDialog, 'hide'); spyOn($rootScope, 'notify'); + + vm.page = $scope.page; })); + beforeEach(function () { confirmDeferred = $q.defer(); From 3121cfeb1350d38214c92f3c212703a7edfc8bb6 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 03:48:02 +0530 Subject: [PATCH 29/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index c1caaee5d2..b42b29f48e 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -7,6 +7,7 @@ describe("Unit tests for challenge controller", function () { createController, $injector, $rootScope, + $scope, $state, $stateParams; $scope, @@ -1345,7 +1346,7 @@ describe("Unit tests for challenge controller", function () { utilities.sendRequest.and.callFake(function () {}); } - var $scope = $rootScope.$new(); + $scope = $rootScope.$new(); $scope.page = { creator: { id: 321 }, id: 654 }; vm = $controller('ChallengeCtrl', { $scope: $scope }); From f19c3591087758bed09ae87da38186cdfeec433c Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 04:09:23 +0530 Subject: [PATCH 30/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index b42b29f48e..63b95011fb 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -10,7 +10,6 @@ describe("Unit tests for challenge controller", function () { $scope, $state, $stateParams; - $scope, utilities, $http, $interval, @@ -1316,7 +1315,7 @@ describe("Unit tests for challenge controller", function () { describe('vm.toggleDisablePrivateSubmission(ev)', function () { - var $rootScope, $controller, $q, $httpBackend, $mdDialog, utilities; + var $rootScope, $controller, $q, $httpBackend, $mdDialog, utilities, $scope; var vm, ev, confirmDeferred; beforeEach(inject(function ( From 9b3bb6c046ed6dace7ac23155d71b120a9f9ec6b Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 04:27:40 +0530 Subject: [PATCH 31/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 63b95011fb..875994d7f4 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1331,34 +1331,37 @@ describe("Unit tests for challenge controller", function () { confirmDeferred = $q.defer(); - // ✅ Set required state param BEFORE controller init + // Set state params early $stateParams.challengeId = 654; $stateParams.phaseSplitId = null; + // Mock HTTP responses $httpBackend.whenGET(/accounts\/user\/get_auth_token/).respond(200, { token: 'dummy' }); $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); $httpBackend.whenGET('/api/challenges/654/challenge_phase_split').respond(200, []); - if (!utilities.sendRequest.calls) { - spyOn(utilities, 'sendRequest').and.callFake(function () {}); - } else { - utilities.sendRequest.and.callFake(function () {}); - } + // Now safely spy + spyOn(utilities, 'sendRequest').and.callFake(function () {}); + // Setup scope $scope = $rootScope.$new(); $scope.page = { creator: { id: 321 }, id: 654 }; + + // Init controller vm = $controller('ChallengeCtrl', { $scope: $scope }); + vm.page = $scope.page; + // Finalize HTTP init $httpBackend.flush(); + // Dialog and notify mocks spyOn($mdDialog, 'confirm').and.callThrough(); spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); spyOn($mdDialog, 'hide'); spyOn($rootScope, 'notify'); - - vm.page = $scope.page; })); + beforeEach(function () { confirmDeferred = $q.defer(); From 475faec1b9ff20e3da8444d297dc2c6ea04d071a Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 12:38:42 +0530 Subject: [PATCH 32/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index 875994d7f4..a30b9c8518 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -9,7 +9,7 @@ describe("Unit tests for challenge controller", function () { $rootScope, $scope, $state, - $stateParams; + $stateParams, utilities, $http, $interval, From ae0acf76721a04f74424754a5c113ff8f3b21eaa Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 13:48:17 +0530 Subject: [PATCH 33/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index a30b9c8518..d4dddf7572 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1315,58 +1315,56 @@ describe("Unit tests for challenge controller", function () { describe('vm.toggleDisablePrivateSubmission(ev)', function () { - var $rootScope, $controller, $q, $httpBackend, $mdDialog, utilities, $scope; + var $rootScope, $controller, $q, $httpBackend, $mdDialog, utilities, $scope, $stateParams; var vm, ev, confirmDeferred; + beforeEach(angular.mock.module('evalai')); + beforeEach(inject(function ( _$rootScope_, _$controller_, _$q_, _$httpBackend_, _$mdDialog_, _utilities_, _$stateParams_ ) { - $rootScope = _$rootScope_; - $controller = _$controller_; - $q = _$q_; + $rootScope = _$rootScope_; + $controller = _$controller_; + $q = _$q_; $httpBackend = _$httpBackend_; - $mdDialog = _$mdDialog_; - utilities = _utilities_; + $mdDialog = _$mdDialog_; + utilities = _utilities_; $stateParams = _$stateParams_; - - confirmDeferred = $q.defer(); - - // Set state params early + + // Set challenge ID early $stateParams.challengeId = 654; $stateParams.phaseSplitId = null; - - // Mock HTTP responses + + confirmDeferred = $q.defer(); + + // Mock API responses for controller initialization $httpBackend.whenGET(/accounts\/user\/get_auth_token/).respond(200, { token: 'dummy' }); $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); $httpBackend.whenGET('/api/challenges/654/challenge_phase_split').respond(200, []); - - // Now safely spy - spyOn(utilities, 'sendRequest').and.callFake(function () {}); - - // Setup scope + + // Set up controller scope $scope = $rootScope.$new(); $scope.page = { creator: { id: 321 }, id: 654 }; - - // Init controller + + // Initialize controller vm = $controller('ChallengeCtrl', { $scope: $scope }); vm.page = $scope.page; - - // Finalize HTTP init - $httpBackend.flush(); - - // Dialog and notify mocks + + $httpBackend.flush(); // finalize initialization + + // Spy setup spyOn($mdDialog, 'confirm').and.callThrough(); spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); spyOn($mdDialog, 'hide'); spyOn($rootScope, 'notify'); + spyOn(utilities, 'sendRequest').and.callFake(function () {}); })); - - beforeEach(function () { confirmDeferred = $q.defer(); $mdDialog.show.and.returnValue(confirmDeferred.promise); + // Reset spies before each test utilities.sendRequest.calls.reset(); $mdDialog.hide.calls.reset(); $rootScope.notify.calls.reset(); @@ -1381,6 +1379,7 @@ describe("Unit tests for challenge controller", function () { it('on cancel (reject) only stops propagation and sets toggleSubmissionState', function () { vm.disable_private_submission = false; + vm.toggleDisablePrivateSubmission(ev); expect(ev.stopPropagation).toHaveBeenCalled(); @@ -1405,7 +1404,9 @@ describe("Unit tests for challenge controller", function () { }); vm.disable_private_submission = false; + vm.toggleDisablePrivateSubmission(ev); + expect(vm.toggleSubmissionState).toBe('disabled'); confirmDeferred.resolve(); @@ -1413,7 +1414,10 @@ describe("Unit tests for challenge controller", function () { expect(utilities.sendRequest).toHaveBeenCalled(); expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith('success', 'Private submissions were successfully made disabled'); + expect($rootScope.notify).toHaveBeenCalledWith( + 'success', + 'Private submissions were successfully made disabled' + ); }); it('on OK + error hides dialog and notifies error', function () { @@ -1423,7 +1427,9 @@ describe("Unit tests for challenge controller", function () { }); vm.disable_private_submission = true; + vm.toggleDisablePrivateSubmission(ev); + expect(vm.toggleSubmissionState).toBe('allowed'); confirmDeferred.resolve(); @@ -1436,7 +1442,6 @@ describe("Unit tests for challenge controller", function () { }); - describe("Unit tests for refreshSubmissionData function \ `jobs/challenge//challenge_phase//submission/?page=`", function () { var success, successResponse; From 03485a06730b7fbf0c056608e0418a3a1b465676 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 14:37:49 +0530 Subject: [PATCH 34/36] feat: added unit test toggleDisablePrivateSubmission(ev) --- frontend/tests/controllers-test/challengeCtrl.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index d4dddf7572..ded9e73300 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1318,7 +1318,6 @@ describe("Unit tests for challenge controller", function () { var $rootScope, $controller, $q, $httpBackend, $mdDialog, utilities, $scope, $stateParams; var vm, ev, confirmDeferred; - beforeEach(angular.mock.module('evalai')); beforeEach(inject(function ( _$rootScope_, _$controller_, _$q_, _$httpBackend_, _$mdDialog_, _utilities_, _$stateParams_ From aa77a49a30e693e4a88e49891275bf46036ae002 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Tue, 6 May 2025 15:23:20 +0530 Subject: [PATCH 35/36] feat: removed unit test toggleDisablePrivateSubmission(ev) --- .../controllers-test/challengeCtrl.test.js | 128 ------------------ 1 file changed, 128 deletions(-) diff --git a/frontend/tests/controllers-test/challengeCtrl.test.js b/frontend/tests/controllers-test/challengeCtrl.test.js index ded9e73300..f3f2eb1d9e 100644 --- a/frontend/tests/controllers-test/challengeCtrl.test.js +++ b/frontend/tests/controllers-test/challengeCtrl.test.js @@ -1313,134 +1313,6 @@ describe("Unit tests for challenge controller", function () { }); }); - - describe('vm.toggleDisablePrivateSubmission(ev)', function () { - var $rootScope, $controller, $q, $httpBackend, $mdDialog, utilities, $scope, $stateParams; - var vm, ev, confirmDeferred; - - - beforeEach(inject(function ( - _$rootScope_, _$controller_, _$q_, _$httpBackend_, _$mdDialog_, _utilities_, _$stateParams_ - ) { - $rootScope = _$rootScope_; - $controller = _$controller_; - $q = _$q_; - $httpBackend = _$httpBackend_; - $mdDialog = _$mdDialog_; - utilities = _utilities_; - $stateParams = _$stateParams_; - - // Set challenge ID early - $stateParams.challengeId = 654; - $stateParams.phaseSplitId = null; - - confirmDeferred = $q.defer(); - - // Mock API responses for controller initialization - $httpBackend.whenGET(/accounts\/user\/get_auth_token/).respond(200, { token: 'dummy' }); - $httpBackend.whenGET('/api/challenges/challenge/654/').respond(200, {}); - $httpBackend.whenGET('/api/challenges/654/challenge_phase_split').respond(200, []); - - // Set up controller scope - $scope = $rootScope.$new(); - $scope.page = { creator: { id: 321 }, id: 654 }; - - // Initialize controller - vm = $controller('ChallengeCtrl', { $scope: $scope }); - vm.page = $scope.page; - - $httpBackend.flush(); // finalize initialization - - // Spy setup - spyOn($mdDialog, 'confirm').and.callThrough(); - spyOn($mdDialog, 'show').and.returnValue(confirmDeferred.promise); - spyOn($mdDialog, 'hide'); - spyOn($rootScope, 'notify'); - spyOn(utilities, 'sendRequest').and.callFake(function () {}); - })); - - beforeEach(function () { - confirmDeferred = $q.defer(); - $mdDialog.show.and.returnValue(confirmDeferred.promise); - - // Reset spies before each test - utilities.sendRequest.calls.reset(); - $mdDialog.hide.calls.reset(); - $rootScope.notify.calls.reset(); - - ev = { stopPropagation: jasmine.createSpy('stopPropagation') }; - }); - - afterEach(function () { - $httpBackend.verifyNoOutstandingExpectation(); - $httpBackend.verifyNoOutstandingRequest(); - }); - - it('on cancel (reject) only stops propagation and sets toggleSubmissionState', function () { - vm.disable_private_submission = false; - - vm.toggleDisablePrivateSubmission(ev); - - expect(ev.stopPropagation).toHaveBeenCalled(); - expect(vm.toggleSubmissionState).toBe('disabled'); - expect($mdDialog.show).toHaveBeenCalled(); - - confirmDeferred.reject(); - $rootScope.$apply(); - - expect(utilities.sendRequest).not.toHaveBeenCalled(); - expect($rootScope.notify).not.toHaveBeenCalled(); - }); - - it('on OK + success sends PATCH, flips flag, hides dialog, notifies success', function () { - utilities.sendRequest.and.callFake(function (params) { - expect(vm.disable_private_submission).toBe(false); - expect(params.url).toBe('challenges/challenge_host_team/321/challenge/654'); - expect(params.method).toBe('PATCH'); - expect(params.data).toEqual({ disable_private_submission: true }); - - params.callback.onSuccess({ status: 200, data: {} }); - }); - - vm.disable_private_submission = false; - - vm.toggleDisablePrivateSubmission(ev); - - expect(vm.toggleSubmissionState).toBe('disabled'); - - confirmDeferred.resolve(); - $rootScope.$apply(); - - expect(utilities.sendRequest).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith( - 'success', - 'Private submissions were successfully made disabled' - ); - }); - - it('on OK + error hides dialog and notifies error', function () { - utilities.sendRequest.and.callFake(function (params) { - expect(vm.disable_private_submission).toBe(true); - params.callback.onError({ data: 'oops!' }); - }); - - vm.disable_private_submission = true; - - vm.toggleDisablePrivateSubmission(ev); - - expect(vm.toggleSubmissionState).toBe('allowed'); - - confirmDeferred.resolve(); - $rootScope.$apply(); - - expect(utilities.sendRequest).toHaveBeenCalled(); - expect($mdDialog.hide).toHaveBeenCalled(); - expect($rootScope.notify).toHaveBeenCalledWith('error', 'oops!'); - }); -}); - - describe("Unit tests for refreshSubmissionData function \ `jobs/challenge//challenge_phase//submission/?page=`", function () { var success, successResponse; From 881502c027cdb98b9feab56df5bf779f44dc4821 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Wed, 7 May 2025 14:00:58 +0530 Subject: [PATCH 36/36] refactor: my-submission --- frontend/src/views/web/challenge/my-submission.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/views/web/challenge/my-submission.html b/frontend/src/views/web/challenge/my-submission.html index 41d0f061c5..f7acdb1d7e 100644 --- a/frontend/src/views/web/challenge/my-submission.html +++ b/frontend/src/views/web/challenge/my-submission.html @@ -66,7 +66,7 @@
    My Participated Team: {{challenge.participated_team_name}}
    Environment Logs Submitted at - Show on leaderboard Baseline @@ -121,7 +121,7 @@
    My Participated Team: {{challenge.participated_team_name}}
    {{key.submitted_at | date:'medium'}} - +