|
| 1 | +"""Tests for the security protocol enforcement on countries write actions.""" |
| 2 | +import json |
| 3 | +from http import HTTPStatus |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from server.app import create_app |
| 9 | +from server.auth import ROLE_ADMIN, ROLE_USER, create_access_token |
| 10 | + |
| 11 | + |
| 12 | +SAMPLE_COUNTRY = { |
| 13 | + "country_name": "Test Country", |
| 14 | + "country_code": "TC", |
| 15 | + "continent": "North America", |
| 16 | + "capital": "Test Capital", |
| 17 | + "population": 1000000, |
| 18 | + "area_km2": 50000.0, |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def enforced_client(monkeypatch): |
| 24 | + monkeypatch.setenv("SECURITY_ENFORCEMENT", "true") |
| 25 | + monkeypatch.delenv("SECURITY_AUDIT_ONLY", raising=False) |
| 26 | + app = create_app() |
| 27 | + app.config["TESTING"] = True |
| 28 | + with app.test_client() as client: |
| 29 | + yield client |
| 30 | + |
| 31 | + |
| 32 | +def _bearer(role: str, user_id: str = "alice") -> dict: |
| 33 | + token = create_access_token(user_id, role, expires_hours=1) |
| 34 | + return {"Authorization": f"Bearer {token}"} |
| 35 | + |
| 36 | + |
| 37 | +def test_post_country_without_token_is_forbidden(enforced_client): |
| 38 | + response = enforced_client.post( |
| 39 | + "/countries", |
| 40 | + data=json.dumps(SAMPLE_COUNTRY), |
| 41 | + content_type="application/json", |
| 42 | + ) |
| 43 | + assert response.status_code == HTTPStatus.FORBIDDEN |
| 44 | + |
| 45 | + |
| 46 | +def test_post_country_with_user_role_is_forbidden(enforced_client): |
| 47 | + response = enforced_client.post( |
| 48 | + "/countries", |
| 49 | + data=json.dumps(SAMPLE_COUNTRY), |
| 50 | + content_type="application/json", |
| 51 | + headers=_bearer(ROLE_USER), |
| 52 | + ) |
| 53 | + assert response.status_code == HTTPStatus.FORBIDDEN |
| 54 | + |
| 55 | + |
| 56 | +def test_post_country_with_admin_role_is_allowed(enforced_client): |
| 57 | + with patch("data.countries.add_country") as mock_add: |
| 58 | + mock_add.return_value = True |
| 59 | + response = enforced_client.post( |
| 60 | + "/countries", |
| 61 | + data=json.dumps(SAMPLE_COUNTRY), |
| 62 | + content_type="application/json", |
| 63 | + headers=_bearer(ROLE_ADMIN), |
| 64 | + ) |
| 65 | + assert response.status_code == HTTPStatus.CREATED |
| 66 | + |
| 67 | + |
| 68 | +def test_put_country_without_token_is_forbidden(enforced_client): |
| 69 | + response = enforced_client.put( |
| 70 | + "/countries/US", |
| 71 | + data=json.dumps({"population": 350000000}), |
| 72 | + content_type="application/json", |
| 73 | + ) |
| 74 | + assert response.status_code == HTTPStatus.FORBIDDEN |
| 75 | + |
| 76 | + |
| 77 | +def test_put_country_with_admin_role_is_allowed(enforced_client): |
| 78 | + with patch("data.countries.update_country") as mock_update, \ |
| 79 | + patch("data.countries.get_country_by_code") as mock_get: |
| 80 | + mock_update.return_value = True |
| 81 | + mock_get.return_value = SAMPLE_COUNTRY |
| 82 | + response = enforced_client.put( |
| 83 | + "/countries/TC", |
| 84 | + data=json.dumps({"population": 2000000}), |
| 85 | + content_type="application/json", |
| 86 | + headers=_bearer(ROLE_ADMIN), |
| 87 | + ) |
| 88 | + assert response.status_code == HTTPStatus.OK |
| 89 | + |
| 90 | + |
| 91 | +def test_delete_country_without_token_is_forbidden(enforced_client): |
| 92 | + response = enforced_client.delete("/countries/US") |
| 93 | + assert response.status_code == HTTPStatus.FORBIDDEN |
| 94 | + |
| 95 | + |
| 96 | +def test_delete_country_with_admin_role_is_allowed(enforced_client): |
| 97 | + with patch("data.countries.delete_country") as mock_delete: |
| 98 | + mock_delete.return_value = True |
| 99 | + response = enforced_client.delete( |
| 100 | + "/countries/TC", |
| 101 | + headers=_bearer(ROLE_ADMIN), |
| 102 | + ) |
| 103 | + assert response.status_code == HTTPStatus.NO_CONTENT |
| 104 | + |
| 105 | + |
| 106 | +def test_get_countries_remains_open_under_enforcement(enforced_client): |
| 107 | + with patch("data.countries.get_countries_filtered") as mock_get: |
| 108 | + mock_get.return_value = [] |
| 109 | + response = enforced_client.get("/countries") |
| 110 | + assert response.status_code == HTTPStatus.OK |
0 commit comments