-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sync distinct ids with billing (#16919)
* update billing distinct IDs on user join/leave org * temporarily update on usage report send to update existing orgs * abstract a bit more * update existing tests * add billing_manager test for the method * Update query snapshots * specify date * move outside the transaction * cache the instance license * use new instance license method in other places * fix mypy and some tests * Update query snapshots * Update query snapshots * fix caching in tests * constrain try/except * Update query snapshots * make sure we hae license before update billing * Update query snapshots * Update query snapshots * set to false if no instance license * fix date * Update query snapshots * use correct var * Update UI snapshots for `chromium` (1) * clear license cache before running tests * Update UI snapshots for `chromium` (2) * Update UI snapshots for `chromium` (2) * fix tests --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ecf7a95
commit 4e53dc7
Showing
18 changed files
with
188 additions
and
50 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
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,33 @@ | ||
from typing import cast | ||
from unittest.mock import MagicMock, patch | ||
|
||
from django.utils import timezone | ||
|
||
from ee.billing.billing_manager import BillingManager | ||
from ee.models.license import License, LicenseManager | ||
from posthog.models.organization import OrganizationMembership | ||
from posthog.models.user import User | ||
from posthog.test.base import BaseTest | ||
|
||
|
||
class TestBillingManager(BaseTest): | ||
@patch( | ||
"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): | ||
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 |
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
Binary file modified
BIN
+87 Bytes
(100%)
frontend/__snapshots__/scenes-app-batchexports--view-export.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-10.6 KB
(77%)
frontend/__snapshots__/scenes-app-insights--trends-table-breakdown--webkit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-16 KB
(69%)
frontend/__snapshots__/scenes-app-insights--trends-table-breakdown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
from django.utils import timezone | ||
from rest_framework import status | ||
|
||
from posthog.cloud_utils import TEST_clear_instance_license_cache | ||
from posthog.constants import AvailableFeature | ||
from posthog.models import Dashboard, Organization, Team, User | ||
from posthog.models.instance_setting import override_instance_config | ||
|
@@ -26,7 +27,7 @@ class TestSignupAPI(APIBaseTest): | |
@classmethod | ||
def setUpTestData(cls): | ||
# Do not set up any test data | ||
pass | ||
TEST_clear_instance_license_cache() | ||
|
||
@pytest.mark.skip_on_multitenancy | ||
@patch("posthoganalytics.capture") | ||
|
@@ -475,15 +476,17 @@ def test_social_signup_with_whitelisted_domain_on_self_hosted( | |
self.run_test_for_whitelisted_domain(mock_sso_providers, mock_request, mock_capture) | ||
|
||
@patch("posthoganalytics.capture") | ||
@mock.patch("ee.billing.billing_manager.BillingManager.update_billing_distinct_ids") | ||
@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") | ||
@pytest.mark.ee | ||
def test_social_signup_with_whitelisted_domain_on_cloud( | ||
self, mock_identify, mock_sso_providers, mock_request, mock_capture | ||
self, mock_identify, mock_sso_providers, mock_request, mock_update_distinct_ids, mock_capture | ||
): | ||
with self.is_cloud(True): | ||
self.run_test_for_whitelisted_domain(mock_sso_providers, mock_request, mock_capture) | ||
assert mock_update_distinct_ids.called_once() | ||
|
||
@mock.patch("social_core.backends.base.BaseAuth.request") | ||
@mock.patch("posthog.api.authentication.get_instance_available_sso_providers") | ||
|
@@ -909,7 +912,8 @@ def test_api_invite_sign_up_member_joined_email_is_not_sent_if_disabled(self): | |
self.assertEqual(len(mail.outbox), 0) | ||
|
||
@patch("posthoganalytics.capture") | ||
def test_existing_user_can_sign_up_to_a_new_organization(self, mock_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): | ||
user = self._create_user("[email protected]", "test_password") | ||
new_org = Organization.objects.create(name="TestCo") | ||
new_team = Team.objects.create(organization=new_org) | ||
|
@@ -921,6 +925,15 @@ def test_existing_user_can_sign_up_to_a_new_organization(self, mock_capture): | |
|
||
count = User.objects.count() | ||
|
||
try: | ||
from ee.models.license import License, LicenseManager | ||
except ImportError: | ||
pass | ||
else: | ||
super(LicenseManager, cast(LicenseManager, License.objects)).create( | ||
key="key_123", plan="enterprise", valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7) | ||
) | ||
|
||
with self.is_cloud(True): | ||
response = self.client.post(f"/api/signup/{invite.id}/") | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
@@ -973,6 +986,9 @@ def test_existing_user_can_sign_up_to_a_new_organization(self, mock_capture): | |
response = self.client.get("/api/users/@me/") | ||
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) | ||
|
||
@patch("posthoganalytics.capture") | ||
def test_cannot_use_claim_invite_endpoint_to_update_user(self, mock_capture): | ||
""" | ||
|
Oops, something went wrong.