-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: automatically associate user to agency (#2325)
- Loading branch information
Showing
7 changed files
with
152 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
benefits/core/migrations/0024_transitagency_sso_domain_customer_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Generated by Django 5.0.7 on 2024-08-27 16:02 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("auth", "0012_alter_user_first_name_max_length"), | ||
("core", "0023_transitagency_staff_group"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="transitagency", | ||
name="sso_domain", | ||
field=models.TextField( | ||
blank=True, | ||
default="", | ||
help_text="The email domain of users to automatically add to this agency's staff group upon login.", | ||
null=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="transitagency", | ||
name="staff_group", | ||
field=models.OneToOneField( | ||
blank=True, | ||
default=None, | ||
help_text="The group of users associated with this TransitAgency.", | ||
null=True, | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="transit_agency", | ||
to="auth.group", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="transitagency", | ||
name="customer_service_group", | ||
field=models.OneToOneField( | ||
blank=True, | ||
default=None, | ||
help_text="The group of users who are allowed to do in-person eligibility verification and enrollment.", | ||
null=True, | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="+", | ||
to="auth.group", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import pytest | ||
from django.conf import settings | ||
from django.contrib.auth.models import User, Group | ||
|
||
import benefits.core.admin | ||
from benefits.core.admin import GOOGLE_USER_INFO_URL, pre_login_user | ||
|
||
|
@@ -94,3 +95,27 @@ def test_pre_login_user_does_not_add_transit_staff_to_group(mocker, settings): | |
staff_group = Group.objects.get(name=settings.STAFF_GROUP_NAME) | ||
assert staff_group.user_set.count() == 0 | ||
assert agency_user.groups.count() == 0 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_pre_login_user_add_transit_staff_to_transit_staff_group(mocker, settings, model_TransitAgency): | ||
mocked_request = mocker.Mock() | ||
mocked_request.session.get.return_value = None | ||
|
||
transit_agency_staff_group = Group.objects.create(name="CST Staff") | ||
model_TransitAgency.pk = None | ||
model_TransitAgency.staff_group = transit_agency_staff_group | ||
model_TransitAgency.sso_domain = "cst.org" | ||
model_TransitAgency.save() | ||
|
||
settings.GOOGLE_SSO_STAFF_LIST = ["*"] | ||
settings.GOOGLE_SSO_ALLOWABLE_DOMAINS = ["cst.org"] | ||
|
||
# simulate what `django_google_sso` does for us (sets is_staff to True) | ||
agency_user = User.objects.create_user(username="agency_user", email="[email protected]", is_staff=True) | ||
|
||
pre_login_user(agency_user, mocked_request) | ||
|
||
# assert that a transit agency user gets added to their TransitAgency's staff group based on SSO domain | ||
assert agency_user.groups.count() == 1 | ||
assert agency_user.groups.first() == transit_agency_staff_group |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters