示例#1
0
def test_session_with_user_upload_to_archive(client, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    user = UserFactory()
    archive = ArchiveFactory()
    archive.add_editor(user=user)

    upload = create_upload_from_file(
        file_path=Path(__file__).parent / "resources" / "image10x10x10.mha",
        creator=user,
    )
    # with interface
    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],
                "archive": archive.slug,
                "interface": "generic-overlay",
            },
            HTTP_X_FORWARDED_PROTO="https",
        )

    assert response.status_code == 201
    upload_session = response.json()
    assert upload_session["uploads"] == [upload.api_url]
    item = ArchiveItem.objects.get()
    assert item.values.get().interface.slug == "generic-overlay"

    ArchiveItem.objects.all().delete()
    upload2 = create_upload_from_file(
        file_path=Path(__file__).parent / "resources" / "image10x10x10.mha",
        creator=user,
    )
    # without interface
    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": [upload2.api_url],
                "archive": archive.slug
            },
            HTTP_X_FORWARDED_PROTO="https",
        )

    assert response.status_code == 201
    upload_session = response.json()
    assert upload_session["uploads"] == [upload2.api_url]
    item = ArchiveItem.objects.get()
    assert item.values.get().interface.slug == "generic-medical-image"
示例#2
0
def test_process_images_api_view(client, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    user = UserFactory()
    us = RawImageUploadSessionFactory(creator=user)

    archive = ArchiveFactory()
    archive.add_uploader(user)

    f = StagedFileFactory(file__from_path=Path(__file__).parent / "resources" /
                          "image10x10x10.mha")

    RawImageFileFactory(upload_session=us, staged_file_id=f.file_id)

    def request_processing():
        return get_view_for_user(
            viewname="api:upload-session-process-images",
            reverse_kwargs={"pk": us.pk},
            user=user,
            client=client,
            method=client.patch,
            data={"archive": archive.slug},
            content_type="application/json",
        )

    # First request should work
    response = request_processing()
    assert response.status_code == 200

    # Jobs should only be run once
    response = request_processing()
    assert response.status_code == 400
示例#3
0
def test_user_upload_to_archive_item_without_interface(client, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    user = UserFactory()
    archive = ArchiveFactory()
    archive.add_editor(user=user)
    ci = ComponentInterface.objects.filter(slug="generic-overlay").get()
    civ = ComponentInterfaceValueFactory(interface=ci)
    item = ArchiveItemFactory(archive=archive)
    item.values.add(civ)
    assert item.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],
                "archive_item": item.pk
            },
            HTTP_X_FORWARDED_PROTO="https",
        )

    assert response.status_code == 400
    assert ("An interface needs to be defined to upload to an archive item."
            in response.json()["non_field_errors"])
示例#4
0
def create_archive_with_user_and_image(patient_id=""):
    a = ArchiveFactory()
    u = UserFactory()
    i = ImageFactory(patient_id=patient_id)
    add_image_to_archive(i, a)
    a.add_user(u)
    return a, u, i
示例#5
0
def generate_archive_patient_study_image_set():
    patient11 = PatientFactory()
    patient12 = PatientFactory()
    study111 = StudyFactory(patient=patient11)
    study112 = StudyFactory(patient=patient11)
    study113 = StudyFactory(patient=patient11)
    study121 = StudyFactory(patient=patient12)
    study122 = StudyFactory(patient=patient12)
    images111 = ImageFactoryWithoutImageFile.create_batch(4, study=study111)
    images112 = ImageFactoryWithoutImageFile.create_batch(5, study=study112)
    images113 = ImageFactoryWithoutImageFile.create_batch(6, study=study113)
    images121 = ImageFactoryWithoutImageFile.create_batch(2, study=study121)
    images122 = ImageFactoryWithoutImageFile.create_batch(3, study=study122)
    images211 = ImageFactoryWithoutImageFile.create_batch(4, study=None)
    archive1 = ArchiveFactory.create(
        images=[*images111, *images112, *images113, *images121, *images122])
    archive2 = ArchiveFactory.create(images=images211)
    return ArchivePatientStudyImageSet(
        archive1=archive1,
        patient11=patient11,
        patient12=patient12,
        study111=study111,
        study112=study112,
        study113=study113,
        study121=study121,
        study122=study122,
        images111=images111,
        images112=images112,
        images113=images113,
        images121=images121,
        images122=images122,
        archive2=archive2,
        images211=images211,
    )
