1919
2020@pytest .fixture
2121def feishu_client ():
22- """Create FeiShu OAuth2 client instance for testing."""
2322 return FeiShuOAuth20 (client_id = TEST_CLIENT_ID , client_secret = TEST_CLIENT_SECRET )
2423
2524
2625class TestFeiShuOAuth20 :
27- """Test FeiShu OAuth2 client functionality."""
28-
29- def test_feishu_client_initialization (self , feishu_client ):
30- """Test FeiShu client initialization with correct parameters."""
26+ def test_client_initialization (self , feishu_client ):
3127 assert feishu_client .client_id == TEST_CLIENT_ID
3228 assert feishu_client .client_secret == TEST_CLIENT_SECRET
3329 assert feishu_client .authorize_endpoint == 'https://passport.feishu.cn/suite/passport/oauth/authorize'
@@ -39,130 +35,97 @@ def test_feishu_client_initialization(self, feishu_client):
3935 'contact:user.email:readonly' ,
4036 ]
4137
42- def test_feishu_client_initialization_with_custom_credentials (self ):
43- """Test FeiShu client initialization with custom credentials."""
44- client = FeiShuOAuth20 (client_id = TEST_CLIENT_ID , client_secret = TEST_CLIENT_SECRET )
45- assert client .client_id == TEST_CLIENT_ID
46- assert client .client_secret == TEST_CLIENT_SECRET
47-
48- def test_feishu_client_inheritance (self , feishu_client ):
49- """Test that FeiShu client properly inherits from OAuth20Base."""
38+ def test_client_inheritance (self , feishu_client ):
5039 assert isinstance (feishu_client , OAuth20Base )
5140
52- def test_feishu_client_scopes_are_lists (self , feishu_client ):
53- """Test that default scopes are properly configured as lists."""
41+ def test_client_scopes_are_lists (self , feishu_client ):
5442 assert isinstance (feishu_client .default_scopes , list )
5543 assert len (feishu_client .default_scopes ) == 3
5644 assert all (isinstance (scope , str ) for scope in feishu_client .default_scopes )
5745
58- def test_feishu_client_endpoint_urls (self ):
59- """Test that FeiShu client uses correct endpoint URLs."""
60- client = FeiShuOAuth20 (TEST_CLIENT_ID , TEST_CLIENT_SECRET )
61-
62- # Test that endpoints are correctly set without hardcoding them in tests
63- assert client .authorize_endpoint .endswith ('/suite/passport/oauth/authorize' )
64- assert client .access_token_endpoint .endswith ('/suite/passport/oauth/token' )
65- assert client .refresh_token_endpoint .endswith ('/suite/passport/oauth/authorize' )
66-
67- # Test that all endpoints use the correct domain
68- for endpoint in [client .authorize_endpoint , client .access_token_endpoint , client .refresh_token_endpoint ]:
46+ def test_client_endpoint_urls (self , feishu_client ):
47+ assert feishu_client .authorize_endpoint .endswith ('/suite/passport/oauth/authorize' )
48+ assert feishu_client .access_token_endpoint .endswith ('/suite/passport/oauth/token' )
49+ assert feishu_client .refresh_token_endpoint .endswith ('/suite/passport/oauth/authorize' )
50+ for endpoint in [
51+ feishu_client .authorize_endpoint ,
52+ feishu_client .access_token_endpoint ,
53+ feishu_client .refresh_token_endpoint ,
54+ ]:
6955 assert 'passport.feishu.cn' in endpoint
7056
71- def test_feishu_client_multiple_instances (self ):
72- """Test that multiple FeiShu client instances work independently."""
57+ def test_client_multiple_instances (self ):
7358 client1 = FeiShuOAuth20 ('client1' , 'secret1' )
7459 client2 = FeiShuOAuth20 ('client2' , 'secret2' )
75-
7660 assert client1 .client_id != client2 .client_id
7761 assert client1 .client_secret != client2 .client_secret
7862 assert client1 .authorize_endpoint == client2 .authorize_endpoint
79- assert client1 .access_token_endpoint == client2 .access_token_endpoint
8063
8164 @pytest .mark .asyncio
8265 @respx .mock
8366 async def test_get_userinfo_success (self , feishu_client ):
84- """Test successful user info retrieval from FeiShu API."""
8567 mock_user_data = create_mock_user_data ('feishu' )
8668 mock_user_info_response (respx , FEISHU_USER_INFO_URL , mock_user_data )
87-
8869 result = await feishu_client .get_userinfo (TEST_ACCESS_TOKEN )
8970 assert result == mock_user_data
9071
9172 @pytest .mark .asyncio
9273 @respx .mock
9374 async def test_get_userinfo_with_different_access_token (self , feishu_client ):
94- """Test user info retrieval with different access tokens."""
9575 mock_user_data = create_mock_user_data ('feishu' , user_id = 'user_789' , name = 'Another User' )
9676 mock_user_info_response (respx , FEISHU_USER_INFO_URL , mock_user_data )
97-
9877 result = await feishu_client .get_userinfo ('different_token' )
9978 assert result == mock_user_data
10079
10180 @pytest .mark .asyncio
10281 @respx .mock
10382 async def test_get_userinfo_empty_response (self , feishu_client ):
104- """Test handling of empty user info response."""
10583 mock_user_info_response (respx , FEISHU_USER_INFO_URL , {})
106-
10784 result = await feishu_client .get_userinfo (TEST_ACCESS_TOKEN )
10885 assert result == {}
10986
11087 @pytest .mark .asyncio
11188 @respx .mock
11289 async def test_get_userinfo_partial_data (self , feishu_client ):
113- """Test handling of partial user info response."""
11490 partial_data = {'user_id' : 'test_user' , 'name' : 'Test User' }
11591 mock_user_info_response (respx , FEISHU_USER_INFO_URL , partial_data )
116-
11792 result = await feishu_client .get_userinfo (TEST_ACCESS_TOKEN )
11893 assert result == partial_data
11994
12095 @pytest .mark .asyncio
12196 @respx .mock
12297 async def test_get_userinfo_authorization_header (self , feishu_client ):
123- """Test that authorization header is correctly formatted."""
12498 mock_user_data = {'user_id' : 'test_user' }
12599 route = mock_user_info_response (respx , FEISHU_USER_INFO_URL , mock_user_data )
126-
127100 await feishu_client .get_userinfo (TEST_ACCESS_TOKEN )
128-
129- # Verify the request was made with correct authorization header
130101 assert route .called
131102 request = route .calls [0 ].request
132103 assert request .headers ['authorization' ] == f'Bearer { TEST_ACCESS_TOKEN } '
133104
134105 @pytest .mark .asyncio
135106 @respx .mock
136107 async def test_get_userinfo_http_error_401 (self , feishu_client ):
137- """Test handling of 401 HTTP error when getting user info."""
138108 respx .get (FEISHU_USER_INFO_URL ).mock (return_value = httpx .Response (401 , text = 'Unauthorized' ))
139-
140109 with pytest .raises (HTTPXOAuth20Error ):
141110 await feishu_client .get_userinfo (INVALID_TOKEN )
142111
143112 @pytest .mark .asyncio
144113 @respx .mock
145114 async def test_get_userinfo_http_error_403 (self , feishu_client ):
146- """Test handling of 403 HTTP error when getting user info."""
147115 respx .get (FEISHU_USER_INFO_URL ).mock (return_value = httpx .Response (403 , text = 'Forbidden' ))
148-
149116 with pytest .raises (HTTPXOAuth20Error ):
150117 await feishu_client .get_userinfo (TEST_ACCESS_TOKEN )
151118
152119 @pytest .mark .asyncio
153120 @respx .mock
154121 async def test_get_userinfo_http_error_500 (self , feishu_client ):
155- """Test handling of 500 HTTP error when getting user info."""
156122 respx .get (FEISHU_USER_INFO_URL ).mock (return_value = httpx .Response (500 , text = 'Internal Server Error' ))
157-
158123 with pytest .raises (HTTPXOAuth20Error ):
159124 await feishu_client .get_userinfo (TEST_ACCESS_TOKEN )
160125
161126 @pytest .mark .asyncio
162127 @respx .mock
163128 async def test_get_userinfo_invalid_json (self , feishu_client ):
164- """Test handling of invalid JSON response."""
165129 respx .get (FEISHU_USER_INFO_URL ).mock (return_value = httpx .Response (200 , text = 'invalid json' ))
166-
167130 with pytest .raises (GetUserInfoError ):
168131 await feishu_client .get_userinfo (TEST_ACCESS_TOKEN )
0 commit comments