Skip to content

Commit

Permalink
Merge pull request #65 from pelme/ip-address-cache
Browse files Browse the repository at this point in the history
Cache ip addresses in test suite.
  • Loading branch information
hbldh authored Apr 15, 2024
2 parents 151d30b + b4773ab commit c0c8158
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
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

0 comments on commit c0c8158

Please sign in to comment.