示例#6
0
def test_civ_file_download(client):
    """Only viewers of the job should be allowed to download result files."""
    detection_interface = ComponentInterface(
        store_in_database=False,
        relative_path="detection_results.json",
        slug="detection-results",
        title="Detection Results",
        kind=ComponentInterface.Kind.ANY,
    )
    detection_interface.save()
    output_civ = ComponentInterfaceValue.objects.create(
        interface=detection_interface)
    detection = {
        "detected points": [{
            "type": "Point",
            "start": [0, 1, 2],
            "end": [3, 4, 5]
        }]
    }
    output_civ.file.save(
        "detection_results.json",
        ContentFile(
            bytes(json.dumps(detection, ensure_ascii=True, indent=2),
                  "utf-8")),
    )
    user1, user2 = UserFactory(), UserFactory()

    def has_correct_access(user_allowed, user_denied, url):
        tests = [(403, None), (302, user_allowed), (403, user_denied)]

        for test in tests:
            response = get_view_for_user(url=url, client=client, user=test[1])
            assert response.status_code == test[0]

    # test algorithm
    job = AlgorithmJobFactory(creator=user1)
    job.algorithm_image.algorithm.outputs.add(detection_interface)
    job.outputs.add(output_civ)

    has_correct_access(user1, user2, job.outputs.first().file.url)
    job.outputs.remove(output_civ)

    # test evaluation
    evaluation = EvaluationFactory()
    evaluation.output_interfaces.add(detection_interface)
    evaluation.outputs.add(output_civ)
    assign_perm("view_evaluation", user1, evaluation)
    has_correct_access(user1, user2, evaluation.outputs.first().file.url)
    evaluation.outputs.remove(output_civ)

    # test archive
    archive = ArchiveFactory()
    archive_item = ArchiveItemFactory(archive=archive)
    archive_item.values.add(output_civ)
    archive.add_editor(user1)
    has_correct_access(user1, user2, archive_item.values.first().file.url)
    archive.remove_editor(user1)
    archive.add_user(user1)
    has_correct_access(user1, user2, archive_item.values.first().file.url)
    archive.remove_user(user1)
