Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache ip addresses in test suite. #65

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@
from bankid.certs import get_test_cert_and_key


@pytest.fixture()
@pytest.fixture(scope="session")
def ip_address() -> str:
with httpx.Client() as client:
response = client.get("https://httpbin.org/ip")
return response.json()["origin"].split(",")[0]


@pytest_asyncio.fixture()
async def ip_address_async() -> str:
async with httpx.AsyncClient() as client:
response = await client.get("https://httpbin.org/ip")
return response.json()["origin"].split(",")[0]


@pytest.fixture()
def cert_and_key():
cert, key = get_test_cert_and_key()
Expand Down
24 changes: 12 additions & 12 deletions tests/test_asyncclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@


@pytest.mark.asyncio
async def test_authentication_and_collect(cert_and_key, ip_address_async):
async def test_authentication_and_collect(cert_and_key, ip_address):
"""Authenticate call and then collect with the returned orderRef UUID."""
c = BankIDAsyncClient(certificates=cert_and_key, test_server=True)
assert "appapi2.test.bankid.com.pem" in c.verify_cert
out = await c.authenticate(ip_address_async)
out = await c.authenticate(ip_address)
assert isinstance(out, dict)
# UUID.__init__ performs the UUID compliance assertion.
uuid.UUID(out.get("orderRef"), version=4)
Expand All @@ -34,12 +34,12 @@ async def test_authentication_and_collect(cert_and_key, ip_address_async):


@pytest.mark.asyncio
async def test_sign_and_collect(cert_and_key, ip_address_async):
async def test_sign_and_collect(cert_and_key, ip_address):
"""Sign call and then collect with the returned orderRef UUID."""

c = BankIDAsyncClient(certificates=cert_and_key, test_server=True)
out = await c.sign(
ip_address_async,
ip_address,
user_visible_data="The data to be signed",
user_non_visible_data="Non visible data",
)
Expand Down Expand Up @@ -73,32 +73,32 @@ async def test_invalid_orderref_raises_error(cert_and_key):


@pytest.mark.asyncio
async def test_already_in_progress_raises_error(cert_and_key, ip_address_async, random_personal_number):
async def test_already_in_progress_raises_error(cert_and_key, ip_address, random_personal_number):
c = BankIDAsyncClient(certificates=cert_and_key, test_server=True)
await c.authenticate(ip_address_async, requirement={"personalNumber": random_personal_number})
await c.authenticate(ip_address, requirement={"personalNumber": random_personal_number})
with pytest.raises(exceptions.AlreadyInProgressError):
await c.authenticate(ip_address_async, requirement={"personalNumber": random_personal_number})
await c.authenticate(ip_address, requirement={"personalNumber": random_personal_number})


@pytest.mark.asyncio
async def test_already_in_progress_raises_error_2(cert_and_key, ip_address_async, random_personal_number):
async def test_already_in_progress_raises_error_2(cert_and_key, ip_address, random_personal_number):
c = BankIDAsyncClient(certificates=cert_and_key, test_server=True)
await c.sign(
ip_address_async,
ip_address,
requirement={"personalNumber": random_personal_number},
user_visible_data="Text to sign",
)
with pytest.raises(exceptions.AlreadyInProgressError):
await c.sign(
ip_address_async, requirement={"personalNumber": random_personal_number}, user_visible_data="Text to sign"
ip_address, requirement={"personalNumber": random_personal_number}, user_visible_data="Text to sign"
)


@pytest.mark.asyncio
async def test_authentication_and_cancel(cert_and_key, ip_address_async):
async def test_authentication_and_cancel(cert_and_key, ip_address):
"""Authenticate call and then cancel it"""
c = BankIDAsyncClient(certificates=cert_and_key, test_server=True)
out = await c.authenticate(ip_address_async)
out = await c.authenticate(ip_address)
assert isinstance(out, dict)
# UUID.__init__ performs the UUID compliance assertion.
order_ref = uuid.UUID(out.get("orderRef"), version=4)
Expand Down