Exemplo n.º 1
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()
Exemplo n.º 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
Exemplo n.º 3
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
Exemplo n.º 4
0
def test_archive_item_form(client, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True,)
    settings.task_always_eager = (True,)

    archive = ArchiveFactory()

    editor = UserFactory()
    user = UserFactory()

    archive.editors_group.user_set.add(editor)

    ci = ComponentInterfaceFactory(
        kind=InterfaceKind.InterfaceKindChoices.BOOL
    )
    civ = ComponentInterfaceValueFactory(
        interface=ci, value=True, file=None, image=None
    )
    ai = ArchiveItemFactory(archive=archive)
    ai.values.add(civ)

    # user cannot edit archive item
    response = get_view_for_user(
        viewname="archives:item-edit",
        client=client,
        method=client.get,
        reverse_kwargs={
            "archive_slug": archive.slug,
            "pk": ai.pk,
            "interface_slug": ci.slug,
        },
        follow=True,
        user=user,
    )
    assert response.status_code == 403

    archive.add_uploader(user)
    response = get_view_for_user(
        viewname="archives:item-edit",
        client=client,
        method=client.get,
        reverse_kwargs={
            "archive_slug": archive.slug,
            "pk": ai.pk,
            "interface_slug": ci.slug,
        },
        follow=True,
        user=user,
    )
    assert response.status_code == 200

    response = get_view_for_user(
        viewname="archives:item-edit",
        client=client,
        method=client.get,
        reverse_kwargs={
            "archive_slug": archive.slug,
            "pk": ai.pk,
            "interface_slug": ci.slug,
        },
        follow=True,
        user=editor,
    )
    assert response.status_code == 200

    assert ci.slug in response.rendered_content

    assert f'id="id_{ci.slug}" checked' in response.rendered_content

    assert Job.objects.count() == 0

    alg = AlgorithmFactory()
    AlgorithmImageFactory(algorithm=alg, ready=True)
    alg.inputs.set([ci])
    with capture_on_commit_callbacks(execute=True):
        archive.algorithms.add(alg)

    assert Job.objects.count() == 1

    civ_count = ComponentInterfaceValue.objects.count()

    with capture_on_commit_callbacks(execute=True):
        with capture_on_commit_callbacks(execute=True):
            response = get_view_for_user(
                viewname="archives:item-edit",
                client=client,
                method=client.post,
                reverse_kwargs={
                    "archive_slug": archive.slug,
                    "pk": ai.pk,
                    "interface_slug": ci.slug,
                },
                data={ci.slug: False},
                follow=True,
                user=editor,
            )

    assert ai.values.filter(pk=civ.pk).count() == 0
    # This should created a new CIV as they are immutable
    assert ComponentInterfaceValue.objects.count() == civ_count + 1

    # A new job should have been created, because the value for 'bool'
    # has changed
    assert Job.objects.count() == 2

    with capture_on_commit_callbacks(execute=True):
        with capture_on_commit_callbacks(execute=True):
            response = get_view_for_user(
                viewname="archives:item-edit",
                client=client,
                method=client.post,
                reverse_kwargs={
                    "archive_slug": archive.slug,
                    "pk": ai.pk,
                    "interface_slug": ci.slug,
                },
                data={ci.slug: True},
                follow=True,
                user=editor,
            )

    # New jobs should be created as there is a new CIV
    assert Job.objects.count() == 3
    assert ComponentInterfaceValue.objects.count() == civ_count + 2