示例#7
0
def test_api_archive_item_add_and_update_value(client, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    archive = ArchiveFactory()
    editor = UserFactory()
    archive.add_editor(editor)
    item = ArchiveItemFactory(archive=archive)
    ci = ComponentInterfaceFactory(
        kind=InterfaceKind.InterfaceKindChoices.BOOL)
    # add civ
    with capture_on_commit_callbacks(execute=True):
        response = get_view_for_user(
            viewname="api:archives-item-detail",
            reverse_kwargs={"pk": item.pk},
            data={"values": [{
                "interface": ci.slug,
                "value": True
            }]},
            user=editor,
            client=client,
            method=client.patch,
            content_type="application/json",
            HTTP_X_FORWARDED_PROTO="https",
        )
    assert response.status_code == 200
    assert response.json()["pk"] == str(item.pk)
    item.refresh_from_db()
    assert item.values.count() == 1
    civ = item.values.get()
    assert civ.interface.slug == ci.slug
    assert civ.value
    #  update civ
    with capture_on_commit_callbacks(execute=True):
        response = get_view_for_user(
            viewname="api:archives-item-detail",
            reverse_kwargs={"pk": item.pk},
            data={"values": [{
                "interface": ci.slug,
                "value": False
            }]},
            user=editor,
            client=client,
            method=client.patch,
            content_type="application/json",
            HTTP_X_FORWARDED_PROTO="https",
        )
    assert response.status_code == 200
    assert response.json()["pk"] == str(item.pk)
    item.refresh_from_db()
    assert item.values.count() == 1
    new_civ = item.values.get()
    assert new_civ.interface.slug == ci.slug
    assert new_civ != civ
示例#8
0
def test_api_archive_item_interface_type_update(client, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    archive = ArchiveFactory()
    editor = UserFactory()
    archive.add_editor(editor)
    item = ArchiveItemFactory(archive=archive)

    session, _ = create_raw_upload_image_session(images=["image10x10x10.mha"],
                                                 user=editor)
    session.refresh_from_db()
    im = session.image_set.get()
    ci = ComponentInterfaceFactory(
        kind=InterfaceKind.InterfaceKindChoices.IMAGE)
    civ = ComponentInterfaceValueFactory(interface=ci, image=im)
    item.values.add(civ)
    civ.image.update_viewer_groups_permissions()
    assert item.values.count() == 1

    # change interface type from generic medical image to generic overlay
    # for the already uploaded image
    with capture_on_commit_callbacks(execute=True):
        response = get_view_for_user(
            viewname="api:archives-item-detail",
            reverse_kwargs={"pk": item.pk},
            data={
                "values": [{
                    "interface": "generic-overlay",
                    "image": im.api_url
                }]
            },
            user=editor,
            client=client,
            method=client.patch,
            content_type="application/json",
            HTTP_X_FORWARDED_PROTO="https",
        )
    assert response.status_code == 200
    assert response.json()["pk"] == str(item.pk)
    item.refresh_from_db()
    # check that the old item was removed and a new one was added with the same
    # image but the new interface type
    assert item.values.count() == 1
    new_civ = item.values.get()
    assert new_civ.interface.slug == "generic-overlay"
    assert new_civ.image == im
    assert new_civ != civ
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
示例#11
0
    def test_archive_permissions(self, public):
        a: Archive = ArchiveFactory(public=public)

        expected_perms = {
            a.editors_group: {
                "view_archive",
                "use_archive",
                "upload_archive",
                "change_archive",
            },
            a.uploaders_group: {
                "view_archive",
                "use_archive",
                "upload_archive",
            },
            a.users_group: {"view_archive", "use_archive"},
        }

        if public:
            reg_and_anon = Group.objects.get(
                name=settings.REGISTERED_AND_ANON_USERS_GROUP_NAME)
            expected_perms[reg_and_anon] = {"view_archive"}

        assert get_groups_with_set_perms(a) == expected_perms
        assert get_users_with_perms(a, with_group_users=False).count() == 0
示例#12
0
    def setUp(self) -> None:
        self.method = MethodFactory(ready=True,
                                    phase__archive=ArchiveFactory())
        self.algorithm_image = AlgorithmImageFactory()

        self.images = ImageFactory.create_batch(3)
        self.method.phase.archive.images.set(self.images[:2])
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"},
    }
示例#14
0
def test_api_archive_item_update_permissions(client, settings, add_to_group,
                                             status):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    archive = ArchiveFactory()
    user = UserFactory()
    item = ArchiveItemFactory(archive=archive)

    if add_to_group:
        add_to_group(archive, user)

    ci = ComponentInterfaceFactory(
        kind=InterfaceKind.InterfaceKindChoices.BOOL)

    with capture_on_commit_callbacks(execute=True):
        response = get_view_for_user(
            viewname="api:archives-item-detail",
            reverse_kwargs={"pk": item.pk},
            data={"values": [{
                "interface": ci.slug,
                "value": True
            }]},
            user=user,
            client=client,
            method=client.patch,
            content_type="application/json",
            HTTP_X_FORWARDED_PROTO="https",
        )
    assert response.status_code == status
示例#15
0
    def test_job_permissions_for_archive(self):
        ai = AlgorithmImageFactory(ready=True)
        archive = ArchiveFactory()

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

        archive.images.set([im])
        archive.algorithms.set([ai.algorithm])

        create_algorithm_jobs_for_archive(archive_pks=[archive.pk])

        job = Job.objects.get()

        # The archive editors, users and uploaders, algorithm editors and job
        # viewers should be able to view the job
        assert get_groups_with_set_perms(job) == {
            archive.editors_group: {"view_job"},
            archive.users_group: {"view_job"},
            archive.uploaders_group: {"view_job"},
            ai.algorithm.editors_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()
示例#16
0
    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])

        archive.images.set([im])

        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()
    def test_permission_required_list_views(self, client):
        a = ArchiveFactory()
        u = UserFactory()

        for view_name, kwargs, permission, objs in [
            ("list", {}, "view_archive", {a}),
        ]:

            def _get_view():
                return get_view_for_user(
                    client=client,
                    viewname=f"archives:{view_name}",
                    reverse_kwargs=kwargs,
                    user=u,
                )

            response = _get_view()
            assert response.status_code == 200
            assert set() == {*response.context[-1]["object_list"]}

            assign_perm(permission, u, list(objs))

            response = _get_view()
            assert response.status_code == 200
            assert objs == {*response.context[-1]["object_list"]}

            for obj in objs:
                remove_perm(permission, u, obj)
