forked from PostHog/posthog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update billing manager sync to use a single func (PostHog#24205)
- Loading branch information
1 parent
7340f09
commit 2aa27f8
Showing
7 changed files
with
100 additions
and
112 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 |
---|---|---|
|
@@ -58,53 +58,35 @@ def test_get_billing_unlicensed(self, billing_patch_request_mock): | |
"ee.billing.billing_manager.requests.patch", | ||
return_value=MagicMock(status_code=200, json=MagicMock(return_value={"text": "ok"})), | ||
) | ||
def test_update_billing_distinct_ids(self, billing_patch_request_mock: MagicMock): | ||
def test_update_billing_organization_users(self, billing_patch_request_mock: MagicMock): | ||
organization = self.organization | ||
license = super(LicenseManager, cast(LicenseManager, License.objects)).create( | ||
key="key123::key123", | ||
plan="enterprise", | ||
valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7), | ||
) | ||
User.objects.create_and_join( | ||
organization=organization, | ||
email="[email protected]", | ||
password=None, | ||
level=OrganizationMembership.Level.ADMIN, | ||
) | ||
organization.refresh_from_db() | ||
assert len(organization.members.values_list("distinct_id", flat=True)) == 2 # one exists in the test base | ||
BillingManager(license).update_billing_distinct_ids(organization) | ||
assert billing_patch_request_mock.call_count == 1 | ||
assert len(billing_patch_request_mock.call_args[1]["json"]["distinct_ids"]) == 2 | ||
|
||
@patch( | ||
"ee.billing.billing_manager.requests.patch", | ||
return_value=MagicMock(status_code=200, json=MagicMock(return_value={"text": "ok"})), | ||
) | ||
def test_update_billing_customer_email(self, billing_patch_request_mock: MagicMock): | ||
organization = self.organization | ||
license = super(LicenseManager, cast(LicenseManager, License.objects)).create( | ||
key="key123::key123", | ||
plan="enterprise", | ||
valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7), | ||
) | ||
User.objects.create_and_join( | ||
y = User.objects.create_and_join( | ||
organization=organization, | ||
email="[email protected]", | ||
password=None, | ||
level=OrganizationMembership.Level.OWNER, | ||
) | ||
organization.refresh_from_db() | ||
assert len(organization.members.values_list("distinct_id", flat=True)) == 2 # one exists in the test base | ||
BillingManager(license).update_billing_customer_email(organization) | ||
BillingManager(license).update_billing_organization_users(organization) | ||
assert billing_patch_request_mock.call_count == 1 | ||
assert len(billing_patch_request_mock.call_args[1]["json"]["distinct_ids"]) == 2 | ||
assert billing_patch_request_mock.call_args[1]["json"]["org_customer_email"] == "[email protected]" | ||
assert billing_patch_request_mock.call_args[1]["json"]["org_admin_emails"] == ["[email protected]"] | ||
assert billing_patch_request_mock.call_args[1]["json"]["org_users"] == [ | ||
{"email": "[email protected]", "distinct_id": y.distinct_id, "role": 15}, | ||
] | ||
|
||
@patch( | ||
"ee.billing.billing_manager.requests.patch", | ||
return_value=MagicMock(status_code=200, json=MagicMock(return_value={"text": "ok"})), | ||
) | ||
def test_update_billing_admin_emails(self, billing_patch_request_mock: MagicMock): | ||
def test_update_billing_organization_users_with_multiple_members(self, billing_patch_request_mock: MagicMock): | ||
organization = self.organization | ||
license = super(LicenseManager, cast(LicenseManager, License.objects)).create( | ||
key="key123::key123", | ||
|
@@ -114,22 +96,32 @@ def test_update_billing_admin_emails(self, billing_patch_request_mock: MagicMock | |
User.objects.create_and_join( | ||
organization=organization, | ||
email="[email protected]", | ||
first_name="y1", | ||
last_name="y1", | ||
password=None, | ||
level=OrganizationMembership.Level.MEMBER, | ||
) | ||
User.objects.create_and_join( | ||
y2 = User.objects.create_and_join( | ||
organization=organization, | ||
email="[email protected]", | ||
first_name="y2", | ||
last_name="y2", | ||
password=None, | ||
level=OrganizationMembership.Level.ADMIN, | ||
) | ||
User.objects.create_and_join( | ||
y3 = User.objects.create_and_join( | ||
organization=organization, | ||
email="[email protected]", | ||
password=None, | ||
level=OrganizationMembership.Level.OWNER, | ||
) | ||
organization.refresh_from_db() | ||
BillingManager(license).update_billing_admin_emails(organization) | ||
BillingManager(license).update_billing_organization_users(organization) | ||
assert billing_patch_request_mock.call_count == 1 | ||
assert len(billing_patch_request_mock.call_args[1]["json"]["distinct_ids"]) == 4 | ||
assert billing_patch_request_mock.call_args[1]["json"]["org_customer_email"] == "[email protected]" | ||
assert sorted(billing_patch_request_mock.call_args[1]["json"]["org_admin_emails"]) == ["[email protected]", "[email protected]"] | ||
assert billing_patch_request_mock.call_args[1]["json"]["org_users"] == [ | ||
{"email": "[email protected]", "distinct_id": y2.distinct_id, "role": 8}, | ||
{"email": "[email protected]", "distinct_id": y3.distinct_id, "role": 15}, | ||
] |
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 |
---|---|---|
|
@@ -50,8 +50,8 @@ def test_cant_list_members_for_an_alien_organization(self): | |
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
self.assertEqual(response.json(), self.permission_denied_response()) | ||
|
||
@patch("posthog.models.user.User.update_billing_admin_emails") | ||
def test_delete_organization_member(self, mock_update_billing_admin_emails): | ||
@patch("posthog.models.user.User.update_billing_organization_users") | ||
def test_delete_organization_member(self, mock_update_billing_organization_users): | ||
user = User.objects.create_and_join(self.organization, "[email protected]", None, "X") | ||
membership_queryset = OrganizationMembership.objects.filter(user=user, organization=self.organization) | ||
self.assertTrue(membership_queryset.exists()) | ||
|
@@ -66,26 +66,27 @@ def test_delete_organization_member(self, mock_update_billing_admin_emails): | |
self.assertEqual(response.status_code, 204) | ||
self.assertFalse(membership_queryset.exists(), False) | ||
|
||
assert mock_update_billing_admin_emails.call_count == 1 | ||
assert mock_update_billing_admin_emails.call_args_list == [ | ||
assert mock_update_billing_organization_users.call_count == 2 | ||
assert mock_update_billing_organization_users.call_args_list == [ | ||
call(self.organization), | ||
call(self.organization), | ||
] | ||
|
||
@patch("posthog.models.user.User.update_billing_admin_emails") | ||
def test_leave_organization(self, mock_update_billing_admin_emails): | ||
@patch("posthog.models.user.User.update_billing_organization_users") | ||
def test_leave_organization(self, mock_update_billing_organization_users): | ||
membership_queryset = OrganizationMembership.objects.filter(user=self.user, organization=self.organization) | ||
self.assertEqual(membership_queryset.count(), 1) | ||
response = self.client.delete(f"/api/organizations/@current/members/{self.user.uuid}/") | ||
self.assertEqual(response.status_code, 204) | ||
self.assertEqual(membership_queryset.count(), 0) | ||
|
||
assert mock_update_billing_admin_emails.call_count == 1 | ||
assert mock_update_billing_admin_emails.call_args_list == [ | ||
assert mock_update_billing_organization_users.call_count == 1 | ||
assert mock_update_billing_organization_users.call_args_list == [ | ||
call(self.organization), | ||
] | ||
|
||
@patch("posthog.models.user.User.update_billing_admin_emails") | ||
def test_change_organization_member_level(self, mock_update_billing_admin_emails): | ||
@patch("posthog.models.user.User.update_billing_organization_users") | ||
def test_change_organization_member_level(self, mock_update_billing_organization_users): | ||
self.organization_membership.level = OrganizationMembership.Level.OWNER | ||
self.organization_membership.save() | ||
user = User.objects.create_user("[email protected]", None, "X") | ||
|
@@ -120,13 +121,13 @@ def test_change_organization_member_level(self, mock_update_billing_admin_emails | |
"level": OrganizationMembership.Level.ADMIN.value, | ||
}, | ||
) | ||
assert mock_update_billing_admin_emails.call_count == 1 | ||
assert mock_update_billing_admin_emails.call_args_list == [ | ||
assert mock_update_billing_organization_users.call_count == 1 | ||
assert mock_update_billing_organization_users.call_args_list == [ | ||
call(self.organization), | ||
] | ||
|
||
@patch("posthog.models.user.User.update_billing_admin_emails") | ||
def test_admin_can_promote_to_admin(self, mock_update_billing_admin_emails): | ||
@patch("posthog.models.user.User.update_billing_organization_users") | ||
def test_admin_can_promote_to_admin(self, mock_update_billing_organization_users): | ||
self.organization_membership.level = OrganizationMembership.Level.ADMIN | ||
self.organization_membership.save() | ||
user = User.objects.create_user("[email protected]", None, "X") | ||
|
@@ -140,13 +141,13 @@ def test_admin_can_promote_to_admin(self, mock_update_billing_admin_emails): | |
updated_membership = OrganizationMembership.objects.get(user=user, organization=self.organization) | ||
self.assertEqual(updated_membership.level, OrganizationMembership.Level.ADMIN) | ||
|
||
assert mock_update_billing_admin_emails.call_count == 1 | ||
assert mock_update_billing_admin_emails.call_args_list == [ | ||
assert mock_update_billing_organization_users.call_count == 1 | ||
assert mock_update_billing_organization_users.call_args_list == [ | ||
call(self.organization), | ||
] | ||
|
||
@patch("posthog.models.user.User.update_billing_admin_emails") | ||
def test_change_organization_member_level_requires_admin(self, mock_update_billing_admin_emails): | ||
@patch("posthog.models.user.User.update_billing_organization_users") | ||
def test_change_organization_member_level_requires_admin(self, mock_update_billing_organization_users): | ||
user = User.objects.create_user("[email protected]", None, "X") | ||
membership = OrganizationMembership.objects.create(user=user, organization=self.organization) | ||
self.assertEqual(membership.level, OrganizationMembership.Level.MEMBER) | ||
|
@@ -168,7 +169,7 @@ def test_change_organization_member_level_requires_admin(self, mock_update_billi | |
) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
assert mock_update_billing_admin_emails.call_count == 0 | ||
assert mock_update_billing_organization_users.call_count == 0 | ||
|
||
def test_cannot_change_own_organization_member_level(self): | ||
self.organization_membership.level = OrganizationMembership.Level.ADMIN | ||
|
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 |
---|---|---|
|
@@ -695,9 +695,7 @@ def test_social_signup_with_allowed_domain_on_self_hosted( | |
self.run_test_for_allowed_domain(mock_sso_providers, mock_request, mock_capture) | ||
|
||
@patch("posthoganalytics.capture") | ||
@mock.patch("ee.billing.billing_manager.BillingManager.update_billing_distinct_ids") | ||
@mock.patch("ee.billing.billing_manager.BillingManager.update_billing_customer_email") | ||
@mock.patch("ee.billing.billing_manager.BillingManager.update_billing_admin_emails") | ||
@mock.patch("ee.billing.billing_manager.BillingManager.update_billing_organization_users") | ||
@mock.patch("social_core.backends.base.BaseAuth.request") | ||
@mock.patch("posthog.api.authentication.get_instance_available_sso_providers") | ||
@mock.patch("posthog.tasks.user_identify.identify_task") | ||
|
@@ -707,21 +705,15 @@ def test_social_signup_with_allowed_domain_on_cloud( | |
mock_identify, | ||
mock_sso_providers, | ||
mock_request, | ||
mock_update_distinct_ids, | ||
mock_update_billing_customer_email, | ||
mock_update_billing_admin_emails, | ||
mock_update_billing_organization_users, | ||
mock_capture, | ||
): | ||
with self.is_cloud(True): | ||
self.run_test_for_allowed_domain(mock_sso_providers, mock_request, mock_capture) | ||
assert mock_update_distinct_ids.called_once() | ||
assert mock_update_billing_customer_email.called_once() | ||
assert mock_update_billing_admin_emails.called_once() | ||
assert mock_update_billing_organization_users.called_once() | ||
|
||
@patch("posthoganalytics.capture") | ||
@mock.patch("ee.billing.billing_manager.BillingManager.update_billing_distinct_ids") | ||
@mock.patch("ee.billing.billing_manager.BillingManager.update_billing_customer_email") | ||
@mock.patch("ee.billing.billing_manager.BillingManager.update_billing_admin_emails") | ||
@mock.patch("ee.billing.billing_manager.BillingManager.update_billing_organization_users") | ||
@mock.patch("social_core.backends.base.BaseAuth.request") | ||
@mock.patch("posthog.api.authentication.get_instance_available_sso_providers") | ||
@mock.patch("posthog.tasks.user_identify.identify_task") | ||
|
@@ -731,16 +723,12 @@ def test_social_signup_with_allowed_domain_on_cloud_with_existing_invite( | |
mock_identify, | ||
mock_sso_providers, | ||
mock_request, | ||
mock_update_distinct_ids, | ||
mock_update_billing_customer_email, | ||
mock_update_billing_admin_emails, | ||
mock_update_billing_organization_users, | ||
mock_capture, | ||
): | ||
with self.is_cloud(True): | ||
self.run_test_for_allowed_domain(mock_sso_providers, mock_request, mock_capture, use_invite=True) | ||
assert mock_update_distinct_ids.called_once() | ||
assert mock_update_billing_customer_email.called_once() | ||
assert mock_update_billing_admin_emails.called_once() | ||
assert mock_update_billing_organization_users.called_once() | ||
|
||
@mock.patch("social_core.backends.base.BaseAuth.request") | ||
@mock.patch("posthog.api.authentication.get_instance_available_sso_providers") | ||
|
@@ -1285,8 +1273,10 @@ def test_api_invite_sign_up_member_joined_email_is_not_sent_if_disabled(self): | |
self.assertEqual(len(mail.outbox), 0) | ||
|
||
@patch("posthoganalytics.capture") | ||
@patch("ee.billing.billing_manager.BillingManager.update_billing_distinct_ids") | ||
def test_existing_user_can_sign_up_to_a_new_organization(self, mock_update_distinct_ids, mock_capture): | ||
@patch("ee.billing.billing_manager.BillingManager.update_billing_organization_users") | ||
def test_existing_user_can_sign_up_to_a_new_organization( | ||
self, mock_update_billing_organization_users, mock_capture | ||
): | ||
user = self._create_user("[email protected]", VALID_TEST_PASSWORD) | ||
new_org = Organization.objects.create(name="TestCo") | ||
new_team = Team.objects.create(organization=new_org) | ||
|
@@ -1364,7 +1354,7 @@ def test_existing_user_can_sign_up_to_a_new_organization(self, mock_update_disti | |
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
# Assert that the org's distinct IDs are sent to billing | ||
mock_update_distinct_ids.assert_called_once_with(new_org) | ||
mock_update_billing_organization_users.assert_called_once_with(new_org) | ||
|
||
@patch("posthoganalytics.capture") | ||
def test_cannot_use_claim_invite_endpoint_to_update_user(self, mock_capture): | ||
|
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.