示例#1
0
def test_assert_modification_allowed():
    rs = ReaderStudyFactory(use_display_sets=False)
    ci = ComponentInterfaceFactory(
        kind=InterfaceKind.InterfaceKindChoices.BOOL)
    civ = ComponentInterfaceValueFactory(interface=ci, value=True)
    ds = DisplaySetFactory(reader_study=rs)
    ds.values.add(civ)

    del ds.is_editable

    civ2 = ComponentInterfaceValueFactory(interface=ci, value=True)
    ds.values.remove(civ)
    ds.values.add(civ2)

    assert ds.values.count() == 1
    assert ds.values.first() == civ2

    q = QuestionFactory(reader_study=rs)
    AnswerFactory(question=q, display_set=ds)

    del ds.is_editable

    with pytest.raises(ValidationError):
        with transaction.atomic():
            ds.values.remove(civ2)

    assert ds.values.count() == 1
    assert ds.values.first() == civ2
示例#2
0
def test_view_permission_when_reused(in_archive, in_rs, in_job):
    """When an image is reused it should have view_image set correctly"""
    im = ImageFactory()

    job = AlgorithmJobFactory()
    rs = ReaderStudyFactory(use_display_sets=False)
    archive = ArchiveFactory()

    if in_archive:
        civ = ComponentInterfaceValueFactory(image=im)
        ai = ArchiveItemFactory(archive=archive)
        with capture_on_commit_callbacks(execute=True):
            ai.values.add(civ)
    if in_rs:
        rs.images.add(im)
    if in_job:
        civ = ComponentInterfaceValueFactory(image=im)
        job.inputs.add(civ)

    assert ("view_image" in get_perms(archive.editors_group, im)) is in_archive
    assert ("view_image" in get_perms(archive.uploaders_group,
                                      im)) is in_archive
    assert ("view_image" in get_perms(archive.users_group, im)) is in_archive

    assert ("view_image" in get_perms(rs.editors_group, im)) is in_rs
    assert ("view_image" in get_perms(rs.readers_group, im)) is in_rs

    for g in job.viewer_groups.all():
        assert ("view_image" in get_perms(g, im)) is in_job
示例#3
0
def test_input_prefixes(tmp_path, settings):
    interfaces = [
        ComponentInterfaceFactory(
            kind=InterfaceKindChoices.BOOL, relative_path="test/bool.json"
        ),
        ComponentInterfaceFactory(
            kind=InterfaceKindChoices.IMAGE, relative_path="images/test-image"
        ),
        ComponentInterfaceFactory(
            kind=InterfaceKindChoices.CSV, relative_path="test.csv"
        ),
    ]
    civs = [
        ComponentInterfaceValueFactory(interface=interfaces[0], value=True),
        ComponentInterfaceValueFactory(
            interface=interfaces[1],
            image=ImageFileFactory(
                file__from_path=Path(__file__).parent.parent
                / "algorithms_tests"
                / "resources"
                / "input_file.tif"
            ).image,
        ),
        ComponentInterfaceValueFactory(interface=interfaces[2]),
    ]
    settings.COMPONENTS_AMAZON_ECS_NFS_MOUNT_POINT = tmp_path

    executor = AmazonECSExecutorStub(
        job_id="algorithms-job-00000000-0000-0000-0000-000000000000",
        exec_image_sha256="",
        exec_image_repo_tag="",
        memory_limit=4,
        time_limit=60,
        requires_gpu=False,
    )
    executor.provision(
        input_civs=civs,
        input_prefixes={
            str(civs[0].pk): "first/output/",
            str(civs[1].pk): "second/output",
        },
    )

    assert {str(f.relative_to(tmp_path)) for f in tmp_path.glob("**/*")} == {
        "algorithms",
        "algorithms/job",
        "algorithms/job/00000000-0000-0000-0000-000000000000",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/test.csv",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/first",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/first/output",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/first/output/test",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/first/output/test/bool.json",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/second",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/second/output",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/second/output/images",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/second/output/images/test-image",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/second/output/images/test-image/input_file.tif",
        "algorithms/job/00000000-0000-0000-0000-000000000000/output",
    }
    def test_group_clearing(self, reverse):
        job = AlgorithmJobFactory()
        civ_in, civ_out = (
            ComponentInterfaceValueFactory(image=ImageFactory()),
            ComponentInterfaceValueFactory(image=ImageFactory()),
        )
        job.inputs.add(civ_in)
        job.outputs.add(civ_out)
        groups = job.viewer_groups.all()

        assert len(groups) > 0
        for group in groups:
            assert "view_job" in get_perms(group, job)
            assert "view_image" in get_perms(group, civ_in.image)
            assert "view_image" in get_perms(group, civ_out.image)

        if reverse:
            for group in groups:
                group.job_set.clear()
        else:
            job.viewer_groups.clear()

        for group in groups:
            assert "view_job" not in get_perms(group, job)
            assert "view_image" not in get_perms(group, civ_in.image)
            assert "view_image" not in get_perms(group, civ_out.image)