示例#18
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
示例#19
0
def test_user_update_form(client):
    archive = ArchiveFactory()

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

    assert archive.users_group.user_set.count() == 0

    new_user = UserFactory()
    pr = ArchivePermissionRequestFactory(user=new_user, archive=archive)

    assert not archive.is_user(user=new_user)
    assert pr.status == ArchivePermissionRequest.PENDING
    response = get_view_for_user(
        viewname="archives:users-update",
        client=client,
        method=client.post,
        data={
            "user": new_user.pk,
            "action": "ADD"
        },
        reverse_kwargs={"slug": archive.slug},
        follow=True,
        user=editor,
    )
    assert response.status_code == 200

    archive.refresh_from_db()
    pr.refresh_from_db()
    assert archive.users_group.user_set.count() == 1
    assert archive.is_user(user=new_user)
    assert pr.status == ArchivePermissionRequest.ACCEPTED

    response = get_view_for_user(
        viewname="archives:users-update",
        client=client,
        method=client.post,
        data={
            "user": new_user.pk,
            "action": "REMOVE"
        },
        reverse_kwargs={"slug": archive.slug},
        follow=True,
        user=editor,
    )
    assert response.status_code == 200

    archive.refresh_from_db()
    pr.refresh_from_db()
    assert archive.users_group.user_set.count() == 0
    assert not archive.is_user(user=new_user)
    assert pr.status == ArchivePermissionRequest.REJECTED
示例#20
0
    def test_autocomplete_for_verified_email(self, client):
        archive = ArchiveFactory()
        admin = UserFactory()
        archive.add_editor(admin)

        user = UserFactory()
        VerificationFactory(user=user, is_verified=True)

        response = get_view_for_user(
            client=client,
            viewname="users-autocomplete",
            user=admin,
            data={"q": user.verification.email},
        )
        assert response.status_code == 200

        assert str(user.pk) in response.json()["results"][0]["id"]
示例#21
0
    def test_autocomplete_filter_options(self, client, filter, is_verified):
        archive = ArchiveFactory()

        admin = UserFactory()
        archive.add_editor(admin)
        first_name = "Jane"
        last_name = "Doe"

        if is_verified:
            u = UserFactory()
            VerificationFactory(user=u, is_verified=True)
            u.first_name = first_name
            u.last_name = last_name
            u.save()

        else:
            u = UserFactory(first_name=first_name, last_name=last_name)

        u.full_name = u.get_full_name().title()
        filter_criterion = getattr(u, filter)

        def get_user_autocomplete():
            return get_view_for_user(
                client=client,
                viewname="users-autocomplete",
                user=admin,
                data={"q": filter_criterion},
            )

        response = get_user_autocomplete()
        assert response.status_code == 200

        assert str(u.pk) in response.json()["results"][0]["id"]
        assert (
            html_escape(str(u.user_profile.get_mugshot_url()))
            in response.json()["results"][0]["text"]
        )
        assert u.username in response.json()["results"][0]["text"]
        assert u.get_full_name() in response.json()["results"][0]["text"]
        if is_verified:
            assert (
                u.verification.email.split("@")[1]
                in response.json()["results"][0]["text"]
            )
