|
| 1 | +"""Test the custom-translations API.""" |
| 2 | + |
| 3 | +import responses |
| 4 | +from rest_framework.test import APIClient |
| 5 | + |
| 6 | + |
| 7 | +def test_api_custom_translations_without_settings_configured(settings): |
| 8 | + """Test the custom-translations API without settings configured.""" |
| 9 | + settings.FRONTEND_URL_JSON_CUSTOM_TRANSLATIONS = None |
| 10 | + client = APIClient() |
| 11 | + response = client.get("/api/v1.0/custom-translations/") |
| 12 | + assert response.status_code == 200 |
| 13 | + assert response.json() == {} |
| 14 | + |
| 15 | + |
| 16 | +@responses.activate |
| 17 | +def test_api_custom_translations_with_invalid_request(settings): |
| 18 | + """Test the custom-translations API with an invalid request.""" |
| 19 | + settings.FRONTEND_URL_JSON_CUSTOM_TRANSLATIONS = "https://invalid-request.com" |
| 20 | + |
| 21 | + custom_translations_response = responses.get( |
| 22 | + settings.FRONTEND_URL_JSON_CUSTOM_TRANSLATIONS, status=404 |
| 23 | + ) |
| 24 | + |
| 25 | + client = APIClient() |
| 26 | + response = client.get("/api/v1.0/custom-translations/") |
| 27 | + assert response.status_code == 200 |
| 28 | + assert response.json() == {} |
| 29 | + assert custom_translations_response.call_count == 1 |
| 30 | + |
| 31 | + |
| 32 | +@responses.activate |
| 33 | +def test_api_custom_translations_with_invalid_json(settings): |
| 34 | + """Test the custom-translations API with an invalid JSON response.""" |
| 35 | + settings.FRONTEND_URL_JSON_CUSTOM_TRANSLATIONS = "https://valid-request.com" |
| 36 | + |
| 37 | + custom_translations_response = responses.get( |
| 38 | + settings.FRONTEND_URL_JSON_CUSTOM_TRANSLATIONS, status=200, body="invalid json" |
| 39 | + ) |
| 40 | + |
| 41 | + client = APIClient() |
| 42 | + response = client.get("/api/v1.0/custom-translations/") |
| 43 | + assert response.status_code == 200 |
| 44 | + assert response.json() == {} |
| 45 | + assert custom_translations_response.call_count == 1 |
| 46 | + |
| 47 | + |
| 48 | +@responses.activate |
| 49 | +def test_api_custom_translations_with_valid_json(settings): |
| 50 | + """Test the custom-translations API with an invalid JSON response.""" |
| 51 | + settings.FRONTEND_URL_JSON_CUSTOM_TRANSLATIONS = "https://valid-request.com" |
| 52 | + |
| 53 | + custom_translations_response = responses.get( |
| 54 | + settings.FRONTEND_URL_JSON_CUSTOM_TRANSLATIONS, status=200, json={"foo": "bar"} |
| 55 | + ) |
| 56 | + |
| 57 | + client = APIClient() |
| 58 | + response = client.get("/api/v1.0/custom-translations/") |
| 59 | + assert response.status_code == 200 |
| 60 | + assert response.json() == {"foo": "bar"} |
| 61 | + assert custom_translations_response.call_count == 1 |
| 62 | + |
| 63 | + |
| 64 | +@responses.activate |
| 65 | +def test_api_custom_translations_with_valid_json_and_cache(settings): |
| 66 | + """Test the custom-translations API with an invalid JSON response.""" |
| 67 | + settings.FRONTEND_URL_JSON_CUSTOM_TRANSLATIONS = "https://valid-request.com" |
| 68 | + |
| 69 | + custom_translations_response = responses.get( |
| 70 | + settings.FRONTEND_URL_JSON_CUSTOM_TRANSLATIONS, status=200, json={"foo": "bar"} |
| 71 | + ) |
| 72 | + |
| 73 | + client = APIClient() |
| 74 | + response = client.get("/api/v1.0/custom-translations/") |
| 75 | + assert response.status_code == 200 |
| 76 | + assert response.json() == {"foo": "bar"} |
| 77 | + assert custom_translations_response.call_count == 1 |
| 78 | + |
| 79 | + response = client.get("/api/v1.0/custom-translations/") |
| 80 | + assert response.status_code == 200 |
| 81 | + assert response.json() == {"foo": "bar"} |
| 82 | + # The cache should have been used |
| 83 | + assert custom_translations_response.call_count == 1 |
0 commit comments