|
| 1 | +import pytest |
| 2 | +from common.core.utils import get_file_contents, get_versions_from_manifest |
| 3 | +from pyfakefs.fake_filesystem import FakeFilesystem |
| 4 | +from rest_framework import status |
| 5 | +from rest_framework.test import APIClient |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(autouse=True) |
| 9 | +def clear_lru_caches() -> None: |
| 10 | + get_file_contents.cache_clear() |
| 11 | + get_versions_from_manifest.cache_clear() |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + "url", |
| 16 | + [ |
| 17 | + "/robots.txt", |
| 18 | + "/api/v1/auth/users/me/", |
| 19 | + ], |
| 20 | +) |
| 21 | +@pytest.mark.parametrize( |
| 22 | + "version_file_contents, expected_version", |
| 23 | + [ |
| 24 | + ('{".": "v1.2.3"}', "v1.2.3"), |
| 25 | + ('{"foo": "bar"}', "unknown"), |
| 26 | + ("", "unknown"), |
| 27 | + ], |
| 28 | +) |
| 29 | +def test_api_version_is_added_to_success_response_headers( |
| 30 | + admin_client: APIClient, |
| 31 | + expected_version: str, |
| 32 | + fs: FakeFilesystem, |
| 33 | + url: str, |
| 34 | + version_file_contents: str, |
| 35 | +) -> None: |
| 36 | + fs.create_file(".versions.json", contents=version_file_contents) |
| 37 | + |
| 38 | + response = admin_client.get(url) |
| 39 | + assert response.status_code == status.HTTP_200_OK |
| 40 | + assert response.headers["Flagsmith-Version"] == expected_version |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize( |
| 44 | + "version_file_contents, expected_version", |
| 45 | + [ |
| 46 | + ('{".": "v1.2.3"}', "v1.2.3"), |
| 47 | + ('{"foo": "bar"}', "unknown"), |
| 48 | + ("", "unknown"), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_api_version_is_added_to_error_response_headers( |
| 52 | + client: APIClient, |
| 53 | + expected_version: str, |
| 54 | + fs: FakeFilesystem, |
| 55 | + version_file_contents: str, |
| 56 | +) -> None: |
| 57 | + fs.create_file(".versions.json", contents=version_file_contents) |
| 58 | + |
| 59 | + response = client.get("/wat") |
| 60 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 61 | + assert response.headers["Flagsmith-Version"] == expected_version |
0 commit comments