-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: preserve SCIM userName for Okta integration #42253
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e9e075e
SCIM provisioned user with username and active status
a-lider b39f4a0
Fix circular dependency import
a-lider 695d42e
Migrations
a-lider e99a913
Delete scim provisioned user on delete requests & tests
a-lider 9ca3e3c
Return 409 on subsequest Post user requests
a-lider e1b7256
Use UUID primary keys
a-lider 841a26a
Use update_or_create instead of just update
a-lider 741088d
Remove nested transaction from deactivate and delete
a-lider c7d8784
Query SCIMProvisionedUser for userName filters
a-lider 6e64aa7
Remove create_defaults - it's not supported in our Django version
a-lider 1c8105b
Update tests to create SCIMProvisionedUser
a-lider 374cce3
Check if the user is already provisioned in the transaction
a-lider ebc83a1
Fix mypy errors
a-lider File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| from ee.api.scim.auth import generate_scim_token | ||
| from ee.api.test.base import APILicensedTest | ||
| from ee.models.rbac.role import RoleMembership | ||
| from ee.models.scim_provisioned_user import SCIMProvisionedUser | ||
|
|
||
|
|
||
| class TestSCIMUsersAPI(APILicensedTest): | ||
|
|
@@ -129,9 +130,9 @@ def test_users_list_filter_unrecognized_returns_empty_list(self): | |
| def test_create_user(self): | ||
| user_data = { | ||
| "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], | ||
| "userName": "newuser@example.com", | ||
| "userName": "Newuser@example.com", | ||
| "name": {"givenName": "New", "familyName": "User"}, | ||
| "emails": [{"value": "newuser@example.com", "primary": True}], | ||
| "emails": [{"value": "Newuser@example.com", "primary": True}], | ||
| "active": True, | ||
| } | ||
|
|
||
|
|
@@ -141,11 +142,11 @@ def test_create_user(self): | |
|
|
||
| assert response.status_code == status.HTTP_201_CREATED | ||
| data = response.json() | ||
| assert data["userName"] == "newuser@example.com" | ||
| assert data["userName"] == "Newuser@example.com" | ||
| assert data["name"]["givenName"] == "New" | ||
| assert data["name"]["familyName"] == "User" | ||
|
|
||
| # Verify user was created | ||
| # Verify user was created with lowercase email | ||
| user = User.objects.get(email="[email protected]") | ||
| assert user.first_name == "New" | ||
| assert user.last_name == "User" | ||
|
|
@@ -155,6 +156,12 @@ def test_create_user(self): | |
| membership = OrganizationMembership.objects.get(user=user, organization=self.organization) | ||
| assert membership.level == OrganizationMembership.Level.MEMBER | ||
|
|
||
| # Verify SCIM provisioned user record was created | ||
| scim_user = SCIMProvisionedUser.objects.get(user=user, organization_domain=self.domain) | ||
| assert scim_user.username == "[email protected]" | ||
| assert scim_user.active is True | ||
| assert scim_user.identity_provider == SCIMProvisionedUser.IdentityProvider.OTHER | ||
|
|
||
| def test_existing_user_is_added_to_org(self): | ||
| # Create user in different org | ||
| other_org = Organization.objects.create(name="Other Org") | ||
|
|
@@ -184,9 +191,11 @@ def test_existing_user_is_added_to_org(self): | |
| assert OrganizationMembership.objects.filter(user=existing_user, organization=self.organization).exists() | ||
| assert OrganizationMembership.objects.filter(user=existing_user, organization=other_org).exists() | ||
|
|
||
| def test_repeated_post_does_not_create_duplicate_user(self): | ||
| # In case the IdP failed to match user by id, it can send POST request to create a new user. | ||
| # The user should be merged with existing one by email, not create a duplicate. | ||
| # Verify SCIM provisioned user record was created for this domain | ||
| scim_user = SCIMProvisionedUser.objects.get(user=existing_user, organization_domain=self.domain) | ||
| assert scim_user.active is True | ||
|
|
||
| def test_repeated_post_returns_409_for_already_provisioned_user(self): | ||
| user_data_first = { | ||
| "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], | ||
| "userName": "[email protected]", | ||
|
|
@@ -202,7 +211,7 @@ def test_repeated_post_does_not_create_duplicate_user(self): | |
| assert response.status_code == status.HTTP_201_CREATED | ||
| first_user = User.objects.get(email="[email protected]") | ||
|
|
||
| # IdP sends POST request again with same email | ||
| # IdP sends POST request again with same email - should fail with 409 | ||
| user_data_second = { | ||
| "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], | ||
| "userName": "[email protected]", | ||
|
|
@@ -215,14 +224,14 @@ def test_repeated_post_does_not_create_duplicate_user(self): | |
| f"/scim/v2/{self.domain.id}/Users", data=user_data_second, content_type="application/scim+json" | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_201_CREATED | ||
| assert response.status_code == status.HTTP_409_CONFLICT | ||
|
|
||
| # Should NOT create duplicate user | ||
| assert User.objects.filter(email="[email protected]").count() == 1 | ||
|
|
||
| # User should be updated with new data from second POST | ||
| # User should NOT be updated (still has first POST data) | ||
| first_user.refresh_from_db() | ||
| assert first_user.first_name == "Second" | ||
| assert first_user.first_name == "First" | ||
| assert first_user.last_name == "Time" | ||
|
|
||
| # User should have only one membership | ||
|
|
@@ -261,6 +270,14 @@ def test_deactivate_user(self): | |
| OrganizationMembership.objects.create( | ||
| user=user, organization=self.organization, level=OrganizationMembership.Level.MEMBER | ||
| ) | ||
| # Create SCIM provisioned user record | ||
| SCIMProvisionedUser.objects.create( | ||
| user=user, | ||
| organization_domain=self.domain, | ||
| username="[email protected]", | ||
| identity_provider=SCIMProvisionedUser.IdentityProvider.OTHER, | ||
| active=True, | ||
| ) | ||
|
|
||
| patch_data = { | ||
| "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], | ||
|
|
@@ -280,13 +297,25 @@ def test_deactivate_user(self): | |
| user.refresh_from_db() | ||
| assert user.is_active is True # User is still active globally | ||
|
|
||
| # Verify SCIM provisioned user record still exists but is marked inactive | ||
| scim_user = SCIMProvisionedUser.objects.get(user=user, organization_domain=self.domain) | ||
| assert scim_user.active is False | ||
|
|
||
| def test_delete_user(self): | ||
| user = User.objects.create_user( | ||
| email="[email protected]", password=None, first_name="Delete", is_email_verified=True | ||
| ) | ||
| OrganizationMembership.objects.create( | ||
| user=user, organization=self.organization, level=OrganizationMembership.Level.MEMBER | ||
| ) | ||
| # Create SCIM provisioned user record | ||
| SCIMProvisionedUser.objects.create( | ||
| user=user, | ||
| organization_domain=self.domain, | ||
| username="[email protected]", | ||
| identity_provider=SCIMProvisionedUser.IdentityProvider.OTHER, | ||
| active=True, | ||
| ) | ||
|
|
||
| response = self.client.delete(f"/scim/v2/{self.domain.id}/Users/{user.id}") | ||
|
|
||
|
|
@@ -295,13 +324,24 @@ def test_delete_user(self): | |
| # Verify membership was removed | ||
| assert not OrganizationMembership.objects.filter(user=user, organization=self.organization).exists() | ||
|
|
||
| # Verify SCIM provisioned user record was deleted | ||
| assert not SCIMProvisionedUser.objects.filter(user=user, organization_domain=self.domain).exists() | ||
|
|
||
| def test_put_user(self): | ||
| user = User.objects.create_user( | ||
| email="[email protected]", password=None, first_name="Old", last_name="Name", is_email_verified=True | ||
| ) | ||
| OrganizationMembership.objects.create( | ||
| user=user, organization=self.organization, level=OrganizationMembership.Level.MEMBER | ||
| ) | ||
| # Create SCIM provisioned user record | ||
| SCIMProvisionedUser.objects.create( | ||
| user=user, | ||
| organization_domain=self.domain, | ||
| username="[email protected]", | ||
| identity_provider=SCIMProvisionedUser.IdentityProvider.OTHER, | ||
| active=True, | ||
| ) | ||
|
|
||
| put_data = { | ||
| "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], | ||
|
|
@@ -321,6 +361,11 @@ def test_put_user(self): | |
| assert user.last_name == "User" | ||
| assert user.email == "[email protected]" | ||
|
|
||
| # Verify SCIM provisioned user was updated | ||
| scim_user = SCIMProvisionedUser.objects.get(user=user, organization_domain=self.domain) | ||
| assert scim_user.username == "[email protected]" | ||
| assert scim_user.active is True | ||
|
|
||
| def test_put_user_not_found(self): | ||
| put_data = { | ||
| "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], | ||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.