Skip to content

Commit 08153ba

Browse files
authored
26806 - Fix AUTH-API concurrency bug in unit tests (#3330)
1 parent 40c071d commit 08153ba

14 files changed

+60
-60
lines changed

auth-api/tests/unit/api/test_activity_log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_fetch_activity_log_masking(client, jwt, session): # pylint:disable=unu
8383

8484
factory_activity_log_model(actor=user.id, action=ActivityAction.CREATE_AFFILIATION.value, org_id=org.id)
8585

86-
user_with_token = TestUserInfo.user_staff_admin
86+
user_with_token = dict(TestUserInfo.user_staff_admin)
8787
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
8888
staff_user = factory_user_model(TestUserInfo.user_staff_admin)
8989
factory_activity_log_model(actor=staff_user.id, action=ActivityAction.REMOVE_AFFILIATION.value, org_id=org.id)

auth-api/tests/unit/api/test_task.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def test_put_task_org(client, jwt, session, keycloak_mock, monkeypatch): # pyli
161161
# 4. Create Org
162162
# 5. Update the created task and the relationship
163163
monkeypatch.setattr("auth_api.utils.user_context._get_token_info", lambda: TestJwtClaims.public_bceid_user)
164-
user_with_token = TestUserInfo.user_staff_admin
164+
user_with_token = dict(TestUserInfo.user_staff_admin)
165165
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
166166
user_with_token["idp_userid"] = TestJwtClaims.public_user_role["idp_userid"]
167167
user = factory_user_model_with_contact(user_with_token)
@@ -217,7 +217,7 @@ def test_put_task_org_on_hold(client, jwt, session, keycloak_mock, monkeypatch):
217217
# 4. Create Org
218218
# 5. Update the created task and the relationship
219219
monkeypatch.setattr("auth_api.utils.user_context._get_token_info", lambda: TestJwtClaims.public_bceid_user)
220-
user_with_token = TestUserInfo.user_bceid_tester
220+
user_with_token = dict(TestUserInfo.user_bceid_tester)
221221
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
222222
user_with_token["idp_userid"] = TestJwtClaims.public_user_role["idp_userid"]
223223
user = factory_user_model_with_contact(user_with_token)
@@ -275,7 +275,7 @@ def test_put_task_product(client, jwt, session, keycloak_mock, monkeypatch): #
275275

276276
# Post user, org and product subscription
277277
headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.staff_admin_role)
278-
user_with_token = TestUserInfo.user_staff_admin
278+
user_with_token = dict(TestUserInfo.user_staff_admin)
279279
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
280280
user_with_token["idp_userid"] = TestJwtClaims.public_user_role["idp_userid"]
281281
user = factory_user_model_with_contact(user_with_token)

auth-api/tests/unit/api/test_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_add_user_staff_org(client, jwt, session, keycloak_mock, monkeypatch):
110110

111111
def test_delete_bcros_valdiations(client, jwt, session, keycloak_mock, monkeypatch):
112112
"""Assert different conditions of user deletion."""
113-
admin_user = TestUserInfo.user_bcros_active
113+
admin_user = dict(TestUserInfo.user_bcros_active)
114114
org = factory_org_model(org_info=TestOrgInfo.org_anonymous)
115115
user = factory_user_model(user_info=TestUserInfo.user_bcros_active)
116116
factory_membership_model(user.id, org.id)

auth-api/tests/unit/api/test_user_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_get_user_settings(client, jwt, session, keycloak_mock, monkeypatch): #
6262
assert schema_utils.validate(item_list, "user_settings_response")[0]
6363
assert account["productSettings"] == f'/account/{account["id"]}/restricted-product'
6464

65-
kc_id_no_user = TestUserInfo.user1.get("keycloak_guid")
65+
kc_id_no_user = dict(TestUserInfo.user1).get("keycloak_guid")
6666
claims = copy.deepcopy(TestJwtClaims.updated_test.value)
6767
claims["sub"] = str(kc_id_no_user)
6868
patch_token_info(claims, monkeypatch)

