Coverage for tests/test_microservice_websocket/test_routes/test_route_jwt.py: 100%

23 statements  

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

1from fastapi.testclient import TestClient 

2 

3 

4class TestJwtAuth: 

5 endpoint = "/test/jwt-test" 

6 # Test decorator 

7 

8 def test_jwt_no_header(self, app_client: TestClient): 

9 response = app_client.get(self.endpoint) 

10 

11 assert ( 

12 response.status_code == 401 

13 ), "Invalid response code when trying to access protected route without Auth header" 

14 

15 def test_jwt(self, app_client: TestClient, auth_header): 

16 response = app_client.get(self.endpoint, headers=auth_header) 

17 

18 assert ( 

19 response.status_code == 200 

20 and response.json()["message"] == "Hi foo@bar.com" 

21 ), "Invalid response when trying to access protected route with Auth header" 

22 

23 

24class TestJwtAuthentication: 

25 endpoint = "/api/jwt/" 

26 

27 # Trying to authenticate with invalid payload 

28 def test_auth_invalid_payload(self, app_client: TestClient): 

29 response = app_client.post(self.endpoint, json={}) 

30 

31 assert ( 

32 response.status_code == 422 

33 ), "Invalid response code when trying to authenticate with invalid payload" 

34 

35 # Trying to authenticate as a non-registered user 

36 def test_auth_non_registered(self, app_client: TestClient): 

37 response = app_client.post( 

38 self.endpoint, json={"email": "foo", "password": "bar"} 

39 ) 

40 

41 assert ( 

42 response.status_code == 401 

43 ), "Invalid response code when trying to authenticate as a non-registered user" 

44 

45 # Trying to authenticate with wrong password 

46 def test_auth_wrong_password(self, app_client: TestClient): 

47 response = app_client.post( 

48 self.endpoint, json={"email": "bettarini@monema.it", "password": "foo"} 

49 ) 

50 

51 assert ( 

52 response.status_code == 401 

53 ), "Invalid response code when trying to authenticate with wrong password" 

54 

55 # Normal authentication 

56 def test_auth(self, app_client: TestClient): 

57 response = app_client.post( 

58 self.endpoint, 

59 json={"email": "foo@bar.com", "password": "baz"}, 

60 ) 

61 

62 assert ( 

63 response.status_code == 200 and "access_token" in response.json() 

64 ), "Invalid response when posting valid data" 

65 

66 

67# class TestJwtRefresh: 

68# endpoint = "/api/jwt/refresh" 

69# 

70# def test_refresh(self, app_client: TestClient, refresh_header): 

71# response = app_client.post(self.endpoint, headers=refresh_header) 

72# 

73# assert ( 

74# response.status_code == 200 

75# ), "Invalid response code when trying to renew jwt token" 

76# 

77# assert ( 

78# "access_token" in response.json() 

79# ), "Invalid response when trying to renew jwt token"