diff --git a/repository_service_tuf_api/artifacts.py b/repository_service_tuf_api/artifacts.py index c3063588..3f139267 100644 --- a/repository_service_tuf_api/artifacts.py +++ b/repository_service_tuf_api/artifacts.py @@ -4,7 +4,7 @@ # SPDX-License-Identifier: MIT import json -from datetime import datetime +from datetime import datetime, timezone from typing import Any, Dict, List from fastapi import HTTPException, status @@ -201,7 +201,7 @@ def post(payload: AddPayload) -> ResponsePostAdd: data = { "artifacts": [artifact.path for artifact in payload.artifacts], "task_id": task_id, - "last_update": datetime.now(), + "last_update": datetime.now(timezone.utc), } return ResponsePostAdd(data=data, message=message) @@ -239,7 +239,7 @@ def delete(payload: DeletePayload) -> ResponsePostDelete: data = { "artifacts": payload.artifacts, "task_id": task_id, - "last_update": datetime.now(), + "last_update": datetime.now(timezone.utc), } message = "Remove Artifact(s) successfully submitted." @@ -263,7 +263,7 @@ def post_publish_artifacts() -> ResponsePostPublish: data = { "artifacts": [], "task_id": task_id, - "last_update": datetime.now(), + "last_update": datetime.now(timezone.utc), } return ResponsePostPublish( diff --git a/repository_service_tuf_api/bootstrap.py b/repository_service_tuf_api/bootstrap.py index e8fd5074..cbd6fba0 100644 --- a/repository_service_tuf_api/bootstrap.py +++ b/repository_service_tuf_api/bootstrap.py @@ -7,7 +7,7 @@ import logging import re import time -from datetime import datetime +from datetime import datetime, timezone from threading import Thread from typing import Any, Dict, List, Optional @@ -233,7 +233,7 @@ def post_bootstrap(payload: BootstrapPayload) -> BootstrapPostResponse: data = { "task_id": task_id, - "last_update": datetime.now(), + "last_update": datetime.now(timezone.utc), } return BootstrapPostResponse( diff --git a/repository_service_tuf_api/config.py b/repository_service_tuf_api/config.py index 219312b4..b748bcf8 100644 --- a/repository_service_tuf_api/config.py +++ b/repository_service_tuf_api/config.py @@ -4,7 +4,7 @@ # SPDX-License-Identifier: MIT import json -from datetime import datetime +from datetime import datetime, timezone from typing import Any, Dict from fastapi import HTTPException, status @@ -85,7 +85,7 @@ def put(payload: PutPayload): data = { "task_id": task_id, - "last_update": datetime.now(), + "last_update": datetime.now(timezone.utc), } return PutResponse(data=data, message="Settings successfully submitted.") diff --git a/repository_service_tuf_api/metadata.py b/repository_service_tuf_api/metadata.py index e327f1b2..c5e22115 100644 --- a/repository_service_tuf_api/metadata.py +++ b/repository_service_tuf_api/metadata.py @@ -4,7 +4,7 @@ # SPDX-License-Identifier: MIT import json -from datetime import datetime +from datetime import datetime, timezone from typing import Dict, Literal from fastapi import HTTPException, status @@ -83,7 +83,7 @@ def post_metadata(payload: MetadataPostPayload) -> MetadataPostResponse: message = "Metadata update accepted." data = { "task_id": task_id, - "last_update": datetime.now(), + "last_update": datetime.now(timezone.utc), } return MetadataPostResponse(data=data, message=message) @@ -224,7 +224,7 @@ def post_metadata_sign( message = "Metadata sign accepted." data = { "task_id": task_id, - "last_update": datetime.now(), + "last_update": datetime.now(timezone.utc), } return MetadataPostResponse(data=data, message=message) @@ -283,7 +283,7 @@ def delete_metadata_sign(payload: MetadataSignDeletePayload): message = "Metadata sign delete accepted." data = { "task_id": task_id, - "last_update": datetime.now(), + "last_update": datetime.now(timezone.utc), } return MetadataSignDeleteResponse(data=data, message=message) diff --git a/tests/conftest.py b/tests/conftest.py index d894698a..80b5d958 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,9 @@ # SPDX-FileCopyrightText: 2022-2023 VMware Inc # # SPDX-License-Identifier: MIT +from datetime import datetime, timezone + +import pretend import pytest from fastapi.testclient import TestClient @@ -13,3 +16,9 @@ def test_client(monkeypatch): client = TestClient(rstuf_app) return client + + +@pytest.fixture() +def fake_datetime(monkeypatch): + fake_time = datetime(2019, 6, 16, 9, 5, 1, tzinfo=timezone.utc) + return pretend.stub(now=pretend.call_recorder(lambda a: fake_time)) diff --git a/tests/unit/api/test_bootstrap.py b/tests/unit/api/test_bootstrap.py index 2bd263ad..aa3356eb 100644 --- a/tests/unit/api/test_bootstrap.py +++ b/tests/unit/api/test_bootstrap.py @@ -3,14 +3,15 @@ # # SPDX-License-Identifier: MIT -import datetime import json +from datetime import timezone import pretend import pytest from fastapi import status BOOTSTRAP_URL = "/api/v1/bootstrap/" +MOCK_PATH = "repository_service_tuf_api.bootstrap" class TestGetBootstrap: @@ -19,8 +20,7 @@ def test_get_bootstrap_available(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=False, state=None, task_id=None) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.get(BOOTSTRAP_URL) @@ -39,8 +39,7 @@ def test_get_bootstrap_not_available(self, test_client, monkeypatch): ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.get(BOOTSTRAP_URL) @@ -61,8 +60,7 @@ def test_get_bootstrap_already_bootstrap_in_pre( ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.get(BOOTSTRAP_URL) @@ -83,8 +81,7 @@ def test_get_bootstrap_already_bootstrap_in_signing( ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.get(BOOTSTRAP_URL) @@ -98,15 +95,16 @@ def test_get_bootstrap_already_bootstrap_in_signing( class TestPostBootstrap: - def test_post_bootstrap_bins_delegation(self, test_client, monkeypatch): + def test_post_bootstrap_bins_delegation( + self, test_client, monkeypatch, fake_datetime + ): mocked_bootstrap_state = pretend.call_recorder( lambda *a: pretend.stub( bootstrap=False, state="finished", task_id="task_id" ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_async_result = pretend.stub(state="SUCCESS") mocked_repository_metadata = pretend.stub( @@ -114,29 +112,18 @@ def test_post_bootstrap_bins_delegation(self, test_client, monkeypatch): AsyncResult=pretend.call_recorder(lambda *a: mocked_async_result), ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.repository_metadata", - mocked_repository_metadata, - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.get_task_id", lambda: "123" - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.pre_lock_bootstrap", - lambda *a: None, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: "123") + monkeypatch.setattr(f"{MOCK_PATH}.pre_lock_bootstrap", lambda *a: None) mocked__check_bootstrap_status = pretend.call_recorder(lambda *a: None) + monkeypatch.setattr( - "repository_service_tuf_api.bootstrap._check_bootstrap_status", + f"{MOCK_PATH}._check_bootstrap_status", mocked__check_bootstrap_status, ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) with open("tests/data_examples/bootstrap/payload_bins.json") as f: f_data = f.read() @@ -144,27 +131,28 @@ def test_post_bootstrap_bins_delegation(self, test_client, monkeypatch): response = test_client.post(BOOTSTRAP_URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED assert response.url == f"{test_client.base_url}{BOOTSTRAP_URL}" assert response.json() == { "message": "Bootstrap accepted.", - "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01"}, + "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01Z"}, } assert mocked_bootstrap_state.calls == [pretend.call()] assert mocked__check_bootstrap_status.calls == [ pretend.call(task_id="123", timeout=300) ] - def test_post_bootstrap_custom_delegation(self, test_client, monkeypatch): + def test_post_bootstrap_custom_delegation( + self, test_client, monkeypatch, fake_datetime + ): mocked_bootstrap_state = pretend.call_recorder( lambda *a: pretend.stub( bootstrap=False, state="finished", task_id="task_id" ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_async_result = pretend.stub(state="SUCCESS") mocked_repository_metadata = pretend.stub( @@ -172,29 +160,17 @@ def test_post_bootstrap_custom_delegation(self, test_client, monkeypatch): AsyncResult=pretend.call_recorder(lambda *a: mocked_async_result), ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.repository_metadata", - mocked_repository_metadata, - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.get_task_id", lambda: "123" - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.pre_lock_bootstrap", - lambda *a: None, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: "123") + monkeypatch.setattr(f"{MOCK_PATH}.pre_lock_bootstrap", lambda *a: None) mocked__check_bootstrap_status = pretend.call_recorder(lambda *a: None) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap._check_bootstrap_status", + f"{MOCK_PATH}._check_bootstrap_status", mocked__check_bootstrap_status, ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) path = "tests/data_examples/bootstrap/payload_custom_targets.json" with open(path) as f: @@ -203,27 +179,28 @@ def test_post_bootstrap_custom_delegation(self, test_client, monkeypatch): response = test_client.post(BOOTSTRAP_URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED assert response.url == f"{test_client.base_url}{BOOTSTRAP_URL}" assert response.json() == { "message": "Bootstrap accepted.", - "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01"}, + "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01Z"}, } assert mocked_bootstrap_state.calls == [pretend.call()] assert mocked__check_bootstrap_status.calls == [ pretend.call(task_id="123", timeout=300) ] - def test_post_bootstrap_unrecognized_field(self, test_client, monkeypatch): + def test_post_bootstrap_unrecognized_field( + self, test_client, monkeypatch, fake_datetime + ): mocked_bootstrap_state = pretend.call_recorder( lambda *a: pretend.stub( bootstrap=False, state="finished", task_id="task_id" ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_async_result = pretend.stub(state="SUCCESS") mocked_repository_metadata = pretend.stub( @@ -231,29 +208,17 @@ def test_post_bootstrap_unrecognized_field(self, test_client, monkeypatch): AsyncResult=pretend.call_recorder(lambda *a: mocked_async_result), ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.repository_metadata", - mocked_repository_metadata, - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.get_task_id", lambda: "123" - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.pre_lock_bootstrap", - lambda *a: None, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: "123") + monkeypatch.setattr(f"{MOCK_PATH}.pre_lock_bootstrap", lambda *a: None) mocked__check_bootstrap_status = pretend.call_recorder(lambda *a: None) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap._check_bootstrap_status", + f"{MOCK_PATH}._check_bootstrap_status", mocked__check_bootstrap_status, ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) with open("tests/data_examples/bootstrap/payload_bins.json") as f: f_data = f.read() @@ -261,12 +226,12 @@ def test_post_bootstrap_unrecognized_field(self, test_client, monkeypatch): payload["metadata"]["root"]["signed"]["x-v-n-url"] = "http://url.com" response = test_client.post(BOOTSTRAP_URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED assert response.url == f"{test_client.base_url}{BOOTSTRAP_URL}" assert response.json() == { "message": "Bootstrap accepted.", - "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01"}, + "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01Z"}, } assert mocked_bootstrap_state.calls == [pretend.call()] assert mocked__check_bootstrap_status.calls == [ @@ -282,8 +247,7 @@ def test_post_bootstrap_unrecognized_field_invalid( ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_async_result = pretend.stub(state="SUCCESS") mocked_repository_metadata = pretend.stub( @@ -291,16 +255,10 @@ def test_post_bootstrap_unrecognized_field_invalid( AsyncResult=pretend.call_recorder(lambda *a: mocked_async_result), ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.repository_metadata", - mocked_repository_metadata, - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.get_task_id", lambda: "123" - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.pre_lock_bootstrap", - lambda *a: None, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: "123") + monkeypatch.setattr(f"{MOCK_PATH}.pre_lock_bootstrap", lambda *a: None) with open("tests/data_examples/bootstrap/payload_bins.json") as f: f_data = f.read() @@ -325,8 +283,7 @@ def test_post_bootstrap_unrecognized_field_must_start_with_x( ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_async_result = pretend.stub(state="SUCCESS") mocked_repository_metadata = pretend.stub( @@ -334,16 +291,10 @@ def test_post_bootstrap_unrecognized_field_must_start_with_x( AsyncResult=pretend.call_recorder(lambda *a: mocked_async_result), ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.repository_metadata", - mocked_repository_metadata, - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.get_task_id", lambda: "123" - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.pre_lock_bootstrap", - lambda *a: None, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: "123") + monkeypatch.setattr(f"{MOCK_PATH}.pre_lock_bootstrap", lambda *a: None) with open("tests/data_examples/bootstrap/payload_bins.json") as f: f_data = f.read() @@ -359,15 +310,16 @@ def test_post_bootstrap_unrecognized_field_must_start_with_x( in response.text ) - def test_post_bootstrap_custom_timeout(self, test_client, monkeypatch): + def test_post_bootstrap_custom_timeout( + self, test_client, monkeypatch, fake_datetime + ): mocked_bootstrap_state = pretend.call_recorder( lambda *a: pretend.stub( bootstrap=False, state="finished", task_id="task_id" ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_async_result = pretend.stub(state="SUCCESS") mocked_repository_metadata = pretend.stub( @@ -375,29 +327,17 @@ def test_post_bootstrap_custom_timeout(self, test_client, monkeypatch): AsyncResult=pretend.call_recorder(lambda *a: mocked_async_result), ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.repository_metadata", - mocked_repository_metadata, - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.get_task_id", lambda: "123" - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.pre_lock_bootstrap", - lambda *a: None, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: "123") + monkeypatch.setattr(f"{MOCK_PATH}.pre_lock_bootstrap", lambda *a: None) mocked__check_bootstrap_status = pretend.call_recorder(lambda *a: None) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap._check_bootstrap_status", + f"{MOCK_PATH}._check_bootstrap_status", mocked__check_bootstrap_status, ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) with open("tests/data_examples/bootstrap/payload_bins.json") as f: f_data = f.read() @@ -406,12 +346,12 @@ def test_post_bootstrap_custom_timeout(self, test_client, monkeypatch): response = test_client.post(BOOTSTRAP_URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED assert response.url == f"{test_client.base_url}{BOOTSTRAP_URL}" assert response.json() == { "message": "Bootstrap accepted.", - "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01"}, + "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01Z"}, } assert mocked_bootstrap_state.calls == [pretend.call()] assert mocked__check_bootstrap_status.calls == [ @@ -425,8 +365,7 @@ def test_post_bootstrap_already_bootstrap(self, test_client, monkeypatch): ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) with open("tests/data_examples/bootstrap/payload_bins.json") as f: f_data = f.read() @@ -452,8 +391,7 @@ def test_post_bootstrap_already_bootstrap_in_pre( ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) with open("tests/data_examples/bootstrap/payload_bins.json") as f: f_data = f.read() @@ -477,8 +415,7 @@ def test_post_bootstrap_already_bootstrap_in_signing( ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) with open("tests/data_examples/bootstrap/payload_bins.json") as f: f_data = f.read() @@ -528,8 +465,7 @@ def test_post_payload_no_bins_or_delegated_targets( ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) with open("tests/data_examples/bootstrap/payload_bins.json") as f: f_data = f.read() @@ -552,8 +488,7 @@ def test_post_payload_bad_delegated_role_names( ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) path = "tests/data_examples/bootstrap/payload_custom_targets.json" with open(path) as f: @@ -582,8 +517,7 @@ def test_post_payload_delegated_role_with_empty_path_pattern( ) ) monkeypatch.setattr( - "repository_service_tuf_api.bootstrap.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) path = "tests/data_examples/bootstrap/payload_custom_targets.json" with open(path) as f: diff --git a/tests/unit/api/test_config.py b/tests/unit/api/test_config.py index eb1fb935..a2546d64 100644 --- a/tests/unit/api/test_config.py +++ b/tests/unit/api/test_config.py @@ -3,17 +3,18 @@ # # SPDX-License-Identifier: MIT -import datetime import json +from datetime import timezone import pretend from fastapi import status URL = "/api/v1/config" +MOCK_PATH = "repository_service_tuf_api.config" class TestPutSettings: - def test_put_settings(self, test_client, monkeypatch): + def test_put_settings(self, test_client, monkeypatch, fake_datetime): with open("tests/data_examples/config/update_settings.json") as f: f_data = f.read() @@ -23,34 +24,24 @@ def test_put_settings(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=True) ) monkeypatch.setattr( - "repository_service_tuf_api.config.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_get_task_id = pretend.call_recorder(lambda: "task-id") - monkeypatch.setattr( - "repository_service_tuf_api.config.get_task_id", mocked_get_task_id - ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", mocked_get_task_id) mocked_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda **kw: None) ) monkeypatch.setattr( - "repository_service_tuf_api.config.repository_metadata", - mocked_repository_metadata, - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.config.datetime", fake_datetime + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) response = test_client.put(URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED assert response.json() == { "data": { "task_id": "task-id", - "last_update": "2019-06-16T09:05:01", + "last_update": "2019-06-16T09:05:01Z", }, "message": "Settings successfully submitted.", } @@ -65,7 +56,9 @@ def test_put_settings(self, test_client, monkeypatch): ) ] - def test_put_settings_with_custom_targets(self, test_client, monkeypatch): + def test_put_settings_with_custom_targets( + self, test_client, monkeypatch, fake_datetime + ): path = "tests/data_examples/config/update_settings_custom_targets.json" with open(path) as f: f_data = f.read() @@ -76,34 +69,25 @@ def test_put_settings_with_custom_targets(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=True) ) monkeypatch.setattr( - "repository_service_tuf_api.config.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_get_task_id = pretend.call_recorder(lambda: "task-id") - monkeypatch.setattr( - "repository_service_tuf_api.config.get_task_id", mocked_get_task_id - ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", mocked_get_task_id) mocked_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda **kw: None) ) monkeypatch.setattr( - "repository_service_tuf_api.config.repository_metadata", - mocked_repository_metadata, - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.config.datetime", fake_datetime + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) + + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) response = test_client.put(URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED assert response.json() == { "data": { "task_id": "task-id", - "last_update": "2019-06-16T09:05:01", + "last_update": "2019-06-16T09:05:01Z", }, "message": "Settings successfully submitted.", } @@ -127,8 +111,7 @@ def test_put_settings_without_bootstrap(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=False, state=None) ) monkeypatch.setattr( - "repository_service_tuf_api.config.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.put(URL, json=payload) assert response.status_code == status.HTTP_404_NOT_FOUND @@ -149,8 +132,7 @@ def test_put_settings_intermediate_state(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=False, state="signing") ) monkeypatch.setattr( - "repository_service_tuf_api.config.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.put(URL, json=payload) assert response.status_code == status.HTTP_404_NOT_FOUND @@ -172,8 +154,7 @@ def test_get_settings(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=True) ) monkeypatch.setattr( - "repository_service_tuf_api.config.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) fake_settings = pretend.stub( fresh=pretend.call_recorder(lambda: None), @@ -181,10 +162,7 @@ def test_get_settings(self, test_client, monkeypatch): lambda: {"k": "v", "j": ["v1", "v2"], "l": "none"} ), ) - monkeypatch.setattr( - "repository_service_tuf_api.config.settings_repository", - fake_settings, - ) + monkeypatch.setattr(f"{MOCK_PATH}.settings_repository", fake_settings) test_response = test_client.get(url) assert test_response.status_code == status.HTTP_200_OK @@ -203,8 +181,7 @@ def test_get_settings_without_bootstrap(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=False, state="None") ) monkeypatch.setattr( - "repository_service_tuf_api.config.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) test_response = test_client.get(url) diff --git a/tests/unit/api/test_metadata.py b/tests/unit/api/test_metadata.py index 2f5eb97d..4962266e 100644 --- a/tests/unit/api/test_metadata.py +++ b/tests/unit/api/test_metadata.py @@ -4,8 +4,8 @@ # SPDX-License-Identifier: MIT import copy -import datetime import json +from datetime import timezone import pretend from fastapi import status @@ -13,16 +13,16 @@ METADATA_URL = "/api/v1/metadata/" SIGN_URL = "/api/v1/metadata/sign/" DELETE_SIGN_URL = "/api/v1/metadata/sign/delete" +MOCK_PATH = "repository_service_tuf_api.metadata" class TestPostMetadata: - def test_post_metadata(self, test_client, monkeypatch): + def test_post_metadata(self, test_client, monkeypatch, fake_datetime): mocked_bootstrap_state = pretend.call_recorder( lambda *a: pretend.stub(bootstrap=True) ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_async_result = pretend.stub(state="SUCCESS") mocked_repository_metadata = pretend.stub( @@ -30,19 +30,10 @@ def test_post_metadata(self, test_client, monkeypatch): AsyncResult=pretend.call_recorder(lambda *a: mocked_async_result), ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.repository_metadata", - mocked_repository_metadata, - ) - monkeypatch.setattr( - "repository_service_tuf_api.metadata.get_task_id", lambda: "123" - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.metadata.datetime", fake_datetime + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: "123") + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) with open( "tests/data_examples/metadata/update-root-payload.json" ) as f: @@ -51,12 +42,12 @@ def test_post_metadata(self, test_client, monkeypatch): payload = json.loads(f_data) response = test_client.post(METADATA_URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED assert response.url == f"{test_client.base_url}{METADATA_URL}" assert response.json() == { "message": "Metadata update accepted.", - "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01"}, + "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01Z"}, } assert mocked_bootstrap_state.calls == [pretend.call()] @@ -65,8 +56,7 @@ def test_post_metadata_without_bootstrap(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=False, state=None) ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) with open( "tests/data_examples/metadata/update-root-payload.json" @@ -93,8 +83,7 @@ def test_post_metadata_bootstrap_intermediate_state( lambda *a: pretend.stub(bootstrap=False, state="signing") ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) with open( "tests/data_examples/metadata/update-root-payload.json" @@ -169,8 +158,7 @@ def test_get_metadata_sign(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=True, state="signing") ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) with open("tests/data_examples/bootstrap/payload_bins.json") as f: md_content = f.read() @@ -192,8 +180,7 @@ def get_role(role_setting: str): ROOT_SIGNING=fake_metadata, ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.settings_repository", - mocked_settings_repository, + f"{MOCK_PATH}.settings_repository", mocked_settings_repository ) response = test_client.get(SIGN_URL) @@ -217,8 +204,7 @@ def test_get_metadata_sign_with_trusted_root( lambda *a: pretend.stub(bootstrap=True, state="signing") ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) with open("tests/data_examples/bootstrap/payload_bins.json") as f: md_content = f.read() @@ -249,8 +235,7 @@ def get_role(setting: str): TRUSTED_ROOT=fake_metadata, ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.settings_repository", - mocked_settings_repository, + f"{MOCK_PATH}.settings_repository", mocked_settings_repository ) response = test_client.get(SIGN_URL) @@ -280,16 +265,14 @@ def test_get_metadata_sign_no_pending_roles( lambda *a: pretend.stub(bootstrap=True, state="signing") ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_settings_repository = pretend.stub( reload=pretend.call_recorder(lambda: None), ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.settings_repository", - mocked_settings_repository, + f"{MOCK_PATH}.settings_repository", mocked_settings_repository ) response = test_client.get(SIGN_URL) @@ -305,8 +288,7 @@ def test_get_metadata_sign_no_bootstrap(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=False, state=None) ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.get(SIGN_URL) @@ -324,8 +306,7 @@ def test_get_metadata_sign_bootstrap_pre(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=False, state="pre") ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.get(SIGN_URL) @@ -340,41 +321,30 @@ def test_get_metadata_sign_bootstrap_pre(self, test_client, monkeypatch): class TestPostMetadataSign: - def test_post_metadata_sign(self, test_client, monkeypatch): + def test_post_metadata_sign(self, test_client, monkeypatch, fake_datetime): mocked_bootstrap_state = pretend.call_recorder( lambda *a: pretend.stub(bootstrap=True, state="signing") ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, - ) - monkeypatch.setattr( - "repository_service_tuf_api.metadata.get_task_id", - lambda: "fake_id", + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: "fake_id") fake_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda *a, **kw: None) ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.repository_metadata", - fake_repository_metadata, - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.metadata.datetime", fake_datetime + f"{MOCK_PATH}.repository_metadata", fake_repository_metadata ) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) payload = {"role": "root", "signature": {"keyid": "k1", "sig": "s1"}} response = test_client.post(SIGN_URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED, response.text assert response.json() == { "data": { "task_id": "fake_id", - "last_update": "2019-06-16T09:05:01", + "last_update": "2019-06-16T09:05:01Z", }, "message": "Metadata sign accepted.", } @@ -399,8 +369,7 @@ def test_post_metadata_no_bootstrap(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=False, state=None) ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) payload = {"role": "root", "signature": {"keyid": "k1", "sig": "s1"}} @@ -419,8 +388,7 @@ def test_post_metadata_bootstrap_finished(self, test_client, monkeypatch): lambda *a: pretend.stub(bootstrap=False, state="finished") ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) payload = {"role": "root", "signature": {"keyid": "k1", "sig": "s1"}} @@ -438,42 +406,34 @@ def test_post_metadata_bootstrap_finished(self, test_client, monkeypatch): class TestPostMetadataSignDelete: - def test_post_metadata_sign_delete(self, test_client, monkeypatch): + def test_post_metadata_sign_delete( + self, test_client, monkeypatch, fake_datetime + ): mocked_settings_repository = pretend.stub( reload=pretend.call_recorder(lambda: None), get_fresh=pretend.call_recorder(lambda *a: "metadata"), ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.settings_repository", - mocked_settings_repository, + f"{MOCK_PATH}.settings_repository", mocked_settings_repository ) fake_get_task_id = pretend.call_recorder(lambda: "123") - monkeypatch.setattr( - "repository_service_tuf_api.metadata.get_task_id", fake_get_task_id - ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", fake_get_task_id) mocked_async_result = pretend.stub(state="SUCCESS") mocked_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda *a, **kw: None), AsyncResult=pretend.call_recorder(lambda *a: mocked_async_result), ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) payload = {"role": "root"} - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.metadata.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) response = test_client.post(DELETE_SIGN_URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED, response.text assert response.json() == { - "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01"}, + "data": {"task_id": "123", "last_update": "2019-06-16T09:05:01Z"}, "message": "Metadata sign delete accepted.", } assert mocked_settings_repository.reload.calls == [pretend.call()] @@ -501,8 +461,7 @@ def test_metadata_sign_delete_role_not_in_signing_status( get_fresh=pretend.call_recorder(lambda *a: None), ) monkeypatch.setattr( - "repository_service_tuf_api.metadata.settings_repository", - mocked_settings_repository, + f"{MOCK_PATH}.settings_repository", mocked_settings_repository ) payload = {"role": "root"} diff --git a/tests/unit/api/test_targets.py b/tests/unit/api/test_targets.py index 05c452f7..5f8f339a 100644 --- a/tests/unit/api/test_targets.py +++ b/tests/unit/api/test_targets.py @@ -2,8 +2,8 @@ # SPDX-FileCopyrightText: 2022-2023 VMware Inc # # SPDX-License-Identifier: MIT -import datetime import json +from datetime import timezone from uuid import uuid4 import pretend @@ -12,10 +12,11 @@ ARTIFACTS_URL = "/api/v1/artifacts/" ARTIFACTS_DELETE_URL = "/api/v1/artifacts/delete" ARTIFACTS_POST_URL = "/api/v1/artifacts/publish/" +MOCK_PATH = "repository_service_tuf_api.artifacts" class TestPostArtifacts: - def test_post(self, monkeypatch, test_client): + def test_post(self, monkeypatch, test_client, fake_datetime): with open("tests/data_examples/artifacts/add_payload.json") as f: f_data = f.read() @@ -25,35 +26,25 @@ def test_post(self, monkeypatch, test_client): lambda *a: pretend.stub(bootstrap=True) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda **kw: None) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) fake_task_id = uuid4().hex - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.get_task_id", - lambda: fake_task_id, - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: fake_task_id) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) + response = test_client.post(ARTIFACTS_URL, json=payload) assert response.status_code == status.HTTP_202_ACCEPTED assert response.json() == { "data": { "artifacts": ["file1.tar.gz", "file2.tar.gz", "file3.tar.gz"], "task_id": fake_task_id, - "last_update": "2019-06-16T09:05:01", + "last_update": "2019-06-16T09:05:01Z", }, "message": "New Artifact(s) successfully submitted.", } @@ -74,7 +65,9 @@ def test_post(self, monkeypatch, test_client): ) ] - def test_post_with_add_task_id_to_custom(self, monkeypatch, test_client): + def test_post_with_add_task_id_to_custom( + self, monkeypatch, test_client, fake_datetime + ): with open("tests/data_examples/artifacts/add_payload.json") as f: f_data = f.read() @@ -84,28 +77,17 @@ def test_post_with_add_task_id_to_custom(self, monkeypatch, test_client): lambda *a: pretend.stub(bootstrap=True) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda **kw: None) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) fake_task_id = uuid4().hex - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.get_task_id", - lambda: fake_task_id, - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: fake_task_id) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) # enable to add task id to custom metadata field payload["add_task_id_to_custom"] = True @@ -116,7 +98,7 @@ def test_post_with_add_task_id_to_custom(self, monkeypatch, test_client): "data": { "artifacts": ["file1.tar.gz", "file2.tar.gz", "file3.tar.gz"], "task_id": fake_task_id, - "last_update": "2019-06-16T09:05:01", + "last_update": "2019-06-16T09:05:01Z", }, "message": "New Artifact(s) successfully submitted.", } @@ -148,7 +130,9 @@ def test_post_with_add_task_id_to_custom(self, monkeypatch, test_client): ) ] - def test_post_publish_artifacts_false(self, monkeypatch, test_client): + def test_post_publish_artifacts_false( + self, monkeypatch, test_client, fake_datetime + ): with open("tests/data_examples/artifacts/add_payload.json") as f: f_data = f.read() @@ -161,27 +145,16 @@ def test_post_publish_artifacts_false(self, monkeypatch, test_client): lambda *a: pretend.stub(bootstrap=True) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) fake_task_id = uuid4().hex - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.get_task_id", - lambda: fake_task_id, - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: fake_task_id) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) mocked_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda **kw: None) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) response = test_client.post(ARTIFACTS_URL, json=payload) assert response.status_code == status.HTTP_202_ACCEPTED @@ -193,7 +166,7 @@ def test_post_publish_artifacts_false(self, monkeypatch, test_client): "data": { "artifacts": ["file1.tar.gz", "file2.tar.gz", "file3.tar.gz"], "task_id": fake_task_id, - "last_update": "2019-06-16T09:05:01", + "last_update": "2019-06-16T09:05:01Z", }, "message": msg, } @@ -218,8 +191,7 @@ def test_post_without_bootstrap(self, monkeypatch, test_client): lambda *a: pretend.stub(bootstrap=False, state=None) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) payload = json.loads(f_data) @@ -244,8 +216,7 @@ def test_post_with_bootstrap_intermediate_state( lambda *a: pretend.stub(bootstrap=False, state="signing") ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) payload = json.loads(f_data) @@ -277,7 +248,7 @@ def test_post_missing_required_field(self, test_client): class TestPostArtifactsDelete: - def test_post_delete(self, monkeypatch, test_client): + def test_post_delete(self, monkeypatch, test_client, fake_datetime): payload = { "artifacts": ["file-v1.0.0_i683.tar.gz", "v0.4.1/file.tar.gz"], } @@ -286,38 +257,27 @@ def test_post_delete(self, monkeypatch, test_client): lambda *a: pretend.stub(bootstrap=True) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda **kw: None) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) fake_task_id = uuid4().hex - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.get_task_id", - lambda: fake_task_id, - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: fake_task_id) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) response = test_client.post(ARTIFACTS_DELETE_URL, json=payload) - assert fake_datetime.now.calls == [pretend.call()] + assert fake_datetime.now.calls == [pretend.call(timezone.utc)] assert response.status_code == status.HTTP_202_ACCEPTED assert response.json() == { "data": { "artifacts": ["file-v1.0.0_i683.tar.gz", "v0.4.1/file.tar.gz"], "task_id": fake_task_id, - "last_update": "2019-06-16T09:05:01", + "last_update": "2019-06-16T09:05:01Z", }, "message": "Remove Artifact(s) successfully submitted.", } @@ -335,7 +295,7 @@ def test_post_delete(self, monkeypatch, test_client): ] def test_post_publish_artifacts_delete_false( - self, monkeypatch, test_client + self, monkeypatch, test_client, fake_datetime ): payload = { "artifacts": ["file-v1.0.0_i683.tar.gz", "v0.4.1/file.tar.gz"], @@ -346,28 +306,17 @@ def test_post_publish_artifacts_delete_false( lambda *a: pretend.stub(bootstrap=True) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) mocked_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda **kw: None) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) fake_task_id = uuid4().hex - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.get_task_id", - lambda: fake_task_id, - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: fake_task_id) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) response = test_client.post(ARTIFACTS_DELETE_URL, json=payload) @@ -380,7 +329,7 @@ def test_post_publish_artifacts_delete_false( "data": { "artifacts": ["file-v1.0.0_i683.tar.gz", "v0.4.1/file.tar.gz"], "task_id": fake_task_id, - "last_update": "2019-06-16T09:05:01", + "last_update": "2019-06-16T09:05:01Z", }, "message": msg, } @@ -405,8 +354,7 @@ def test_post_without_bootstrap_delete(self, monkeypatch, test_client): lambda *a: pretend.stub(bootstrap=False, state=None) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.post(ARTIFACTS_DELETE_URL, json=payload) @@ -429,8 +377,7 @@ def test_post_with_bootstrap_intermediate_state_delete( lambda *a: pretend.stub(bootstrap=False, state="signing") ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.bootstrap_state", - mocked_bootstrap_state, + f"{MOCK_PATH}.bootstrap_state", mocked_bootstrap_state ) response = test_client.post(ARTIFACTS_DELETE_URL, json=payload) @@ -451,33 +398,24 @@ def test_post_missing_required_field_delete(self, test_client): class TestPostArtifactsPublish: - def test_post_publish(self, monkeypatch, test_client): + def test_post_publish(self, monkeypatch, test_client, fake_datetime): mocked_repository_metadata = pretend.stub( apply_async=pretend.call_recorder(lambda **kw: None) ) monkeypatch.setattr( - "repository_service_tuf_api.artifacts.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) fake_task_id = uuid4().hex - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.get_task_id", - lambda: fake_task_id, - ) - fake_time = datetime.datetime(2019, 6, 16, 9, 5, 1) - fake_datetime = pretend.stub( - now=pretend.call_recorder(lambda: fake_time) - ) - monkeypatch.setattr( - "repository_service_tuf_api.artifacts.datetime", fake_datetime - ) + monkeypatch.setattr(f"{MOCK_PATH}.get_task_id", lambda: fake_task_id) + monkeypatch.setattr(f"{MOCK_PATH}.datetime", fake_datetime) + response = test_client.post(ARTIFACTS_POST_URL) assert response.status_code == status.HTTP_202_ACCEPTED assert response.json() == { "data": { "artifacts": [], "task_id": fake_task_id, - "last_update": "2019-06-16T09:05:01", + "last_update": "2019-06-16T09:05:01Z", }, "message": "Publish artifacts successfully submitted.", } diff --git a/tests/unit/api/test_tasks.py b/tests/unit/api/test_tasks.py index d883a399..a77e845d 100644 --- a/tests/unit/api/test_tasks.py +++ b/tests/unit/api/test_tasks.py @@ -7,6 +7,7 @@ from fastapi import status TASK_URL = "/api/v1/task/" +MOCK_PATH = "repository_service_tuf_api.tasks" class TestGetTask: @@ -33,8 +34,7 @@ def test_get(self, test_client, monkeypatch): AsyncResult=pretend.call_recorder(lambda t: mocked_task_result) ) monkeypatch.setattr( - "repository_service_tuf_api.tasks.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) test_response = test_client.get(f"{TASK_URL}?task_id=test_id") @@ -73,8 +73,7 @@ def test_get_result_is_exception(self, test_client, monkeypatch): AsyncResult=pretend.call_recorder(lambda t: mocked_task_result) ) monkeypatch.setattr( - "repository_service_tuf_api.tasks.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) test_response = test_client.get(f"{TASK_URL}?task_id=test_id") @@ -108,8 +107,7 @@ def test_get_result_is_errored(self, test_client, monkeypatch): AsyncResult=pretend.call_recorder(lambda t: mocked_task_result) ) monkeypatch.setattr( - "repository_service_tuf_api.tasks.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) test_response = test_client.get(f"{TASK_URL}?task_id=test_id") @@ -144,8 +142,7 @@ def test_get_result_success_with_empty_result( AsyncResult=pretend.call_recorder(lambda t: mocked_task_result) ) monkeypatch.setattr( - "repository_service_tuf_api.tasks.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) test_response = test_client.get(f"{TASK_URL}?task_id=test_id") @@ -174,8 +171,7 @@ def test_get_result_failure_with_empty_result( AsyncResult=pretend.call_recorder(lambda t: mocked_task_result) ) monkeypatch.setattr( - "repository_service_tuf_api.tasks.repository_metadata", - mocked_repository_metadata, + f"{MOCK_PATH}.repository_metadata", mocked_repository_metadata ) test_response = test_client.get(f"{TASK_URL}?task_id=test_id")