forked from breatheco-de/apiv2
-
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.
fix hook manager timedelta serialization
- Loading branch information
Showing
7 changed files
with
202 additions
and
4 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
Empty file.
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,80 @@ | ||
from datetime import timedelta | ||
from unittest.mock import MagicMock, call | ||
|
||
import pytest | ||
|
||
from breathecode.notify import tasks | ||
from breathecode.notify.utils.hook_manager import HookManager | ||
from breathecode.tests.mixins.breathecode_mixin.breathecode import Breathecode | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mocks(db, monkeypatch): | ||
m1 = MagicMock() | ||
monkeypatch.setattr(tasks.async_deliver_hook, 'delay', m1) | ||
yield m1 | ||
|
||
|
||
def get_model_label(instance): | ||
if instance is None: | ||
return None | ||
opts = instance._meta.concrete_model._meta | ||
try: | ||
return opts.label | ||
except AttributeError: | ||
return '.'.join([opts.app_label, opts.object_name]) | ||
|
||
|
||
def test_transform_timedeltas(bc: Breathecode, enable_signals, enable_hook_manager, mocks): | ||
enable_hook_manager() | ||
mock = mocks | ||
|
||
model = bc.database.create(hook={'event': 'survey.created'}, | ||
user={ | ||
'username': 'test', | ||
'is_superuser': True | ||
}, | ||
academy={ | ||
'slug': 'test', | ||
'available_as_saas': True, | ||
}, | ||
survey=1) | ||
|
||
x = {'feedback.Survey': {'created': ('survey.created', False)}} | ||
HookManager.HOOK_EVENTS = {'survey.created': 'feedback.Survey.created+'} | ||
# HookManager._HOOK_EVENT_ACTIONS_CONFIG = x | ||
HookManager._HOOK_EVENT_ACTIONS_CONFIG = None | ||
|
||
HookManager.process_model_event( | ||
model.survey, | ||
get_model_label(model.survey.__class__), | ||
'created', | ||
payload_override={ | ||
'delta': timedelta(days=3), | ||
'children': [ | ||
{ | ||
'delta': timedelta(days=4) | ||
}, | ||
{ | ||
'delta': timedelta(days=5) | ||
}, | ||
] | ||
}, | ||
) | ||
|
||
assert bc.database.list_of('feedback.Survey') == [bc.format.to_dict(model.survey)] | ||
|
||
assert mock.call_args_list == [ | ||
call(model.hook.target, { | ||
'delta': 259200.0, | ||
'children': [ | ||
{ | ||
'delta': 345600.0 | ||
}, | ||
{ | ||
'delta': 432000.0 | ||
}, | ||
] | ||
}, | ||
hook_id=1), | ||
] |
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
81 changes: 81 additions & 0 deletions
81
breathecode/payments/tests/signals/tests_subscription_created.py
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,81 @@ | ||
from datetime import datetime | ||
from unittest.mock import MagicMock, call | ||
|
||
import pytest | ||
|
||
from breathecode.notify import tasks | ||
from breathecode.tests.mixins.breathecode_mixin.breathecode import Breathecode | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mocks(db, monkeypatch): | ||
m1 = MagicMock() | ||
monkeypatch.setattr(tasks.async_deliver_hook, 'delay', m1) | ||
yield m1 | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def base(db, bc: Breathecode): | ||
model = bc.database.create(hook={'event': 'subscription.subscription_created'}, | ||
user={'username': 'test'}, | ||
academy={ | ||
'slug': 'test', | ||
'available_as_saas': True, | ||
}) | ||
yield model | ||
|
||
|
||
def serializer(subscription, user=None, academy=None): | ||
academy_obj = None | ||
if academy: | ||
academy_obj = { | ||
'id': academy.id, | ||
'name': academy.name, | ||
'slug': academy.slug, | ||
} | ||
|
||
user_obj = None | ||
if user: | ||
user_obj = { | ||
'first_name': user.first_name, | ||
'last_name': user.last_name, | ||
'email': user.email, | ||
} | ||
|
||
return { | ||
'id': subscription.id, | ||
'status': subscription.status, | ||
'status_message': subscription.status_message, | ||
'user': user_obj, | ||
'academy': academy_obj, | ||
'selected_cohort_set': subscription.selected_cohort_set, | ||
'selected_mentorship_service_set': subscription.selected_mentorship_service_set, | ||
'selected_event_type_set': subscription.selected_event_type_set, | ||
'plans': [], | ||
'invoices': [], | ||
'next_payment_at': subscription.next_payment_at, | ||
'valid_until': subscription.valid_until, | ||
'paid_at': subscription.paid_at, | ||
'is_refundable': subscription.is_refundable, | ||
'pay_every': subscription.pay_every, | ||
'pay_every_unit': subscription.pay_every_unit, | ||
} | ||
|
||
|
||
def test_nothing_happens(bc: Breathecode, enable_signals, enable_hook_manager, mocks, base): | ||
enable_signals('breathecode.payments.signals.subscription_created') | ||
enable_hook_manager() | ||
|
||
mock = mocks | ||
model = bc.database.create( | ||
subscription=2, | ||
user=base.user, | ||
academy=base.academy, | ||
) | ||
|
||
assert bc.database.list_of('payments.subscription') == bc.format.to_dict(model.subscription) | ||
|
||
assert mock.call_args_list == [ | ||
call(base.hook.target, serializer(model.subscription[0], user=model.user, academy=model.academy), hook_id=1), | ||
call(base.hook.target, serializer(model.subscription[1], user=model.user, academy=model.academy), hook_id=1), | ||
] |
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