示例#5
0
    def test_existing_jobs(self):
        ai = AlgorithmImageFactory()
        cis = ComponentInterfaceFactory.create_batch(2)
        ai.algorithm.inputs.set(cis)

        civs = [ComponentInterfaceValueFactory(interface=c) for c in cis]

        j = AlgorithmJobFactory(algorithm_image=ai)
        j.inputs.set(civs)

        civ_sets = [
            civs,  # Job already exists
            {
                # New values
                ComponentInterfaceValueFactory(interface=cis[0]),
                ComponentInterfaceValueFactory(interface=cis[1]),
            },
            {
                # Changed values
                civs[0],
                ComponentInterfaceValueFactory(interface=cis[1]),
            },
        ]

        filtered_civ_sets = filter_civs_for_algorithm(civ_sets=civ_sets,
                                                      algorithm_image=ai)

        assert filtered_civ_sets == civ_sets[1:]
def test_used_by_other_public_result_permissions():
    g_reg_anon = Group.objects.get(
        name=settings.REGISTERED_AND_ANON_USERS_GROUP_NAME)
    g_reg = Group.objects.get(name=settings.REGISTERED_USERS_GROUP_NAME)

    j1 = AlgorithmJobFactory(public=True)
    j2 = AlgorithmJobFactory(public=True)

    shared_image = ImageFactory()

    civ1 = ComponentInterfaceValueFactory(image=shared_image)
    j1.outputs.add(civ1)
    civ2 = ComponentInterfaceValueFactory(image=shared_image)
    j2.outputs.add(civ2)

    assert "view_image" not in get_perms(g_reg, shared_image)
    assert "view_image" in get_perms(g_reg_anon, shared_image)

    j2.outputs.clear()

    assert "view_image" not in get_perms(g_reg, shared_image)
    assert "view_image" in get_perms(g_reg_anon, shared_image)

    j2.outputs.add(civ2)
    j2.public = False
    j2.save()

    assert "view_image" not in get_perms(g_reg, shared_image)
    assert "view_image" in get_perms(g_reg_anon, shared_image)

    j1.public = False
    j1.save()

    assert "view_image" not in get_perms(g_reg, shared_image)
    assert "view_image" not in get_perms(g_reg_anon, shared_image)
示例#7
0
    def test_unmatched_interface_filter(self):
        ai = AlgorithmImageFactory()
        cis = ComponentInterfaceFactory.create_batch(2)
        ai.algorithm.inputs.set(cis)

        civ_sets = [
            {},  # No interfaces
            {ComponentInterfaceValueFactory(interface=cis[0])
             },  # Missing interface
            {
                # OK
                ComponentInterfaceValueFactory(interface=cis[0]),
                ComponentInterfaceValueFactory(interface=cis[1]),
            },
            {
                # Unmatched interface
                ComponentInterfaceValueFactory(interface=cis[0]),
                ComponentInterfaceValueFactory(
                    interface=ComponentInterfaceFactory()),
            },
        ]

        filtered_civ_sets = filter_civs_for_algorithm(civ_sets=civ_sets,
                                                      algorithm_image=ai)

        assert filtered_civ_sets == [civ_sets[2]]
