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
Exemple #2
0
def test_algorithm_multiple_inputs(client, algorithm_io_image, settings,
                                   component_interfaces):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    creator = UserFactory()

    assert Job.objects.count() == 0

    # Create the algorithm image
    algorithm_container, sha256 = algorithm_io_image
    alg = AlgorithmImageFactory(image__from_path=algorithm_container,
                                image_sha256=sha256,
                                ready=True)
    alg.algorithm.add_editor(creator)

    alg.algorithm.inputs.set(ComponentInterface.objects.all())
    alg.algorithm.outputs.set(
        [ComponentInterface.objects.get(slug="results-json-file")])
    # create the job
    job = Job.objects.create(creator=creator, algorithm_image=alg)

    expected = []
    for ci in ComponentInterface.objects.exclude(
            kind=InterfaceKindChoices.ZIP):
        if ci.is_image_kind:
            image_file = ImageFileFactory(
                file__from_path=Path(__file__).parent / "resources" /
                "input_file.tif")
            job.inputs.add(
                ComponentInterfaceValueFactory(interface=ci,
                                               image=image_file.image))
            expected.append("file")
        elif ci.is_file_kind:
            civ = ComponentInterfaceValueFactory(interface=ci)
            civ.file.save("test", File(BytesIO(b"")))
            civ.save()
            job.inputs.add(civ)
            expected.append("file")
        else:
            job.inputs.add(
                ComponentInterfaceValueFactory(interface=ci, value="test"))
            expected.append("test")

    with capture_on_commit_callbacks() as callbacks:
        run_algorithm_job_for_inputs(job_pk=job.pk, upload_pks=[])
    recurse_callbacks(callbacks=callbacks)

    job.refresh_from_db()
    assert job.error_message == ""
    assert job.status == job.SUCCESS

    # Remove fake value for score
    output_dict = job.outputs.first().value
    output_dict.pop("score")

    assert {f"/input/{x.relative_path}"
            for x in job.inputs.all()} == set(output_dict.keys())
    assert sorted(map(lambda x: x if x != {} else "json",
                      output_dict.values())) == sorted(expected)