auth-api/tests/unit/services/test_affiliation_invitation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def test_accept_affiliation_invitation(
350350
"""Accept the affiliation invitation and add the affiliation from the invitation."""
351351
with patch.object(AffiliationInvitationService, "send_affiliation_invitation", return_value=None):
352352
with patch.object(auth, "check_auth", return_value=True):
353-
user_with_token = TestUserInfo.user_test
353+
user_with_token = dict(TestUserInfo.user_test)
354354
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
355355
user_with_token["idp_userid"] = TestJwtClaims.public_user_role["idp_userid"]
356356
user = factory_user_model(user_with_token)
@@ -364,7 +364,7 @@ def test_accept_affiliation_invitation(
364364
business_identifier=entity_dictionary["business_identifier"],
365365
)
366366

367-
user_with_token_invitee = TestUserInfo.user1
367+
user_with_token_invitee = dict(TestUserInfo.user1)
368368
user_with_token_invitee["keycloak_guid"] = TestJwtClaims.edit_role_2["sub"]
369369
user_invitee = factory_user_model(user_with_token_invitee)
370370

@@ -447,7 +447,7 @@ def test_get_invitations_by_from_org_id(
447447
"""Find an existing invitation with the provided from org id."""
448448
with patch.object(AffiliationInvitationService, "send_affiliation_invitation", return_value=None):
449449
patch_token_info(TestJwtClaims.public_user_role, monkeypatch)
450-
user_with_token = TestUserInfo.user_test
450+
user_with_token = dict(TestUserInfo.user_test)
451451
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
452452
user = factory_user_model(user_with_token)
453453
patch_token_info({"sub": user.keycloak_guid, "idp_userid": user.idp_userid}, monkeypatch)
@@ -478,7 +478,7 @@ def test_get_invitations_by_to_org_id(
478478
"""Find an existing invitation with the provided to org id."""
479479
with patch.object(AffiliationInvitationService, "send_affiliation_invitation", return_value=None):
480480
patch_token_info(TestJwtClaims.public_user_role, monkeypatch)
481-
user_with_token = TestUserInfo.user_test
481+
user_with_token = dict(TestUserInfo.user_test)
482482
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
483483
user = factory_user_model(user_with_token)
484484
patch_token_info({"sub": user.keycloak_guid, "idp_userid": user.idp_userid}, monkeypatch)

auth-api/tests/unit/services/test_entity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_update_entity_existing_success(session, monkeypatch): # pylint:disable
106106
"name": TestEntityInfo.bc_entity_passcode4["name"],
107107
"corpTypeCode": TestEntityInfo.bc_entity_passcode4["corpTypeCode"],
108108
}
109-
user_with_token = TestUserInfo.user_test
109+
user_with_token = dict(TestUserInfo.user_test)
110110
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
111111

112112
patch_token_info({"loginSource": "", "realm_access": {"roles": ["system"]}, "corp_type": "BC"}, monkeypatch)
@@ -138,7 +138,7 @@ def test_update_entity_existing_failures(session, monkeypatch): # pylint:disabl
138138
"name": TestEntityInfo.bc_entity_passcode4["name"],
139139
"corpTypeCode": TestEntityInfo.bc_entity_passcode4["corpTypeCode"],
140140
}
141-
user_with_token = TestUserInfo.user_test
141+
user_with_token = dict(TestUserInfo.user_test)
142142
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
143143

144144
with pytest.raises(BusinessException) as exception:

auth-api/tests/unit/services/test_invitation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test_accept_invitation(session, auth_mock, keycloak_mock, monkeypatch): # p
216216
with patch.object(InvitationService, "send_invitation", return_value=None):
217217
with patch.object(auth, "check_auth", return_value=True):
218218
with patch.object(InvitationService, "notify_admin", return_value=None):
219-
user_with_token = TestUserInfo.user_test
219+
user_with_token = dict(TestUserInfo.user_test)
220220
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
221221
user_with_token["idp_userid"] = TestJwtClaims.public_user_role["idp_userid"]
222222
user = factory_user_model(user_with_token)
@@ -225,7 +225,7 @@ def test_accept_invitation(session, auth_mock, keycloak_mock, monkeypatch): # p
225225
org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
226226
org_dictionary = org.as_dict()
227227
invitation_info = factory_invitation(org_dictionary["id"])
228-
user_with_token_invitee = TestUserInfo.user1
228+
user_with_token_invitee = dict(TestUserInfo.user1)
229229
user_with_token_invitee["keycloak_guid"] = TestJwtClaims.edit_role_2["sub"]
230230
user_invitee = factory_user_model(user_with_token_invitee)
231231
new_invitation = InvitationService.create_invitation(invitation_info, User(user_invitee), "")
@@ -243,7 +243,7 @@ def test_accept_invitation_for_govm(session, auth_mock, keycloak_mock, monkeypat
243243
with patch.object(InvitationService, "send_invitation", return_value=None):
244244
with patch.object(auth, "check_auth", return_value=True):
245245
with patch.object(InvitationService, "notify_admin", return_value=None):
246-
user_with_token = TestUserInfo.user_staff_admin
246+
user_with_token = dict(TestUserInfo.user_staff_admin)
247247
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
248248
user = factory_user_model(user_with_token)
249249

@@ -252,7 +252,7 @@ def test_accept_invitation_for_govm(session, auth_mock, keycloak_mock, monkeypat
252252
org = OrgService.create_org(TestOrgInfo.org_govm, user_id=user.id)
253253
org_dictionary = org.as_dict()
254254
invitation_info = factory_invitation(org_dictionary["id"])
255-
user_with_token_invitee = TestUserInfo.user1
255+
user_with_token_invitee = dict(TestUserInfo.user1)
256256
user_with_token_invitee["keycloak_guid"] = TestJwtClaims.edit_role_2["sub"]
257257
user_invitee = factory_user_model(user_with_token_invitee)
258258
new_invitation = InvitationService.create_invitation(invitation_info, User(user_invitee), "")
@@ -314,7 +314,7 @@ def test_get_invitations_by_org_id(session, auth_mock, keycloak_mock, monkeypatc
314314
"""Find an existing invitation with the provided org id."""
315315
with patch.object(InvitationService, "send_invitation", return_value=None):
316316
patch_token_info(TestJwtClaims.public_user_role, monkeypatch)
317-
user_with_token = TestUserInfo.user_test
317+
user_with_token = dict(TestUserInfo.user_test)
318318
user_with_token["keycloak_guid"] = TestJwtClaims.public_user_role["sub"]
319319
user = factory_user_model(user_with_token)
320320
patch_token_info({"sub": user.keycloak_guid, "idp_userid": user.idp_userid}, monkeypatch)

auth-api/tests/unit/services/test_invitation_auth.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_token_user_context(session, auth_mock, monkeypatch):
9090
@mock.patch("auth_api.services.affiliation_invitation.RestService.get_service_account_token", mock_token)
9191
def test_change_authentication_subsequent_invites(session, auth_mock, keycloak_mock, monkeypatch):
9292
"""Assert that changing org authentication method changes new invitation required login source."""
93-
user_with_token = TestUserInfo.user_tester
93+
user_with_token = dict(TestUserInfo.user_tester)
9494
user_with_token["keycloak_guid"] = TestJwtClaims.tester_role["sub"]
9595
user_with_token["idp_userid"] = TestJwtClaims.tester_role["idp_userid"]
9696
inviter_user = factory_user_model(user_info=user_with_token)
@@ -176,7 +176,7 @@ def test_change_authentication_non_govm(session, auth_mock, keycloak_mock, monke
176176
invitee_bcsc_user = factory_user_model(TestUserInfo.user1)
177177
invitee_bceid_user = factory_user_model(TestUserInfo.user2)
178178

179-
patch_token_info(TestJwtClaims.tester_role, monkeypatch)
179+
patch_token_info(TestJwtClaims.user_test, monkeypatch)
180180
org = OrgService.create_org(TestOrgInfo.org1, user_id=inviter_user.id)
181181
org_dictionary = org.as_dict()
182182

@@ -219,14 +219,14 @@ def test_change_authentication_non_govm(session, auth_mock, keycloak_mock, monke
219219
assert invitation_model.login_source == LoginSource.BCSC.value
220220
assert invitation_model.invitation_status_code == InvitationStatus.ACCEPTED.value
221221

222-
patch_token_info(TestJwtClaims.tester_role, monkeypatch)
222+
patch_token_info(TestJwtClaims.user_test, monkeypatch)
223223
members = MembershipService.get_members_for_org(org_dictionary["id"], "PENDING_APPROVAL")
224224
assert members
225225
assert len(members) == 1
226226

227227
# Confirm that an invitation with BCSC login source can be accepted as another user login source and
228228
# updates the invitation login source based on the accepting user login source
229-
patch_token_info(TestJwtClaims.tester_role, monkeypatch)
229+
patch_token_info(TestJwtClaims.user_test, monkeypatch)
230230
with patch.object(InvitationService, "send_invitation", return_value=None):
231231
# Create invitation with BCSC login source
232232
with patch.object(ActivityLogPublisher, "publish_activity", return_value=None) as mock_alp:
@@ -265,7 +265,7 @@ def test_change_authentication_non_govm(session, auth_mock, keycloak_mock, monke
265265
assert invitation_model.login_source == LoginSource.BCEID.value
266266
assert invitation_model.invitation_status_code == InvitationStatus.ACCEPTED.value
267267

268-
patch_token_info(TestJwtClaims.tester_role, monkeypatch)
268+
patch_token_info(TestJwtClaims.user_test, monkeypatch)
269269
members = MembershipService.get_members_for_org(org_dictionary["id"], "PENDING_APPROVAL")
270270
assert members
271271
assert len(members) == 2
@@ -362,7 +362,7 @@ def test_invitation_anonymous(session, auth_mock, keycloak_mock, monkeypatch):
362362
inviter_user = factory_user_model(TestUserInfo.user_tester)
363363
invitee_bcsc_user = factory_user_model(TestUserInfo.user1)
364364

365-
patch_token_info(TestJwtClaims.tester_role, monkeypatch)
365+
patch_token_info(TestJwtClaims.user_test, monkeypatch)
366366
org = OrgService.create_org(TestOrgInfo.org1, user_id=inviter_user.id)
367367
org_dictionary = org.as_dict()
368368

@@ -400,7 +400,7 @@ def test_invitation_anonymous(session, auth_mock, keycloak_mock, monkeypatch):
400400
assert invitation_model.login_source is None
401401
assert invitation_model.invitation_status_code == InvitationStatus.ACCEPTED.value
402402

403-
patch_token_info(TestJwtClaims.tester_role, monkeypatch)
403+
patch_token_info(TestJwtClaims.user_test, monkeypatch)
404404
members = MembershipService.get_members_for_org(org_dictionary["id"], "PENDING_APPROVAL")
405405
assert members
406406
assert len(members) == 1

0 commit comments

Comments
 (0)