示例#8
0
def test_add_image_to_public_result():
    g_reg_anon = Group.objects.get(
        name=settings.REGISTERED_AND_ANON_USERS_GROUP_NAME
    )
    g_reg = Group.objects.get(name=settings.REGISTERED_USERS_GROUP_NAME)

    job = AlgorithmJobFactory(public=True)
    civ_images = (
        ComponentInterfaceValueFactory(image=ImageFactory()),
        ComponentInterfaceValueFactory(image=ImageFactory()),
    )

    for im in civ_images:
        assert "view_image" not in get_perms(g_reg, im.image)
        assert "view_image" not in get_perms(g_reg_anon, im.image)

    job.outputs.add(*civ_images)

    for im in civ_images:
        assert "view_image" not in get_perms(g_reg, im.image)
        assert "view_image" in get_perms(g_reg_anon, im.image)

    job.outputs.remove(civ_images[0].pk)

    assert "view_image" not in get_perms(g_reg, civ_images[0].image)
    assert "view_image" not in get_perms(g_reg_anon, civ_images[0].image)
    assert "view_image" not in get_perms(g_reg, civ_images[1].image)
    assert "view_image" in get_perms(g_reg_anon, civ_images[1].image)

    job.outputs.clear()

    for im in civ_images:
        assert "view_image" not in get_perms(g_reg, im.image)
        assert "view_image" not in get_perms(g_reg_anon, im.image)
def test_view_permission_when_reused(in_archive, in_rs, in_job):
    """When an image is reused it should have view_image set correctly"""
    im = ImageFactory()

    job = AlgorithmJobFactory()
    rs = ReaderStudyFactory()
    archive = ArchiveFactory()

    if in_archive:
        archive.images.add(im)
    if in_rs:
        rs.images.add(im)
    if in_job:
        civ = ComponentInterfaceValueFactory()
        civ.image = im
        civ.save()
        job.inputs.add(civ)

    assert ("view_image" in get_perms(archive.editors_group, im)) is in_archive
    assert (
        "view_image" in get_perms(archive.uploaders_group, im)
    ) is in_archive
    assert ("view_image" in get_perms(archive.users_group, im)) is in_archive

    assert ("view_image" in get_perms(rs.editors_group, im)) is in_rs
    assert ("view_image" in get_perms(rs.readers_group, im)) is in_rs

    for g in job.viewer_groups.all():
        assert ("view_image" in get_perms(g, im)) is in_job
示例#10
0
    def setUp(self):
        interface = ComponentInterface.objects.get(
            slug="generic-medical-image")

        archive = ArchiveFactory()
        ais = ArchiveItemFactory.create_batch(2)
        archive.items.set(ais)

        input_civs = ComponentInterfaceValueFactory.create_batch(
            2, interface=interface)
        output_civs = ComponentInterfaceValueFactory.create_batch(
            2, interface=interface)

        for ai, civ in zip(ais, input_civs):
            ai.values.set([civ])

        alg = AlgorithmImageFactory()
        submission = SubmissionFactory(algorithm_image=alg)
        submission.phase.archive = archive
        submission.phase.save()
        submission.phase.algorithm_inputs.set([interface])

        jobs = []
        for inpt, output in zip(input_civs, output_civs):
            j = AlgorithmJobFactory(status=Job.SUCCESS, algorithm_image=alg)
            j.inputs.set([inpt])
            j.outputs.set([output])
            jobs.append(j)

        self.evaluation = EvaluationFactory(
            submission=submission, status=Evaluation.EXECUTING_PREREQUISITES)
        self.jobs = jobs
        self.output_civs = output_civs
def test_archive_item_permissions_signal(client, reverse):  # noqa: C901
    ai1, ai2 = ArchiveItemFactory.create_batch(2)
    im1, im2, im3, im4 = ImageFactory.create_batch(4)

    civ1, civ2, civ3, civ4 = (
        ComponentInterfaceValueFactory(image=im1),
        ComponentInterfaceValueFactory(image=im2),
        ComponentInterfaceValueFactory(image=im3),
        ComponentInterfaceValueFactory(image=im4),
    )

    with capture_on_commit_callbacks(execute=True):
        if reverse:
            for civ in [civ1, civ2, civ3, civ4]:
                civ.archive_items.add(ai1, ai2)
            for civ in [civ3, civ4]:
                civ.archive_items.remove(ai1, ai2)
            for civ in [civ1, civ2]:
                civ.archive_items.remove(ai2)
        else:
            # Test that adding images works
            ai1.values.add(civ1, civ2, civ3, civ4)
            # Test that removing images works
            ai1.values.remove(civ3, civ4)

    assert get_groups_with_set_perms(im1) == {
        ai1.archive.editors_group: {"view_image"},
        ai1.archive.uploaders_group: {"view_image"},
        ai1.archive.users_group: {"view_image"},
    }
    assert get_groups_with_set_perms(im2) == {
        ai1.archive.editors_group: {"view_image"},
        ai1.archive.uploaders_group: {"view_image"},
        ai1.archive.users_group: {"view_image"},
    }
    assert get_groups_with_set_perms(im3) == {}
    assert get_groups_with_set_perms(im4) == {}

    # Test clearing
    with capture_on_commit_callbacks(execute=True):
        if reverse:
            civ1.archive_items.clear()
            civ2.archive_items.clear()
        else:
            ai1.values.clear()

    assert get_groups_with_set_perms(im1) == {}
    assert get_groups_with_set_perms(im2) == {}
