Skip to content

Commit

Permalink
refactor(model/tests): update test fixtures and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lalver1 committed Aug 8, 2024
1 parent 339b5b6 commit 75a9037
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 108 deletions.
86 changes: 38 additions & 48 deletions tests/pytest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,7 @@ def model_ClaimsProvider():


@pytest.fixture
def model_ClaimsProvider_with_scope_and_claim(model_ClaimsProvider):
model_ClaimsProvider.scope = "scope"
model_ClaimsProvider.claim = "claim"
model_ClaimsProvider.save()

return model_ClaimsProvider


@pytest.fixture
def model_ClaimsProvider_with_scope_and_claim_no_sign_out(model_ClaimsProvider):
model_ClaimsProvider.scope = "scope"
model_ClaimsProvider.claim = "claim"
model_ClaimsProvider.sign_out_button_template = None
model_ClaimsProvider.sign_out_link_template = None
model_ClaimsProvider.save()

return model_ClaimsProvider


@pytest.fixture
def model_ClaimsProvider_no_scope_and_claim(model_ClaimsProvider):
model_ClaimsProvider.scope = None
model_ClaimsProvider.claim = None
model_ClaimsProvider.save()

return model_ClaimsProvider


@pytest.fixture
def model_ClaimsProvider_no_scope_and_claim_no_sign_out(model_ClaimsProvider):
model_ClaimsProvider.scope = None
model_ClaimsProvider.claim = None
def model_ClaimsProvider_no_sign_out(model_ClaimsProvider):
model_ClaimsProvider.sign_out_button_template = None
model_ClaimsProvider.sign_out_link_template = None
model_ClaimsProvider.save()
Expand Down Expand Up @@ -152,7 +121,7 @@ def model_EligibilityType_supports_expiration(model_EligibilityType):


@pytest.fixture
def model_EligibilityVerifier(model_PemData, model_EligibilityType):
def model_EligibilityVerifier(model_PemData, model_EligibilityType, model_ClaimsProvider):
verifier = EligibilityVerifier.objects.create(
name="Test Verifier",
active=True,
Expand All @@ -162,16 +131,25 @@ def model_EligibilityVerifier(model_PemData, model_EligibilityType):
eligibility_type=model_EligibilityType,
eligibility_api_public_key=model_PemData,
selection_label_template="eligibility/includes/selection-label.html",
claims_provider=model_ClaimsProvider,
_claims_scheme=None,
)

return verifier


@pytest.fixture
def model_EligibilityVerifier_ClaimsProvider_with_scope_and_claim(
model_ClaimsProvider_with_scope_and_claim, model_EligibilityVerifier
):
model_EligibilityVerifier.claims_provider = model_ClaimsProvider_with_scope_and_claim
def model_EligibilityVerifier_with_scope_and_claim(model_EligibilityVerifier):
model_EligibilityVerifier.claims_scope = "scope"
model_EligibilityVerifier.claims_claim = "claim"
model_EligibilityVerifier.save()

return model_EligibilityVerifier


@pytest.fixture
def model_EligibilityVerifier_with_claims_scheme(model_EligibilityVerifier):
model_EligibilityVerifier._claims_scheme = "scheme"
model_EligibilityVerifier.save()

return model_EligibilityVerifier
Expand Down Expand Up @@ -278,37 +256,49 @@ def mocked_session_verifier(mocker, model_EligibilityVerifier):


@pytest.fixture
def mocked_session_verifier_oauth(mocker, model_EligibilityVerifier_ClaimsProvider_with_scope_and_claim):
def mocked_session_verifier_oauth(mocker, model_EligibilityVerifier_with_scope_and_claim):
return mocker.patch(
"benefits.core.session.verifier",
autospec=True,
return_value=model_EligibilityVerifier_with_scope_and_claim,
)


@pytest.fixture
def mocked_session_verifier_no_scope_and_claim_oauth(mocker, model_EligibilityVerifier):
return mocker.patch(
"benefits.core.session.verifier",
autospec=True,
return_value=model_EligibilityVerifier_ClaimsProvider_with_scope_and_claim,
return_value=model_EligibilityVerifier,
)


