Skip to content

Commit 5ee3503

Browse files
authored
Merge pull request #4043 from bcgov/fix/prashanth-seeded-user-assoc-issues
Fix: issues related to seeded user association
2 parents ef4e1d8 + 4a44f10 commit 5ee3503

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

backend/lcfs/tests/user/test_user_repo.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,62 @@ async def test_bceid_role_removed_when_not_in_new_roles():
320320
names = {ur.role.name for ur in user.user_roles}
321321
assert RoleEnum.TRANSFER not in names
322322
assert RoleEnum.COMPLIANCE_REPORTING in names
323+
324+
325+
# ---------------------------------------------------------------------------
326+
# update_user organization guard
327+
# ---------------------------------------------------------------------------
328+
329+
330+
@pytest.mark.anyio
331+
async def test_update_user_allows_organization_update_for_test_user():
332+
repo = UserRepository(db=MagicMock())
333+
user = MagicMock(spec=UserProfile)
334+
user.keycloak_username = "lcfs12"
335+
user.organization_id = 4
336+
user.organization = None
337+
user.role_names = []
338+
user.user_roles = []
339+
340+
user_update = UserCreateSchema(
341+
title="Developer",
342+
keycloak_username="lcfs12",
343+
keycloak_email="lcfs12@example.com",
344+
email="lcfs12@example.com",
345+
first_name="Test",
346+
last_name="User",
347+
is_active=True,
348+
organization_id=9,
349+
roles=[],
350+
)
351+
352+
await repo.update_user(user, user_update)
353+
354+
assert user.organization_id == 9
355+
356+
357+
@pytest.mark.anyio
358+
async def test_update_user_blocks_organization_update_for_non_test_user():
359+
repo = UserRepository(db=MagicMock())
360+
user = MagicMock(spec=UserProfile)
361+
user.keycloak_username = "real.user"
362+
user.organization_id = 4
363+
user.organization = None
364+
user.role_names = []
365+
user.user_roles = []
366+
367+
user_update = UserCreateSchema(
368+
title="Developer",
369+
keycloak_username="real.user",
370+
keycloak_email="real.user@example.com",
371+
email="real.user@example.com",
372+
first_name="Real",
373+
last_name="User",
374+
is_active=True,
375+
organization_id=9,
376+
roles=[],
377+
)
378+
379+
await repo.update_user(user, user_update)
380+
381+
assert user.organization_id == 4

backend/lcfs/web/api/user/repo.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from lcfs.web.core.decorators import repo_handler
5656

5757
logger = structlog.get_logger(__name__)
58+
ORG_BOUND_TEST_USER_RE = re.compile(r"^(lcfs|tfs)\d+$", re.IGNORECASE)
5859

5960

6061
class UserRepository:
@@ -63,6 +64,11 @@ class UserRepository:
6364
def __init__(self, db: AsyncSession = Depends(get_async_db_session)):
6465
self.db = db
6566

67+
@staticmethod
68+
def is_test_user(user: UserProfile) -> bool:
69+
username = getattr(user, "keycloak_username", "") or ""
70+
return bool(ORG_BOUND_TEST_USER_RE.match(username))
71+
6672
def apply_filters(self, pagination, conditions, full_name):
6773
role_filter_present = False
6874
for filter in pagination.filters:
@@ -574,7 +580,8 @@ async def update_user(
574580
user.first_name = user_update.first_name
575581
user.last_name = user_update.last_name
576582
user.is_active = user_update.is_active
577-
user.organization_id = user_update.organization_id
583+
if self.is_test_user(user):
584+
user.organization_id = user_update.organization_id
578585
user.keycloak_email = user_update.keycloak_email
579586
user.keycloak_username = user_update.keycloak_username
580587
user.phone = user_update.phone

frontend/src/components/BCDataGrid/BCGridBase.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const BCGridBase = forwardRef(
4040
autoSizeStrategy,
4141
autoHeight,
4242
containerStyle,
43-
enableCellTextSelection,
43+
enableCellTextSelection=true,
4444
getRowId,
4545
overlayNoRowsTemplate,
4646
queryData,

frontend/src/routes/routeConfig/adminRoutes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const seededRoute: AppRouteObject[] = isNonProdEnvironment
1414
? [
1515
{
1616
path: ROUTES.ADMIN.SEEDED_USER_ASSOCIATION,
17-
element: <AdminMenu tabIndex={4} />,
17+
element: <AdminMenu tabIndex={5} />,
1818
handle: { title: 'Seeded user association' }
1919
}
2020
]

0 commit comments

Comments
 (0)