示例#12
0
def test_ecs_unzip(tmp_path, settings, submission_file):
    interface = ComponentInterfaceFactory(
        kind=InterfaceKindChoices.ZIP, relative_path="preds.zip"
    )
    civ = ComponentInterfaceValueFactory(interface=interface)

    with open(submission_file, "rb") as f:
        civ.file.save("my_submission.zip", File(f))

    settings.COMPONENTS_AMAZON_ECS_NFS_MOUNT_POINT = tmp_path

    executor = AmazonECSExecutorStub(
        job_id="algorithms-job-00000000-0000-0000-0000-000000000000",
        exec_image_sha256="",
        exec_image_repo_tag="",
        memory_limit=4,
        time_limit=60,
        requires_gpu=False,
    )
    executor.provision(input_civs=[civ], input_prefixes={})

    assert {str(f.relative_to(tmp_path)) for f in tmp_path.glob("**/*")} == {
        "algorithms",
        "algorithms/job",
        "algorithms/job/00000000-0000-0000-0000-000000000000",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/submission.csv",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/images",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/images/image10x10x10.mhd",
        "algorithms/job/00000000-0000-0000-0000-000000000000/input/images/image10x10x10.zraw",
        "algorithms/job/00000000-0000-0000-0000-000000000000/output",
    }
def test_changing_archive_updates_permissions():
    ai = ArchiveItemFactory()
    im = ImageFactory()
    civ = ComponentInterfaceValueFactory(image=im)

    with capture_on_commit_callbacks(execute=True):
        ai.values.set([civ])

    assert get_groups_with_set_perms(im) == {
        ai.archive.editors_group: {"view_image"},
        ai.archive.uploaders_group: {"view_image"},
        ai.archive.users_group: {"view_image"},
    }

    a2 = ArchiveFactory()

    ai.archive = a2

    with capture_on_commit_callbacks(execute=True):
        ai.save()

    assert get_groups_with_set_perms(im) == {
        a2.editors_group: {"view_image"},
        a2.uploaders_group: {"view_image"},
        a2.users_group: {"view_image"},
    }
    def test_job_permissions_for_challenge(self):
        ai = AlgorithmImageFactory(ready=True)
        archive = ArchiveFactory()
        evaluation = EvaluationFactory(submission__phase__archive=archive,
                                       submission__algorithm_image=ai)

        # Fake an image upload via a session
        u = UserFactory()
        s = UploadSessionFactory(creator=u)
        im = ImageFactory()
        s.image_set.set([im])

        civ = ComponentInterfaceValueFactory(image=im)
        archive_item = ArchiveItemFactory(archive=archive)
        with capture_on_commit_callbacks(execute=True):
            archive_item.values.add(civ)

        create_algorithm_jobs_for_evaluation(evaluation_pk=evaluation.pk)

        job = Job.objects.get()

        # Only the challenge admins and job viewers should be able to view the
        # job. NOTE: NOT THE ALGORITHM EDITORS, they are the participants
        # to the challenge and should not be able to see the test data
        assert get_groups_with_set_perms(job) == {
            evaluation.submission.phase.challenge.admins_group: {"view_job"},
            job.viewers: {"view_job"},
        }
        # No-one should be able to change the job
        assert (get_users_with_perms(job,
                                     attach_perms=True,
                                     with_group_users=False) == {})
        # No-one should be in the viewers group
        assert {*job.viewers.user_set.all()} == set()
