Skip to content

Commit

Permalink
Refactor: AuthProvider supports_sign_out (#1462)
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed Jun 29, 2023
2 parents ed0d818 + 6ed9405 commit ced3749
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 14 deletions.
1 change: 1 addition & 0 deletions benefits/core/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def authentication(request):
data = {
"required": verifier.is_auth_required,
"logged_in": session.logged_in(request),
"supports_sign_out": verifier.supports_sign_out,
"sign_out_route": reverse("oauth:logout"),
}

Expand Down
4 changes: 2 additions & 2 deletions benefits/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2 on 2023-06-22 18:44
# Generated by Django 4.2 on 2023-06-28 22:28

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -15,7 +15,7 @@ class Migration(migrations.Migration):
fields=[
("id", models.AutoField(primary_key=True, serialize=False)),
("sign_in_button_label", models.TextField()),
("sign_out_button_label", models.TextField()),
("sign_out_button_label", models.TextField(null=True)),
("client_name", models.TextField()),
("client_id", models.TextField()),
("authority", models.TextField()),
Expand Down
2 changes: 1 addition & 1 deletion benefits/core/migrations/0002_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def load_data(app, *args, **kwargs):

veteran_auth_provider = AuthProvider.objects.create(
sign_in_button_label=_("eligibility.buttons.veteran.signin"),
sign_out_button_label=_("eligibility.buttons.veteran.signout"),
sign_out_button_label=None,
client_name=os.environ.get("VETERAN_AUTH_PROVIDER_CLIENT_NAME", "veteran-benefits-oauth-client-name"),
client_id=os.environ.get("AUTH_PROVIDER_CLIENT_ID", "benefits-oauth-client-id"),
authority=os.environ.get("AUTH_PROVIDER_AUTHORITY", "https://example.com"),
Expand Down
6 changes: 5 additions & 1 deletion benefits/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AuthProvider(models.Model):

id = models.AutoField(primary_key=True)
sign_in_button_label = models.TextField()
sign_out_button_label = models.TextField()
sign_out_button_label = models.TextField(null=True)
client_name = models.TextField()
client_id = models.TextField()
authority = models.TextField()
Expand Down Expand Up @@ -153,6 +153,10 @@ def uses_auth_verification(self):
"""True if this Verifier verifies via the auth provider. False otherwise."""
return self.is_auth_required and self.auth_provider.scope and self.auth_provider.claim

@property
def supports_sign_out(self):
return bool(self.is_auth_required and self.auth_provider.sign_out_button_label)

@staticmethod
def by_id(id):
"""Get an EligibilityVerifier instance by its ID."""
Expand Down
2 changes: 1 addition & 1 deletion benefits/core/templates/core/includes/sign-out-link.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% load i18n %}

{% if authentication %}
{% if authentication.required and authentication.logged_in %}
{% if authentication.supports_sign_out and authentication.logged_in %}
<div class="w-100 position-absolute">
<div class="container">
<div class="row nav-button-row">
Expand Down
2 changes: 1 addition & 1 deletion benefits/enrollment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def success(request):

page = viewmodels.Page(title=_("enrollment.pages.success.title"), headline=_("enrollment.pages.success.headline"))

if verifier.is_auth_required and session.logged_in(request):
if verifier.supports_sign_out and session.logged_in(request):
# overwrite origin for a logged in user
# if they click the logout button, they are taken to the new route
session.update(request, origin=reverse(ROUTE_LOGGED_OUT))
Expand Down
5 changes: 1 addition & 4 deletions benefits/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: https://github.com/cal-itp/benefits/issues \n"
"POT-Creation-Date: 2023-06-28 22:08+0000\n"
"POT-Creation-Date: 2023-06-28 23:56+0000\n"
"Language: English\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
Expand All @@ -22,9 +22,6 @@ msgstr "Sign out of Login.gov"
msgid "eligibility.buttons.veteran.signin"
msgstr "Continue to VA.gov"

msgid "eligibility.buttons.veteran.signout"
msgstr "Sign out of VA.gov"

msgid "eligibility.pages.index.login_gov.label"
msgstr "I am 65 years of age or older"

Expand Down
5 changes: 1 addition & 4 deletions benefits/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: https://github.com/cal-itp/benefits/issues \n"
"POT-Creation-Date: 2023-06-28 22:08+0000\n"
"POT-Creation-Date: 2023-06-28 23:56+0000\n"
"Language: Español\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
Expand All @@ -22,9 +22,6 @@ msgstr "Cierre sesión de Login.gov"
msgid "eligibility.buttons.veteran.signin"
msgstr "TODO: Continue with VA.gov"

msgid "eligibility.buttons.veteran.signout"
msgstr "TODO: Cierre sesión de VA.gov"

msgid "eligibility.pages.index.login_gov.label"
msgstr "Tengo 65 años de edad o más"

Expand Down
20 changes: 20 additions & 0 deletions tests/pytest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ def model_AuthProvider_with_verification(model_AuthProvider):
return model_AuthProvider


@pytest.fixture
def model_AuthProvider_with_verification_no_sign_out(model_AuthProvider):
model_AuthProvider.scope = "scope"
model_AuthProvider.claim = "claim"
model_AuthProvider.sign_out_button_label = None
model_AuthProvider.save()

return model_AuthProvider


@pytest.fixture
def model_AuthProvider_without_verification(model_AuthProvider):
model_AuthProvider.scope = None
Expand All @@ -72,6 +82,16 @@ def model_AuthProvider_without_verification(model_AuthProvider):
return model_AuthProvider


@pytest.fixture
def model_AuthProvider_without_verification_no_sign_out(model_AuthProvider):
model_AuthProvider.scope = None
model_AuthProvider.claim = None
model_AuthProvider.sign_out_button_label = None
model_AuthProvider.save()

return model_AuthProvider


@pytest.fixture
def model_EligibilityType():
eligibility = EligibilityType.objects.create(name="test", label="Test Eligibility Type", group_id="1234")
Expand Down
24 changes: 24 additions & 0 deletions tests/pytest/core/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ def test_EligibilityVerifier_with_AuthProvider_with_verification(

assert model_EligibilityVerifier.is_auth_required
assert model_EligibilityVerifier.uses_auth_verification
assert model_EligibilityVerifier.supports_sign_out


@pytest.mark.django_db
def test_EligibilityVerifier_with_AuthProvider_with_verification_no_sign_out(
model_EligibilityVerifier, model_AuthProvider_with_verification_no_sign_out
):
model_EligibilityVerifier.auth_provider = model_AuthProvider_with_verification_no_sign_out

assert model_EligibilityVerifier.is_auth_required
assert model_EligibilityVerifier.uses_auth_verification
assert not model_EligibilityVerifier.supports_sign_out


@pytest.mark.django_db
Expand All @@ -124,6 +136,18 @@ def test_EligibilityVerifier_with_AuthProvider_without_verification(

assert model_EligibilityVerifier.is_auth_required
assert not model_EligibilityVerifier.uses_auth_verification
assert model_EligibilityVerifier.supports_sign_out


@pytest.mark.django_db
def test_EligibilityVerifier_with_AuthProvider_without_verification_no_sign_out(
model_EligibilityVerifier, model_AuthProvider_without_verification_no_sign_out
):
model_EligibilityVerifier.auth_provider = model_AuthProvider_without_verification_no_sign_out

assert model_EligibilityVerifier.is_auth_required
assert not model_EligibilityVerifier.uses_auth_verification
assert not model_EligibilityVerifier.supports_sign_out


@pytest.mark.django_db
Expand Down

0 comments on commit ced3749

Please sign in to comment.