def test_session_limit(http_image, docker_client, settings):
    path, sha256 = http_image

    wsi = WorkstationImageFactory(image__from_path=path,
                                  image_sha256=sha256,
                                  ready=True)

    # Execute the celery in place
    settings.WORKSTATIONS_MAXIMUM_SESSIONS = 1
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    try:
        with capture_on_commit_callbacks(execute=True):
            s1 = SessionFactory(workstation_image=wsi)
        s1.refresh_from_db()
        assert s1.status == s1.STARTED

        with capture_on_commit_callbacks(execute=True):
            s2 = SessionFactory(workstation_image=wsi)
        s2.refresh_from_db()
        assert s2.status == s2.FAILED

        s1.stop()

        with capture_on_commit_callbacks(execute=True):
            s3 = SessionFactory(workstation_image=wsi)
        s3.refresh_from_db()
        assert s3.status == s3.STARTED
    finally:
        stop_all_sessions()
Exemple #2
0
def test_submission_evaluation(
    client, evaluation_image, submission_file, settings
):
    # Override the celery settings
    settings.task_eager_propagates = (True,)
    settings.task_always_eager = (True,)

    # Upload a submission and create an evaluation
    eval_container, sha256 = evaluation_image
    method = MethodFactory(
        image__from_path=eval_container, image_sha256=sha256, ready=True
    )

    # We should not be able to download methods
    with pytest.raises(NotImplementedError):
        _ = method.image.url

    # This will create an evaluation, and we'll wait for it to be executed
    with capture_on_commit_callbacks() as callbacks:
        submission = SubmissionFactory(
            predictions_file__from_path=submission_file, phase=method.phase
        )

    recurse_callbacks(callbacks=callbacks)

    # The evaluation method should return the correct answer
    assert len(submission.evaluation_set.all()) == 1

    evaluation = submission.evaluation_set.first()
    assert evaluation.stdout.endswith("Greetings from stdout\n")
    assert evaluation.stderr.endswith('warn("Hello from stderr")\n')
    assert evaluation.error_message == ""
    assert evaluation.status == evaluation.SUCCESS
    assert (
        evaluation.outputs.get(interface__slug="metrics-json-file").value[
            "acc"
        ]
        == 0.5
    )

    # Try with a csv file
    with capture_on_commit_callbacks() as callbacks:
        submission = SubmissionFactory(
            predictions_file__from_path=Path(__file__).parent
            / "resources"
            / "submission.csv",
            phase=method.phase,
        )

    recurse_callbacks(callbacks=callbacks)

    evaluation = submission.evaluation_set.first()
    assert len(submission.evaluation_set.all()) == 1
    assert evaluation.status == evaluation.SUCCESS
    assert (
        evaluation.outputs.get(interface__slug="metrics-json-file").value[
            "acc"
        ]
        == 0.5
    )
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"},
    }
Exemple #4
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"
Exemple #5
0
def test_failed_job_notifications(client, algorithm_io_image, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    creator = UserFactory()
    editor = UserFactory()

    # 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(editor)

    job = Job.objects.create(creator=creator, algorithm_image=alg)

    # mark job as failed
    job.status = Job.FAILURE
    job.save()

    with capture_on_commit_callbacks(execute=True):
        send_failed_job_notification(job_pk=job.pk)

    # 2 notifications: for the editor of the algorithm and the job creator
    assert Notification.objects.count() == 2
    assert creator.username in str(Notification.objects.all())
    assert editor.username in str(Notification.objects.all())
    for notification in Notification.objects.all():
        assert (
            f"Unfortunately one of the jobs for algorithm {alg.algorithm.title} failed with an error"
            in notification.print_notification(user=notification.user))

    # delete notifications for easier testing below
    Notification.objects.all().delete()
    # unsubscribe editor from job notifications
    _ = get_view_for_user(
        viewname="api:follow-detail",
        client=client,
        method=client.patch,
        reverse_kwargs={
            "pk": Follow.objects.filter(user=editor,
                                        flag="job-active").get().pk
        },
        content_type="application/json",
        data={"flag": "job-inactive"},
        user=editor,
    )

    job = Job.objects.create(creator=creator, algorithm_image=alg)

    # mark job as failed
    job.status = Job.FAILURE
    job.save()

    with capture_on_commit_callbacks(execute=True):
        send_failed_job_notification(job_pk=job.pk)

    assert Notification.objects.count() == 1
    assert Notification.objects.get().user is not editor
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"},
    }
