Skip to content

Commit

Permalink
feat(analytics): implement error codes for multiple returned claims
Browse files Browse the repository at this point in the history
  • Loading branch information
lalver1 committed Oct 30, 2024
1 parent 280684c commit 43492c6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
7 changes: 3 additions & 4 deletions benefits/oauth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def authorize(request):
flow_claims = flow.claims_all_claims
stored_claims = []

error_claim = None
error_claim = {}

if flow_claims:
userinfo = token.get("userinfo")
Expand All @@ -141,9 +141,8 @@ def authorize(request):
elif claim_value == 1:
# if userinfo contains our claim and the flag is 1 (true), store the *claim*
stored_claims.append(claim)
elif claim_value >= 10 and claim == flow.claims_eligibility_claim:
# error_claim is only set if claim is the eligibility claim
error_claim = claim_value
elif claim_value >= 10:
error_claim[claim] = claim_value

session.update(request, oauth_token=id_token, oauth_claims=stored_claims)
analytics.finished_sign_in(request, error=error_claim)
Expand Down
59 changes: 49 additions & 10 deletions tests/pytest/oauth/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,31 +237,60 @@ def test_authorize_success(

@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_analytics_module")
@pytest.mark.parametrize(
"extra_claims,userinfo,oauth_claims",
[
(None, {"claim": 1}, ["claim"]),
("extra_claim", {"claim": 1, "extra_claim": 1}, ["claim", "extra_claim"]),
(
"extra_claim_1 extra_claim_2",
{"claim": 1, "extra_claim_1": 1, "extra_claim_2": 1},
["claim", "extra_claim_1", "extra_claim_2"],
),
],
)
def test_authorize_success_with_claim_true(
app_request, mocked_session_flow_uses_claims_verification, mocked_oauth_client_or_error_redirect__client
app_request,
mocked_session_flow_uses_claims_verification,
mocked_oauth_client_or_error_redirect__client,
extra_claims,
userinfo,
oauth_claims,
):
flow = mocked_session_flow_uses_claims_verification.return_value
flow.claims_extra_claims = ""
flow.claims_extra_claims = extra_claims
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"}}
mocked_oauth_client.authorize_access_token.return_value = {"id_token": "token", "userinfo": userinfo}

result = authorize(app_request)

mocked_oauth_client.authorize_access_token.assert_called_with(app_request)
assert session.oauth_claims(app_request) == ["claim"]
assert session.oauth_claims(app_request) == oauth_claims
assert result.status_code == 302
assert result.url == reverse(routes.ELIGIBILITY_CONFIRM)


@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_analytics_module")
@pytest.mark.parametrize(
"extra_claims,userinfo",
[
(None, {"claim": 0}),
("extra_claim", {"claim": 0, "extra_claim": 0}),
("extra_claim_1 extra_claim_2", {"claim": 0, "extra_claim_1": 0, "extra_claim_2": 0}),
],
)
def test_authorize_success_with_claim_false(
app_request, mocked_session_flow_uses_claims_verification, mocked_oauth_client_or_error_redirect__client
app_request,
mocked_session_flow_uses_claims_verification,
mocked_oauth_client_or_error_redirect__client,
extra_claims,
userinfo,
):
flow = mocked_session_flow_uses_claims_verification.return_value
flow.claims_extra_claims = ""
flow.claims_extra_claims = extra_claims
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"}}
mocked_oauth_client.authorize_access_token.return_value = {"id_token": "token", "userinfo": userinfo}

result = authorize(app_request)

Expand All @@ -272,21 +301,31 @@ def test_authorize_success_with_claim_false(


@pytest.mark.django_db
@pytest.mark.parametrize(
"extra_claims,userinfo",
[
(None, {"claim": 10}),
("extra_claim", {"claim": 10, "extra_claim": 10}),
("extra_claim_1 extra_claim_2", {"claim": 10, "extra_claim_1": 10, "extra_claim_2": 10}),
],
)
def test_authorize_success_with_claim_error(
app_request,
mocked_session_flow_uses_claims_verification,
mocked_oauth_client_or_error_redirect__client,
mocked_analytics_module,
extra_claims,
userinfo,
):
flow = mocked_session_flow_uses_claims_verification.return_value
flow.claims_extra_claims = ""
flow.claims_extra_claims = extra_claims
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"}}
mocked_oauth_client.authorize_access_token.return_value = {"id_token": "token", "userinfo": userinfo}

result = authorize(app_request)

mocked_oauth_client.authorize_access_token.assert_called_with(app_request)
mocked_analytics_module.finished_sign_in.assert_called_with(app_request, error=10)
mocked_analytics_module.finished_sign_in.assert_called_with(app_request, error=userinfo)
assert session.oauth_claims(app_request) == []
assert result.status_code == 302
assert result.url == reverse(routes.ELIGIBILITY_CONFIRM)
Expand Down

0 comments on commit 43492c6

Please sign in to comment.