forked from rapidpro/rapidpro
-
Notifications
You must be signed in to change notification settings - Fork 25
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
Prevent workspace admins to modify the system user on a workspace #5664
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
|
@@ -1728,6 +1728,13 @@ class UserCRUDLTest(TembaTest, CRUDLTestMixin): | |
def test_list(self): | ||
list_url = reverse("orgs.user_list") | ||
|
||
system_user = self.create_user("[email protected]") | ||
system_user.settings.is_system = True | ||
system_user.settings.save(update_fields=("is_system",)) | ||
|
||
# add system user to workspace | ||
self.org.add_user(system_user, OrgRole.ADMINISTRATOR) | ||
|
||
# nobody can access if users feature not enabled | ||
response = self.requestView(list_url, self.admin) | ||
self.assertRedirect(response, reverse("orgs.org_workspace")) | ||
|
@@ -1748,12 +1755,21 @@ def test_list(self): | |
response = self.assertListFetch( | ||
list_url, [self.admin], context_objects=[self.admin, self.agent, self.editor, self.user] | ||
) | ||
self.assertEqual(response.context["admin_count"], 1) | ||
self.assertContains(response, "(All Topics)") | ||
|
||
# can search by name or email | ||
self.assertListFetch(list_url + "?search=andy", [self.admin], context_objects=[self.admin]) | ||
self.assertListFetch(list_url + "[email protected]", [self.admin], context_objects=[self.editor]) | ||
|
||
response = self.requestView(list_url, self.customer_support, choose_org=self.org) | ||
self.assertEqual( | ||
set(list(response.context["object_list"])), | ||
{self.admin, self.agent, self.editor, self.user, system_user}, | ||
) | ||
self.assertContains(response, "(All Topics)") | ||
self.assertEqual(response.context["admin_count"], 2) | ||
|
||
def test_team(self): | ||
team_url = reverse("orgs.user_team", args=[self.org.default_ticket_team.id]) | ||
|
||
|
@@ -1775,6 +1791,10 @@ def test_team(self): | |
self.assertContentMenu(team_url, self.admin, ["Edit", "Delete"]) | ||
|
||
def test_update(self): | ||
system_user = self.create_user("[email protected]") | ||
system_user.settings.is_system = True | ||
system_user.settings.save(update_fields=("is_system",)) | ||
|
||
update_url = reverse("orgs.user_update", args=[self.agent.id]) | ||
|
||
# nobody can access if users feature not enabled | ||
|
@@ -1825,16 +1845,34 @@ def test_update(self): | |
self.assertEqual({self.user, self.editor}, set(self.org.get_users(roles=[OrgRole.EDITOR]))) | ||
self.assertEqual({self.admin}, set(self.org.get_users(roles=[OrgRole.ADMINISTRATOR]))) | ||
|
||
# even if we add system user to workspace | ||
self.org.add_user(system_user, OrgRole.ADMINISTRATOR) | ||
response = self.assertUpdateSubmit(update_url, self.admin, {"role": "E"}, object_unchanged=self.admin) | ||
self.assertRedirect(response, reverse("orgs.user_list")) | ||
self.assertEqual({self.user, self.editor}, set(self.org.get_users(roles=[OrgRole.EDITOR]))) | ||
self.assertEqual({self.admin, system_user}, set(self.org.get_users(roles=[OrgRole.ADMINISTRATOR]))) | ||
|
||
# add another admin to workspace and try again | ||
self.org.add_user(self.admin2, OrgRole.ADMINISTRATOR) | ||
|
||
response = self.assertUpdateSubmit(update_url, self.admin, {"role": "E"}, object_unchanged=self.admin) | ||
self.assertRedirect(response, reverse("orgs.org_start")) # no longer have access to user list page | ||
|
||
self.assertEqual({self.user, self.editor, self.admin}, set(self.org.get_users(roles=[OrgRole.EDITOR]))) | ||
self.assertEqual({self.admin2}, set(self.org.get_users(roles=[OrgRole.ADMINISTRATOR]))) | ||
self.assertEqual({self.admin2, system_user}, set(self.org.get_users(roles=[OrgRole.ADMINISTRATOR]))) | ||
|
||
# cannot update system user on a workspace | ||
update_url = reverse("orgs.user_update", args=[system_user.id]) | ||
response = self.requestView(update_url, self.admin2) | ||
self.assertRedirect(response, reverse("orgs.org_choose")) | ||
self.assertEqual({self.user, self.editor, self.admin}, set(self.org.get_users(roles=[OrgRole.EDITOR]))) | ||
self.assertEqual({self.admin2, system_user}, set(self.org.get_users(roles=[OrgRole.ADMINISTRATOR]))) | ||
|
||
def test_delete(self): | ||
system_user = self.create_user("[email protected]") | ||
system_user.settings.is_system = True | ||
system_user.settings.save(update_fields=("is_system",)) | ||
|
||
delete_url = reverse("orgs.user_delete", args=[self.agent.id]) | ||
|
||
# nobody can access if users feature not enabled | ||
|
@@ -1868,6 +1906,16 @@ def test_delete(self): | |
self.assertRedirect(response, reverse("orgs.user_list")) | ||
self.assertEqual({self.user, self.editor, self.admin}, set(self.org.get_users())) | ||
|
||
# cannot still even when the other admin is a system user | ||
self.org.add_user(system_user, OrgRole.ADMINISTRATOR) | ||
response = self.assertDeleteSubmit(delete_url, self.admin, object_unchanged=self.admin) | ||
self.assertRedirect(response, reverse("orgs.user_list")) | ||
self.assertEqual({self.user, self.editor, self.admin, system_user}, set(self.org.get_users())) | ||
|
||
# cannot remove system user too | ||
self.assertRequestDisallowed(reverse("orgs.user_delete", args=[system_user.id]), [self.admin]) | ||
self.assertEqual({self.user, self.editor, self.admin, system_user}, set(self.org.get_users())) | ||
|
||
# add another admin to workspace and try again | ||
self.org.add_user(self.admin2, OrgRole.ADMINISTRATOR) | ||
|
||
|
@@ -1876,7 +1924,7 @@ def test_delete(self): | |
# this time we could remove ourselves | ||
response = self.assertDeleteSubmit(delete_url, self.admin, object_unchanged=self.admin) | ||
self.assertRedirect(response, reverse("orgs.org_choose")) | ||
self.assertEqual({self.user, self.editor, self.admin2}, set(self.org.get_users())) | ||
self.assertEqual({self.user, self.editor, self.admin2, system_user}, set(self.org.get_users())) | ||
|
||
def test_account(self): | ||
self.login(self.agent) | ||
|
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
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.
need to update
admin_count
below on line 435