示例#22
0
def test_user_upload_to_archive_item_with_new_interface(client, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    user = UserFactory()
    archive = ArchiveFactory()
    archive.add_editor(user=user)
    ci = ComponentInterfaceFactory(kind=ComponentInterface.Kind.STRING,
                                   title="Test")
    civ = ComponentInterfaceValueFactory(interface=ci)
    item = ArchiveItemFactory(archive=archive)
    item.values.add(civ)
    assert item.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],
                "archive_item": item.pk,
                "interface": "generic-overlay",
            },
            HTTP_X_FORWARDED_PROTO="https",
        )

    assert response.status_code == 201
    upload_session = response.json()
    assert upload_session["uploads"] == [upload.api_url]
    item.refresh_from_db()
    assert item.values.count() == 2
    assert "generic-overlay" in [
        item.interface.slug for item in item.values.all()
    ]
示例#23
0
    def test_autocomplete_for_verified_email(self, client):
        archive = ArchiveFactory()
        admin = UserFactory()
        archive.add_editor(admin)

        call_command("check_permissions")
        user = UserenaSignup.objects.create_user("userena",
                                                 "*****@*****.**",
                                                 "testpassword",
                                                 active=True)
        VerificationFactory(user=user, is_verified=True)

        response = get_view_for_user(
            client=client,
            viewname="users-autocomplete",
            user=admin,
            data={"q": user.verification.email},
        )
        assert response.status_code == 200

        assert str(user.pk) in response.json()["results"][0]["id"]
示例#24
0
def test_editor_update_form(client):
    archive = ArchiveFactory()

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

    assert archive.editors_group.user_set.count() == 1

    new_editor = UserFactory()
    assert not archive.is_editor(user=new_editor)
    response = get_view_for_user(
        viewname="archives:editors-update",
        client=client,
        method=client.post,
        data={
            "user": new_editor.pk,
            "action": "ADD"
        },
        reverse_kwargs={"slug": archive.slug},
        follow=True,
        user=editor,
    )
    assert response.status_code == 200

    archive.refresh_from_db()
    assert archive.editors_group.user_set.count() == 2
    assert archive.is_editor(user=new_editor)

    response = get_view_for_user(
        viewname="archives:editors-update",
        client=client,
        method=client.post,
        data={
            "user": new_editor.pk,
            "action": "REMOVE"
        },
        reverse_kwargs={"slug": archive.slug},
        follow=True,
        user=editor,
    )
    assert response.status_code == 200

    archive.refresh_from_db()
    assert archive.editors_group.user_set.count() == 1
    assert not archive.is_editor(user=new_editor)
示例#25
0
def test_api_archive_item_retrieve_permissions(client):
    archive = ArchiveFactory()
    editor, user = UserFactory(), UserFactory()
    archive.add_editor(editor)
    i1 = ArchiveItemFactory(archive=archive)

    # editor can retrieve archive item
    response = get_view_for_user(
        viewname="api:archives-item-detail",
        reverse_kwargs={"pk": i1.pk},
        user=editor,
        client=client,
    )
    assert response.status_code == 200
    assert response.json()["pk"] == str(i1.pk)

    # user cannot retrieve archive item
    response = get_view_for_user(
        viewname="api:archives-item-detail",
        reverse_kwargs={"pk": i1.pk},
        user=user,
        client=client,
    )
    assert response.status_code == 404

    # add user to archive
    archive.add_user(user)
    response = get_view_for_user(
        viewname="api:archives-item-detail",
        reverse_kwargs={"pk": i1.pk},
        user=user,
        client=client,
    )
    assert response.status_code == 200
    assert response.json()["pk"] == str(i1.pk)
