Skip to content

Commit

Permalink
Add kwargs to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed Jan 29, 2025
1 parent de03443 commit 591fb17
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 50 deletions.
29 changes: 16 additions & 13 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ def test_make_credential_wrong_app_id(self, PatchedCtap2):
try:
client.make_credential(
PublicKeyCredentialCreationOptions(
{"id": "bar.example.com", "name": "Invalid RP"},
user,
challenge,
[{"type": "public-key", "alg": -7}],
rp={"id": "bar.example.com", "name": "Invalid RP"},
user=user,
challenge=challenge,
pub_key_cred_params=[{"type": "public-key", "alg": -7}],
)
)
self.fail("make_credential did not raise error")
Expand All @@ -104,10 +104,10 @@ def test_make_credential_existing_key(self, PatchedCtap2):
try:
client.make_credential(
PublicKeyCredentialCreationOptions(
rp,
user,
challenge,
[{"type": "public-key", "alg": -7}],
rp=rp,
user=user,
challenge=challenge,
pub_key_cred_params=[{"type": "public-key", "alg": -7}],
authenticator_selection={"userVerification": "discouraged"},
)
)
Expand All @@ -132,10 +132,10 @@ def test_make_credential_ctap2(self, PatchedCtap2):

response = client.make_credential(
PublicKeyCredentialCreationOptions(
rp,
user,
challenge,
[{"type": "public-key", "alg": -7}],
rp=rp,
user=user,
challenge=challenge,
pub_key_cred_params=[{"type": "public-key", "alg": -7}],
timeout=1000,
authenticator_selection={"userVerification": "discouraged"},
)
Expand Down Expand Up @@ -175,7 +175,10 @@ def test_make_credential_ctap1(self):

response = client.make_credential(
PublicKeyCredentialCreationOptions(
rp, user, challenge, [{"type": "public-key", "alg": -7}]
rp=rp,
user=user,
challenge=challenge,
pub_key_cred_params=[{"type": "public-key", "alg": -7}],
)
).response

Expand Down
16 changes: 8 additions & 8 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class TestPublicKeyCredentialRpEntity(unittest.TestCase):
def test_id_hash(self):
rp = PublicKeyCredentialRpEntity("Example", "example.com")
rp = PublicKeyCredentialRpEntity(name="Example", id="example.com")
rp_id_hash = (
b"\xa3y\xa6\xf6\xee\xaf\xb9\xa5^7\x8c\x11\x804\xe2u\x1eh/"
b"\xab\x9f-0\xab\x13\xd2\x12U\x86\xce\x19G"
Expand All @@ -30,7 +30,7 @@ def test_id_hash(self):

class TestFido2Server(unittest.TestCase):
def test_register_begin_rp(self):
rp = PublicKeyCredentialRpEntity("Example", "example.com")
rp = PublicKeyCredentialRpEntity(name="Example", id="example.com")
server = Fido2Server(rp)

request, state = server.register_begin(USER)
Expand All @@ -40,7 +40,7 @@ def test_register_begin_rp(self):
)

def test_register_begin_custom_challenge(self):
rp = PublicKeyCredentialRpEntity("Example", "example.com")
rp = PublicKeyCredentialRpEntity(name="Example", id="example.com")
server = Fido2Server(rp)

challenge = b"1234567890123456"
Expand All @@ -49,15 +49,15 @@ def test_register_begin_custom_challenge(self):
self.assertEqual(request["publicKey"]["challenge"], websafe_encode(challenge))

def test_register_begin_custom_challenge_too_short(self):
rp = PublicKeyCredentialRpEntity("Example", "example.com")
rp = PublicKeyCredentialRpEntity(name="Example", id="example.com")
server = Fido2Server(rp)

challenge = b"123456789012345"
with self.assertRaises(ValueError):
request, state = server.register_begin(USER, challenge=challenge)

def test_authenticate_complete_invalid_signature(self):
rp = PublicKeyCredentialRpEntity("Example", "example.com")
rp = PublicKeyCredentialRpEntity(name="Example", id="example.com")
server = Fido2Server(rp)

state = {
Expand All @@ -75,9 +75,9 @@ def test_authenticate_complete_invalid_signature(self):
response = AuthenticationResponse(
raw_id=_CRED_ID,
response=AuthenticatorAssertionResponse(
client_data,
AuthenticatorData(_AUTH_DATA),
b"INVALID",
client_data=client_data,
authenticator_data=AuthenticatorData(_AUTH_DATA),
signature=b"INVALID",
),
)

Expand Down
66 changes: 37 additions & 29 deletions tests/test_webauthn.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def test_collected_client_data(self):

def test_authenticator_selection_criteria(self):
o = AuthenticatorSelectionCriteria(
"platform", require_resident_key=True, user_verification="required"
authenticator_attachment="platform",
require_resident_key=True,
user_verification="required",
)
self.assertEqual(
dict(o),
Expand Down Expand Up @@ -171,7 +173,7 @@ def test_authenticator_selection_criteria(self):
self.assertEqual(o.require_resident_key, False)

def test_rp_entity(self):
o = PublicKeyCredentialRpEntity("Example", "example.com")
o = PublicKeyCredentialRpEntity(name="Example", id="example.com")
self.assertEqual(o, {"id": "example.com", "name": "Example"})
self.assertEqual(o.id, "example.com")
self.assertEqual(o.name, "Example")
Expand All @@ -183,7 +185,9 @@ def test_rp_entity(self):
PublicKeyCredentialRpEntity()

def test_user_entity(self):
o = PublicKeyCredentialUserEntity("Example", b"user", display_name="Display")
o = PublicKeyCredentialUserEntity(
name="Example", id=b"user", display_name="Display"
)
self.assertEqual(
o,
{
Expand All @@ -203,12 +207,12 @@ def test_user_entity(self):
PublicKeyCredentialUserEntity()

def test_parameters(self):
o = PublicKeyCredentialParameters("public-key", -7)
o = PublicKeyCredentialParameters(type="public-key", alg=-7)
self.assertEqual(o, {"type": "public-key", "alg": -7})
self.assertEqual(o.type, "public-key")
self.assertEqual(o.alg, -7)

p = PublicKeyCredentialParameters("invalid-type", -7)
p = PublicKeyCredentialParameters(type="invalid-type", alg=-7)
assert p.type is None

with self.assertRaises(TypeError):
Expand All @@ -218,7 +222,7 @@ def test_parameters(self):
PublicKeyCredentialParameters()

def test_descriptor(self):
o = PublicKeyCredentialDescriptor("public-key", b"credential_id")
o = PublicKeyCredentialDescriptor(type="public-key", id=b"credential_id")
self.assertEqual(
o, {"type": "public-key", "id": websafe_encode(b"credential_id")}
)
Expand All @@ -227,7 +231,7 @@ def test_descriptor(self):
self.assertIsNone(o.transports)

o = PublicKeyCredentialDescriptor(
"public-key", b"credential_id", ["usb", "nfc"]
type="public-key", id=b"credential_id", transports=["usb", "nfc"]
)
self.assertEqual(
o,
Expand All @@ -239,31 +243,33 @@ def test_descriptor(self):
)
self.assertEqual(o.transports, ["usb", "nfc"])

PublicKeyCredentialDescriptor("public-key", b"credential_id", ["valid_value"])
PublicKeyCredentialDescriptor(
type="public-key", id=b"credential_id", transports=["valid_value"]
)

d = PublicKeyCredentialDescriptor("wrong-type", b"credential_id")
d = PublicKeyCredentialDescriptor(type="wrong-type", id=b"credential_id")
assert d.type is None

with self.assertRaises(TypeError):
PublicKeyCredentialDescriptor("public-key")
PublicKeyCredentialDescriptor(type="public-key")

with self.assertRaises(TypeError):
PublicKeyCredentialDescriptor()

def test_creation_options(self):
o = PublicKeyCredentialCreationOptions(
PublicKeyCredentialRpEntity(id="example.com", name="Example"),
PublicKeyCredentialUserEntity(id=b"user_id", name="A. User"),
b"request_challenge",
[{"type": "public-key", "alg": -7}],
10000,
[{"type": "public-key", "id": b"credential_id"}],
{
rp=PublicKeyCredentialRpEntity(id="example.com", name="Example"),
user=PublicKeyCredentialUserEntity(id=b"user_id", name="A. User"),
challenge=b"request_challenge",
pub_key_cred_params=[{"type": "public-key", "alg": -7}],
timeout=10000,
exclude_credentials=[{"type": "public-key", "id": b"credential_id"}],
authenticator_selection={
"authenticatorAttachment": "platform",
"residentKey": "required",
"userVerification": "required",
},
"direct",
attestation="direct",
)
self.assertEqual(o.rp, {"id": "example.com", "name": "Example"})
self.assertEqual(o.user, {"id": websafe_encode(b"user_id"), "name": "A. User"})
Expand All @@ -289,10 +295,10 @@ def test_creation_options(self):

self.assertIsNone(
PublicKeyCredentialCreationOptions(
{"id": "example.com", "name": "Example"},
{"id": b"user_id", "name": "A. User"},
b"request_challenge",
[{"type": "public-key", "alg": -7}],
rp={"id": "example.com", "name": "Example"},
user={"id": b"user_id", "name": "A. User"},
challenge=b"request_challenge",
pub_key_cred_params=[{"type": "public-key", "alg": -7}],
attestation="invalid",
).attestation
)
Expand All @@ -304,11 +310,13 @@ def test_creation_options(self):

def test_request_options(self):
o = PublicKeyCredentialRequestOptions(
b"request_challenge",
10000,
"example.com",
[PublicKeyCredentialDescriptor(type="public-key", id=b"credential_id")],
"discouraged",
challenge=b"request_challenge",
timeout=10000,
rp_id="example.com",
allow_credentials=[
PublicKeyCredentialDescriptor(type="public-key", id=b"credential_id")
],
user_verification="discouraged",
)
self.assertEqual(o.challenge, b"request_challenge")
self.assertEqual(o.rp_id, "example.com")
Expand All @@ -319,14 +327,14 @@ def test_request_options(self):
o2 = PublicKeyCredentialRequestOptions.from_dict(json.loads(js))
self.assertEqual(o, o2)

o = PublicKeyCredentialRequestOptions(b"request_challenge")
o = PublicKeyCredentialRequestOptions(challenge=b"request_challenge")
self.assertIsNone(o.timeout)
self.assertIsNone(o.rp_id)
self.assertIsNone(o.allow_credentials)
self.assertIsNone(o.user_verification)

self.assertIsNone(
PublicKeyCredentialRequestOptions(
b"request_challenge", user_verification="invalid"
challenge=b"request_challenge", user_verification="invalid"
).user_verification
)

0 comments on commit 591fb17

Please sign in to comment.