Skip to content

Commit

Permalink
Replace parametrize parameters with pytest.param (#2066)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen authored Sep 17, 2024
1 parent 3069f2e commit a18d4d8
Show file tree
Hide file tree
Showing 15 changed files with 491 additions and 400 deletions.
20 changes: 10 additions & 10 deletions tests/manager/api/lcp/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@

class TestHasherFactory:
@pytest.mark.parametrize(
"_,hashing_algorithm,value,expected_value",
"hashing_algorithm,value,expected_value",
[
(
"sha256",
pytest.param(
HashingAlgorithm.SHA256,
"12345",
"5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5",
id="sha256",
),
(
"sha256_value",
pytest.param(
HashingAlgorithm.SHA256.value,
"12345",
"5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5",
id="sha256_value",
),
(
"sha512",
pytest.param(
HashingAlgorithm.SHA512,
"12345",
"3627909a29c31381a071ec27f7c9ca97726182aed29a7ddd2e54353322cfb30abb9e3a6df2ac2c20fe23436311d678564d0c8d305930575f60e2d3d048184d79",
id="sha512",
),
(
"sha512_value",
pytest.param(
HashingAlgorithm.SHA512.value,
"12345",
"3627909a29c31381a071ec27f7c9ca97726182aed29a7ddd2e54353322cfb30abb9e3a6df2ac2c20fe23436311d678564d0c8d305930575f60e2d3d048184d79",
id="sha512_value",
),
],
)
def test_create(self, _, hashing_algorithm, value, expected_value):
def test_create(self, hashing_algorithm, value, expected_value):
hasher_factory = HasherFactory()
hasher = hasher_factory.create(hashing_algorithm)

Expand Down
12 changes: 6 additions & 6 deletions tests/manager/api/saml/configuration/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,9 @@ def test_get_identity_provider_settings_returns_correct_result(self):
onelogin_configuration.get_identity_providers.assert_called_once_with(db)

@pytest.mark.parametrize(
"_,service_provider,expected_result",
"service_provider,expected_result",
[
(
"service_provider_without_certificates",
pytest.param(
SERVICE_PROVIDER_WITHOUT_CERTIFICATE,
{
"sp": {
Expand All @@ -344,9 +343,9 @@ def test_get_identity_provider_settings_returns_correct_result(self):
"authnRequestsSigned": SERVICE_PROVIDER_WITH_CERTIFICATE.authn_requests_signed
},
},
id="service_provider_without_certificates",
),
(
"service_provider_with_certificate",
pytest.param(
SERVICE_PROVIDER_WITH_CERTIFICATE,
{
"sp": {
Expand All @@ -365,11 +364,12 @@ def test_get_identity_provider_settings_returns_correct_result(self):
"authnRequestsSigned": SERVICE_PROVIDER_WITH_CERTIFICATE.authn_requests_signed
},
},
id="service_provider_with_certificate",
),
],
)
def test_get_service_provider_settings_returns_correct_result(
self, _, service_provider, expected_result
self, service_provider, expected_result
):
# Arrange
configuration = create_autospec(spec=SAMLWebSSOAuthSettings)
Expand Down
35 changes: 17 additions & 18 deletions tests/manager/api/saml/configuration/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,68 @@

class TestSAMLSettingsValidator:
@pytest.mark.parametrize(
"_,sp_xml_metadata,idp_xml_metadata,patron_id_regular_expression,expected_validation_result",
"sp_xml_metadata,idp_xml_metadata,patron_id_regular_expression,expected_validation_result",
[
(
"missing_sp_metadata_and_missing_idp_metadata",
pytest.param(
None,
None,
None,
INCOMPLETE_CONFIGURATION,
id="missing_sp_metadata_and_missing_idp_metadata",
),
(
"empty_sp_metadata_and_empty_idp_metadata",
pytest.param(
saml_strings.INCORRECT_XML,
saml_strings.INCORRECT_XML,
None,
INCOMPLETE_CONFIGURATION,
id="empty_sp_metadata_and_empty_idp_metadata",
),
(
"incorrect_sp_metadata_and_incorrect_idp_metadata",
pytest.param(
saml_strings.INCORRECT_XML_WITH_ONE_SP_METADATA_WITHOUT_ACS_SERVICE,
saml_strings.INCORRECT_XML_WITH_ONE_IDP_METADATA_WITHOUT_SSO_SERVICE,
None,
SAML_INCORRECT_METADATA,
id="incorrect_sp_metadata_and_incorrect_idp_metadata",
),
(
"correct_sp_metadata_and_incorrect_idp_metadata",
pytest.param(
saml_strings.CORRECT_XML_WITH_ONE_SP,
saml_strings.INCORRECT_XML_WITH_ONE_IDP_METADATA_WITHOUT_SSO_SERVICE,
None,
SAML_INCORRECT_METADATA,
id="correct_sp_metadata_and_incorrect_idp_metadata",
),
(
"correct_sp_and_idp_metadata",
pytest.param(
saml_strings.CORRECT_XML_WITH_ONE_SP,
saml_strings.CORRECT_XML_WITH_IDP_1,
None,
None,
id="correct_sp_and_idp_metadata",
),
(
"correct_patron_id_regular_expression",
pytest.param(
saml_strings.CORRECT_XML_WITH_ONE_SP,
saml_strings.CORRECT_XML_WITH_IDP_1,
r"(?P<patron_id>.+)@university\.org",
None,
id="correct_patron_id_regular_expression",
),
(
"correct_patron_id_regular_expression_without_patron_id_named_group",
pytest.param(
saml_strings.CORRECT_XML_WITH_ONE_SP,
saml_strings.CORRECT_XML_WITH_IDP_1,
r"(?P<patron>.+)@university\.org",
SAML_INCORRECT_PATRON_ID_REGULAR_EXPRESSION,
id="correct_patron_id_regular_expression_without_patron_id_named_group",
),
(
"incorrect_patron_id_regular_expression",
pytest.param(
saml_strings.CORRECT_XML_WITH_ONE_SP,
saml_strings.CORRECT_XML_WITH_IDP_1,
r"[",
INVALID_CONFIGURATION_OPTION,
id="incorrect_patron_id_regular_expression",
),
],
)
def test_validate(
self,
_,
sp_xml_metadata,
idp_xml_metadata,
patron_id_regular_expression,
Expand Down
75 changes: 37 additions & 38 deletions tests/manager/api/saml/metadata/federations/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,111 +19,110 @@

class TestSAMLFederatedMetadataExpirationValidator:
@pytest.mark.parametrize(
"_,current_time,metadata,expected_exception",
"current_time,metadata,expected_exception",
[
(
"incorrect_xml_str_type",
pytest.param(
utc_now(),
fixtures.INCORRECT_XML,
SAMLFederatedMetadataValidationError,
id="incorrect_xml_str_type",
),
(
"incorrect_xml_bytes_type",
pytest.param(
utc_now(),
fixtures.INCORRECT_XML.encode(),
SAMLFederatedMetadataValidationError,
id="incorrect_xml_bytes_type",
),
(
"without_valid_until_attribute_metadata_str_type",
pytest.param(
utc_now(),
fixtures.FEDERATED_METADATA_WITHOUT_VALID_UNTIL_ATTRIBUTE,
SAMLFederatedMetadataValidationError,
id="without_valid_until_attribute_metadata_str_type",
),
(
"without_valid_until_attribute_metadata_bytes_type",
pytest.param(
utc_now(),
fixtures.FEDERATED_METADATA_WITHOUT_VALID_UNTIL_ATTRIBUTE.encode(),
SAMLFederatedMetadataValidationError,
id="without_valid_until_attribute_metadata_bytes_type",
),
(
"with_expired_valid_until_attribute_metadata_str_type",
pytest.param(
fixtures.FEDERATED_METADATA_VALID_UNTIL
+ SAMLFederatedMetadataExpirationValidator.MAX_CLOCK_SKEW
+ datetime.timedelta(minutes=1),
fixtures.FEDERATED_METADATA_WITH_VALID_UNTIL_ATTRIBUTE,
SAMLFederatedMetadataValidationError,
id="with_expired_valid_until_attribute_metadata_str_type",
),
(
"with_expired_valid_until_attribute_metadata_bytes_type",
pytest.param(
fixtures.FEDERATED_METADATA_VALID_UNTIL
+ SAMLFederatedMetadataExpirationValidator.MAX_CLOCK_SKEW
+ datetime.timedelta(minutes=1),
fixtures.FEDERATED_METADATA_WITH_VALID_UNTIL_ATTRIBUTE.encode(),
SAMLFederatedMetadataValidationError,
id="with_expired_valid_until_attribute_metadata_bytes_type",
),
(
"with_valid_until_attribute_too_far_in_the_future_metadata_str_type",
pytest.param(
fixtures.FEDERATED_METADATA_VALID_UNTIL
- SAMLFederatedMetadataExpirationValidator.MAX_VALID_TIME
- datetime.timedelta(minutes=1),
fixtures.FEDERATED_METADATA_WITH_VALID_UNTIL_ATTRIBUTE,
SAMLFederatedMetadataValidationError,
id="with_valid_until_attribute_too_far_in_the_future_metadata_str_type",
),
(
"with_valid_until_attribute_too_far_in_the_future_metadata_bytes_type",
pytest.param(
fixtures.FEDERATED_METADATA_VALID_UNTIL
- SAMLFederatedMetadataExpirationValidator.MAX_VALID_TIME
- datetime.timedelta(minutes=1),
fixtures.FEDERATED_METADATA_WITH_VALID_UNTIL_ATTRIBUTE.encode(),
SAMLFederatedMetadataValidationError,
id="with_valid_until_attribute_too_far_in_the_future_metadata_bytes_type",
),
(
"with_valid_until_attribute_less_than_current_time_and_less_than_max_clock_skew_metadata_str_type",
pytest.param(
fixtures.FEDERATED_METADATA_VALID_UNTIL
+ SAMLFederatedMetadataExpirationValidator.MAX_CLOCK_SKEW,
fixtures.FEDERATED_METADATA_WITH_VALID_UNTIL_ATTRIBUTE,
None,
id="with_valid_until_attribute_less_than_current_time_and_less_than_max_clock_skew_metadata_str_type",
),
(
"with_valid_until_attribute_less_than_current_time_and_less_than_max_clock_skew_metadata_bytes_type",
pytest.param(
fixtures.FEDERATED_METADATA_VALID_UNTIL
+ SAMLFederatedMetadataExpirationValidator.MAX_CLOCK_SKEW,
fixtures.FEDERATED_METADATA_WITH_VALID_UNTIL_ATTRIBUTE.encode(),
None,
id="with_valid_until_attribute_less_than_current_time_and_less_than_max_clock_skew_metadata_bytes_type",
),
(
"with_valid_until_attribute_greater_than_current_time_and_less_than_max_valid_time_metadata_str_type",
pytest.param(
fixtures.FEDERATED_METADATA_VALID_UNTIL
- SAMLFederatedMetadataExpirationValidator.MAX_VALID_TIME
+ datetime.timedelta(minutes=1),
fixtures.FEDERATED_METADATA_WITH_VALID_UNTIL_ATTRIBUTE,
None,
id="with_valid_until_attribute_greater_than_current_time_and_less_than_max_valid_time_metadata_str_type",
),
(
"with_valid_until_attribute_greater_than_current_time_and_less_than_max_valid_time_metadata_bytes_type",
pytest.param(
fixtures.FEDERATED_METADATA_VALID_UNTIL
- SAMLFederatedMetadataExpirationValidator.MAX_VALID_TIME
+ datetime.timedelta(minutes=1),
fixtures.FEDERATED_METADATA_WITH_VALID_UNTIL_ATTRIBUTE.encode(),
None,
id="with_valid_until_attribute_greater_than_current_time_and_less_than_max_valid_time_metadata_bytes_type",
),
(
"with_real_incommon_metadata_str_type",
pytest.param(
datetime_utc(2020, 11, 26, 14, 32, 42),
SamlFilesFixture.sample_text("incommon-metadata-idp-only.xml"),
None,
id="with_real_incommon_metadata_str_type",
),
(
"with_real_incommon_metadata_bytes_type",
pytest.param(
datetime_utc(2020, 11, 26, 14, 32, 42),
SamlFilesFixture.sample_data("incommon-metadata-idp-only.xml"),
None,
id="with_real_incommon_metadata_bytes_type",
),
],
)
def test_validate(
self,
_,
current_time: datetime.datetime,
metadata: str | bytes,
expected_exception: type[Exception] | None,
Expand All @@ -145,29 +144,29 @@ def test_validate(

class TestSAMLMetadataSignatureValidator:
@pytest.mark.parametrize(
"_,certificate,metadata,expected_exception",
"certificate,metadata,expected_exception",
[
(
"without_signature",
pytest.param(
fixtures.FEDERATED_METADATA_CERTIFICATE,
fixtures.FEDERATED_METADATA_WITH_VALID_UNTIL_ATTRIBUTE,
SAMLFederatedMetadataValidationError,
id="without_signature",
),
(
"with_invalid_signature",
pytest.param(
fixtures.FEDERATED_METADATA_CERTIFICATE.strip(),
fixtures.FEDERATED_METADATA_WITH_INVALID_SIGNATURE,
SAMLFederatedMetadataValidationError,
id="with_invalid_signature",
),
(
"with_valid_signature",
pytest.param(
fixtures.FEDERATED_METADATA_CERTIFICATE.strip(),
SamlFilesFixture.sample_text("incommon-metadata-idp-only.xml"),
None,
id="with_valid_signature",
),
],
)
def test_validate(self, _, certificate, metadata, expected_exception):
def test_validate(self, certificate, metadata, expected_exception):
# Arrange
validator = SAMLMetadataSignatureValidator()
federation = SAMLFederation(
Expand Down
Loading

0 comments on commit a18d4d8

Please sign in to comment.