Skip to content

Commit

Permalink
tests: increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Apr 16, 2024
1 parent 22017d8 commit 617c053
Showing 1 changed file with 110 additions and 15 deletions.
125 changes: 110 additions & 15 deletions ckanext/dc_view/tests/test_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import ckan.tests.factories as factories
import ckanext.dcor_schemas.plugin
import dcor_shared
from dcor_shared.testing import make_dataset, synchronous_enqueue_job
from dcor_shared.testing import (
make_dataset, make_dataset_via_s3, synchronous_enqueue_job
)

import pytest

Expand All @@ -19,7 +21,7 @@
@pytest.mark.usefixtures('clean_db', 'with_request_context')
@mock.patch('ckan.plugins.toolkit.enqueue_job',
side_effect=synchronous_enqueue_job)
def test_route_redircet_preview_to_s3_private(
def test_route_redirect_preview_to_s3_private(
enqueue_job_mock, app, tmpdir, create_with_upload, monkeypatch,
ckan_config):
monkeypatch.setitem(ckan_config, 'ckan.storage_path', str(tmpdir))
Expand All @@ -36,17 +38,13 @@ def test_route_redircet_preview_to_s3_private(
monkeypatch.setattr(ckan.common,
'current_user',
user_obj)
owner_org = factories.Organization(users=[{
'name': user['id'],
'capacity': 'admin'
}])
# Note: `call_action` bypasses authorization!
create_context = {'ignore_auth': False,
'user': user['name'],
'api_version': 3}
# create a dataset
ds_dict, res_dict = make_dataset(
create_context, owner_org,
create_context=create_context,
create_with_upload=create_with_upload,
resource_path=data_path / "calibration_beads_47.rtdc",
activate=True,
Expand Down Expand Up @@ -106,28 +104,125 @@ def test_route_preview_to_s3_public(
'DISABLE_AFTER_DATASET_CREATE_FOR_CONCURRENT_JOB_TESTS',
True)

user = factories.User()
# create a dataset
ds_dict, res_dict = make_dataset(
create_with_upload=create_with_upload,
resource_path=data_path / "calibration_beads_47.rtdc",
activate=True)
rid = res_dict["id"]
assert "s3_available" in res_dict
assert "s3_url" in res_dict

# Since version 0.9.0, we are no longer storing the preview image locally.
path = dcor_shared.get_resource_path(rid)
path_prev = path.with_name(path.name + "_preview.jpg")
assert not path_prev.exists(), "sanity check"

did = ds_dict["id"]
resp = app.get(
f"/dataset/{did}/resource/{rid}/preview.jpg",
)

endpoint = dcor_shared.get_ckan_config_option(
"dcor_object_store.endpoint_url")
bucket_name = dcor_shared.get_ckan_config_option(
"dcor_object_store.bucket_name").format(
organization_id=ds_dict["organization"]["id"])
redirect = resp.history[0]
assert redirect.status_code == 302
assert redirect.location.startswith(f"{endpoint}/{bucket_name}/preview/"
f"{rid[:3]}/{rid[3:6]}/{rid[6:]}")


@pytest.mark.ckan_config('ckan.plugins',
'dcor_depot dcor_schemas dc_serve dc_view')
@pytest.mark.usefixtures('clean_db', 'with_request_context')
@mock.patch('ckan.plugins.toolkit.enqueue_job',
side_effect=synchronous_enqueue_job)
def test_route_s3_redirect_preview_to_s3_private(
enqueue_job_mock, app, tmpdir, monkeypatch,
ckan_config):
monkeypatch.setitem(ckan_config, 'ckan.storage_path', str(tmpdir))
monkeypatch.setattr(ckan.lib.uploader,
'get_storage_path',
lambda: str(tmpdir))

user = factories.UserWithToken()
user_obj = ckan.model.User.by_name(user["name"])
monkeypatch.setattr(ckan.common,
'current_user',
user_obj)
owner_org = factories.Organization(users=[{
'name': user['id'],
'capacity': 'admin'
}])

# Note: `call_action` bypasses authorization!
create_context = {'ignore_auth': False,
'user': user['name'],
'api_version': 3}
# create a dataset
ds_dict, res_dict = make_dataset(
create_context, owner_org,
create_with_upload=create_with_upload,
ds_dict, res_dict = make_dataset_via_s3(
create_context=create_context,
resource_path=data_path / "calibration_beads_47.rtdc",
activate=True,
private=True
)
rid = res_dict["id"]
assert "s3_available" in res_dict
assert "s3_url" in res_dict
assert len(res_dict.get("url"))

# Since version 0.9.0, we are no longer storing the preview image locally.
path = dcor_shared.get_resource_path(rid)
path_prev = path.with_name(path.name + "_preview.jpg")
assert not path_prev.exists(), "sanity check"

did = ds_dict["id"]
# We should not be authorized to access the resource without API token
resp0 = app.get(
f"/dataset/{did}/resource/{rid}/preview.jpg",
status=404
)
assert len(resp0.history) == 0

# Try again with token
resp = app.get(
f"/dataset/{did}/resource/{rid}/preview.jpg",
headers={u"authorization": user["token"]},
)

endpoint = dcor_shared.get_ckan_config_option(
"dcor_object_store.endpoint_url")
bucket_name = dcor_shared.get_ckan_config_option(
"dcor_object_store.bucket_name").format(
organization_id=ds_dict["organization"]["id"])
redirect = resp.history[0]
assert redirect.status_code == 302
redirect_stem = (f"{endpoint}/{bucket_name}/preview/"
f"{rid[:3]}/{rid[3:6]}/{rid[6:]}")
# Since we have a presigned URL, it is longer than the normal S3 URL.
assert redirect.location.startswith(redirect_stem)
assert len(redirect.location) > len(redirect_stem)


@pytest.mark.ckan_config('ckan.plugins',
'dcor_depot dcor_schemas dc_serve dc_view')
@pytest.mark.usefixtures('clean_db', 'with_request_context')
@mock.patch('ckan.plugins.toolkit.enqueue_job',
side_effect=synchronous_enqueue_job)
def test_route_s3_preview_to_s3_public(
enqueue_job_mock, app, tmpdir, monkeypatch,
ckan_config):
monkeypatch.setitem(ckan_config, 'ckan.storage_path', str(tmpdir))
monkeypatch.setattr(ckan.lib.uploader,
'get_storage_path',
lambda: str(tmpdir))

# create a dataset
ds_dict, res_dict = make_dataset_via_s3(
resource_path=data_path / "calibration_beads_47.rtdc",
activate=True)
rid = res_dict["id"]
assert "s3_available" in res_dict
assert "s3_url" in res_dict
assert len(res_dict.get("url"))

# Since version 0.9.0, we are no longer storing the preview image locally.
path = dcor_shared.get_resource_path(rid)
Expand Down

0 comments on commit 617c053

Please sign in to comment.