-
Notifications
You must be signed in to change notification settings - Fork 429
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
fix: user delete social auth #3693
Merged
matthewelwell
merged 2 commits into
Flagsmith:main
from
shubham-padia:fix/user-delete-social-auth
Apr 29, 2024
Merged
Changes from all commits
Commits
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 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,3 +1,5 @@ | ||
USER_REGISTRATION_WITHOUT_INVITE_ERROR_MESSAGE = ( | ||
"User registration without an invite is disabled for this installation." | ||
) | ||
INVALID_PASSWORD_ERROR = "Invalid password." | ||
FIELD_BLANK_ERROR = "This field may not be blank." |
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 |
---|---|---|
|
@@ -608,22 +608,25 @@ def test_group_admin_can_retrieve_group( | |
assert response.status_code == status.HTTP_200_OK | ||
|
||
|
||
def delete_user( | ||
user: FFAdminUser, | ||
password: str = None, | ||
delete_orphan_organisations: bool = True, | ||
): | ||
client = APIClient() | ||
client.force_authenticate(user) | ||
data = { | ||
"delete_orphan_organisations": delete_orphan_organisations, | ||
} | ||
if password: | ||
data["password"] = password | ||
|
||
url = "/api/v1/auth/users/me/" | ||
return client.delete(url, data=json.dumps(data), content_type="application/json") | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_delete_user(): | ||
def delete_user( | ||
user: FFAdminUser, password: str, delete_orphan_organisations: bool = True | ||
): | ||
client = APIClient() | ||
client.force_authenticate(user) | ||
data = { | ||
"current_password": password, | ||
"delete_orphan_organisations": delete_orphan_organisations, | ||
} | ||
url = "/api/v1/auth/users/me/" | ||
return client.delete( | ||
url, data=json.dumps(data), content_type="application/json" | ||
) | ||
|
||
# create a couple of users | ||
email1 = "[email protected]" | ||
email2 = "[email protected]" | ||
|
@@ -633,7 +636,7 @@ def delete_user( | |
user2 = FFAdminUser.objects.create_user(email=email2, password=password) | ||
user3 = FFAdminUser.objects.create_user(email=email3, password=password) | ||
|
||
# crete some organizations | ||
# create some organizations | ||
org1 = Organisation.objects.create(name="org1") | ||
org2 = Organisation.objects.create(name="org2") | ||
org3 = Organisation.objects.create(name="org3") | ||
|
@@ -681,6 +684,42 @@ def delete_user( | |
assert Organisation.objects.filter(name="org1").count() == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize("password", [None, "", "random"]) | ||
def test_delete_user_social_auth_with_no_password(password): | ||
google_auth_user_email = "[email protected]" | ||
github_auth_user_email = "[email protected]" | ||
|
||
# We have given each social auth test user their own org since all the other org | ||
# logic has been checked in the email/password users tests and we're just doing a | ||
# sanity check here to make sure that the related org is deleted. | ||
google_auth_user_org = Organisation.objects.create(name="google_auth_user_org") | ||
github_auth_user_org = Organisation.objects.create(name="github_auth_user_org") | ||
|
||
google_auth_user = FFAdminUser.objects.create_user( | ||
email=google_auth_user_email, google_user_id=123456 | ||
) | ||
github_auth_user = FFAdminUser.objects.create_user( | ||
email=github_auth_user_email, github_user_id=123456 | ||
) | ||
|
||
# Add social auth users to their orgs | ||
google_auth_user_org.users.add(google_auth_user) | ||
github_auth_user_org.users.add(github_auth_user) | ||
|
||
# Delete google_auth_user | ||
response = delete_user(google_auth_user, password, delete_orphan_organisations=True) | ||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||
assert not FFAdminUser.objects.filter(email=google_auth_user_email).exists() | ||
assert Organisation.objects.filter(name="google_auth_user_org").count() == 0 | ||
|
||
# Delete github_auth_user | ||
response = delete_user(github_auth_user, password, delete_orphan_organisations=True) | ||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||
assert not FFAdminUser.objects.filter(email=github_auth_user_email).exists() | ||
assert Organisation.objects.filter(name="github_auth_user_org").count() == 0 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_change_email_address_api(mocker): | ||
# Given | ||
|
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 |
---|---|---|
|
@@ -56,12 +56,14 @@ class TheComponent extends Component { | |
}) | ||
} | ||
|
||
confirmDeleteAccount = (lastUserOrganisations, id) => { | ||
confirmDeleteAccount = (lastUserOrganisations, id, email, auth_type) => { | ||
openModal( | ||
'Are you sure?', | ||
<ConfirmDeleteAccount | ||
userId={id} | ||
lastUserOrganisations={lastUserOrganisations} | ||
email={email} | ||
auth_type={auth_type} | ||
/>, | ||
'p-0', | ||
) | ||
|
@@ -126,6 +128,7 @@ class TheComponent extends Component { | |
render() { | ||
const { | ||
state: { | ||
auth_type, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using snake case here, since first_name, last_name and similar fields are also doing the same for fields fetched from the API There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
current_password, | ||
email, | ||
error, | ||
|
@@ -305,7 +308,12 @@ class TheComponent extends Component { | |
id='delete-user-btn' | ||
data-test='delete-user-btn' | ||
onClick={() => | ||
this.confirmDeleteAccount(lastUserOrganisations, id) | ||
this.confirmDeleteAccount( | ||
lastUserOrganisations, | ||
id, | ||
email, | ||
auth_type, | ||
) | ||
} | ||
className='btn-with-icon btn-remove' | ||
> | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using string literals here, since I could not find enums being used elsewhere in the frontend codebase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.