示例#15
0
def test_image_permission_with_public_job():
    g_reg_anon = Group.objects.get(
        name=settings.REGISTERED_AND_ANON_USERS_GROUP_NAME
    )
    g_reg = Group.objects.get(name=settings.REGISTERED_USERS_GROUP_NAME)

    job = AlgorithmJobFactory()

    output_image = ImageFactory()
    civ = ComponentInterfaceValueFactory(image=output_image)
    job.outputs.add(civ)

    assert "view_image" not in get_perms(g_reg, output_image)
    assert "view_image" not in get_perms(g_reg_anon, output_image)
    assert "view_image" not in get_perms(g_reg, job.inputs.first().image)
    assert "view_image" not in get_perms(g_reg_anon, job.inputs.first().image)

    job.public = True
    job.save()

    assert "view_image" not in get_perms(g_reg, output_image)
    assert "view_image" in get_perms(g_reg_anon, output_image)
    assert "view_image" not in get_perms(g_reg, job.inputs.first().image)
    assert "view_image" in get_perms(g_reg_anon, job.inputs.first().image)

    job.public = False
    job.save()

    assert "view_image" not in get_perms(g_reg, output_image)
    assert "view_image" not in get_perms(g_reg_anon, output_image)
    assert "view_image" not in get_perms(g_reg, job.inputs.first().image)
    assert "view_image" not in get_perms(g_reg_anon, job.inputs.first().image)
def test_deleting_archive_item_removes_permissions():
    ai1, ai2 = ArchiveItemFactory.create_batch(2)
    im = ImageFactory()
    civ = ComponentInterfaceValueFactory(image=im)

    with capture_on_commit_callbacks(execute=True):
        ai1.values.set([civ])
        ai2.values.set([civ])

    assert get_groups_with_set_perms(im) == {
        ai1.archive.editors_group: {"view_image"},
        ai1.archive.uploaders_group: {"view_image"},
        ai1.archive.users_group: {"view_image"},
        ai2.archive.editors_group: {"view_image"},
        ai2.archive.uploaders_group: {"view_image"},
        ai2.archive.users_group: {"view_image"},
    }

    with capture_on_commit_callbacks(execute=True):
        ai1.delete()

    assert get_groups_with_set_perms(im) == {
        ai2.archive.editors_group: {"view_image"},
        ai2.archive.uploaders_group: {"view_image"},
        ai2.archive.users_group: {"view_image"},
    }
