@@ -315,17 +315,19 @@ async def test_oauth2_client_credential_and_multiple_authentication_can_be_combi
315315
316316@pytest .mark .asyncio
317317async def test_oauth2_authorization_code_and_api_key_authentication_can_be_combined (
318- token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock
318+ token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock , unused_tcp_port : int
319319):
320320 authorization_code_auth = httpx_auth .OAuth2AuthorizationCode (
321- "https://provide_code" , "https://provide_access_token"
321+ "https://provide_code" ,
322+ "https://provide_access_token" ,
323+ redirect_uri_port = unused_tcp_port ,
322324 )
323325 api_key_auth = httpx_auth .HeaderApiKey ("my_provided_api_key" )
324326 auth = authorization_code_auth + api_key_auth
325327
326328 tab = browser_mock .add_response (
327- opened_url = "https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A5000 %2F" ,
328- reply_url = "http://localhost:5000 #code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5" ,
329+ opened_url = f "https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A { unused_tcp_port } %2F" ,
330+ reply_url = f "http://localhost:{ unused_tcp_port } #code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5" ,
329331 )
330332
331333 httpx_mock .add_response (
@@ -357,10 +359,12 @@ async def test_oauth2_authorization_code_and_api_key_authentication_can_be_combi
357359
358360@pytest .mark .asyncio
359361async def test_oauth2_authorization_code_and_multiple_authentication_can_be_combined (
360- token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock
362+ token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock , unused_tcp_port : int
361363):
362364 authorization_code_auth = httpx_auth .OAuth2AuthorizationCode (
363- "https://provide_code" , "https://provide_access_token"
365+ "https://provide_code" ,
366+ "https://provide_access_token" ,
367+ redirect_uri_port = unused_tcp_port ,
364368 )
365369 api_key_auth = httpx_auth .HeaderApiKey ("my_provided_api_key" )
366370 api_key_auth2 = httpx_auth .HeaderApiKey (
@@ -369,8 +373,8 @@ async def test_oauth2_authorization_code_and_multiple_authentication_can_be_comb
369373 auth = authorization_code_auth + (api_key_auth + api_key_auth2 )
370374
371375 tab = browser_mock .add_response (
372- opened_url = "https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A5000 %2F" ,
373- reply_url = "http://localhost:5000 #code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5" ,
376+ opened_url = f "https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A { unused_tcp_port } %2F" ,
377+ reply_url = f "http://localhost:{ unused_tcp_port } #code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5" ,
374378 )
375379
376380 httpx_mock .add_response (
@@ -403,20 +407,26 @@ async def test_oauth2_authorization_code_and_multiple_authentication_can_be_comb
403407
404408@pytest .mark .asyncio
405409async def test_oauth2_pkce_and_api_key_authentication_can_be_combined (
406- token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock , monkeypatch
410+ token_cache ,
411+ httpx_mock : HTTPXMock ,
412+ browser_mock : BrowserMock ,
413+ monkeypatch ,
414+ unused_tcp_port : int ,
407415):
408416 monkeypatch .setattr (
409417 httpx_auth ._oauth2 .authorization_code_pkce .os , "urandom" , lambda x : b"1" * 63
410418 )
411419 pkce_auth = httpx_auth .OAuth2AuthorizationCodePKCE (
412- "https://provide_code" , "https://provide_access_token"
420+ "https://provide_code" ,
421+ "https://provide_access_token" ,
422+ redirect_uri_port = unused_tcp_port ,
413423 )
414424 api_key_auth = httpx_auth .HeaderApiKey ("my_provided_api_key" )
415425 auth = pkce_auth + api_key_auth
416426
417427 tab = browser_mock .add_response (
418- opened_url = "https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A5000 %2F&code_challenge=5C_ph_KZ3DstYUc965SiqmKAA-ShvKF4Ut7daKd3fjc&code_challenge_method=S256" ,
419- reply_url = "http://localhost:5000 #code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5" ,
428+ opened_url = f "https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A { unused_tcp_port } %2F&code_challenge=5C_ph_KZ3DstYUc965SiqmKAA-ShvKF4Ut7daKd3fjc&code_challenge_method=S256" ,
429+ reply_url = f "http://localhost:{ unused_tcp_port } #code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5" ,
420430 )
421431
422432 httpx_mock .add_response (
@@ -448,13 +458,19 @@ async def test_oauth2_pkce_and_api_key_authentication_can_be_combined(
448458
449459@pytest .mark .asyncio
450460async def test_oauth2_pkce_and_multiple_authentication_can_be_combined (
451- token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock , monkeypatch
461+ token_cache ,
462+ httpx_mock : HTTPXMock ,
463+ browser_mock : BrowserMock ,
464+ monkeypatch ,
465+ unused_tcp_port : int ,
452466):
453467 monkeypatch .setattr (
454468 httpx_auth ._oauth2 .authorization_code_pkce .os , "urandom" , lambda x : b"1" * 63
455469 )
456470 pkce_auth = httpx_auth .OAuth2AuthorizationCodePKCE (
457- "https://provide_code" , "https://provide_access_token"
471+ "https://provide_code" ,
472+ "https://provide_access_token" ,
473+ redirect_uri_port = unused_tcp_port ,
458474 )
459475 api_key_auth = httpx_auth .HeaderApiKey ("my_provided_api_key" )
460476 api_key_auth2 = httpx_auth .HeaderApiKey (
@@ -463,8 +479,8 @@ async def test_oauth2_pkce_and_multiple_authentication_can_be_combined(
463479 auth = pkce_auth + (api_key_auth + api_key_auth2 )
464480
465481 tab = browser_mock .add_response (
466- opened_url = "https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A5000 %2F&code_challenge=5C_ph_KZ3DstYUc965SiqmKAA-ShvKF4Ut7daKd3fjc&code_challenge_method=S256" ,
467- reply_url = "http://localhost:5000 #code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5" ,
482+ opened_url = f "https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A { unused_tcp_port } %2F&code_challenge=5C_ph_KZ3DstYUc965SiqmKAA-ShvKF4Ut7daKd3fjc&code_challenge_method=S256" ,
483+ reply_url = f "http://localhost:{ unused_tcp_port } #code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5" ,
468484 )
469485
470486 httpx_mock .add_response (
@@ -497,9 +513,11 @@ async def test_oauth2_pkce_and_multiple_authentication_can_be_combined(
497513
498514@pytest .mark .asyncio
499515async def test_oauth2_implicit_and_api_key_authentication_can_be_combined (
500- token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock
516+ token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock , unused_tcp_port : int
501517):
502- implicit_auth = httpx_auth .OAuth2Implicit ("https://provide_token" )
518+ implicit_auth = httpx_auth .OAuth2Implicit (
519+ "https://provide_token" , redirect_uri_port = unused_tcp_port
520+ )
503521 api_key_auth = httpx_auth .HeaderApiKey ("my_provided_api_key" )
504522 auth = implicit_auth + api_key_auth
505523
@@ -508,8 +526,8 @@ async def test_oauth2_implicit_and_api_key_authentication_can_be_combined(
508526 ) + datetime .timedelta (hours = 1 )
509527 token = create_token (expiry_in_1_hour )
510528 tab = browser_mock .add_response (
511- opened_url = "https://provide_token?response_type=token&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c&redirect_uri=http%3A%2F%2Flocalhost%3A5000 %2F" ,
512- reply_url = "http://localhost:5000 " ,
529+ opened_url = f "https://provide_token?response_type=token&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c&redirect_uri=http%3A%2F%2Flocalhost%3A { unused_tcp_port } %2F" ,
530+ reply_url = f "http://localhost:{ unused_tcp_port } " ,
513531 data = f"access_token={ token } &state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c" ,
514532 )
515533
@@ -530,9 +548,11 @@ async def test_oauth2_implicit_and_api_key_authentication_can_be_combined(
530548
531549@pytest .mark .asyncio
532550async def test_oauth2_implicit_and_multiple_authentication_can_be_combined (
533- token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock
551+ token_cache , httpx_mock : HTTPXMock , browser_mock : BrowserMock , unused_tcp_port : int
534552):
535- implicit_auth = httpx_auth .OAuth2Implicit ("https://provide_token" )
553+ implicit_auth = httpx_auth .OAuth2Implicit (
554+ "https://provide_token" , redirect_uri_port = unused_tcp_port
555+ )
536556 api_key_auth = httpx_auth .HeaderApiKey ("my_provided_api_key" )
537557 api_key_auth2 = httpx_auth .HeaderApiKey (
538558 "my_provided_api_key2" , header_name = "X-Api-Key2"
@@ -544,8 +564,8 @@ async def test_oauth2_implicit_and_multiple_authentication_can_be_combined(
544564 ) + datetime .timedelta (hours = 1 )
545565 token = create_token (expiry_in_1_hour )
546566 tab = browser_mock .add_response (
547- opened_url = "https://provide_token?response_type=token&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c&redirect_uri=http%3A%2F%2Flocalhost%3A5000 %2F" ,
548- reply_url = "http://localhost:5000 " ,
567+ opened_url = f "https://provide_token?response_type=token&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c&redirect_uri=http%3A%2F%2Flocalhost%3A { unused_tcp_port } %2F" ,
568+ reply_url = f "http://localhost:{ unused_tcp_port } " ,
549569 data = f"access_token={ token } &state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c" ,
550570 )
551571
0 commit comments