@pytest.fixture
def mocked_session_verifier_uses_claims_verification(
model_EligibilityVerifier_ClaimsProvider_with_scope_and_claim, mocked_session_verifier_oauth
model_EligibilityVerifier_with_scope_and_claim, mocked_session_verifier_oauth
):
mock_verifier = model_EligibilityVerifier_ClaimsProvider_with_scope_and_claim
mock_verifier.name = model_EligibilityVerifier_ClaimsProvider_with_scope_and_claim.name
mock_verifier = model_EligibilityVerifier_with_scope_and_claim
mock_verifier.name = model_EligibilityVerifier_with_scope_and_claim.name
mock_verifier.claims_provider.sign_out_button_template = (
model_EligibilityVerifier_ClaimsProvider_with_scope_and_claim.claims_provider.sign_out_button_template
model_EligibilityVerifier_with_scope_and_claim.claims_provider.sign_out_button_template
)
mock_verifier.claims_provider.sign_out_link_template = (
model_EligibilityVerifier_ClaimsProvider_with_scope_and_claim.claims_provider.sign_out_link_template
model_EligibilityVerifier_with_scope_and_claim.claims_provider.sign_out_link_template
)
mocked_session_verifier_oauth.return_value = mock_verifier
return mocked_session_verifier_oauth


@pytest.fixture
def mocked_session_verifier_does_not_use_claims_verification(
mocked_session_verifier_uses_claims_verification, model_ClaimsProvider_no_scope_and_claim
model_EligibilityVerifier, mocked_session_verifier_no_scope_and_claim_oauth
):
mocked_verifier = mocked_session_verifier_uses_claims_verification
mocked_verifier.claims_provider = model_ClaimsProvider_no_scope_and_claim
return mocked_verifier
mock_verifier = model_EligibilityVerifier
mock_verifier.name = model_EligibilityVerifier.name
mock_verifier.claims_provider.sign_out_button_template = model_EligibilityVerifier.claims_provider.sign_out_button_template
mock_verifier.claims_provider.sign_out_link_template = model_EligibilityVerifier.claims_provider.sign_out_link_template
mocked_session_verifier_no_scope_and_claim_oauth.return_value = mock_verifier
return mocked_session_verifier_no_scope_and_claim_oauth


