Skip to content

Commit

Permalink
Test upload resources (#2493)
Browse files Browse the repository at this point in the history
  • Loading branch information
javitonino authored Sep 25, 2024
1 parent 0b77d6b commit 3b21024
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions nucliadb/src/nucliadb/writer/api/v1/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,11 @@ async def store_file_on_nuclia_db(
uuid=rid,
x_skip_store=False,
)
else:
# Use defaults for everything, but don't forget hidden which depends on KB config
kb_config = await datamanagers.atomic.kb.get_config(kbid=kbid)
if kb_config and kb_config.hidden_resources_hide_on_creation:
writer.basic.hidden = True

async with unique_slug_context_manager:
if override_resource_title and filename is not None:
Expand Down
49 changes: 49 additions & 0 deletions nucliadb/tests/writer/unit/api/v1/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from nucliadb.writer.tus.exceptions import HTTPConflict, HTTPNotFound
from nucliadb_models.resource import QueueType
from nucliadb_protos.knowledgebox_pb2 import KnowledgeBoxConfig

UPLOAD_PACKAGE = "nucliadb.writer.api.v1.upload"

Expand Down Expand Up @@ -64,6 +65,13 @@ async def get_storage_mock():
yield get_storage_mock


@pytest.fixture(scope="function", autouse=True)
async def kb_config_mock():
with patch(f"{UPLOAD_PACKAGE}.datamanagers.atomic.kb.get_config") as mock:
mock.return_value = KnowledgeBoxConfig()
yield mock


@pytest.mark.asyncio
async def test_store_file_on_nucliadb_does_not_store_passwords(
processing_mock, partitioning_mock, transaction_mock
Expand Down Expand Up @@ -127,3 +135,44 @@ async def test_validate_field_upload(rid, field, md5, exists: bool, result):
else:
with pytest.raises(result):
_, result_rid, result_field = await validate_field_upload("kbid", rid, field, md5)


@pytest.mark.asyncio
async def test_store_file_on_nucliadb_sets_hidden(
processing_mock, partitioning_mock, transaction_mock, kb_config_mock
):
field = "myfield"

await store_file_on_nuclia_db(
10,
"kbid",
"/some/path",
Request({"type": "http", "headers": []}),
"bucket",
Source.INGEST,
"rid",
field,
password="mypassword",
)
transaction_mock.commit.assert_awaited_once()
writer_bm = transaction_mock.commit.call_args[0][0]
assert writer_bm.basic.hidden is False

transaction_mock.commit.reset_mock()
kb_config_mock.return_value.hidden_resources_enabled = True
kb_config_mock.return_value.hidden_resources_hide_on_creation = True

await store_file_on_nuclia_db(
10,
"kbid",
"/some/path",
Request({"type": "http", "headers": []}),
"bucket",
Source.INGEST,
"rid",
field,
password="mypassword",
)
transaction_mock.commit.assert_awaited_once()
writer_bm = transaction_mock.commit.call_args[0][0]
assert writer_bm.basic.hidden is True

0 comments on commit 3b21024

Please sign in to comment.