Exemple #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
Exemple #8
0
def test_session_with_user_upload_to_readerstudy(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)

    upload = create_upload_from_file(
        file_path=Path(__file__).parent / "resources" / "image10x10x10.mha",
        creator=user,
    )

    # try upload 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],
                "reader_study": rs.slug,
                "interface": "generic-overlay",
            },
            HTTP_X_FORWARDED_PROTO="https",
        )

    assert response.status_code == 400
    assert (
        "An interface can only be defined for archive, archive item or display set uploads."
        in response.json()["non_field_errors"])

    # try 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": [upload.api_url],
                "reader_study": rs.slug
            },
            HTTP_X_FORWARDED_PROTO="https",
        )

    assert response.status_code == 201
    upload_session = response.json()
    assert upload_session["uploads"] == [upload.api_url]
Exemple #9
0
    def test_create_evaluation_is_idempotent(self):
        s = SubmissionFactory(phase=self.method.phase,
                              algorithm_image=self.algorithm_image)

        with capture_on_commit_callbacks(execute=False) as callbacks1:
            create_evaluation(submission_pk=s.pk, max_initial_jobs=None)

        with capture_on_commit_callbacks(execute=False) as callbacks2:
            create_evaluation(submission_pk=s.pk, max_initial_jobs=None)

        # Execute the callbacks non-recursively
        for c in chain(callbacks1, callbacks2):
            c()

        assert Job.objects.count() == 2
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) == {}
    def test_zaak_delete_oio_removed(self, m):
        mock_service_oas_get(m, APITypes.drc, self.base)
        document = f"{self.base}enkelvoudiginformatieobjecten/{uuid.uuid4()}"
        eio_response = get_eio_response(
            document,
            informatieobjecttype="http://testserver/catalogi/api/v1/iot/dummy",
        )
        m.get(document, json=eio_response)
        zaak = ZaakFactory.create()
        zio = ZaakInformatieObjectFactory.create(
            zaak=zaak,
            informatieobject=document,
            _objectinformatieobject_url=f"{self.base}_objectinformatieobjecten/{uuid.uuid4()}",
        )
        m.delete(zio._objectinformatieobject_url, status_code=204)
        zaak_delete_url = get_operation_url("zaak_delete", uuid=zaak.uuid)

        with capture_on_commit_callbacks(execute=True):
            response = self.client.delete(zaak_delete_url, **ZAAK_WRITE_KWARGS)

        self.assertEqual(
            response.status_code, status.HTTP_204_NO_CONTENT, response.data
        )

        delete_call = next((req for req in m.request_history if req.method == "DELETE"))
        self.assertEqual(delete_call.url, zio._objectinformatieobject_url)
    def test_unhiding_challenge_updates_perms(self, settings):
        """If a challenge is unhidden then the viewer group should be updated"""
        e: Evaluation = EvaluationFactory(
            submission__phase__auto_publish_new_results=True,
            submission__phase__challenge__hidden=True,
        )

        participants = e.submission.phase.challenge.participants_group
        all_users = Group.objects.get(
            name=settings.REGISTERED_AND_ANON_USERS_GROUP_NAME)

        assert get_groups_with_set_perms(e) == {
            e.submission.phase.challenge.admins_group: {
                "change_evaluation",
                "view_evaluation",
            },
            participants: {"view_evaluation"},
        }

        # Override the celery settings
        settings.task_eager_propagates = (True, )
        settings.task_always_eager = (True, )

        with capture_on_commit_callbacks(execute=True):
            e.submission.phase.challenge.hidden = False
            e.submission.phase.challenge.save()

        assert get_groups_with_set_perms(e) == {
            e.submission.phase.challenge.admins_group: {
                "change_evaluation",
                "view_evaluation",
            },
            all_users: {"view_evaluation"},
        }
