Skip to content

Commit

Permalink
fix: status code from OK to NOT_FOUND (#651)
Browse files Browse the repository at this point in the history
This commit fixes the status code from OK (200) to NOT_FOUND (404) when
the resource is not available or not in expected state when requested.

Fixes: #467

Signed-off-by: Kairo Araujo <[email protected]>
  • Loading branch information
kairoaraujo authored Jul 23, 2024
1 parent 6eee4eb commit 3d8f8a4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions repository_service_tuf_api/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def post(payload: AddPayload) -> ResponsePostAdd:
bs_state = bootstrap_state()
if bs_state.bootstrap is False:
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": "Task not accepted.",
"error": (
Expand Down Expand Up @@ -217,7 +217,7 @@ def delete(payload: DeletePayload) -> ResponsePostDelete:
bs_state = bootstrap_state()
if bs_state.bootstrap is False:
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": "Task not accepted.",
"error": (
Expand Down
2 changes: 1 addition & 1 deletion repository_service_tuf_api/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def post_bootstrap(payload: BootstrapPayload) -> BootstrapPostResponse:
# or is in the process of DAS signing ("signing") we consider it as locked.
if bs_state.bootstrap is True or bs_state.state in ["pre", "signing"]:
raise HTTPException(
status_code=status.HTTP_200_OK,
status_code=status.HTTP_404_NOT_FOUND,
detail=BaseErrorResponse(
error=(
"System already has a Metadata. "
Expand Down
16 changes: 8 additions & 8 deletions repository_service_tuf_api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def post_metadata(payload: MetadataPostPayload) -> MetadataPostResponse:
bs_state = bootstrap_state()
if bs_state.bootstrap is False:
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": "Task not accepted.",
"error": (
Expand Down Expand Up @@ -119,7 +119,7 @@ def post_metadata_online(
bs_state = bootstrap_state()
if bs_state.bootstrap is False:
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": "Task not accepted.",
"error": (
Expand All @@ -134,7 +134,7 @@ def post_metadata_online(
targets_online = settings_repository.get_fresh("TARGETS_ONLINE_KEY", True)
if targets_in and not targets_online:
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": "Task not accepted.",
"error": (
Expand All @@ -152,7 +152,7 @@ def post_metadata_online(
# This indicates succinct hash bins are used
if len(roles) > 0 and any(not Roles.is_role(role) for role in roles):
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": "Task not accepted.",
"error": (
Expand All @@ -164,7 +164,7 @@ def post_metadata_online(
else:
if Roles.BINS.value in roles:
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": "Task not accepted.",
"error": (
Expand Down Expand Up @@ -227,7 +227,7 @@ def get_metadata_sign() -> MetadataSignGetResponse:
# Adds support only when bootstrap is signing state
if bs_state.bootstrap is False and bs_state.state != "signing":
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": "No metadata pending signing available",
"error": (
Expand Down Expand Up @@ -319,7 +319,7 @@ def post_metadata_sign(
bs_state = bootstrap_state()
if bs_state.bootstrap is False and bs_state.state != "signing":
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": "No signing pending.",
"error": (
Expand Down Expand Up @@ -377,7 +377,7 @@ def delete_metadata_sign(payload: MetadataSignDeletePayload):
signing_status = settings_repository.get_fresh(f"{role.upper()}_SIGNING")
if signing_status is None:
raise HTTPException(
status.HTTP_200_OK,
status.HTTP_404_NOT_FOUND,
detail={
"message": f"No signing process for {role}.",
"error": f"The {role} role is not in a signing process.",
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/api/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def test_post_bootstrap_already_bootstrap(self, test_client, monkeypatch):
payload = json.loads(f_data)
response = test_client.post(BOOTSTRAP_URL, json=payload)

assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.url == f"{test_client.base_url}{BOOTSTRAP_URL}"
assert response.json() == {
"detail": {
Expand All @@ -399,7 +399,7 @@ def test_post_bootstrap_already_bootstrap_in_pre(
payload = json.loads(f_data)
response = test_client.post(BOOTSTRAP_URL, json=payload)

assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.url == f"{test_client.base_url}{BOOTSTRAP_URL}"
assert response.json() == {
"detail": {"error": "System already has a Metadata. State: pre"}
Expand All @@ -423,7 +423,7 @@ def test_post_bootstrap_already_bootstrap_in_signing(
payload = json.loads(f_data)
response = test_client.post(BOOTSTRAP_URL, json=payload)

assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.url == f"{test_client.base_url}{BOOTSTRAP_URL}"
assert response.json() == {
"detail": {
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/api/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_post_metadata_without_bootstrap(self, test_client, monkeypatch):
payload = json.loads(f_data)
response = test_client.post(METADATA_URL, json=payload)

assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
assert response.url == f"{test_client.base_url}{METADATA_URL}"
assert response.json() == {
"detail": {
Expand All @@ -96,7 +96,7 @@ def test_post_metadata_bootstrap_intermediate_state(
payload = json.loads(f_data)
response = test_client.post(METADATA_URL, json=payload)

assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.url == f"{test_client.base_url}{METADATA_URL}"
assert response.json() == {
"detail": {
Expand Down Expand Up @@ -317,7 +317,7 @@ def test_post_metadata_online_bootstrap_not_ready(
)

response = test_client.post(METADATA_ONLINE_URL, json=payload)
assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
assert response.json() == {
"detail": {
"message": "Task not accepted.",
Expand Down Expand Up @@ -345,7 +345,7 @@ def test_post_metadata_online_targets_offline_role_cannot_update(
payload = {"roles": ["snapshot", "targets"]}

response = test_client.post(METADATA_ONLINE_URL, json=payload)
assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
err_msg = "Targets is an offline role - use other endpoint to update"
assert response.json() == {
"detail": {
Expand Down Expand Up @@ -386,7 +386,7 @@ def fake_get_fresh(attr: str) -> bool:
payload = {"roles": ["snapshot", "targets", "abcsdaw"]}

response = test_client.post(METADATA_ONLINE_URL, json=payload)
assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
roles = common_models.Roles.all_str()
err_msg = (
f"Hash bin delegation is used and only {roles} roles can be bumped"
Expand Down Expand Up @@ -433,7 +433,7 @@ def fake_get_fresh(attr: str) -> bool:
payload = {"roles": ["snapshot", "targets", "bins"]}

response = test_client.post(METADATA_ONLINE_URL, json=payload)
assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
err_msg = "Custom target delegation used and bins cannot be bumped"
assert response.json() == {
"detail": {
Expand Down Expand Up @@ -591,7 +591,7 @@ def test_get_metadata_sign_no_bootstrap(self, test_client, monkeypatch):
)
response = test_client.get(SIGN_URL)

assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
assert response.json() == {
"detail": {
"message": "No metadata pending signing available",
Expand All @@ -609,7 +609,7 @@ def test_get_metadata_sign_bootstrap_pre(self, test_client, monkeypatch):
)
response = test_client.get(SIGN_URL)

assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
assert response.json() == {
"detail": {
"message": "No metadata pending signing available",
Expand Down Expand Up @@ -673,7 +673,7 @@ def test_post_metadata_no_bootstrap(self, test_client, monkeypatch):
payload = {"role": "root", "signature": {"keyid": "k1", "sig": "s1"}}

response = test_client.post(SIGN_URL, json=payload)
assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
assert response.json() == {
"detail": {
"message": "No signing pending.",
Expand All @@ -692,7 +692,7 @@ def test_post_metadata_bootstrap_finished(self, test_client, monkeypatch):
payload = {"role": "root", "signature": {"keyid": "k1", "sig": "s1"}}

response = test_client.post(SIGN_URL, json=payload)
assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
assert response.json() == {
"detail": {
"message": "No signing pending.",
Expand Down Expand Up @@ -766,7 +766,7 @@ def test_metadata_sign_delete_role_not_in_signing_status(
payload = {"role": "root"}

response = test_client.post(DELETE_SIGN_URL, json=payload)
assert response.status_code == status.HTTP_200_OK, response.text
assert response.status_code == status.HTTP_404_NOT_FOUND, response.text
assert response.json() == {
"detail": {
"message": "No signing process for root.",
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/api/test_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_post_without_bootstrap(self, monkeypatch, test_client):
payload = json.loads(f_data)

response = test_client.post(ARTIFACTS_URL, json=payload)
assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json() == {
"detail": {
"message": "Task not accepted.",
Expand All @@ -222,7 +222,7 @@ def test_post_with_bootstrap_intermediate_state(
payload = json.loads(f_data)

response = test_client.post(ARTIFACTS_URL, json=payload)
assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json() == {
"detail": {
"message": "Task not accepted.",
Expand Down Expand Up @@ -358,7 +358,7 @@ def test_post_without_bootstrap_delete(self, monkeypatch, test_client):
)
response = test_client.post(ARTIFACTS_DELETE_URL, json=payload)

assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json() == {
"detail": {
"message": "Task not accepted.",
Expand All @@ -381,7 +381,7 @@ def test_post_with_bootstrap_intermediate_state_delete(
)
response = test_client.post(ARTIFACTS_DELETE_URL, json=payload)

assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json() == {
"detail": {
"message": "Task not accepted.",
Expand Down

0 comments on commit 3d8f8a4

Please sign in to comment.