mirror of
https://github.com/simple-login/app.git
synced 2024-11-16 08:58:30 +01:00
Remove old webhook to send plan changes and rely on events (#2265)
This commit is contained in:
parent
4986ace59f
commit
b8be0f470a
4 changed files with 109 additions and 138 deletions
|
@ -601,7 +601,6 @@ SKIP_MX_LOOKUP_ON_CHECK = False
|
|||
|
||||
DISABLE_RATE_LIMIT = "DISABLE_RATE_LIMIT" in os.environ
|
||||
|
||||
SUBSCRIPTION_CHANGE_WEBHOOK = os.environ.get("SUBSCRIPTION_CHANGE_WEBHOOK", None)
|
||||
MAX_API_KEYS = int(os.environ.get("MAX_API_KEYS", 30))
|
||||
|
||||
UPCLOUD_USERNAME = os.environ.get("UPCLOUD_USERNAME", None)
|
||||
|
|
|
@ -1,40 +1,16 @@
|
|||
import requests
|
||||
from requests import RequestException
|
||||
|
||||
from app import config
|
||||
from app.db import Session
|
||||
from app.events.event_dispatcher import EventDispatcher
|
||||
from app.events.generated.event_pb2 import EventContent, UserPlanChanged
|
||||
from app.log import LOG
|
||||
from app.models import User
|
||||
|
||||
|
||||
def execute_subscription_webhook(user: User):
|
||||
webhook_url = config.SUBSCRIPTION_CHANGE_WEBHOOK
|
||||
if webhook_url is None:
|
||||
return
|
||||
subscription_end = user.get_active_subscription_end(
|
||||
include_partner_subscription=False
|
||||
)
|
||||
sl_subscription_end = None
|
||||
if subscription_end:
|
||||
sl_subscription_end = subscription_end.timestamp
|
||||
payload = {
|
||||
"user_id": user.id,
|
||||
"is_premium": user.is_premium(),
|
||||
"active_subscription_end": sl_subscription_end,
|
||||
}
|
||||
try:
|
||||
response = requests.post(webhook_url, json=payload, timeout=2)
|
||||
if response.status_code == 200:
|
||||
LOG.i("Sent request to subscription update webhook successfully")
|
||||
else:
|
||||
LOG.i(
|
||||
f"Request to webhook failed with status {response.status_code}: {response.text}"
|
||||
)
|
||||
except RequestException as e:
|
||||
LOG.error(f"Subscription request exception: {e}")
|
||||
|
||||
event = UserPlanChanged(plan_end_time=sl_subscription_end)
|
||||
EventDispatcher.send_event(user, EventContent(user_plan_change=event))
|
||||
Session.commit()
|
||||
|
|
109
tests/events/test_subscription_webhook.py
Normal file
109
tests/events/test_subscription_webhook.py
Normal file
|
@ -0,0 +1,109 @@
|
|||
import arrow
|
||||
|
||||
from app import config
|
||||
from app.events.event_dispatcher import GlobalDispatcher
|
||||
from app.events.generated.event_pb2 import UserPlanChanged
|
||||
from app.models import (
|
||||
Subscription,
|
||||
AppleSubscription,
|
||||
CoinbaseSubscription,
|
||||
ManualSubscription,
|
||||
User,
|
||||
PartnerUser,
|
||||
)
|
||||
|
||||
from .event_test_utils import (
|
||||
OnMemoryDispatcher,
|
||||
_create_linked_user,
|
||||
_get_event_from_string,
|
||||
)
|
||||
from tests.utils import random_token
|
||||
|
||||
from app.subscription_webhook import execute_subscription_webhook
|
||||
|
||||
|
||||
on_memory_dispatcher = OnMemoryDispatcher()
|
||||
|
||||
|
||||
def setup_module():
|
||||
GlobalDispatcher.set_dispatcher(on_memory_dispatcher)
|
||||
config.EVENT_WEBHOOK = "http://test"
|
||||
|
||||
|
||||
def teardown_module():
|
||||
GlobalDispatcher.set_dispatcher(None)
|
||||
config.EVENT_WEBHOOK = None
|
||||
|
||||
|
||||
def setup_function(func):
|
||||
on_memory_dispatcher.clear()
|
||||
|
||||
|
||||
def check_event(user: User, pu: PartnerUser) -> UserPlanChanged:
|
||||
assert len(on_memory_dispatcher.memory) == 1
|
||||
event_data = on_memory_dispatcher.memory[0]
|
||||
event_content = _get_event_from_string(event_data, user, pu)
|
||||
assert event_content.user_plan_change is not None
|
||||
plan_change = event_content.user_plan_change
|
||||
return plan_change
|
||||
|
||||
|
||||
def test_webhook_with_trial():
|
||||
(user, pu) = _create_linked_user()
|
||||
execute_subscription_webhook(user)
|
||||
assert check_event(user, pu).plan_end_time == 0
|
||||
|
||||
|
||||
def test_webhook_with_subscription():
|
||||
(user, pu) = _create_linked_user()
|
||||
end_at = arrow.utcnow().shift(days=1).replace(hour=0, minute=0, second=0)
|
||||
Subscription.create(
|
||||
user_id=user.id,
|
||||
cancel_url="",
|
||||
update_url="",
|
||||
subscription_id=random_token(10),
|
||||
event_time=arrow.now(),
|
||||
next_bill_date=end_at.date(),
|
||||
plan="yearly",
|
||||
flush=True,
|
||||
)
|
||||
execute_subscription_webhook(user)
|
||||
assert check_event(user, pu).plan_end_time == end_at.timestamp
|
||||
|
||||
|
||||
def test_webhook_with_apple_subscription():
|
||||
(user, pu) = _create_linked_user()
|
||||
end_at = arrow.utcnow().shift(days=2).replace(hour=0, minute=0, second=0)
|
||||
AppleSubscription.create(
|
||||
user_id=user.id,
|
||||
receipt_data=arrow.now().date().strftime("%Y-%m-%d"),
|
||||
expires_date=end_at.date().strftime("%Y-%m-%d"),
|
||||
original_transaction_id=random_token(10),
|
||||
plan="yearly",
|
||||
product_id="",
|
||||
flush=True,
|
||||
)
|
||||
execute_subscription_webhook(user)
|
||||
assert check_event(user, pu).plan_end_time == end_at.timestamp
|
||||
|
||||
|
||||
def test_webhook_with_coinbase_subscription():
|
||||
(user, pu) = _create_linked_user()
|
||||
end_at = arrow.utcnow().shift(days=3).replace(hour=0, minute=0, second=0)
|
||||
CoinbaseSubscription.create(
|
||||
user_id=user.id, end_at=end_at.date().strftime("%Y-%m-%d"), flush=True
|
||||
)
|
||||
|
||||
execute_subscription_webhook(user)
|
||||
assert check_event(user, pu).plan_end_time == end_at.timestamp
|
||||
|
||||
|
||||
def test_webhook_with_manual_subscription():
|
||||
(user, pu) = _create_linked_user()
|
||||
end_at = arrow.utcnow().shift(days=3).replace(hour=0, minute=0, second=0)
|
||||
ManualSubscription.create(
|
||||
user_id=user.id, end_at=end_at.date().strftime("%Y-%m-%d"), flush=True
|
||||
)
|
||||
|
||||
execute_subscription_webhook(user)
|
||||
assert check_event(user, pu).plan_end_time == end_at.timestamp
|
|
@ -1,113 +0,0 @@
|
|||
import http.server
|
||||
import json
|
||||
import threading
|
||||
|
||||
import arrow
|
||||
|
||||
from app import config
|
||||
from app.models import (
|
||||
Subscription,
|
||||
AppleSubscription,
|
||||
CoinbaseSubscription,
|
||||
ManualSubscription,
|
||||
)
|
||||
from tests.utils import create_new_user, random_token
|
||||
|
||||
from app.subscription_webhook import execute_subscription_webhook
|
||||
|
||||
http_server = None
|
||||
last_http_request = None
|
||||
|
||||
|
||||
def setup_module():
|
||||
global http_server
|
||||
http_server = http.server.ThreadingHTTPServer(("", 0), HTTPTestServer)
|
||||
print(http_server.server_port)
|
||||
threading.Thread(target=http_server.serve_forever, daemon=True).start()
|
||||
config.SUBSCRIPTION_CHANGE_WEBHOOK = f"http://localhost:{http_server.server_port}"
|
||||
|
||||
|
||||
def teardown_module():
|
||||
global http_server
|
||||
config.SUBSCRIPTION_CHANGE_WEBHOOK = None
|
||||
http_server.shutdown()
|
||||
|
||||
|
||||
class HTTPTestServer(http.server.BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
global last_http_request
|
||||
content_len = int(self.headers.get("Content-Length"))
|
||||
body_data = self.rfile.read(content_len)
|
||||
last_http_request = json.loads(body_data)
|
||||
self.send_response(200)
|
||||
|
||||
|
||||
def test_webhook_with_trial():
|
||||
user = create_new_user()
|
||||
execute_subscription_webhook(user)
|
||||
assert last_http_request["user_id"] == user.id
|
||||
assert last_http_request["is_premium"]
|
||||
assert last_http_request["active_subscription_end"] is None
|
||||
|
||||
|
||||
def test_webhook_with_subscription():
|
||||
user = create_new_user()
|
||||
end_at = arrow.utcnow().shift(days=1).replace(hour=0, minute=0, second=0)
|
||||
Subscription.create(
|
||||
user_id=user.id,
|
||||
cancel_url="",
|
||||
update_url="",
|
||||
subscription_id=random_token(10),
|
||||
event_time=arrow.now(),
|
||||
next_bill_date=end_at.date(),
|
||||
plan="yearly",
|
||||
flush=True,
|
||||
)
|
||||
execute_subscription_webhook(user)
|
||||
assert last_http_request["user_id"] == user.id
|
||||
assert last_http_request["is_premium"]
|
||||
assert last_http_request["active_subscription_end"] == end_at.timestamp
|
||||
|
||||
|
||||
def test_webhook_with_apple_subscription():
|
||||
user = create_new_user()
|
||||
end_at = arrow.utcnow().shift(days=2).replace(hour=0, minute=0, second=0)
|
||||
AppleSubscription.create(
|
||||
user_id=user.id,
|
||||
receipt_data=arrow.now().date().strftime("%Y-%m-%d"),
|
||||
expires_date=end_at.date().strftime("%Y-%m-%d"),
|
||||
original_transaction_id=random_token(10),
|
||||
plan="yearly",
|
||||
product_id="",
|
||||
flush=True,
|
||||
)
|
||||
execute_subscription_webhook(user)
|
||||
assert last_http_request["user_id"] == user.id
|
||||
assert last_http_request["is_premium"]
|
||||
assert last_http_request["active_subscription_end"] == end_at.timestamp
|
||||
|
||||
|
||||
def test_webhook_with_coinbase_subscription():
|
||||
user = create_new_user()
|
||||
end_at = arrow.utcnow().shift(days=3).replace(hour=0, minute=0, second=0)
|
||||
CoinbaseSubscription.create(
|
||||
user_id=user.id, end_at=end_at.date().strftime("%Y-%m-%d"), flush=True
|
||||
)
|
||||
|
||||
execute_subscription_webhook(user)
|
||||
assert last_http_request["user_id"] == user.id
|
||||
assert last_http_request["is_premium"]
|
||||
assert last_http_request["active_subscription_end"] == end_at.timestamp
|
||||
|
||||
|
||||
def test_webhook_with_manual_subscription():
|
||||
user = create_new_user()
|
||||
end_at = arrow.utcnow().shift(days=3).replace(hour=0, minute=0, second=0)
|
||||
ManualSubscription.create(
|
||||
user_id=user.id, end_at=end_at.date().strftime("%Y-%m-%d"), flush=True
|
||||
)
|
||||
|
||||
execute_subscription_webhook(user)
|
||||
assert last_http_request["user_id"] == user.id
|
||||
assert last_http_request["is_premium"]
|
||||
assert last_http_request["active_subscription_end"] == end_at.timestamp
|
Loading…
Reference in a new issue