Exemple #13
0
def create_raw_upload_image_session(
    *,
    images: List[str],
    delete_file=False,
    user=None,
    linked_task=None,
) -> Tuple[RawImageUploadSession, Dict[str, RawImageFile]]:
    creator = user or UserFactory(email="*****@*****.**")
    upload_session = RawImageUploadSession(creator=creator)

    uploaded_images = {}
    for image in images:
        staged_file = create_file_from_filepath(RESOURCE_PATH / image)
        image = RawImageFile.objects.create(
            upload_session=upload_session,
            filename=staged_file.name,
            staged_file_id=staged_file.uuid,
        )
        uploaded_images[staged_file.name] = image

    if delete_file:
        StagedAjaxFile(
            uploaded_images["image10x10x10.zraw"].staged_file_id).delete()

    upload_session.save()

    with capture_on_commit_callbacks(execute=True):
        upload_session.process_images(linked_task=linked_task)

    return upload_session, uploaded_images
    def test_send_welcome_email_after_create(self):
        with capture_on_commit_callbacks(execute=True) as callbacks:
            UserAccount.objects.create(**self.stub_data)

        self.assertEquals(len(callbacks), 1, msg=f"{callbacks}")
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, "Welcome!")
Exemple #15
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_send_notif_delete_resultaat(self, mock_client):
        """
        Check if notifications will be send when resultaat is deleted
        """
        client = mock_client.return_value
        besluit = BesluitFactory.create()
        besluit_url = get_operation_url("besluit_read", uuid=besluit.uuid)
        besluittype_url = reverse(besluit.besluittype)
        bio = BesluitInformatieObjectFactory.create(besluit=besluit)
        bio_url = get_operation_url("besluitinformatieobject_delete",
                                    uuid=bio.uuid)

        with capture_on_commit_callbacks(execute=True):
            response = self.client.delete(bio_url)

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT,
                         response.data)

        client.create.assert_called_once_with(
            "notificaties",
            {
                "kanaal": "besluiten",
                "hoofdObject": f"http://testserver{besluit_url}",
                "resource": "besluitinformatieobject",
                "resourceUrl": f"http://testserver{bio_url}",
                "actie": "destroy",
                "aanmaakdatum": "2018-09-07T00:00:00Z",
                "kenmerken": {
                    "verantwoordelijkeOrganisatie":
                    besluit.verantwoordelijke_organisatie,
                    "besluittype": f"http://testserver{besluittype_url}",
                },
            },
        )