示例#17
0
def test_user_upload_to_display_set_without_interface(client, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    user = UserFactory()
    rs = ReaderStudyFactory(use_display_sets=False)
    rs.add_editor(user=user)
    ci = ComponentInterface.objects.filter(slug="generic-overlay").get()
    civ = ComponentInterfaceValueFactory(interface=ci)
    ds = DisplaySetFactory(reader_study=rs)
    ds.values.add(civ)
    assert ds.values.count() == 1

    upload = create_upload_from_file(
        file_path=Path(__file__).parent / "resources" / "image10x10x10.mha",
        creator=user,
    )
    with capture_on_commit_callbacks(execute=True):
        response = get_view_for_user(
            viewname="api:upload-session-list",
            user=user,
            client=client,
            method=client.post,
            content_type="application/json",
            data={
                "uploads": [upload.api_url],
                "display_set": ds.pk
            },
            HTTP_X_FORWARDED_PROTO="https",
        )

    assert response.status_code == 400
    assert ("An interface needs to be defined to upload to a display set."
            in response.json()["non_field_errors"])
def test_filter_images_api_view(client):
    alg = AlgorithmFactory()
    user = UserFactory()
    alg.add_editor(user=user)

    alg_job = AlgorithmJobFactory(algorithm_image__algorithm=alg, creator=user)

    im = ImageFactory()
    civ = ComponentInterfaceValueFactory(image=im)
    alg_job.outputs.add(civ)

    response = get_view_for_user(
        viewname="api:image-list",
        client=client,
        user=user,
        content_type="application/json",
    )
    assert response.status_code == 200
    assert {r["pk"] for r in response.json()["results"]} == {
        str(i.pk) for i in [*[inpt.image for inpt in alg_job.inputs.all()], im]
    }

    response = get_view_for_user(
        client=client,
        user=user,
        viewname="api:image-list",
        data={"origin": str(im.origin.pk)},
        content_type="application/json",
    )
    assert response.status_code == 200
    assert response.json()["count"] == 1
    assert response.json()["results"][0]["pk"] == str(im.pk)
示例#19
0
def test_reader_study_add_ground_truth_ds(client, settings):
    settings.task_eager_propagates = (True,)
    settings.task_always_eager = (True,)

    rs = ReaderStudyFactory(use_display_sets=True)
    QuestionFactory(
        reader_study=rs,
        question_text="bar",
        answer_type=Question.AnswerType.SINGLE_LINE_TEXT,
    )

    civ = ComponentInterfaceValueFactory(image=ImageFactory())
    ds = DisplaySetFactory(reader_study=rs)
    ds.values.add(civ)

    editor = UserFactory()
    rs.editors_group.user_set.add(editor)

    gt = io.StringIO()
    fake_writer = csv.writer(gt)
    fake_writer.writerows([["images", "foo"], [str(ds.pk), "bar"]])
    gt.seek(0)

    response = get_view_for_user(
        viewname="reader-studies:add-ground-truth",
        client=client,
        method=client.post,
        reverse_kwargs={"slug": rs.slug},
        data={"ground_truth": gt},
        follow=True,
        user=editor,
    )

    assert response.status_code == 200
示例#20
0
def test_civ_value_to_file():
    civ = ComponentInterfaceValueFactory(value={"foo": 1, "bar": None})

    civ_value_to_file(civ_pk=civ.pk)

    civ.refresh_from_db()

    with civ.file.open("r") as f:
        v = json.loads(f.read())

    assert v == {"foo": 1, "bar": None}
    assert civ.value is None

    # Check idempotency
    with pytest.raises(RuntimeError):
        civ_value_to_file(civ_pk=civ.pk)
示例#21
0
def test_display_set_permissions_signal(client, reverse):
    ds1, ds2 = DisplaySetFactory.create_batch(2)
    im1, im2, im3, im4 = ImageFactory.create_batch(4)

    civ1, civ2, civ3, civ4 = (
        ComponentInterfaceValueFactory(image=im1),
        ComponentInterfaceValueFactory(image=im2),
        ComponentInterfaceValueFactory(image=im3),
        ComponentInterfaceValueFactory(image=im4),
    )

    with capture_on_commit_callbacks(execute=True):
        if reverse:
            for civ in [civ1, civ2, civ3, civ4]:
                civ.display_sets.add(ds1, ds2)
            for civ in [civ3, civ4]:
                civ.display_sets.remove(ds1, ds2)
            for civ in [civ1, civ2]:
                civ.display_sets.remove(ds2)
        else:
            # Test that adding images works
            ds1.values.add(civ1, civ2, civ3, civ4)
            # Test that removing images works
            ds1.values.remove(civ3, civ4)

    assert get_groups_with_set_perms(im1) == {
        ds1.reader_study.editors_group: {"view_image"},
        ds1.reader_study.readers_group: {"view_image"},
    }
    assert get_groups_with_set_perms(im2) == {
        ds1.reader_study.editors_group: {"view_image"},
        ds1.reader_study.readers_group: {"view_image"},
    }
    assert get_groups_with_set_perms(im3) == {}
    assert get_groups_with_set_perms(im4) == {}

    # Test clearing
    with capture_on_commit_callbacks(execute=True):
        if reverse:
            civ1.display_sets.clear()
            civ2.display_sets.clear()
        else:
            ds1.values.clear()

    assert get_groups_with_set_perms(im1) == {}
    assert get_groups_with_set_perms(im2) == {}
 def files(self, create, extracted, **kwargs):
     # See https://factoryboy.readthedocs.io/en/latest/recipes.html#simple-many-to-many-relationship
     if not create:
         return
     if extracted:
         self.inputs.set([*extracted])
     if create and not extracted:
         self.inputs.set(
             [ComponentInterfaceValueFactory(image=ImageFactory())])
示例#23
0
    def test_unmatched_interface_filter_subset(self):
        ai = AlgorithmImageFactory()
        cis = ComponentInterfaceFactory.create_batch(2)
        ai.algorithm.inputs.set(cis)

        civ_sets = [{
            # Extra interface
            ComponentInterfaceValueFactory(interface=cis[0]),
            ComponentInterfaceValueFactory(interface=cis[1]),
            ComponentInterfaceValueFactory(
                interface=ComponentInterfaceFactory()),
        }]

        filtered_civ_sets = filter_civs_for_algorithm(civ_sets=civ_sets,
                                                      algorithm_image=ai)

        assert len(filtered_civ_sets) == 1
        assert {civ.interface for civ in filtered_civ_sets[0]} == {*cis}
示例#24
0
 def test_extra_viewer_groups(self):
     ai = AlgorithmImageFactory()
     civ = ComponentInterfaceValueFactory(
         interface=ai.algorithm.inputs.get())
     groups = (GroupFactory(), GroupFactory(), GroupFactory())
     jobs = create_algorithm_jobs(algorithm_image=ai,
                                  civ_sets=[{civ}],
                                  extra_viewer_groups=groups)
     for g in groups:
         assert jobs[0].viewer_groups.filter(pk=g.pk).exists()
示例#25
0
 def test_jobs_workflow(self):
     ai = AlgorithmImageFactory()
     images = [ImageFactory(), ImageFactory()]
     civ_sets = [{
         ComponentInterfaceValueFactory(image=im,
                                        interface=ai.algorithm.inputs.get())
     } for im in images]
     with capture_on_commit_callbacks() as callbacks:
         create_algorithm_jobs(algorithm_image=ai, civ_sets=civ_sets)
     assert len(callbacks) == 2
示例#26
0
 def test_civ_existing_does_nothing(self):
     image = ImageFactory()
     ai = AlgorithmImageFactory()
     j = AlgorithmJobFactory(creator=ai.creator, algorithm_image=ai)
     civ = ComponentInterfaceValueFactory(
         interface=self.default_input_interface, image=image)
     j.inputs.set([civ])
     assert Job.objects.count() == 1
     run_algorithm_job_for_inputs(job_pk=j.pk, upload_pks=[])
     assert Job.objects.count() == 1
示例#27
0
 def test_is_idempotent(self):
     ai = AlgorithmImageFactory()
     image = ImageFactory()
     civ = ComponentInterfaceValueFactory(
         image=image, interface=ai.algorithm.inputs.get())
     assert Job.objects.count() == 0
     create_algorithm_jobs(algorithm_image=ai, civ_sets=[{civ}])
     assert Job.objects.count() == 1
     jobs = create_algorithm_jobs(algorithm_image=ai, civ_sets=[{civ}])
     assert Job.objects.count() == 1
     assert len(jobs) == 0
    def test_group_addition(self, reverse):
        job = AlgorithmJobFactory()
        group = GroupFactory()
        civ_in, civ_out = (
            ComponentInterfaceValueFactory(image=ImageFactory()),
            ComponentInterfaceValueFactory(image=ImageFactory()),
        )
        job.inputs.add(civ_in)
        job.outputs.add(civ_out)
        assert "view_job" not in get_perms(group, job)
        assert "view_image" not in get_perms(group, civ_in.image)
        assert "view_image" not in get_perms(group, civ_out.image)

        if reverse:
            group.job_set.add(job)
        else:
            job.viewer_groups.add(group)

        assert "view_job" in get_perms(group, job)
        assert "view_image" in get_perms(group, civ_in.image)
        assert "view_image" in get_perms(group, civ_out.image)
示例#29
0
def test_add_images_to_component_interface_value():
    # Override the celery settings
    us = RawImageUploadSessionFactory()
    ImageFactory(origin=us), ImageFactory(origin=us)
    ci = ComponentInterface.objects.get(slug=DEFAULT_INPUT_INTERFACE_SLUG)

    civ = ComponentInterfaceValueFactory(interface=ci)

    with pytest.raises(ValueError) as err:
        add_images_to_component_interface_value(
            component_interface_value_pk=civ.pk, upload_pk=us.pk)
    assert "Image imports should result in a single image" in str(err)
    assert civ.image is None

    us2 = RawImageUploadSessionFactory()
    image = ImageFactory(origin=us2)
    civ2 = ComponentInterfaceValueFactory(interface=ci)
    add_images_to_component_interface_value(
        component_interface_value_pk=civ2.pk, upload_pk=us2.pk)
    civ2.refresh_from_db()
    assert civ2.image == image
示例#30
0
 def test_civ_existing_does_nothing(self):
     default_input_interface = ComponentInterface.objects.get(
         slug=DEFAULT_INPUT_INTERFACE_SLUG)
     image = ImageFactory()
     ai = AlgorithmImageFactory()
     j = AlgorithmJobFactory(creator=None, algorithm_image=ai)
     civ = ComponentInterfaceValueFactory(interface=default_input_interface,
                                          image=image)
     j.inputs.set([civ])
     assert Job.objects.count() == 1
     jobs = create_algorithm_jobs(algorithm_image=ai, images=[image])
     assert Job.objects.count() == 1
     assert len(jobs) == 0