0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-24 18:07:17 +01:00
posthog/ee/tasks/test/test_send_license_usage.py
Karl-Aksel Puulmann 7de761dff0
Update BE tracking to use groups (#7355)
* Update some posthoganalytics.capture calls

* Update report_user_logged_in tracking

* Update report_team_member_invited

* Update report_bulk_invited

* Update report_org_usage

* Update AnalyticsDestroyModelMixin

* Update action creation/updating tracking

* Update annotations tracking

* Update cohort tracking

* Update dashboards tracking

* Update feature flags tracking

* Update imports, sessions tracking

* More typeignores

* Default properties blank

* update metadata default

* Stringify

* Fix some tests

* Kill debug code

* Update FF tests

* Update test_organization

* Update test_organization_invites

* Update test_user

* Fix org usage report

* update test_send_license_usage

* Update organization tests

* Respect dry-run on errors as well, log

* Update organization properties on report

* Assert organization props

* Remove dead imports

* Fix test
2021-11-30 10:58:37 +02:00

91 lines
4.3 KiB
Python

from unittest.mock import ANY, patch
from uuid import uuid4
from freezegun import freeze_time
from ee.api.test.base import LicensedTestMixin
from ee.clickhouse.models.event import create_event
from ee.clickhouse.util import ClickhouseDestroyTablesMixin
from ee.models.license import License
from ee.tasks.send_license_usage import send_license_usage
from posthog.models import organization
from posthog.models.team import Team
from posthog.test.base import APIBaseTest
def _create_event(**kwargs):
kwargs.update({"event_uuid": uuid4()})
create_event(**kwargs)
class SendLicenseUsageTest(LicensedTestMixin, ClickhouseDestroyTablesMixin, APIBaseTest):
@freeze_time("2021-10-10T23:01:00Z")
@patch("posthoganalytics.capture")
@patch("requests.post")
def test_send_license_usage(self, mock_post, mock_capture):
team2 = Team.objects.create(organization=self.organization)
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-08T14:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-09T12:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-09T13:01:01Z")
_create_event(
event="$$internal_metrics_shouldnt_be_billed",
team=self.team,
distinct_id=1,
timestamp="2021-10-09T13:01:01Z",
)
_create_event(event="$pageview", team=team2, distinct_id=1, timestamp="2021-10-09T14:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-10T14:01:01Z")
send_license_usage()
mock_post.assert_called_once_with(
"https://license.posthog.com/licenses/usage",
data={"date": "2021-10-09", "key": self.license.key, "events_count": 3},
)
mock_capture.assert_called_once_with(
self.user.distinct_id,
"send license usage data",
{"date": "2021-10-09", "events_count": 3, "license_keys": ["enterprise"], "organization_name": "Test"},
groups={"instance": ANY, "organization": str(self.organization.id)},
)
@freeze_time("2021-10-10T23:01:00Z")
@patch("posthoganalytics.capture")
@patch("ee.tasks.send_license_usage.sync_execute", side_effect=Exception())
def test_send_license_error(self, mock_post, mock_capture):
team2 = Team.objects.create(organization=self.organization)
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-08T14:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-09T12:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-09T13:01:01Z")
_create_event(
event="$$internal_metrics_shouldnt_be_billed",
team=self.team,
distinct_id=1,
timestamp="2021-10-09T13:01:01Z",
)
_create_event(event="$pageview", team=team2, distinct_id=1, timestamp="2021-10-09T14:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-10T14:01:01Z")
send_license_usage()
mock_capture.assert_called_once_with(
self.user.distinct_id,
"send license usage data error",
{"error": "", "date": "2021-10-09", "organization_name": "Test"},
groups={"instance": ANY, "organization": str(self.organization.id)},
)
class SendLicenseUsageNoLicenseTest(APIBaseTest):
@freeze_time("2021-10-10T23:01:00Z")
@patch("requests.post")
def test_no_license(self, mock_post):
# Same test, we just don't include the LicensedTestMixin so no license
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-08T14:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-09T12:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-09T13:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-09T14:01:01Z")
_create_event(event="$pageview", team=self.team, distinct_id=1, timestamp="2021-10-10T14:01:01Z")
send_license_usage()
self.assertEqual(mock_post.call_count, 0)