def test_session_with_user_duplicate_upload(client): user = UserFactory() upload1 = create_upload_from_file( file_path=Path(__file__).parent / "resources" / "image10x10x10.mha", creator=user, ) upload2 = create_upload_from_file( file_path=Path(__file__).parent / "resources" / "image10x10x10.mha", creator=user, ) response = get_view_for_user( viewname="api:upload-session-list", user=user, client=client, method=client.post, content_type="application/json", data={"uploads": [upload1.api_url, upload2.api_url]}, HTTP_X_FORWARDED_PROTO="https", ) assert response.status_code == 400 assert response.json() == { "non_field_errors": ["Filenames must be unique"] }
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"
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_session_with_user_upload(client, obj, factory): user = UserFactory() o = factory() o.add_editor(user=user) upload = create_upload_from_file( file_path=Path(__file__).parent / "resources" / "image10x10x10.mha", creator=user, ) 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], obj: o.slug }, HTTP_X_FORWARDED_PROTO="https", ) assert response.status_code == 201 upload_session = response.json() assert upload_session["uploads"] == [upload.api_url]
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]
def test_upload_some_images(client: Client, challenge_set, settings): # Override the celery settings settings.task_eager_propagates = (True, ) settings.task_always_eager = (True, ) user = UserFactory() # Use reader studies as this uses UploadRawImagesForm rs = ReaderStudyFactory(use_display_sets=False) rs.add_editor(user) response = get_view_for_user( client=client, viewname="reader-studies:add-images", user=user, reverse_kwargs={"slug": rs.slug}, ) assert response.status_code == 200 assert rs.images.count() == 0 assert RawImageUploadSession.objects.count() == 0 user_upload = create_upload_from_file(file_path=RESOURCE_PATH / "image10x10x10.mha", creator=user) with capture_on_commit_callbacks(execute=True): response = get_view_for_user( data={"user_uploads": [user_upload.pk]}, client=client, viewname="reader-studies:add-images", user=user, reverse_kwargs={"slug": rs.slug}, method=client.post, ) assert response.status_code == 302 assert rs.images.count() == 1 sessions = RawImageUploadSession.objects.all() assert len(sessions) == 1 response = get_view_for_user(url=sessions[0].get_absolute_url(), client=client, user=user) assert response.status_code == 200 response = get_view_for_user( url=sessions[0].get_absolute_url(), client=client, user=UserFactory(is_staff=True), ) assert response.status_code == 403
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() ]
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()
def create_raw_upload_image_session( *, images: List[str], delete_file=False, user=None, linked_task=None ) -> Tuple[RawImageUploadSession, Dict[str, UserUpload]]: creator = user or UserFactory(email="*****@*****.**") upload_session = RawImageUploadSession.objects.create(creator=creator) uploaded_images = {} for image in images: upload = create_upload_from_file(file_path=RESOURCE_PATH / image, creator=creator) uploaded_images[upload.filename] = upload upload_session.user_uploads.add(upload) if delete_file: uploaded_images["image10x10x10.zraw"].delete() with capture_on_commit_callbacks(execute=True): upload_session.process_images(linked_task=linked_task) return upload_session, uploaded_images
def test_api_archive_item_add_and_update_non_image_file(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) assert item.values.count() == 0 ci = ComponentInterfaceFactory(kind=InterfaceKind.InterfaceKindChoices.PDF) upload = create_upload_from_file(creator=editor, file_path=RESOURCE_PATH / "test.pdf") # 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, "user_upload": upload.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() assert item.values.count() == 1 civ = item.values.get() assert civ.interface.slug == ci.slug # update civ upload2 = create_upload_from_file(creator=editor, file_path=RESOURCE_PATH / "test.zip") 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, "user_upload": upload2.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() assert item.values.count() == 1 new_civ = item.values.get() assert new_civ.interface.slug == ci.slug assert new_civ != civ