55from unittest import mock
66
77from pytest import fixture , mark , raises
8- from tornado .web import HTTPError
98from traitlets .config import Config
109
1110from ..google import GoogleOAuthenticator
1211from .mocks import setup_oauth_mock
1312
1413
15- def user_model (email , username = "user1" ):
14+ def user_model (email , username = "user1" , hd = None ):
1615 """Return a user model"""
17- return {
16+ model = {
1817 'sub' : hashlib .md5 (email .encode ()).hexdigest (),
1918 'email' : email ,
2019 'custom' : username ,
21- 'hd' : email .split ('@' )[1 ],
2220 'verified_email' : True ,
2321 }
22+ if hd :
23+ model ['hd' ] = hd
24+ return model
2425
2526
2627@fixture
@@ -187,37 +188,49 @@ async def test_google(
187188 assert auth_model == None
188189
189190
190- async def test_hosted_domain_single_entry (google_client ):
191+ @mark .parametrize (
192+ "test_variation_id,user_email,user_hd,expect_username,expect_allowed,expect_admin" ,
193+ [
194+ ("01" , "user1@ok-hd.orG" , "ok-hd.org" , "user1" , True , True ),
195+ ("02" , "user2@ok-hd.orG" , "ok-hd.org" , "user2" , True , None ),
196+ ("03" , "blocked@ok-hd.org" , "ok-hd.org" , None , False , None ),
197+ ("04" , "user2@ok-hd.org" , "" , None , False , None ),
198+ ("05" , "user1@not-ok.org" , "" , None , False , None ),
199+ # Test variation 06 below isn't believed to be possible, but since we
200+ # aren't sure this test clarifies what we expect to happen.
201+ ("06" , "user1@other.org" , "ok-hd.org" , "user1@other.org" , True , None ),
202+ ],
203+ )
204+ async def test_hosted_domain_single_entry (
205+ google_client ,
206+ test_variation_id ,
207+ user_email ,
208+ user_hd ,
209+ expect_username ,
210+ expect_allowed ,
211+ expect_admin ,
212+ ):
191213 """
192214 Tests that sign in is restricted to the listed domain and that the username
193215 represents the part before the `@domain.com` as expected when hosted_domain
194216 contains a single entry.
195217 """
196218 c = Config ()
197- c .GoogleOAuthenticator .hosted_domain = ["In-Hosted-Domain.com " ]
219+ c .GoogleOAuthenticator .hosted_domain = ["ok-hd.org " ]
198220 c .GoogleOAuthenticator .admin_users = {"user1" }
199- c .GoogleOAuthenticator .allowed_users = {"user2" }
221+ c .GoogleOAuthenticator .allowed_users = {"user2" , "blocked" , "user1@other.org" }
222+ c .GoogleOAuthenticator .blocked_users = {"blocked" }
200223 authenticator = GoogleOAuthenticator (config = c )
201224
202- handled_user_model = user_model ("user1@iN-hosteD-domaiN.com" )
225+ handled_user_model = user_model (user_email , hd = user_hd )
203226 handler = google_client .handler_for_user (handled_user_model )
204227 auth_model = await authenticator .get_authenticated_user (handler , None )
205- assert auth_model
206- assert auth_model ["name" ] == "user1"
207- assert auth_model ["admin" ] == True
208-
209- handled_user_model = user_model ("user2@iN-hosteD-domaiN.com" )
210- handler = google_client .handler_for_user (handled_user_model )
211- auth_model = await authenticator .get_authenticated_user (handler , None )
212- assert auth_model
213- assert auth_model ["name" ] == "user2"
214- assert auth_model ["admin" ] == None
215-
216- handled_user_model = user_model ("user1@not-in-hosted-domain.com" )
217- handler = google_client .handler_for_user (handled_user_model )
218- with raises (HTTPError ) as exc :
219- await authenticator .get_authenticated_user (handler , None )
220- assert exc .value .status_code == 403
228+ if expect_allowed :
229+ assert auth_model
230+ assert auth_model ["name" ] == expect_username
231+ assert auth_model ["admin" ] == expect_admin
232+ else :
233+ assert auth_model == None
221234
222235
223236@mark .parametrize (
@@ -235,37 +248,49 @@ async def test_check_allowed_no_auth_state(google_client, name, allowed):
235248 assert await authenticator .check_allowed (name , None )
236249
237250
238- async def test_hosted_domain_multiple_entries (google_client ):
251+ @mark .parametrize (
252+ "test_variation_id,user_email,user_hd,expect_username,expect_allowed" ,
253+ [
254+ ("01" , "user1@ok-hd1.orG" , "ok-hd1.org" , "user1@ok-hd1.org" , True ),
255+ ("02" , "user2@ok-hd2.orG" , "ok-hd2.org" , "user2@ok-hd2.org" , True ),
256+ ("03" , "blocked@ok-hd1.org" , "ok-hd1.org" , None , False ),
257+ ("04" , "user3@ok-hd1.org" , "" , None , False ),
258+ ("05" , "user1@not-ok.org" , "" , None , False ),
259+ # Test variation 06 below isn't believed to be possible, but since we
260+ # aren't sure this test clarifies what we expect to happen.
261+ ("06" , "user1@other.org" , "ok-hd1.org" , "user1@other.org" , True ),
262+ ],
263+ )
264+ async def test_hosted_domain_multiple_entries (
265+ google_client ,
266+ test_variation_id ,
267+ user_email ,
268+ user_hd ,
269+ expect_username ,
270+ expect_allowed ,
271+ ):
239272 """
240273 Tests that sign in is restricted to the listed domains and that the username
241274 represents the full email as expected when hosted_domain contains multiple
242275 entries.
243276 """
244277 c = Config ()
245278 c .GoogleOAuthenticator .hosted_domain = [
246- "In-Hosted-Domain1.com " ,
247- "In-Hosted-Domain2.com " ,
279+ "ok-hd1.org " ,
280+ "ok-hd2.ORG " ,
248281 ]
282+ c .GoogleOAuthenticator .blocked_users = ["blocked@ok-hd1.org" ]
249283 c .GoogleOAuthenticator .allow_all = True
250284 authenticator = GoogleOAuthenticator (config = c )
251285
252- handled_user_model = user_model ("user1@iN-hosteD-domaiN1.com" )
286+ handled_user_model = user_model (user_email , hd = user_hd )
253287 handler = google_client .handler_for_user (handled_user_model )
254288 auth_model = await authenticator .get_authenticated_user (handler , None )
255- assert auth_model
256- assert auth_model ["name" ] == "user1@in-hosted-domain1.com"
257-
258- handled_user_model = user_model ("user2@iN-hosteD-domaiN2.com" )
259- handler = google_client .handler_for_user (handled_user_model )
260- auth_model = await authenticator .get_authenticated_user (handler , None )
261- assert auth_model
262- assert auth_model ["name" ] == "user2@in-hosted-domain2.com"
263-
264- handled_user_model = user_model ("user1@not-in-hosted-domain.com" )
265- handler = google_client .handler_for_user (handled_user_model )
266- with raises (HTTPError ) as exc :
267- await authenticator .get_authenticated_user (handler , None )
268- assert exc .value .status_code == 403
289+ if expect_allowed :
290+ assert auth_model
291+ assert auth_model ["name" ] == expect_username
292+ else :
293+ assert auth_model == None
269294
270295
271296@mark .parametrize (
0 commit comments