Coverage for tests/test_microservice_websocket/test_utils/test_util_api_token.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.0.0, created at 2022-12-20 14:31 +0000

1from fastapi.testclient import TestClient 

2from mock import mock_open, patch 

3 

4 

5class TestApiToken: 

6 endpoint = "/test/api-token-test" 

7 

8 def test_decorator_no_auth_header(self, app_client: TestClient): 

9 response = app_client.get(self.endpoint) 

10 

11 assert ( 

12 response.status_code == 401 

13 ), "Invalid response code from route protected with @api_token_required" 

14 

15 def test_decorator_wrong_header(self, app_client: TestClient): 

16 with patch("builtins.open", mock_open(read_data="1234")): 

17 response = app_client.get( 

18 self.endpoint, headers={"Authorization": "Bearer 5678"} 

19 ) 

20 

21 assert ( 

22 response.status_code == 401 

23 ), "Invalid response code from route protected with @api_token_required" 

24 

25 def test_decorator(self, app_client: TestClient): 

26 with patch("builtins.open", mock_open(read_data="1234")): 

27 response = app_client.get( 

28 self.endpoint, headers={"Authorization": "Bearer 1234"} 

29 ) 

30 

31 assert ( 

32 response.status_code == 200 

33 ), "Cannot access route protected with @api_token_required with correct api-token"