Skip to content

Commit

Permalink
Allow tests to use another port than 5000 when in used (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-b authored Jan 7, 2025
2 parents 0d9a3ac + da6276e commit b568829
Show file tree
Hide file tree
Showing 16 changed files with 2,700 additions and 1,778 deletions.
68 changes: 44 additions & 24 deletions tests/features/multi_auth/test_add_operator_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,19 @@ async def test_oauth2_client_credential_and_multiple_authentication_can_be_combi

@pytest.mark.asyncio
async def test_oauth2_authorization_code_and_api_key_authentication_can_be_combined(
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock, unused_tcp_port: int
):
authorization_code_auth = httpx_auth.OAuth2AuthorizationCode(
"https://provide_code", "https://provide_access_token"
"https://provide_code",
"https://provide_access_token",
redirect_uri_port=unused_tcp_port,
)
api_key_auth = httpx_auth.HeaderApiKey("my_provided_api_key")
auth = authorization_code_auth + api_key_auth

tab = browser_mock.add_response(
opened_url="https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2F",
reply_url="http://localhost:5000#code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5",
opened_url=f"https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A{unused_tcp_port}%2F",
reply_url=f"http://localhost:{unused_tcp_port}#code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5",
)

httpx_mock.add_response(
Expand Down Expand Up @@ -357,10 +359,12 @@ async def test_oauth2_authorization_code_and_api_key_authentication_can_be_combi

@pytest.mark.asyncio
async def test_oauth2_authorization_code_and_multiple_authentication_can_be_combined(
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock, unused_tcp_port: int
):
authorization_code_auth = httpx_auth.OAuth2AuthorizationCode(
"https://provide_code", "https://provide_access_token"
"https://provide_code",
"https://provide_access_token",
redirect_uri_port=unused_tcp_port,
)
api_key_auth = httpx_auth.HeaderApiKey("my_provided_api_key")
api_key_auth2 = httpx_auth.HeaderApiKey(
Expand All @@ -369,8 +373,8 @@ async def test_oauth2_authorization_code_and_multiple_authentication_can_be_comb
auth = authorization_code_auth + (api_key_auth + api_key_auth2)

tab = browser_mock.add_response(
opened_url="https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2F",
reply_url="http://localhost:5000#code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5",
opened_url=f"https://provide_code?response_type=code&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5&redirect_uri=http%3A%2F%2Flocalhost%3A{unused_tcp_port}%2F",
reply_url=f"http://localhost:{unused_tcp_port}#code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5",
)

httpx_mock.add_response(
Expand Down Expand Up @@ -403,20 +407,26 @@ async def test_oauth2_authorization_code_and_multiple_authentication_can_be_comb

@pytest.mark.asyncio
async def test_oauth2_pkce_and_api_key_authentication_can_be_combined(
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock, monkeypatch
token_cache,
httpx_mock: HTTPXMock,
browser_mock: BrowserMock,
monkeypatch,
unused_tcp_port: int,
):
monkeypatch.setattr(
httpx_auth._oauth2.authorization_code_pkce.os, "urandom", lambda x: b"1" * 63
)
pkce_auth = httpx_auth.OAuth2AuthorizationCodePKCE(
"https://provide_code", "https://provide_access_token"
"https://provide_code",
"https://provide_access_token",
redirect_uri_port=unused_tcp_port,
)
api_key_auth = httpx_auth.HeaderApiKey("my_provided_api_key")
auth = pkce_auth + api_key_auth

tab = browser_mock.add_response(
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",
reply_url="http://localhost:5000#code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5",
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",
reply_url=f"http://localhost:{unused_tcp_port}#code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5",
)

httpx_mock.add_response(
Expand Down Expand Up @@ -448,13 +458,19 @@ async def test_oauth2_pkce_and_api_key_authentication_can_be_combined(

@pytest.mark.asyncio
async def test_oauth2_pkce_and_multiple_authentication_can_be_combined(
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock, monkeypatch
token_cache,
httpx_mock: HTTPXMock,
browser_mock: BrowserMock,
monkeypatch,
unused_tcp_port: int,
):
monkeypatch.setattr(
httpx_auth._oauth2.authorization_code_pkce.os, "urandom", lambda x: b"1" * 63
)
pkce_auth = httpx_auth.OAuth2AuthorizationCodePKCE(
"https://provide_code", "https://provide_access_token"
"https://provide_code",
"https://provide_access_token",
redirect_uri_port=unused_tcp_port,
)
api_key_auth = httpx_auth.HeaderApiKey("my_provided_api_key")
api_key_auth2 = httpx_auth.HeaderApiKey(
Expand All @@ -463,8 +479,8 @@ async def test_oauth2_pkce_and_multiple_authentication_can_be_combined(
auth = pkce_auth + (api_key_auth + api_key_auth2)

tab = browser_mock.add_response(
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",
reply_url="http://localhost:5000#code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5",
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",
reply_url=f"http://localhost:{unused_tcp_port}#code=SplxlOBeZQQYbYS6WxSbIA&state=ce9c755b41b5e3c5b64c70598715d5de271023a53f39a67a70215d265d11d2bfb6ef6e9c701701e998e69cbdbf2cee29fd51d2a950aa05f59a20cf4a646099d5",
)

httpx_mock.add_response(
Expand Down Expand Up @@ -497,9 +513,11 @@ async def test_oauth2_pkce_and_multiple_authentication_can_be_combined(

@pytest.mark.asyncio
async def test_oauth2_implicit_and_api_key_authentication_can_be_combined(
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock, unused_tcp_port: int
):
implicit_auth = httpx_auth.OAuth2Implicit("https://provide_token")
implicit_auth = httpx_auth.OAuth2Implicit(
"https://provide_token", redirect_uri_port=unused_tcp_port
)
api_key_auth = httpx_auth.HeaderApiKey("my_provided_api_key")
auth = implicit_auth + api_key_auth

Expand All @@ -508,8 +526,8 @@ async def test_oauth2_implicit_and_api_key_authentication_can_be_combined(
) + datetime.timedelta(hours=1)
token = create_token(expiry_in_1_hour)
tab = browser_mock.add_response(
opened_url="https://provide_token?response_type=token&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2F",
reply_url="http://localhost:5000",
opened_url=f"https://provide_token?response_type=token&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c&redirect_uri=http%3A%2F%2Flocalhost%3A{unused_tcp_port}%2F",
reply_url=f"http://localhost:{unused_tcp_port}",
data=f"access_token={token}&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c",
)

Expand All @@ -530,9 +548,11 @@ async def test_oauth2_implicit_and_api_key_authentication_can_be_combined(

@pytest.mark.asyncio
async def test_oauth2_implicit_and_multiple_authentication_can_be_combined(
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock
token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock, unused_tcp_port: int
):
implicit_auth = httpx_auth.OAuth2Implicit("https://provide_token")
implicit_auth = httpx_auth.OAuth2Implicit(
"https://provide_token", redirect_uri_port=unused_tcp_port
)
api_key_auth = httpx_auth.HeaderApiKey("my_provided_api_key")
api_key_auth2 = httpx_auth.HeaderApiKey(
"my_provided_api_key2", header_name="X-Api-Key2"
Expand All @@ -544,8 +564,8 @@ async def test_oauth2_implicit_and_multiple_authentication_can_be_combined(
) + datetime.timedelta(hours=1)
token = create_token(expiry_in_1_hour)
tab = browser_mock.add_response(
opened_url="https://provide_token?response_type=token&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2F",
reply_url="http://localhost:5000",
opened_url=f"https://provide_token?response_type=token&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c&redirect_uri=http%3A%2F%2Flocalhost%3A{unused_tcp_port}%2F",
reply_url=f"http://localhost:{unused_tcp_port}",
data=f"access_token={token}&state=bee505cb6ceb14b9f6ac3573cd700b3b3e965004078d7bb57c7b92df01e448c992a7a46b4804164fc998ea166ece3f3d5849ca2405c4a548f43b915b0677231c",
)

Expand Down
Loading

0 comments on commit b568829

Please sign in to comment.