Coverage for src/tests/api/frontend/test_app.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for testing the frontend app.""" 

2 

3import pytest 

4 

5from fastapi import FastAPI, status 

6from fastapi.testclient import TestClient 

7from kwai_api.frontend.app import create_frontend 

8 

9 

10pytestmark = [pytest.mark.api] 

11 

12 

13@pytest.fixture(scope="module") 

14def frontend_client() -> TestClient: 

15 """Get an HTTP client.""" 

16 app = FastAPI(title="kwai Frontend -- TEST") 

17 app.mount("", create_frontend()) 

18 return TestClient(app) 

19 

20 

21def test_it_can_serve_a_static_file(frontend_client) -> None: 

22 """Test if it can serve a static file.""" 

23 response = frontend_client.get("/static/favicon.ico") 

24 assert response.status_code == status.HTTP_200_OK 

25 

26 

27def test_it_handles_non_existing_file(frontend_client) -> None: 

28 """Test if it can handle a file that does not exist.""" 

29 response = frontend_client.get("/static/test.ico") 

30 assert response.status_code == status.HTTP_404_NOT_FOUND 

31 

32 

33def test_it_does_not_serve_relative_paths(frontend_client) -> None: 

34 """Test that the app doesn't try to serve files from a relative path.""" 

35 response = frontend_client.get("/static/../__init__.py") 

36 assert response.status_code == status.HTTP_404_NOT_FOUND 

37 

38 

39def test_it_can_serve_favicon(frontend_client) -> None: 

40 """Test if it can serve the favicon.""" 

41 response = frontend_client.get("/favicon.ico") 

42 assert response.status_code == status.HTTP_200_OK