示例#26
0
class TwoArchives:
    def __init__(self):
        self.arch1, self.arch2 = ArchiveFactory(), ArchiveFactory()
        (
            self.editor1,
            self.uploader1,
            self.user1,
            self.editor2,
            self.uploader2,
            self.user2,
        ) = (
            UserFactory(),
            UserFactory(),
            UserFactory(),
            UserFactory(),
            UserFactory(),
            UserFactory(),
        )
        self.arch1.add_editor(user=self.editor1)
        self.arch2.add_editor(user=self.editor2)
        self.arch1.add_uploader(user=self.uploader1)
        self.arch2.add_uploader(user=self.uploader2)
        self.arch1.add_user(user=self.user1)
        self.arch2.add_user(user=self.user2)
        self.u = UserFactory()
示例#27
0
def test_upload_sessions_create(client, settings):
    user = UserFactory()
    a = ArchiveFactory()
    a.add_uploader(user)
    # without interface
    response = get_view_for_user(
        viewname="api:upload-session-list",
        user=user,
        client=client,
        method=client.post,
        content_type="application/json",
        data={
            "archive": a.slug,
            "uploads": [create_completed_upload(user=user).api_url],
        },
    )
    assert response.status_code == 201

    upload_session = RawImageUploadSession.objects.get(
        pk=response.data.get("pk"))
    assert upload_session.creator == user

    # with interface
    response = get_view_for_user(
        viewname="api:upload-session-list",
        user=user,
        client=client,
        method=client.post,
        content_type="application/json",
        data={
            "archive": a.slug,
            "interface": "generic-overlay",
            "uploads": [create_completed_upload(user=user).api_url],
        },
    )
    assert response.status_code == 201

    upload_session = RawImageUploadSession.objects.get(
        pk=response.data.get("pk"))
    assert upload_session.creator == user
示例#28
0
def test_user_upload_to_archive_item_with_existing_interface(client, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    user = UserFactory()
    archive = ArchiveFactory()
    archive.add_editor(user=user)
    ci = ComponentInterface.objects.filter(slug="generic-overlay").get()
    civ = ComponentInterfaceValueFactory(interface=ci)
    item = ArchiveItemFactory(archive=archive)
    item.values.add(civ)
    assert item.values.count() == 1

    # upload another generic-overlay to the same item
    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],
                "archive_item": item.pk,
                "interface": "generic-overlay",
            },
            HTTP_X_FORWARDED_PROTO="https",
        )
    assert response.status_code == 201
    item.refresh_from_db()
    # check that there is only one civ with the generic-overlay interface
    assert item.values.filter(interface__slug="generic-overlay").count() == 1
    # and that the previously added one is no longer associated with the item
    assert civ not in item.values.all()
示例#29
0
    def test_visible_to_public_group_permissions(self):
        g_reg_anon = Group.objects.get(
            name=settings.REGISTERED_AND_ANON_USERS_GROUP_NAME)
        a = ArchiveFactory()

        assert "view_archive" not in get_perms(g_reg_anon, a)

        a.public = True
        a.save()

        assert "view_archive" in get_perms(g_reg_anon, a)

        a.public = False
        a.save()

        assert "view_archive" not in get_perms(g_reg_anon, a)
示例#30
0
    def setUp(self) -> None:
        self.method = MethodFactory(ready=True,
                                    phase__archive=ArchiveFactory())
        self.algorithm_image = AlgorithmImageFactory()

        interface = ComponentInterfaceFactory()
        self.algorithm_image.algorithm.inputs.set([interface])

        self.images = ImageFactory.create_batch(3)

        for image in self.images[:2]:
            civ = ComponentInterfaceValueFactory(image=image,
                                                 interface=interface)
            ai = ArchiveItemFactory(archive=self.method.phase.archive)
            ai.values.add(civ)