Coverage for src/tests/api/v1/auth/endpoints/test_login.py: 100%

41 statements  

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

1"""Module for testing the auth endpoints login.""" 

2 

3import pytest 

4 

5from fastapi import status 

6from fastapi.testclient import TestClient 

7from kwai_bc_identity.user_recoveries.user_recovery import UserRecoveryEntity 

8from kwai_bc_identity.user_recoveries.user_recovery_db_repository import ( 

9 UserRecoveryDbRepository, 

10) 

11from kwai_bc_identity.users.user_account import UserAccountEntity 

12from kwai_core.db.database import Database 

13from kwai_core.db.uow import UnitOfWork 

14from kwai_core.domain.value_objects.timestamp import Timestamp 

15 

16 

17pytestmark = [pytest.mark.api, pytest.mark.db] 

18 

19 

20def test_login(client: TestClient, user_account: UserAccountEntity): 

21 """Test the login api.""" 

22 response = client.post( 

23 "/api/v1/auth/login", 

24 data={"username": str(user_account.user.email), "password": "Nage-waza/1882"}, 

25 ) 

26 assert response.status_code == status.HTTP_200_OK 

27 

28 

29def test_login_with_unknown_user(client: TestClient): 

30 """Test the login api with an unknown email address.""" 

31 response = client.post( 

32 "/api/v1/auth/login", 

33 data={"username": "unknown@kwai.com", "password": "Nage-waza/1882"}, 

34 ) 

35 assert response.status_code == status.HTTP_401_UNAUTHORIZED 

36 

37 

38def test_login_with_wrong_password(client: TestClient, user_account: UserAccountEntity): 

39 """Test the login api with a wrong password.""" 

40 response = client.post( 

41 "/api/v1/auth/login", 

42 data={"username": str(user_account.user.email), "password": "Test/1234"}, 

43 ) 

44 assert response.status_code == status.HTTP_401_UNAUTHORIZED 

45 

46 

47def test_renew_access_token(secure_client: TestClient, user_account: UserAccountEntity): 

48 """Test the renewal of an access token.""" 

49 response = secure_client.post("/api/v1/auth/access_token") 

50 secure_client.cookies.set("access_token", response.cookies["access_token"]) 

51 secure_client.cookies.set("refresh_token", response.cookies["refresh_token"]) 

52 

53 assert response.status_code == status.HTTP_200_OK 

54 

55 

56@pytest.mark.mail 

57def test_recover_user(client: TestClient, user_account: UserAccountEntity): 

58 """Test the recover user api.""" 

59 response = client.post( 

60 "/api/v1/auth/recover", data={"email": str(user_account.user.email)} 

61 ) 

62 assert response.status_code == status.HTTP_200_OK 

63 

64 

65def test_recover_unknown_user(client: TestClient): 

66 """Test the recover user api with an unknown user.""" 

67 response = client.post("/api/v1/auth/recover", data={"email": "unknown@kwai.com"}) 

68 # A wrong user also results in http status code 200. 

69 assert response.status_code == status.HTTP_200_OK 

70 

71 

72@pytest.mark.mail 

73async def test_reset_password( 

74 client: TestClient, make_user_account_in_db, database: Database 

75): 

76 """Test the reset password api.""" 

77 user_recovery = UserRecoveryEntity( 

78 expiration=Timestamp.create_with_delta(hours=2), 

79 user=await make_user_account_in_db(), 

80 ) 

81 async with UnitOfWork(database): 

82 user_recovery = await UserRecoveryDbRepository(database).create(user_recovery) 

83 

84 response = client.post( 

85 "/api/v1/auth/reset", 

86 data={"uuid": str(user_recovery.uuid), "password": "Nage-waza/1882"}, 

87 ) 

88 assert response.status_code == status.HTTP_200_OK 

89 

90 

91def test_logout(secure_client: TestClient, user_account: UserAccountEntity): 

92 """Test the logout api.""" 

93 response = secure_client.post("/api/v1/auth/logout") 

94 assert response.status_code == status.HTTP_200_OK, response.text