Coverage for tests/services/test_auth_service.py: 100%

59 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-02 02:49 +0000

1import pytest 

2from unittest.mock import MagicMock, patch 

3from flask import session 

4 

5import app.services.auth_service as auth_service 

6 

7 

8@patch("app.services.auth_service.client", new_callable=MagicMock) 

9def test_login_with_provider(mock_client, app): 

10 mock_client.auth.sign_in_with_oauth.return_value = {"url": "mock_url"} 

11 provider = "github" 

12 

13 with app.test_request_context(): 

14 with patch.object(session, "clear") as mock_clear: 

15 result = auth_service.login_with_provider(provider) 

16 

17 mock_clear.assert_called_once() 

18 mock_client.auth.sign_in_with_oauth.assert_called_once_with( 

19 { 

20 "provider": provider, 

21 "options": {"redirect_to": auth_service.AUTH_URL}, 

22 } 

23 ) 

24 assert result["url"] == "mock_url" 

25 

26 

27@patch("app.services.auth_service.client", new_callable=MagicMock) 

28def test_login_with_email(mock_client, app): 

29 mock_client.auth.sign_in_with_password.return_value = {"session": "mock_session"} 

30 

31 with app.test_request_context(): 

32 with patch.object(session, "clear") as mock_clear: 

33 result = auth_service.login_with_email("test@example.com", "password123") 

34 

35 mock_clear.assert_called_once() 

36 mock_client.auth.sign_in_with_password.assert_called_once_with( 

37 { 

38 "email": "test@example.com", 

39 "password": "password123", 

40 } 

41 ) 

42 assert result["session"] == "mock_session" 

43 

44 

45@patch("app.services.auth_service.client", new_callable=MagicMock) 

46def test_callback_success(mock_client, app): 

47 mock_client.auth.exchange_code_for_session.return_value = { 

48 "session": "mock_session" 

49 } 

50 result = auth_service.callback("auth_code_123") 

51 

52 mock_client.auth.exchange_code_for_session.assert_called_once_with( 

53 {"auth_code": "auth_code_123"} 

54 ) 

55 assert result["session"] == "mock_session" 

56 

57 

58@patch("app.services.auth_service.client", new_callable=MagicMock) 

59def test_callback_failure(mock_client, app): 

60 mock_client.auth.exchange_code_for_session.side_effect = Exception("bad code") 

61 

62 response, status_code = auth_service.callback("bad_code") 

63 assert status_code == 401 

64 assert response.get_json()["message"].startswith("Authentication failed") 

65 

66 

67@patch("app.services.auth_service.client", new_callable=MagicMock) 

68def test_signout_success(mock_client, app): 

69 result, status_code = auth_service.signout("user123") 

70 

71 mock_client.auth.sign_out.assert_called_once() 

72 assert status_code == 200 

73 assert result.get_json()["message"] == "Signout successful" 

74 

75 

76@patch("app.services.auth_service.client", new_callable=MagicMock) 

77def test_signout_failure(mock_client, app): 

78 mock_client.auth.sign_out.side_effect = Exception("logout failed") 

79 result, status_code = auth_service.signout("user123") 

80 

81 assert status_code == 401 

82 assert result.get_json()["message"].startswith("Signout failed") 

83 

84 

85@patch("app.services.auth_service.client", new_callable=MagicMock) 

86def test_signup_with_email(mock_client, app): 

87 mock_user = MagicMock() 

88 mock_user.id = "new-user-id" 

89 mock_client.auth.sign_up.return_value.user = mock_user 

90 

91 table_mock = MagicMock() 

92 mock_client.table.return_value = table_mock 

93 table_mock.insert.return_value.execute.return_value = None 

94 

95 result = auth_service.signup_with_email( 

96 email="new@example.com", 

97 password="password123", 

98 first_name="New", 

99 last_name="User", 

100 ) 

101 

102 mock_client.auth.sign_up.assert_called_once_with( 

103 { 

104 "email": "new@example.com", 

105 "password": "password123", 

106 } 

107 ) 

108 table_mock.insert.assert_called_once_with( 

109 { 

110 "id": "new-user-id", 

111 "first_name": "New", 

112 "last_name": "User", 

113 "email": "new@example.com", 

114 "status": "ACTIVE", 

115 } 

116 ) 

117 assert result.user.id == "new-user-id"