-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9de88e7
commit 69b2bec
Showing
9 changed files
with
112 additions
and
27 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,32 +12,50 @@ def setUp(self): | |
self.eng_role = Role.objects.create(name="Engineering", organization=self.organization) | ||
self.marketing_role = Role.objects.create(name="Marketing", organization=self.organization) | ||
|
||
def test_adds_member_to_a_role(self): | ||
user = User.objects.create_and_join(self.organization, "[email protected]", None) | ||
|
||
self.organization_membership.level = OrganizationMembership.Level.ADMIN | ||
self.organization_membership.save() | ||
assert RoleMembership.objects.count() == 0 | ||
|
||
res = self.client.post( | ||
f"/api/organizations/@current/roles/{self.eng_role.id}/role_memberships", | ||
{"user_uuid": user.uuid}, | ||
) | ||
|
||
assert res.status_code == status.HTTP_201_CREATED | ||
assert res.json()["id"] == str(RoleMembership.objects.first().id) | ||
assert res.json()["role_id"] == str(self.eng_role.id) | ||
assert res.json()["organization_member"]["user"]["id"] == user.id | ||
assert res.json()["user"]["id"] == user.id | ||
|
||
def test_only_organization_admins_and_higher_can_add_users(self): | ||
user_a = User.objects.create_and_join(self.organization, "[email protected]", None) | ||
user_b = User.objects.create_and_join(self.organization, "[email protected]", None) | ||
self.assertEqual(self.organization_membership.level, OrganizationMembership.Level.MEMBER) | ||
assert self.organization_membership.level == OrganizationMembership.Level.MEMBER | ||
|
||
add_user_b_res = self.client.post( | ||
f"/api/organizations/@current/roles/{self.eng_role.id}/role_memberships", | ||
{"user_uuid": user_b.uuid}, | ||
) | ||
self.assertEqual(add_user_b_res.status_code, status.HTTP_403_FORBIDDEN) | ||
assert add_user_b_res.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
self.organization_membership.level = OrganizationMembership.Level.ADMIN | ||
self.organization_membership.save() | ||
add_user_a_res = self.client.post( | ||
f"/api/organizations/@current/roles/{self.eng_role.id}/role_memberships", | ||
{"user_uuid": user_a.uuid}, | ||
) | ||
self.assertEqual(add_user_a_res.status_code, status.HTTP_201_CREATED) | ||
self.assertEqual(RoleMembership.objects.count(), 1) | ||
self.assertEqual(RoleMembership.objects.first().user, user_a) # type: ignore | ||
assert add_user_a_res.status_code == status.HTTP_201_CREATED | ||
assert RoleMembership.objects.count() == 1 | ||
assert RoleMembership.objects.first().user == user_a | ||
|
||
def test_user_can_belong_to_multiple_roles(self): | ||
user_a = User.objects.create_and_join(self.organization, "[email protected]", None) | ||
self.organization_membership.level = OrganizationMembership.Level.ADMIN | ||
self.organization_membership.save() | ||
self.assertEqual(RoleMembership.objects.count(), 0) | ||
assert RoleMembership.objects.count() == 0 | ||
|
||
self.client.post( | ||
f"/api/organizations/@current/roles/{self.eng_role.id}/role_memberships", | ||
|
@@ -47,7 +65,24 @@ def test_user_can_belong_to_multiple_roles(self): | |
f"/api/organizations/@current/roles/{self.marketing_role.id}/role_memberships", | ||
{"user_uuid": user_a.uuid}, | ||
) | ||
self.assertEqual(RoleMembership.objects.count(), 2) | ||
assert RoleMembership.objects.count() == 2 | ||
|
||
def test_user_can_be_removed_from_role(self): | ||
user_a = User.objects.create_and_join(self.organization, "[email protected]", None) | ||
self.organization_membership.level = OrganizationMembership.Level.ADMIN | ||
self.organization_membership.save() | ||
assert RoleMembership.objects.count() == 0 | ||
|
||
res = self.client.post( | ||
f"/api/organizations/@current/roles/{self.eng_role.id}/role_memberships", | ||
{"user_uuid": user_a.uuid}, | ||
) | ||
assert RoleMembership.objects.count() == 1 | ||
delete_response = self.client.delete( | ||
f"/api/organizations/@current/roles/{self.eng_role.id}/role_memberships/{res.json()['id']}", | ||
) | ||
assert delete_response.status_code == status.HTTP_204_NO_CONTENT | ||
assert RoleMembership.objects.count() == 0 | ||
|
||
def test_returns_correct_results_by_organization(self): | ||
self.organization_membership.level = OrganizationMembership.Level.ADMIN | ||
|
@@ -62,10 +97,10 @@ def test_returns_correct_results_by_organization(self): | |
) | ||
other_org_same_name_role = Role.objects.create(organization=other_org, name="Engineering") | ||
RoleMembership.objects.create(role=other_org_same_name_role, user=user_b) | ||
self.assertEqual(RoleMembership.objects.count(), 2) | ||
assert RoleMembership.objects.count() == 2 | ||
get_res = self.client.get( | ||
f"/api/organizations/@current/roles/{self.eng_role.id}/role_memberships", | ||
) | ||
self.assertEqual(get_res.json()["count"], 1) | ||
self.assertEqual(get_res.json()["results"][0]["user"]["distinct_id"], user_a.distinct_id) | ||
self.assertNotContains(get_res, str(user_b.email)) | ||
assert get_res.json()["count"] == 1 | ||
assert get_res.json()["results"][0]["user"]["distinct_id"] == user_a.distinct_id | ||
assert str(user_b.email) not in get_res.content.decode() |
2 changes: 1 addition & 1 deletion
2
ee/clickhouse/views/test/__snapshots__/test_clickhouse_experiment_secondary_results.ambr
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 4.1.13 on 2024-03-14 13:40 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("posthog", "0397_projects_backfill"), | ||
("ee", "0015_add_verified_properties"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="rolemembership", | ||
name="organization_member", | ||
field=models.ForeignKey( | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="role_memberships", | ||
related_query_name="role_membership", | ||
to="posthog.organizationmembership", | ||
), | ||
), | ||
] |
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
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