def test_redirect_view(get, post, client): resp = Response() resp.status_code = 200 resp.headers["Content-Type"] = "application/json" resp._content = b'{"access_token": "tok", "expires_in": "3600", "refresh_token": "ref", "refresh_token_expires_in":"7200", "id": 1}' post.return_value = resp get.return_value = resp user = UserFactory() response = get_view_for_user( client=client, viewname="github:install-complete", data={"state": "None"}, user=user, ) assert response.status_code == 404 error_msg = ( '<div class="mb-2">Unfortunately something went wrong while trying ' "to find the requested algorithm.</div><div>If you were trying to " "link a github repository to an algorithm, please do so manually in " "the algorithm's settings.</div>") assert error_msg in response.content.decode("utf-8") alg = AlgorithmFactory() alg.add_editor(user) response = get_view_for_user( client=client, viewname="github:install-complete", data={"state": alg.slug}, user=user, follow=True, ) # User is not verified assert response.status_code == 403 VerificationFactory(user=user, is_verified=True) response = get_view_for_user( client=client, viewname="github:install-complete", data={"state": alg.slug}, user=user, follow=True, ) assert response.status_code == 200 assert f"Add GitHub repository to {alg.title}" in response.rendered_content
def test_algorithm_image_create_detail(client): user = UserFactory() VerificationFactory(user=user, is_verified=True) algorithm = AlgorithmFactory() algorithm.add_editor(user) algorithm_image = UserUploadFactory(filename="test_image.tar.gz", creator=user) algorithm_image.status = algorithm_image.StatusChoices.COMPLETED algorithm_image.save() response = get_view_for_user( client=client, viewname="algorithms:image-create", reverse_kwargs={"slug": algorithm.slug}, user=user, ) assert response.status_code == 200 assert AlgorithmImage.objects.all().count() == 0 response = get_view_for_user( client=client, method=client.post, viewname="algorithms:image-create", reverse_kwargs={"slug": algorithm.slug}, user=user, data={ "user_upload": algorithm_image.pk, "requires_memory_gb": 24, "creator": user.pk, "algorithm": algorithm.pk, }, ) assert response.status_code == 302 images = AlgorithmImage.objects.all() assert len(images) == 1 assert images[0].algorithm == algorithm assert response.url == reverse( "algorithms:image-detail", kwargs={ "slug": algorithm.slug, "pk": images[0].pk }, )
def test_algorithm_with_permission(self): user = UserFactory() alg = AlgorithmFactory() alg.add_editor(user=user) AlgorithmImageFactory(ready=True, algorithm=alg) form = SubmissionForm( user=user, algorithm_submission=True, data={ "algorithm": alg.pk, "creator": user }, ) assert "algorithm" not in form.errors assert form.is_valid()
def test_algorithm_with_permission_not_ready(self): user = UserFactory() alg = AlgorithmFactory() alg.add_editor(user=user) alg.inputs.clear() alg.outputs.clear() form = SubmissionForm( user=user, phase=PhaseFactory(submission_kind=Phase.SubmissionKind.ALGORITHM), data={"algorithm": alg.pk}, ) assert form.errors["algorithm"] == [ "This algorithm does not have a usable container image. " "Please add one and try again." ]
def test_follows_deleted_when_request_deleted(client): base_object = AlgorithmFactory( access_request_handling=AccessRequestHandlingOptions.MANUAL_REVIEW) editor = UserFactory() base_object.add_editor(editor) permission_create_url = reverse( "algorithms:permission-request-create", kwargs={"slug": base_object.slug}, ) user = UserFactory() _ = get_view_for_user(client=client, user=user, url=permission_create_url, method=client.post) pr = AlgorithmPermissionRequest.objects.get() assert is_following(user, pr) pr.delete() assert not Follow.objects.filter(object_id=pr.pk)
def test_algorithm_with_permission(self): user = UserFactory() alg = AlgorithmFactory() alg.add_editor(user=user) alg.inputs.clear() alg.outputs.clear() ai = AlgorithmImageFactory(ready=True, algorithm=alg) AlgorithmJobFactory(algorithm_image=ai, status=4) p = PhaseFactory(submission_kind=Phase.SubmissionKind.ALGORITHM) form = SubmissionForm( user=user, phase=p, data={"algorithm": alg.pk, "creator": user, "phase": p}, ) assert form.errors == {} assert "algorithm" not in form.errors assert form.is_valid()
def test_algorithm_detail_flexible_inputs(client): editor = UserFactory() alg = AlgorithmFactory(use_flexible_inputs=False) alg.add_editor(editor) AlgorithmImageFactory(algorithm=alg, ready=True) flexi_input_url = reverse( viewname="algorithms:execution-session-create-new", kwargs={"slug": alg.slug}, ) response = get_view_for_user( viewname="algorithms:detail", reverse_kwargs={"slug": slugify(alg.slug)}, client=client, user=editor, method=client.get, follow=True, **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}, ) assert response.status_code == 200 assert flexi_input_url not in response.rendered_content alg.use_flexible_inputs = True alg.save() response = get_view_for_user( viewname="algorithms:detail", reverse_kwargs={"slug": slugify(alg.slug)}, client=client, user=editor, method=client.get, follow=True, **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}, ) assert response.status_code == 200 assert flexi_input_url in response.rendered_content
def test_algorithm_image_create_detail(client): user = UserFactory() algorithm = AlgorithmFactory() algorithm.add_editor(user) algorithm_image = StagedFileFactory(file__filename="test_image.tar.gz") response = get_view_for_user( client=client, viewname="algorithms:image-create", reverse_kwargs={"slug": algorithm.slug}, user=user, ) assert response.status_code == 200 assert AlgorithmImage.objects.all().count() == 0 response = get_view_for_user( client=client, method=client.post, viewname="algorithms:image-create", reverse_kwargs={"slug": algorithm.slug}, user=user, data={ "chunked_upload": algorithm_image.file_id, "requires_memory_gb": 24, }, ) assert response.status_code == 302 images = AlgorithmImage.objects.all() assert len(images) == 1 assert images[0].algorithm == algorithm assert response.url == reverse( "algorithms:image-detail", kwargs={ "slug": algorithm.slug, "pk": images[0].pk }, )
def test_algorithm_permission_request_notification_for_admins_only(client): base_object = AlgorithmFactory() editor = UserFactory() user = UserFactory() participant = UserFactory() base_object.add_editor(editor) base_object.add_user(participant) # create an algorithm job follow for participant follow(user=participant, obj=base_object, flag="job-active") permission_create_url = reverse( "algorithms:permission-request-create", kwargs={"slug": base_object.slug}, ) # Create the permission request _ = get_view_for_user(client=client, user=user, url=permission_create_url, method=client.post) assert Notification.objects.count() == 1 assert Notification.objects.get().user == editor
def test_algorithm_jobs_list_view(client): editor = UserFactory() alg = AlgorithmFactory(public=True) alg.add_editor(editor) im = AlgorithmImageFactory(algorithm=alg) for x in range(50): created = timezone.now() - datetime.timedelta(days=x + 365) job = AlgorithmJobFactory(algorithm_image=im, status=Job.SUCCESS) job.created = created job.save() job.viewer_groups.add(alg.editors_group) response = get_view_for_user( viewname="algorithms:job-list", reverse_kwargs={"slug": slugify(alg.slug)}, client=client, user=editor, method=client.get, follow=True, ) assert response.status_code == 200 response = get_view_for_user( viewname="algorithms:job-list", reverse_kwargs={"slug": slugify(alg.slug)}, client=client, user=editor, method=client.get, follow=True, data={ "length": 10, "draw": 1, "order[0][dir]": "desc", "order[0][column]": 0, }, **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}, ) resp = response.json() assert resp["recordsTotal"] == 50 assert len(resp["data"]) == 10 response = get_view_for_user( viewname="algorithms:job-list", reverse_kwargs={"slug": slugify(alg.slug)}, client=client, user=editor, method=client.get, follow=True, data={ "length": 50, "draw": 1, "order[0][dir]": "desc", "order[0][column]": 0, }, **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}, ) resp = response.json() assert resp["recordsTotal"] == 50 assert len(resp["data"]) == 50 response = get_view_for_user( viewname="algorithms:job-list", reverse_kwargs={"slug": slugify(alg.slug)}, client=client, user=editor, method=client.get, follow=True, data={ "length": 50, "draw": 1, "order[0][dir]": "asc", "order[0][column]": 0, }, **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}, ) resp_new = response.json() assert resp_new["recordsTotal"] == 50 assert resp_new["data"] == resp["data"][::-1] response = get_view_for_user( viewname="algorithms:job-list", reverse_kwargs={"slug": slugify(alg.slug)}, client=client, user=editor, method=client.get, follow=True, data={ "length": 50, "draw": 1, "search[value]": job.creator.username, "order[0][column]": 0, }, **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}, ) resp = response.json() assert resp["recordsTotal"] == 50 assert resp["recordsFiltered"] == 1 assert len(resp["data"]) == 1
def test_algorithm_permission_request_update(client): user = UserFactory() editor = UserFactory() alg = AlgorithmFactory(public=True) alg.add_editor(editor) pr = AlgorithmPermissionRequestFactory(algorithm=alg, user=user) assert pr.status == AlgorithmPermissionRequest.PENDING response = get_view_for_user( viewname="algorithms:permission-request-update", reverse_kwargs={ "slug": slugify(alg.slug), "pk": pr.pk }, client=client, user=editor, method=client.get, follow=True, ) assert "review access request for user" in response.rendered_content assert "Request access" not in response.rendered_content response = get_view_for_user( viewname="algorithms:permission-request-update", reverse_kwargs={ "slug": slugify(alg.slug), "pk": pr.pk }, client=client, user=editor, method=client.post, follow=True, data={"status": "NONEXISTENT"}, ) pr.refresh_from_db() assert response.status_code == 200 assert pr.status == AlgorithmPermissionRequest.PENDING response = get_view_for_user( viewname="algorithms:permission-request-update", reverse_kwargs={ "slug": slugify(alg.slug), "pk": pr.pk }, client=client, user=editor, method=client.post, follow=True, data={"status": AlgorithmPermissionRequest.REJECTED}, ) pr.refresh_from_db() assert response.status_code == 200 assert pr.status == AlgorithmPermissionRequest.REJECTED response = get_view_for_user( viewname="algorithms:permission-request-update", reverse_kwargs={ "slug": slugify(alg.slug), "pk": pr.pk }, client=client, user=user, method=client.get, follow=True, ) assert response.status_code == 403 # User should not be able to change the status response = get_view_for_user( viewname="algorithms:permission-request-update", reverse_kwargs={ "slug": slugify(alg.slug), "pk": pr.pk }, client=client, user=user, method=client.post, follow=True, data={"status": AlgorithmPermissionRequest.ACCEPTED}, ) pr.refresh_from_db() assert response.status_code == 403 assert pr.status == AlgorithmPermissionRequest.REJECTED response = get_view_for_user( viewname="algorithms:permission-request-update", reverse_kwargs={ "slug": slugify(alg.slug), "pk": pr.pk }, client=client, user=editor, method=client.post, follow=True, data={"status": AlgorithmPermissionRequest.ACCEPTED}, ) pr.refresh_from_db() assert response.status_code == 200 assert pr.status == AlgorithmPermissionRequest.ACCEPTED