|
| 1 | +import unittest |
| 2 | +import pytest |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | +from urllib.parse import urlparse, parse_qs |
| 5 | +import asyncio |
| 6 | + |
| 7 | +from kinde_sdk.auth.oauth import OAuth, IssuerRouteTypes, LoginOptions |
| 8 | + |
| 9 | + |
| 10 | +class TestExpectedLogin(unittest.TestCase): |
| 11 | + def setUp(self): |
| 12 | + # Set up OAuth with minimal required parameters |
| 13 | + self.oauth = OAuth( |
| 14 | + client_id="test_client_id", |
| 15 | + client_secret="test_client_secret", |
| 16 | + redirect_uri="http://localhost/callback", |
| 17 | + framework="None", |
| 18 | + host="https://example.com" # Use host parameter instead of individual URLs |
| 19 | + ) |
| 20 | + |
| 21 | + # Override URLs for testing |
| 22 | + self.oauth.auth_url = "https://example.com/oauth2/auth" |
| 23 | + self.oauth.token_url = "https://example.com/oauth2/token" |
| 24 | + self.oauth.logout_url = "https://example.com/logout" |
| 25 | + self.oauth.userinfo_url = "https://example.com/oauth2/userinfo" |
| 26 | + |
| 27 | + @pytest.mark.asyncio |
| 28 | + async def test_login_flow(self): |
| 29 | + # Initial login |
| 30 | + login_url = await self.oauth.login() |
| 31 | + self.assertEqual(login_url, "https://example.com/oauth2/auth?client_id=test_client_id&redirect_uri=http%3A%2F%2Flocalhost%2Fcallback&response_type=code&state=1234567890") |
| 32 | + |
| 33 | + # Get initial tokens |
| 34 | + tokens = await self.oauth.get_tokens(user_id="test_code") |
| 35 | + self.assertEqual(tokens["access_token"], "test_access_token") |
| 36 | + self.assertEqual(tokens["refresh_token"], "test_refresh_token") |
| 37 | + |
| 38 | + # Logout |
| 39 | + self.oauth.logout(user_id="test_code") |
| 40 | + |
| 41 | + # Try to get tokens after logout - should return None |
| 42 | + tokens = await self.oauth.get_tokens(user_id="test_code") |
| 43 | + self.assertIsNone(tokens, "Tokens should be None after logout") |
| 44 | + |
| 45 | + @pytest.mark.asyncio |
| 46 | + async def test_register_flow(self): |
| 47 | + # Initial register |
| 48 | + register_url = await self.oauth.register() |
| 49 | + self.assertEqual(register_url, "https://example.com/oauth2/auth?client_id=test_client_id&redirect_uri=http%3A%2F%2Flocalhost%2Fcallback&response_type=code&state=1234567890") |
| 50 | + |
| 51 | + # Get initial tokens |
| 52 | + tokens = await self.oauth.get_tokens(user_id="test_code") |
| 53 | + self.assertEqual(tokens["access_token"], "test_access_token") |
| 54 | + self.assertEqual(tokens["refresh_token"], "test_refresh_token") |
| 55 | + |
| 56 | + # Logout |
| 57 | + self.oauth.logout(user_id="test_code") |
| 58 | + |
| 59 | + # Try to get tokens after logout - should return None |
| 60 | + tokens = await self.oauth.get_tokens(user_id="test_code") |
| 61 | + self.assertIsNone(tokens, "Tokens should be None after logout") |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + unittest.main() |
0 commit comments