def test_give_model_permission(self):
        user = UserFactory()
        benchmark: Benchmark = BenchmarkFactory()

        assign_perm('eyra.change_benchmark', user)
        self.assertTrue(user.has_perm('eyra.change_benchmark'))
        self.assertFalse(user.has_perm('eyra.change_benchmark', benchmark))
Пример #2
0
def test_publication_object_visibilty(client, mocker):
    user1 = UserFactory()
    user2 = UserFactory()

    alg = AlgorithmFactory()
    alg.add_user(user1)
    assert user1.has_perm("view_algorithm", alg)
    assert not user2.has_perm("view_algorithm", alg)

    mocker.patch("grandchallenge.publications.utils.get_doi_csl",
                 return_value=TEST_CSL)

    # create publication
    _ = get_view_for_user(
        viewname="publications:create",
        client=client,
        method=client.post,
        data={"identifier": TEST_DOI},
        user=user1,
    )
    # add publication to algorithm
    alg.publications.add(Publication.objects.get())
    alg.save()

    response = get_view_for_user(
        viewname="publications:list",
        client=client,
        method=client.get,
        user=user1,
    )

    assert response.status_code == 200
    assert alg.title in response.rendered_content

    response = get_view_for_user(
        viewname="publications:list",
        client=client,
        method=client.get,
        user=user2,
    )

    assert response.status_code == 200
    assert alg.title not in response.rendered_content
def test_algorithm_job_post_serializer_create(rf):
    # setup
    user = UserFactory()
    upload, upload_2 = (
        RawImageUploadSessionFactory(creator=user),
        RawImageUploadSessionFactory(creator=user),
    )
    image = ImageFactory()
    upload_2.image_set.set([image])
    assign_perm("view_image", user, image)
    assert user.has_perm("view_image", image)
    algorithm_image = AlgorithmImageFactory(ready=True)
    interfaces = {
        ComponentInterfaceFactory(
            kind=ComponentInterface.Kind.STRING,
            title="TestInterface 1",
            default_value="default",
        ),
        ComponentInterfaceFactory(kind=ComponentInterface.Kind.IMAGE,
                                  title="TestInterface 2"),
        ComponentInterfaceFactory(kind=ComponentInterface.Kind.IMAGE,
                                  title="TestInterface 3"),
    }
    algorithm_image.algorithm.inputs.set(interfaces)
    algorithm_image.algorithm.add_editor(user)

    algorithm_image.algorithm.save()

    job = {"algorithm": algorithm_image.algorithm.api_url, "inputs": []}
    job["inputs"].append({
        "interface": "testinterface-2",
        "upload_session": upload.api_url
    })
    job["inputs"].append({
        "interface": "testinterface-3",
        "image": image.api_url
    })

    # test
    request = rf.get("/foo")
    request.user = user
    serializer = JobPostSerializer(data=job, context={"request": request})

    # verify
    assert serializer.is_valid()
    serializer.create(serializer.validated_data)
    assert len(Job.objects.all()) == 1
    job = Job.objects.first()
    assert job.creator == user
    assert len(job.inputs.all()) == 3
Пример #4
0
def test_assign_answer_image(client, settings):
    settings.task_eager_propagates = (True,)
    settings.task_always_eager = (True,)
    rs = ReaderStudyFactory()
    im = ImageFactory()
    editor, reader = UserFactory(), UserFactory()

    rs.images.add(im)
    rs.add_editor(editor)
    rs.add_reader(reader)

    question = QuestionFactory(
        reader_study=rs, answer_type=Question.ANSWER_TYPE_POLYGON_IMAGE
    )

    us = RawImageUploadSessionFactory(creator=reader)

    answer = AnswerFactory(
        creator=reader,
        question=question,
        answer={"upload_session_pk": str(us.pk)},
    )

    f = StagedFileFactory(
        file__from_path=Path(__file__).parent.parent
        / "cases_tests"
        / "resources"
        / "image10x10x10.mha"
    )
    RawImageFileFactory(upload_session=us, staged_file_id=f.file_id)

    response = get_view_for_user(
        viewname="api:upload-session-process-images",
        reverse_kwargs={"pk": us.pk},
        user=reader,
        client=client,
        method=client.patch,
        data={"answer": str(answer.pk)},
        content_type="application/json",
    )

    assert response.status_code == 200

    answer.refresh_from_db()
    image = us.image_set.first()

    assert answer.answer_image == image
    assert reader.has_perm("view_image", image)
    assert editor.has_perm("view_image", image)
Пример #5
0
    def test_creator_permissions(self):
        user = UserFactory()
        benchmark: Benchmark = BenchmarkFactory(creator=user)
        # creator is user
        self.assertEquals(benchmark.creator, user)

        # admin group was made
        self.assertIsNotNone(benchmark.admin_group)

        # user is part of admin group
        self.assertTrue(
            benchmark.admin_group.user_set.filter(pk=user.pk).exists())

        # user can thus edit benchmark
        self.assertTrue(user.has_perm('change_benchmark', benchmark))

        # user can change the admin group
        self.assertTrue(user.has_perm('change_group', benchmark.admin_group))

        other_user = UserFactory()
        # other user has no permissions
        self.assertFalse(other_user.has_perm('change_benchmark', benchmark))
        self.assertFalse(
            other_user.has_perm('change_group', benchmark.admin_group))
    def test_creator_permissions(self):
        user = UserFactory()
        algorithm: Algorithm = AlgorithmFactory(creator=user)
        # creator is user
        self.assertEquals(algorithm.creator, user)

        # admin group was made
        self.assertIsNotNone(algorithm.admin_group)

        # user is part of admin group
        self.assertTrue(
            algorithm.admin_group.user_set.filter(pk=user.pk).exists())

        # user can thus edit algorithm
        self.assertTrue(user.has_perm('change_algorithm', algorithm))

        # user can change the admin group
        self.assertTrue(user.has_perm('change_group', algorithm.admin_group))

        other_user = UserFactory()
        # other user has no permissions
        self.assertFalse(other_user.has_perm('change_algorithm', algorithm))
        self.assertFalse(
            other_user.has_perm('change_group', algorithm.admin_group))
 def test_default_user_permissions(self):
     user = UserFactory()
     self.assertTrue(user.has_perm('eyra.add_algorithm'))
     self.assertTrue(user.has_perm('eyra.add_submission'))
    def test_default_false(self):
        user = UserFactory()
        benchmark: Benchmark = BenchmarkFactory()

        self.assertFalse(user.has_perm('eyra.change_benchmark'))
        self.assertFalse(user.has_perm('eyra.change_benchmark', benchmark))