Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: transit agency user group #2307

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions benefits/core/migrations/0023_transitagency_staff_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.0.7 on 2024-08-19 15:04

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", "0022_refactor_EligibilityType_EnrollmentFlow"),
]

operations = [
migrations.AddField(
model_name="transitagency",
name="staff_group",
field=models.OneToOneField(
blank=True,
default=None,
help_text="The group of users who can access this TransitAgency.",
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="auth.group",
),
),
]
18 changes: 18 additions & 0 deletions benefits/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from django.conf import settings
from django.core.exceptions import ValidationError
from django.contrib.auth.models import Group, User
from django.db import models
from django.urls import reverse

Expand Down Expand Up @@ -339,6 +340,14 @@ class TransitAgency(models.Model):
help_text="The name of the secret containing this agency's client_secret value used to access the TransitProcessor's API.", # noqa: E501
default="",
)
staff_group = models.OneToOneField(
Group,
on_delete=models.PROTECT,
null=True,
blank=True,
default=None,
help_text="The group of users who can access this TransitAgency.",
)

def __str__(self):
return self.long_name
Expand Down Expand Up @@ -389,3 +398,12 @@ def all_active():
"""Get all TransitAgency instances marked active."""
logger.debug(f"Get all active {TransitAgency.__name__}")
return TransitAgency.objects.filter(active=True)

@staticmethod
def for_user(user: User):
group = user.groups.first()

if group is not None:
return group.transitagency
else:
return None
30 changes: 30 additions & 0 deletions tests/pytest/core/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.core.exceptions import ValidationError

import pytest
Expand Down Expand Up @@ -330,3 +331,32 @@ def test_TransitAgency_all_active(model_TransitAgency):
assert len(result) > 0
assert model_TransitAgency in result
assert inactive_agency not in result


@pytest.mark.django_db
def test_TransitAgency_for_user_in_group(model_TransitAgency):
group = Group.objects.create(name="test_group")

agency_for_user = TransitAgency.by_id(model_TransitAgency.id)
agency_for_user.pk = None
agency_for_user.staff_group = group
agency_for_user.save()

user = User.objects.create_user(username="test_user", email="[email protected]", password="test", is_staff=True)
user.groups.add(group)

assert TransitAgency.for_user(user) == agency_for_user


@pytest.mark.django_db
def test_TransitAgency_for_user_not_in_group(model_TransitAgency):
group = Group.objects.create(name="test_group")

agency_for_user = TransitAgency.by_id(model_TransitAgency.id)
agency_for_user.pk = None
agency_for_user.staff_group = group
agency_for_user.save()

user = User.objects.create_user(username="test_user", email="[email protected]", password="test", is_staff=True)

assert TransitAgency.for_user(user) is None
Loading