diff --git a/benefits/oauth/views.py b/benefits/oauth/views.py index 8b633556e..dc0ccd2aa 100644 --- a/benefits/oauth/views.py +++ b/benefits/oauth/views.py @@ -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") @@ -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) diff --git a/tests/pytest/oauth/test_views.py b/tests/pytest/oauth/test_views.py index 767845490..af3c6b45d 100644 --- a/tests/pytest/oauth/test_views.py +++ b/tests/pytest/oauth/test_views.py @@ -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) @@ -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)