Skip to content

Commit

Permalink
chore: merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Jun 12, 2024
1 parent ee6cd6d commit b5341ad
Show file tree
Hide file tree
Showing 116 changed files with 8,781 additions and 3,602 deletions.
14 changes: 14 additions & 0 deletions docker/livestream/configs-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
prod: false
tailscale:
controlUrl:
hostname: 'live-events-dev'
kafka:
brokers: 'kafka:9092'
topic: 'events_plugin_ingestion'
group_id: 'livestream-dev'
mmdb:
path: 'GeoLite2-City.mmdb'
jwt:
token: '<randomly generated secret key>'
postgres:
url: 'postgres://posthog:posthog@db:5432/posthog'
222 changes: 2 additions & 220 deletions ee/api/test/test_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,19 +375,11 @@ def mock_implementation(url: str, headers: Any = None, params: Any = None) -> Ma
"unit_amount_usd": "0.00",
"up_to": 1000000,
"current_amount_usd": "0.00",
"current_usage": 0,
"flat_amount_usd": "0",
"projected_amount_usd": "None",
"projected_usage": None,
},
{
"unit_amount_usd": "0.00045",
"up_to": 2000000,
"current_amount_usd": "0.00",
"current_usage": 0,
"flat_amount_usd": "0",
"projected_amount_usd": "None",
"projected_usage": None,
},
],
"tiered": True,
Expand Down Expand Up @@ -418,19 +410,11 @@ def mock_implementation(url: str, headers: Any = None, params: Any = None) -> Ma
"tiers": [
{
"current_amount_usd": "0.00",
"current_usage": 0,
"flat_amount_usd": "0",
"projected_amount_usd": "None",
"projected_usage": None,
"unit_amount_usd": "0.00",
"up_to": 1000000,
},
{
"current_amount_usd": "0.00",
"current_usage": 0,
"flat_amount_usd": "0",
"projected_amount_usd": "None",
"projected_usage": None,
"unit_amount_usd": "0.0000135",
"up_to": 2000000,
},
Expand Down Expand Up @@ -512,7 +496,7 @@ def mock_implementation(url: str, headers: Any = None, params: Any = None) -> Ma
],
"current_usage": 0,
"percentage_usage": 0,
"current_amount_usd": "0.00",
"current_amount_usd": 0.0,
"has_exceeded_limit": False,
"projected_amount_usd": 0.0,
"projected_amount": 0,
Expand All @@ -525,7 +509,7 @@ def mock_implementation(url: str, headers: Any = None, params: Any = None) -> Ma
"usage_key": "events",
"addons": [
{
"current_amount_usd": "0.00",
"current_amount_usd": 0.0,
"current_usage": 0,
"description": "Test Addon",
"free_allocation": 10000,
Expand Down Expand Up @@ -758,217 +742,15 @@ def mock_implementation(url: str, headers: Any = None, params: Any = None) -> Ma
res_json = res.json()
# Should update product usage to reflect today's usage
assert res_json["products"][0]["current_usage"] == 1101000
assert res_json["products"][0]["current_amount_usd"] == "45.45"
assert res_json["products"][0]["tiers"][0]["current_usage"] == 1000000
assert res_json["products"][0]["tiers"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["tiers"][1]["current_usage"] == 101000
assert res_json["products"][0]["tiers"][1]["current_amount_usd"] == "45.45"

assert res_json["products"][0]["addons"][0]["current_usage"] == 1101000
assert res_json["products"][0]["addons"][0]["current_amount_usd"] == "1.36"
assert res_json["products"][0]["addons"][0]["tiers"][0]["current_usage"] == 1000000
assert res_json["products"][0]["addons"][0]["tiers"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["addons"][0]["tiers"][1]["current_usage"] == 101000
assert res_json["products"][0]["addons"][0]["tiers"][1]["current_amount_usd"] == "1.36"

# Now test when there is a usage_limit.
def mock_implementation_with_limit(url: str, headers: Any = None, params: Any = None) -> MagicMock:
mock = MagicMock()
mock.status_code = 404

if "api/billing/portal" in url:
mock.status_code = 200
mock.json.return_value = {"url": "https://billing.stripe.com/p/session/test_1234"}
elif "api/billing" in url:
mock.status_code = 200
mock.json.return_value = create_billing_response(
customer=create_billing_customer(has_active_subscription=True),
)
mock.json.return_value["customer"]["usage_summary"]["events"]["usage"] = 1000000
mock.json.return_value["customer"]["usage_summary"]["events"]["limit"] = 1000000
elif "api/products" in url:
mock.status_code = 200
mock.json.return_value = create_billing_products_response()

return mock

mock_request.side_effect = mock_implementation_with_limit
self.organization.usage = {"events": {"limit": 1000000, "usage": 1000000, "todays_usage": 100}}
self.organization.save()

res = self.client.get("/api/billing")
assert res.status_code == 200
res_json = res.json()
# Should update product usage to reflect today's usage
assert res_json["products"][0]["current_usage"] == 1000100
assert res_json["products"][0]["current_amount_usd"] == "0.04"
assert res_json["products"][0]["tiers"][0]["current_usage"] == 1000000
assert res_json["products"][0]["tiers"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["tiers"][1]["current_usage"] == 100
assert res_json["products"][0]["tiers"][1]["current_amount_usd"] == "0.04"

assert res_json["products"][0]["addons"][0]["current_usage"] == 1000100
assert res_json["products"][0]["addons"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["addons"][0]["tiers"][0]["current_usage"] == 1000000
assert res_json["products"][0]["addons"][0]["tiers"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["addons"][0]["tiers"][1]["current_usage"] == 100
assert res_json["products"][0]["addons"][0]["tiers"][1]["current_amount_usd"] == "0.00"

def mock_implementation_exceeds_limit(url: str, headers: Any = None, params: Any = None) -> MagicMock:
mock = MagicMock()
mock.status_code = 404

if "api/billing/portal" in url:
mock.status_code = 200
mock.json.return_value = {"url": "https://billing.stripe.com/p/session/test_1234"}
elif "api/billing" in url:
mock.status_code = 200
mock.json.return_value = create_billing_response(
customer=create_billing_customer(has_active_subscription=True),
)
mock.json.return_value["customer"]["usage_summary"]["events"]["usage"] = 1100000
mock.json.return_value["customer"]["usage_summary"]["events"]["limit"] = 1000000
elif "api/products" in url:
mock.status_code = 200
mock.json.return_value = create_billing_products_response()

return mock

mock_request.side_effect = mock_implementation_exceeds_limit
self.organization.usage = {"events": {"limit": 1000000, "usage": 1100000, "todays_usage": 1000}}
self.organization.save()

res = self.client.get("/api/billing")
assert res.status_code == 200
res_json = res.json()
# Should update product usage to reflect today's usage
assert res_json["products"][0]["current_usage"] == 1101000
assert res_json["products"][0]["current_amount_usd"] == "45.00"
assert res_json["products"][0]["tiers"][0]["current_usage"] == 1000000
assert res_json["products"][0]["tiers"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["tiers"][1]["current_usage"] == 100000
assert res_json["products"][0]["tiers"][1]["current_amount_usd"] == "45.00"

assert res_json["products"][0]["addons"][0]["current_usage"] == 1101000
assert res_json["products"][0]["addons"][0]["current_amount_usd"] == "1.35"
assert res_json["products"][0]["addons"][0]["tiers"][0]["current_usage"] == 1000000
assert res_json["products"][0]["addons"][0]["tiers"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["addons"][0]["tiers"][1]["current_usage"] == 100000
assert res_json["products"][0]["addons"][0]["tiers"][1]["current_amount_usd"] == "1.35"

# Test when the customer has no usage. Ensure that the tiered current_usage isn't set to the usage limit.
def mock_implementation_with_limit_no_usage(url: str, headers: Any = None, params: Any = None) -> MagicMock:
mock = MagicMock()
mock.status_code = 404

if "api/billing/portal" in url:
mock.status_code = 200
mock.json.return_value = {"url": "https://billing.stripe.com/p/session/test_1234"}
elif "api/billing" in url:
mock.status_code = 200
mock.json.return_value = create_billing_response(
customer=create_billing_customer(has_active_subscription=True),
)
mock.json.return_value["customer"]["usage_summary"]["events"]["usage"] = 0
mock.json.return_value["customer"]["usage_summary"]["events"]["limit"] = 1000000
elif "api/products" in url:
mock.status_code = 200
mock.json.return_value = create_billing_products_response()

return mock

mock_request.side_effect = mock_implementation_with_limit_no_usage

self.organization.usage = {"events": {"limit": 1000000, "usage": 0, "todays_usage": 0}}
self.organization.save()

res = self.client.get("/api/billing")
assert res.status_code == 200
res_json = res.json()
# Should update product usage to reflect today's usage
assert res_json["products"][0]["current_usage"] == 0
assert res_json["products"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["tiers"][0]["current_usage"] == 0
assert res_json["products"][0]["tiers"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["tiers"][1]["current_usage"] == 0
assert res_json["products"][0]["tiers"][1]["current_amount_usd"] == "0.00"

assert res_json["products"][0]["addons"][0]["current_usage"] == 0
assert res_json["products"][0]["addons"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["addons"][0]["tiers"][0]["current_usage"] == 0
assert res_json["products"][0]["addons"][0]["tiers"][0]["current_amount_usd"] == "0.00"
assert res_json["products"][0]["addons"][0]["tiers"][1]["current_usage"] == 0
assert res_json["products"][0]["addons"][0]["tiers"][1]["current_amount_usd"] == "0.00"

def mock_implementation_missing_customer(url: str, headers: Any = None, params: Any = None) -> MagicMock:
mock = MagicMock()
mock.status_code = 404

if "api/billing/portal" in url:
mock.status_code = 200
mock.json.return_value = {"url": "https://billing.stripe.com/p/session/test_1234"}
elif "api/billing" in url:
mock.status_code = 200
mock.json.return_value = create_billing_response(customer=create_missing_billing_customer())
elif "api/products" in url:
mock.status_code = 200
mock.json.return_value = create_billing_products_response()

return mock

mock_request.side_effect = mock_implementation_missing_customer

# Test unsubscribed config
res = self.client.get("/api/billing")
self.organization.refresh_from_db()
assert self.organization.usage == {
"events": {
"limit": None,
"todays_usage": 0,
"usage": 0,
},
"recordings": {
"limit": None,
"todays_usage": 0,
"usage": 0,
},
"rows_synced": {
"limit": None,
"todays_usage": 0,
"usage": 0,
},
"period": ["2022-10-07T11:12:48", "2022-11-07T11:12:48"],
}
assert self.organization.customer_id == "cus_123"

# Now test when there is a tiered product in the response that isn't in the usage dict
def mock_implementation(url: str, headers: Any = None, params: Any = None) -> MagicMock:
mock = MagicMock()
mock.status_code = 404

if "api/billing/portal" in url:
mock.status_code = 200
mock.json.return_value = {"url": "https://billing.stripe.com/p/session/test_1234"}
elif "api/billing" in url:
mock.status_code = 200
mock.json.return_value = create_billing_response(
customer=create_billing_customer(has_active_subscription=True),
)
mock.json.return_value["customer"]["products"][0]["usage_key"] = "feature_flag_requests"
mock.json.return_value["customer"]["usage_summary"]["events"]["usage"] = 1000
elif "api/products" in url:
mock.status_code = 200
mock.json.return_value = create_billing_products_response()

return mock

mock_request.side_effect = mock_implementation
self.organization.usage = {"events": {"limit": 1000000, "usage": 1000, "todays_usage": 1100000}}
self.organization.save()

res = self.client.get("/api/billing")
assert res.status_code == 200

@patch("ee.api.billing.requests.get")
def test_organization_usage_count_with_demo_project(self, mock_request, *args):
def mock_implementation(url: str, headers: Any = None, params: Any = None) -> MagicMock:
Expand Down
Loading

0 comments on commit b5341ad

Please sign in to comment.