def test_non_zip_submission_failure(client, evaluation_image, submission_file,
                                    settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    # Upload a submission and create an evaluation
    eval_container, sha256 = evaluation_image
    method = MethodFactory(image__from_path=eval_container,
                           image_sha256=sha256,
                           ready=True)

    # Try with a 7z file
    with capture_on_commit_callbacks(execute=True):
        submission = SubmissionFactory(
            predictions_file__from_path=Path(__file__).parent / "resources" /
            "submission.7z",
            phase=method.phase,
        )

    # The evaluation method should return the correct answer
    assert len(submission.evaluation_set.all()) == 1
    evaluation = submission.evaluation_set.first()
    assert evaluation.error_message.endswith(
        "7z-compressed files are not supported.")
    assert evaluation.status == evaluation.FAILURE
Exemple #18
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
    def test_besluitinformatieobject_delete_fail_send_notification_create_db_entry(
        self, ):
        bio = BesluitInformatieObjectFactory.create()
        url = reverse(bio)

        with capture_on_commit_callbacks(execute=True):
            response = self.client.delete(url)

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        self.assertEqual(StatusLog.objects.count(), 1)

        logged_warning = StatusLog.objects.get()
        failed = FailedNotification.objects.get()
        message = {
            "aanmaakdatum": "2019-01-01T12:00:00Z",
            "actie": "destroy",
            "hoofdObject": f"http://testserver{reverse(bio.besluit)}",
            "kanaal": "besluiten",
            "kenmerken": {
                "verantwoordelijkeOrganisatie":
                bio.besluit.verantwoordelijke_organisatie,
                "besluittype":
                f"http://testserver{reverse(bio.besluit.besluittype)}",
            },
            "resource": "besluitinformatieobject",
            "resourceUrl": f"http://testserver{url}",
        }

        self.assertEqual(failed.statuslog_ptr, logged_warning)
        self.assertEqual(failed.message, message)
Exemple #20
0
def recurse_callbacks(callbacks):
    with capture_on_commit_callbacks() as new_callbacks:
        for callback in callbacks:
            callback()

    if new_callbacks:
        recurse_callbacks(callbacks=new_callbacks)
Exemple #21
0
    def test_gebruiksrechten_delete_fail_send_notification_create_db_entry(
            self):
        eio = EnkelvoudigInformatieObjectFactory.create()
        eio_url = f"http://testserver{reverse(eio)}"
        gebruiksrechten = GebruiksrechtenCMISFactory(informatieobject=eio_url)

        url = reverse(gebruiksrechten)

        with capture_on_commit_callbacks(execute=True):
            response = self.client.delete(url)

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        self.assertEqual(StatusLog.objects.count(), 1)

        logged_warning = StatusLog.objects.get()
        failed = FailedNotification.objects.get()
        eio = EnkelvoudigInformatieObject.objects.get()
        message = {
            "aanmaakdatum": "2019-01-01T12:00:00Z",
            "actie": "destroy",
            "hoofdObject": f"http://testserver{reverse(eio)}",
            "kanaal": "documenten",
            "kenmerken": {
                "bronorganisatie": eio.bronorganisatie,
                "informatieobjecttype":
                f"http://testserver{reverse(eio.informatieobjecttype)}",
                "vertrouwelijkheidaanduiding": eio.vertrouwelijkheidaanduiding,
            },
            "resource": "gebruiksrechten",
            "resourceUrl": f"http://testserver{url}",
        }

        self.assertEqual(failed.statuslog_ptr, logged_warning)
        self.assertEqual(failed.message, message)
Exemple #22
0
    def test_zaaktype_delete_fail_send_notification_create_db_entry(self):
        zaaktype = ZaakTypeFactory.create()
        url = reverse(zaaktype)

        with capture_on_commit_callbacks(execute=True):
            response = self.client.delete(url)

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        self.assertEqual(StatusLog.objects.count(), 1)

        logged_warning = StatusLog.objects.get()
        failed = FailedNotification.objects.get()
        message = {
            "aanmaakdatum": "2019-01-01T12:00:00Z",
            "actie": "destroy",
            "hoofdObject": f"http://testserver{url}",
            "kanaal": "zaaktypen",
            "kenmerken": {
                "catalogus": f"http://testserver{reverse(zaaktype.catalogus)}",
            },
            "resource": "zaaktype",
            "resourceUrl": f"http://testserver{url}",
        }

        self.assertEqual(failed.statuslog_ptr, logged_warning)
        self.assertEqual(failed.message, message)
Exemple #23
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
    def test_resultaat_delete_fail_send_notification_create_db_entry(self):
        resultaat = ResultaatFactory.create()
        url = reverse(resultaat)

        with capture_on_commit_callbacks(execute=True):
            response = self.client.delete(url)

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        self.assertEqual(StatusLog.objects.count(), 1)

        logged_warning = StatusLog.objects.get()
        failed = FailedNotification.objects.get()
        message = {
            "aanmaakdatum": "2019-01-01T12:00:00Z",
            "actie": "destroy",
            "hoofdObject": f"http://testserver{reverse(resultaat.zaak)}",
            "kanaal": "zaken",
            "kenmerken": {
                "bronorganisatie":
                resultaat.zaak.bronorganisatie,
                "zaaktype":
                f"http://testserver{reverse(resultaat.zaak.zaaktype)}",
                "vertrouwelijkheidaanduiding":
                resultaat.zaak.vertrouwelijkheidaanduiding,
            },
            "resource": "resultaat",
            "resourceUrl": f"http://testserver{url}",
        }
        self.assertEqual(failed.statuslog_ptr, logged_warning)
        self.assertEqual(failed.message, message)
    def test_zaaktype_no_notify_on_no_change(self, m):
        procestype_url = ("https://selectielijst.openzaak.nl/api/v1/"
                          "procestypen/e1b73b12-b2f6-4c4e-8929-94f84dd2a57d")
        mock_oas_get(m)
        mock_resource_list(m, "procestypen")
        mock_resource_get(m, "procestypen", procestype_url)
        mock_nrc_oas_get(m)
        m.post("https://notificaties-api.vng.cloud/api/v1/notificaties",
               status_code=201)

        zaaktype = ZaakTypeFactory.create(
            concept=True,
            zaaktype_omschrijving="test",
            vertrouwelijkheidaanduiding="openbaar",
            trefwoorden=["test"],
            verantwoordingsrelatie=["bla"],
            selectielijst_procestype=procestype_url,
        )
        url = reverse("admin:catalogi_zaaktype_change", args=(zaaktype.pk, ))

        response = self.app.get(url)
        form = response.forms["zaaktype_form"]

        with capture_on_commit_callbacks(execute=True):
            form.submit("_save")

        called_urls = [item.url for item in m.request_history]
        self.assertNotIn(
            "https://notificaties-api.vng.cloud/api/v1/notificaties",
            called_urls)
    def test_zaaktype_notify_on_create(self, m):
        mock_oas_get(m)
        mock_resource_list(m, "procestypen")
        mock_nrc_oas_get(m)
        m.post("https://notificaties-api.vng.cloud/api/v1/notificaties",
               status_code=201)

        url = reverse("admin:catalogi_zaaktype_add")

        response = self.app.get(url)

        form = response.forms["zaaktype_form"]
        form["zaaktype_omschrijving"] = "test"
        form["doel"] = "test"
        form["aanleiding"] = "test"
        form["indicatie_intern_of_extern"].select("intern")
        form["handeling_initiator"] = "test"
        form["onderwerp"] = "test"
        form["handeling_behandelaar"] = "test"
        form["doorlooptijd_behandeling_days"] = 12
        form["opschorting_en_aanhouding_mogelijk"].select(False)
        form["verlenging_mogelijk"].select(False)
        form["vertrouwelijkheidaanduiding"].select("openbaar")
        form["producten_of_diensten"] = "https://example.com/foobarbaz"
        form["referentieproces_naam"] = "test"
        form["catalogus"] = self.catalogus.pk
        form["datum_begin_geldigheid"] = "21-11-2019"

        with capture_on_commit_callbacks(execute=True):
            form.submit("_save")

        called_urls = [item.url for item in m.request_history]
        self.assertIn("https://notificaties-api.vng.cloud/api/v1/notificaties",
                      called_urls)
    def test_send_notif_create_application(self, mock_client):
        """
        Check if notifications will be send when applicaties is created
        """
        client = mock_client.return_value
        url = get_operation_url("applicatie_create")

        data = {
            "client_ids": ["id1", "id2"],
            "label": "Melding Openbare Ruimte consumer",
            "heeftAlleAutorisaties": True,
        }

        with capture_on_commit_callbacks(execute=True):
            response = self.client.post(url, data)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        data = response.json()
        client.create.assert_called_once_with(
            "notificaties",
            {
                "kanaal": "autorisaties",
                "hoofdObject": data["url"],
                "resource": "applicatie",
                "resourceUrl": data["url"],
                "actie": "create",
                "aanmaakdatum": "2012-01-14T00:00:00Z",
                "kenmerken": {},
            },
        )
Exemple #28
0
def test_algorithm_input_image_multiple_files(build_images, 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())
    # create the job
    job = Job.objects.create(creator=creator, algorithm_image=alg)
    us = RawImageUploadSessionFactory()

    ImageFactory(origin=us), ImageFactory(origin=us)
    ci = ComponentInterface.objects.get(slug=DEFAULT_INPUT_INTERFACE_SLUG)

    civ = ComponentInterfaceValue.objects.create(interface=ci)
    job.inputs.add(civ)

    with pytest.raises(ValueError):
        with capture_on_commit_callbacks(execute=True):
            run_algorithm_job_for_inputs(job_pk=job.pk,
                                         upload_pks={civ.pk: us.pk})
Exemple #29
0
def test_algorithm_with_invalid_output(client, algorithm_image, settings):
    # Override the celery settings
    settings.task_eager_propagates = (True, )
    settings.task_always_eager = (True, )

    assert Job.objects.count() == 0

    # Create the algorithm image
    algorithm_container, sha256 = algorithm_image
    alg = AlgorithmImageFactory(image__from_path=algorithm_container,
                                image_sha256=sha256,
                                ready=True)

    # Make sure the job fails when trying to upload an invalid file
    detection_interface = ComponentInterfaceFactory(
        store_in_database=False,
        relative_path="some_text.txt",
        slug="detection-json-file",
        kind=ComponentInterface.Kind.JSON,
    )
    alg.algorithm.outputs.add(detection_interface)
    alg.save()
    image_file = ImageFileFactory(file__from_path=Path(__file__).parent /
                                  "resources" / "input_file.tif")

    with capture_on_commit_callbacks(execute=True):
        execute_jobs(algorithm_image=alg, images=[image_file.image])

    jobs = Job.objects.filter(algorithm_image=alg,
                              inputs__image=image_file.image,
                              status=Job.FAILURE).all()
    assert len(jobs) == 1
    assert jobs.first().error_message == "Invalid filetype."
    assert len(jobs[0].outputs.all()) == 2
    def test_send_notif_update_application(self, mock_client):
        """
        Check if notifications will be send when applicatie is updated
        """
        client = mock_client.return_value
        autorisatie = AutorisatieFactory.create(
            applicatie__client_ids=["id1", "id2"],
            zaaktype="https://example.com",
            scopes=["dummy.scope"],
            max_vertrouwelijkheidaanduiding=VertrouwelijkheidsAanduiding.
            openbaar,
        )
        applicatie = autorisatie.applicatie

        url = get_operation_url("applicatie_partial_update",
                                uuid=applicatie.uuid)

        with capture_on_commit_callbacks(execute=True):
            response = self.client.patch(url, {"client_ids": ["id1"]})

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()
        client.create.assert_called_once_with(
            "notificaties",
            {
                "kanaal": "autorisaties",
                "hoofdObject": data["url"],
                "resource": "applicatie",
                "resourceUrl": data["url"],
                "actie": "partial_update",
                "aanmaakdatum": "2012-01-14T00:00:00Z",
                "kenmerken": {},
            },
        )