|
1 | 1 | # Standard Library
|
2 | 2 | import unittest
|
| 3 | +from typing import Generator |
| 4 | +from unittest.mock import ANY |
3 | 5 |
|
4 | 6 | # Third Party
|
| 7 | +import pytest |
5 | 8 | from fastapi.testclient import TestClient
|
| 9 | +from fastapi_cache import FastAPICache |
| 10 | +from fastapi_cache.backends.inmemory import InMemoryBackend |
6 | 11 |
|
7 | 12 | # First Party
|
8 | 13 | from resc_backend.constants import (
|
9 | 14 | AZURE_DEVOPS,
|
10 | 15 | BITBUCKET,
|
| 16 | + CACHE_PREFIX, |
11 | 17 | GITHUB_PUBLIC,
|
| 18 | + REDIS_CACHE_EXPIRE, |
12 | 19 | RWS_ROUTE_AUTH_CHECK,
|
13 | 20 | RWS_ROUTE_SUPPORTED_VCS_PROVIDERS,
|
14 | 21 | RWS_VERSION_PREFIX
|
15 | 22 | )
|
16 | 23 | from resc_backend.resc_web_service.api import app
|
| 24 | +from resc_backend.resc_web_service.cache_manager import CacheManager |
17 | 25 | from resc_backend.resc_web_service.dependencies import requires_auth, requires_no_auth
|
18 | 26 |
|
19 | 27 |
|
| 28 | +@pytest.fixture(autouse=True) |
| 29 | +def _init_cache() -> Generator[ANY, ANY, None]: |
| 30 | + FastAPICache.init(InMemoryBackend(), |
| 31 | + prefix=CACHE_PREFIX, |
| 32 | + expire=REDIS_CACHE_EXPIRE, |
| 33 | + key_builder=CacheManager.request_key_builder, |
| 34 | + enable=True) |
| 35 | + yield |
| 36 | + FastAPICache.reset() |
| 37 | + |
| 38 | + |
20 | 39 | class TestFindings(unittest.TestCase):
|
21 | 40 | def setUp(self):
|
22 | 41 | self.client = TestClient(app)
|
23 | 42 | app.dependency_overrides[requires_auth] = requires_no_auth
|
24 | 43 |
|
| 44 | + @staticmethod |
| 45 | + def assert_cache(cached_response): |
| 46 | + assert FastAPICache.get_enable() is True |
| 47 | + assert FastAPICache.get_prefix() == CACHE_PREFIX |
| 48 | + assert FastAPICache.get_expire() == REDIS_CACHE_EXPIRE |
| 49 | + assert FastAPICache.get_key_builder() is not None |
| 50 | + assert FastAPICache.get_coder() is not None |
| 51 | + assert cached_response.headers.get("cache-control") is not None |
| 52 | + |
25 | 53 | def test_get_supported_vcs_providers(self):
|
26 |
| - response = self.client.get(f"{RWS_VERSION_PREFIX}" |
27 |
| - f"{RWS_ROUTE_SUPPORTED_VCS_PROVIDERS}") |
28 |
| - assert response.status_code == 200, response.text |
29 |
| - data = response.json() |
30 |
| - assert len(data) == 3 |
31 |
| - assert data[0] == AZURE_DEVOPS |
32 |
| - assert data[1] == BITBUCKET |
33 |
| - assert data[2] == GITHUB_PUBLIC |
| 54 | + with self.client as client: |
| 55 | + response = client.get(f"{RWS_VERSION_PREFIX}" |
| 56 | + f"{RWS_ROUTE_SUPPORTED_VCS_PROVIDERS}") |
| 57 | + assert response.status_code == 200, response.text |
| 58 | + data = response.json() |
| 59 | + assert len(data) == 3 |
| 60 | + assert data[0] == AZURE_DEVOPS |
| 61 | + assert data[1] == BITBUCKET |
| 62 | + assert data[2] == GITHUB_PUBLIC |
| 63 | + |
| 64 | + # Make the second request to retrieve response from cache |
| 65 | + cached_response = client.get(f"{RWS_VERSION_PREFIX}" |
| 66 | + f"{RWS_ROUTE_SUPPORTED_VCS_PROVIDERS}") |
| 67 | + self.assert_cache(cached_response) |
| 68 | + assert response.json() == cached_response.json() |
34 | 69 |
|
35 | 70 | def test_auth_check(self):
|
36 | 71 | response = self.client.get(f"{RWS_VERSION_PREFIX}"
|
|
0 commit comments