@pytest.fixture
Expand Down
58 changes: 21 additions & 37 deletions tests/pytest/core/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def test_PemData_data_text_secret_name_and_remote__uses_remote(

@pytest.mark.django_db
def test_model_ClaimsProvider(model_ClaimsProvider):
assert not model_ClaimsProvider.supports_claims_verification
assert model_ClaimsProvider.supports_sign_out
assert str(model_ClaimsProvider) == model_ClaimsProvider.client_name

Expand All @@ -108,25 +107,8 @@ def test_model_ClaimsProvider_client_id(model_ClaimsProvider, mock_models_get_se


@pytest.mark.django_db
def test_model_ClaimsProvider_with_scope_and_claim(model_ClaimsProvider_with_scope_and_claim):
assert model_ClaimsProvider_with_scope_and_claim.supports_claims_verification


@pytest.mark.django_db
def test_model_ClaimsProvider_with_scope_and_claim_no_sign_out(model_ClaimsProvider_with_scope_and_claim_no_sign_out):
assert model_ClaimsProvider_with_scope_and_claim_no_sign_out.supports_claims_verification
assert not model_ClaimsProvider_with_scope_and_claim_no_sign_out.supports_sign_out


@pytest.mark.django_db
def test_model_ClaimsProvider_no_scope_and_claim(model_ClaimsProvider_no_scope_and_claim):
assert not model_ClaimsProvider_no_scope_and_claim.supports_claims_verification


@pytest.mark.django_db
def test_model_ClaimsProvider_no_scope_and_claim_no_sign_out(model_ClaimsProvider_no_scope_and_claim_no_sign_out):
assert not model_ClaimsProvider_no_scope_and_claim_no_sign_out.supports_claims_verification
assert not model_ClaimsProvider_no_scope_and_claim_no_sign_out.supports_sign_out
def test_model_ClaimsProvider_no_sign_out(model_ClaimsProvider_no_sign_out):
assert not model_ClaimsProvider_no_sign_out.supports_sign_out


@pytest.mark.django_db
Expand Down Expand Up @@ -297,37 +279,29 @@ def test_EligibilityVerifier_by_id_nonmatching():


@pytest.mark.django_db
def test_EligibilityVerifier_with_ClaimsProvider_with_scope_and_claim(
model_EligibilityVerifier, model_ClaimsProvider_with_scope_and_claim
):
model_EligibilityVerifier.claims_provider = model_ClaimsProvider_with_scope_and_claim
def test_EligibilityVerifier_with_scope_and_claim(model_EligibilityVerifier_with_scope_and_claim):

assert model_EligibilityVerifier.uses_claims_verification
assert model_EligibilityVerifier_with_scope_and_claim.uses_claims_verification


@pytest.mark.django_db
def test_EligibilityVerifier_with_ClaimsProvider_with_scope_and_claim_no_sign_out(
model_EligibilityVerifier, model_ClaimsProvider_with_scope_and_claim_no_sign_out
def test_EligibilityVerifier_with_scope_and_claim_no_sign_out(
model_EligibilityVerifier_with_scope_and_claim, model_ClaimsProvider_no_sign_out
):
model_EligibilityVerifier.claims_provider = model_ClaimsProvider_with_scope_and_claim_no_sign_out
model_EligibilityVerifier_with_scope_and_claim.claims_provider = model_ClaimsProvider_no_sign_out

assert model_EligibilityVerifier.uses_claims_verification
assert model_EligibilityVerifier_with_scope_and_claim.uses_claims_verification


@pytest.mark.django_db
def test_EligibilityVerifier_with_ClaimsProvider_no_scope_and_claim(
model_EligibilityVerifier, model_ClaimsProvider_no_scope_and_claim
):
model_EligibilityVerifier.claims_provider = model_ClaimsProvider_no_scope_and_claim
def test_EligibilityVerifier_no_scope_and_claim(model_EligibilityVerifier):

assert not model_EligibilityVerifier.uses_claims_verification


@pytest.mark.django_db
def test_EligibilityVerifier_with_ClaimsProvider_no_scope_and_claim_no_sign_out(
model_EligibilityVerifier, model_ClaimsProvider_no_scope_and_claim_no_sign_out
):
model_EligibilityVerifier.claims_provider = model_ClaimsProvider_no_scope_and_claim_no_sign_out
def test_EligibilityVerifier_no_scope_and_claim_no_sign_out(model_EligibilityVerifier, model_ClaimsProvider_no_sign_out):
model_EligibilityVerifier.claims_provider = model_ClaimsProvider_no_sign_out

assert not model_EligibilityVerifier.uses_claims_verification

Expand All @@ -347,6 +321,16 @@ def test_EligiblityVerifier_eligibility_api_auth_key(model_EligibilityVerifier,
assert secret_value == mock_models_get_secret_by_name.return_value


@pytest.mark.django_db
def test_EligibilityVerifier_no_claims_scheme(model_EligibilityVerifier):
assert model_EligibilityVerifier.claims_scheme == model_EligibilityVerifier.claims_provider.scheme


@pytest.mark.django_db
def test_EligibilityVerifier_with_claims_scheme(model_EligibilityVerifier_with_claims_scheme):
assert model_EligibilityVerifier_with_claims_scheme.claims_scheme == "scheme"


@pytest.mark.django_db
def test_TransitProcessor_str(model_TransitProcessor):
assert str(model_TransitProcessor) == model_TransitProcessor.name
Expand Down
2 changes: 1 addition & 1 deletion tests/pytest/eligibility/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_eligibility_from_oauth_claim_match(
# mocked_session_verifier_uses_claims_verification is Mocked version of the session.verifier() function
# call it (with a None request) to return a verifier object
verifier = mocked_session_verifier_uses_claims_verification.return_value
verifier.claims_provider.claim = "claim"
verifier.claims_claim = "claim"
verifier.eligibility_type = model_EligibilityType

types = eligibility_from_oauth(verifier, "claim", model_TransitAgency)
Expand Down
20 changes: 10 additions & 10 deletions tests/pytest/oauth/test_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from benefits.core.models import ClaimsProvider
from benefits.core.models import EligibilityVerifier
from benefits.oauth.client import _client_kwargs, _server_metadata_url, _authorize_params, _register_provider, create_client


Expand Down Expand Up @@ -40,9 +40,9 @@ def test_authorize_params_no_scheme():

@pytest.mark.django_db
def test_register_provider(mocker, mocked_oauth_registry):
mocked_client_provider = mocker.Mock(spec=ClaimsProvider)
mocked_client_provider.client_name = "client_name_1"
mocked_client_provider.client_id = "client_id_1"
mocked_client_provider = mocker.Mock(spec=EligibilityVerifier)
mocked_client_provider.claims_provider.client_name = "client_name_1"
mocked_client_provider.claims_provider.client_id = "client_id_1"

mocker.patch("benefits.oauth.client._client_kwargs", return_value={"client": "kwargs"})
mocker.patch("benefits.oauth.client._server_metadata_url", return_value="https://metadata.url")
Expand All @@ -61,9 +61,9 @@ def test_register_provider(mocker, mocked_oauth_registry):

@pytest.mark.django_db
def test_create_client_already_registered(mocker, mocked_oauth_registry):
mocked_client_provider = mocker.Mock(spec=ClaimsProvider)
mocked_client_provider.client_name = "client_name_1"
mocked_client_provider.client_id = "client_id_1"
mocked_client_provider = mocker.Mock(spec=EligibilityVerifier)
mocked_client_provider.claims_provider.client_name = "client_name_1"
mocked_client_provider.claims_provider.client_id = "client_id_1"

create_client(mocked_oauth_registry, mocked_client_provider)

Expand All @@ -73,9 +73,9 @@ def test_create_client_already_registered(mocker, mocked_oauth_registry):

@pytest.mark.django_db
def test_create_client_already_not_registered_yet(mocker, mocked_oauth_registry):
mocked_client_provider = mocker.Mock(spec=ClaimsProvider)
mocked_client_provider.client_name = "client_name_1"
mocked_client_provider.client_id = "client_id_1"
mocked_client_provider = mocker.Mock(spec=EligibilityVerifier)
mocked_client_provider.claims_provider.client_name = "client_name_1"
mocked_client_provider.claims_provider.client_id = "client_id_1"

mocker.patch("benefits.oauth.client._client_kwargs", return_value={"client": "kwargs"})
mocker.patch("benefits.oauth.client._server_metadata_url", return_value="https://metadata.url")
Expand Down
24 changes: 12 additions & 12 deletions tests/pytest/oauth/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ def mocked_oauth_client_or_error_redirect__error(mocked_oauth_create_client):
@pytest.mark.usefixtures("mocked_session_verifier_uses_claims_verification")
def test_oauth_client_or_error_redirect_no_oauth_client(
app_request,
model_ClaimsProvider_with_scope_and_claim,
model_EligibilityVerifier_with_scope_and_claim,
mocked_oauth_create_client,
mocked_analytics_module,
mocked_sentry_sdk_module,
):
mocked_oauth_create_client.return_value = None

result = _oauth_client_or_error_redirect(app_request, model_ClaimsProvider_with_scope_and_claim)
result = _oauth_client_or_error_redirect(app_request, model_EligibilityVerifier_with_scope_and_claim)

assert result.status_code == 302
assert result.url == reverse(ROUTE_SYSTEM_ERROR)
Expand All @@ -70,9 +70,9 @@ def test_oauth_client_or_error_redirect_no_oauth_client(
@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_verifier_uses_claims_verification", "mocked_oauth_client_or_error_redirect__error")
def test_oauth_client_or_error_redirect_oauth_client_exception(
app_request, model_ClaimsProvider_with_scope_and_claim, mocked_analytics_module, mocked_sentry_sdk_module
app_request, model_EligibilityVerifier_with_scope_and_claim, mocked_analytics_module, mocked_sentry_sdk_module
):
result = _oauth_client_or_error_redirect(app_request, model_ClaimsProvider_with_scope_and_claim)
result = _oauth_client_or_error_redirect(app_request, model_EligibilityVerifier_with_scope_and_claim)

assert result.status_code == 302
assert result.url == reverse(ROUTE_SYSTEM_ERROR)
Expand All @@ -83,9 +83,9 @@ def test_oauth_client_or_error_redirect_oauth_client_exception(
@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_verifier_uses_claims_verification", "mocked_oauth_create_client")
def test_oauth_client_or_error_oauth_client(
app_request, model_ClaimsProvider_with_scope_and_claim, mocked_analytics_module, mocked_sentry_sdk_module
app_request, model_EligibilityVerifier_with_scope_and_claim, mocked_analytics_module, mocked_sentry_sdk_module
):
result = _oauth_client_or_error_redirect(app_request, model_ClaimsProvider_with_scope_and_claim)
result = _oauth_client_or_error_redirect(app_request, model_EligibilityVerifier_with_scope_and_claim)

assert hasattr(result, "authorize_redirect")
mocked_analytics_module.error.assert_not_called()
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_authorize_success_with_claim_true(
app_request, mocked_session_verifier_uses_claims_verification, mocked_oauth_client_or_error_redirect__client
):
verifier = mocked_session_verifier_uses_claims_verification.return_value
verifier.claims_provider.claim = "claim"
verifier.claims_claim = "claim"
mocked_oauth_client = mocked_oauth_client_or_error_redirect__client.return_value
mocked_oauth_client.authorize_access_token.return_value = {"id_token": "token", "userinfo": {"claim": "1"}}

Expand All @@ -257,7 +257,7 @@ def test_authorize_success_with_claim_false(
app_request, mocked_session_verifier_uses_claims_verification, mocked_oauth_client_or_error_redirect__client
):
verifier = mocked_session_verifier_uses_claims_verification.return_value
verifier.claims_provider.claim = "claim"
verifier.claims_claim = "claim"
mocked_oauth_client = mocked_oauth_client_or_error_redirect__client.return_value
mocked_oauth_client.authorize_access_token.return_value = {"id_token": "token", "userinfo": {"claim": "0"}}

Expand All @@ -277,7 +277,7 @@ def test_authorize_success_with_claim_error(
mocked_analytics_module,
):
verifier = mocked_session_verifier_uses_claims_verification.return_value
verifier.claims_provider.claim = "claim"
verifier.claims_claim = "claim"
mocked_oauth_client = mocked_oauth_client_or_error_redirect__client.return_value
mocked_oauth_client.authorize_access_token.return_value = {"id_token": "token", "userinfo": {"claim": "10"}}

Expand All @@ -296,9 +296,9 @@ def test_authorize_success_without_verifier_claim(
app_request, mocked_session_verifier_uses_claims_verification, mocked_oauth_client_or_error_redirect__client
):
verifier = mocked_session_verifier_uses_claims_verification.return_value
verifier.claims_provider.claim = ""
verifier.claims_claim = ""
mocked_oauth_client = mocked_oauth_client_or_error_redirect__client.return_value
mocked_oauth_client.authorize_access_token.return_value = {"id_token": "token", "userinfo": {"claim": "True"}}
mocked_oauth_client.authorize_access_token.return_value = {"id_token": "token", "userinfo": {"claim": "1"}}

result = authorize(app_request)

Expand All @@ -323,7 +323,7 @@ def test_authorize_success_without_claim_in_response(
access_token_response,
):
verifier = mocked_session_verifier_uses_claims_verification.return_value
verifier.claims_provider.claim = "claim"
verifier.claims_claim = "claim"
mocked_oauth_client = mocked_oauth_client_or_error_redirect__client.return_value
mocked_oauth_client.authorize_access_token.return_value = access_token_response

Expand Down

0 comments on commit 75a9037

Please sign in to comment.