def test_none_condop(): step = Step(cond_op=StepConditionOperator.NONE, conditions=[ NonEmpty({"v": {"variable": "a"}}), NonEmpty({"v": {"variable": "b"}}), ], actions=[SetDebugFlag({})]) context = Context.from_variables(a=False, b=False) step.execute(context) assert context.get("debug")
def test_render_template(): step = Step( conditions=(), actions=[Action.unserialize(action) for action in TEST_STEP_ACTIONS], ) assert step execution_context = Context(variables={ "customer_phone": "0594036495", "language": "fi", "customer_email": "*****@*****.**" }) step.execute(context=execution_context)
def test_condops(cond_op): step = Step(cond_op=cond_op, conditions=[ NonEmpty({"v": {"variable": "a"}}), NonEmpty({"v": {"variable": "b"}}), ], actions=[SetDebugFlag({})]) context = Context.from_variables(a=True, b=False) step.execute(context) if cond_op == StepConditionOperator.ALL: assert not context.get("debug") elif cond_op == StepConditionOperator.ANY: assert context.get("debug") elif cond_op == StepConditionOperator.NONE: assert not context.get("debug") else: raise ValueError("Unexpected condop %r" % cond_op)
def get_script_steps(self, form): action_data = { "template_data": {}, "recipient": {"constant": form["base"].cleaned_data["recipient"]}, "language": {"variable": "language"}, "fallback_language": {"constant": settings.PARLER_DEFAULT_LANGUAGE_CODE} } for language in form.forms: form_lang = form[language] # tries to get the cleaned data, otherwise the initial value # since cleaned_data can be blank if the user did not change anything action_data["template_data"][language] = { "content_type": "html", "subject": form_lang.cleaned_data.get("subject", form_lang.initial.get("subject", "")).strip(), "body": form_lang.cleaned_data.get("body", form_lang.initial.get("body", "")).strip() } send_mail_action = SendEmail(action_data) conditions = [] if form["base"].cleaned_data.get("last24hrs"): conditions.append(BooleanEqual({ "v1": {"variable": "dispatched_last_24hs"}, "v2": {"constant": (not form["base"].cleaned_data["last24hrs"])} })) return [Step(next=StepNext.STOP, actions=(send_mail_action,), conditions=conditions)]
def test_run(): event = get_initialized_test_event() step = Step(actions=[ AddOrderLogEntry({ "order": { "variable": "order" }, "message": { "constant": "It Works." }, "message_identifier": { "constant": "test_run" }, }) ], next=StepNext.STOP) script = Script(event_identifier=event.identifier, name="Test Script") script.set_steps([step]) script.save() event.run() # The script is disabled by default, of course it won't run assert not event.variable_values["order"].log_entries.filter( identifier="test_run").exists() # Let's try that again. script.enabled = True script.save() event.run() assert event.variable_values["order"].log_entries.filter( identifier="test_run").exists() script.delete()
def get_script_steps(self, form): action_data = { "template_data": {}, "language": { "constant": settings.PARLER_DEFAULT_LANGUAGE_CODE }, } if form["base"].cleaned_data.get("send_to") == "other": action_data["recipient"] = { "constant": form["base"].cleaned_data["recipient"] } else: action_data["recipient"] = {"variable": "customer_email"} for language in form.forms: form_lang = form[language] # tries to get the cleaned data, otherwise the initial value # since cleaned_data will be blank if the user did not change anything action_data["template_data"][language] = { "content_type": "html", "subject": form_lang.cleaned_data.get( "subject", form_lang.initial.get("subject", "")).strip(), "body": form_lang.cleaned_data.get("body", form_lang.initial.get("body", "")).strip() } send_mail_action = SendEmail(action_data) return [Step(next=StepNext.STOP, actions=(send_mail_action, ))]
def test_run_multishop(): shop1 = factories.get_default_shop() shop2 = factories.get_shop(identifier="shop2") event = get_initialized_test_event() step = Step(actions=[ AddOrderLogEntry({ "order": { "variable": "order" }, "message": { "constant": "It Works." }, "message_identifier": { "constant": "test_run" }, }) ], next=StepNext.STOP) script = Script(event_identifier=event.identifier, name="Test Script", shop=shop2, enabled=True) script.set_steps([step]) script.save() # runs for shop1 - no script exists event.run(shop1) assert not event.variable_values["order"].log_entries.filter( identifier="test_run").exists() # run for shop2 - ok event.run(shop2) assert event.variable_values["order"].log_entries.filter( identifier="test_run").exists() script.delete()
def test_notify_on_company_created(regular_user, allow_company_registration): if "shuup.front.apps.customer_information" not in settings.INSTALLED_APPS: pytest.skip("shuup.front.apps.customer_information required in installed apps") if "shuup.notify" not in settings.INSTALLED_APPS: pytest.skip("shuup.notify required in installed apps") configuration.set(None, "allow_company_registration", allow_company_registration) step = Step( cond_op=StepConditionOperator.NONE, actions=[ AddNotification( { "message": {"constant": "It Works. {{ customer_email }}"}, "message_identifier": {"constant": "company_created"}, } ) ], next=StepNext.STOP, ) script = Script( event_identifier=CompanyAccountCreated.identifier, name="Test Script", enabled=True, shop=get_default_shop() ) script.set_steps([step]) script.save() assert not Notification.objects.filter(identifier="company_created").exists() assert get_person_contact(regular_user) assert not get_company_contact(regular_user) client = SmartClient() client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) company_edit_url = reverse("shuup:company_edit") if allow_company_registration: client.soup(company_edit_url) data = _default_company_data() data.update(_default_address_data("billing")) data.update(_default_address_data("shipping")) response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 assert get_company_contact(regular_user) assert Notification.objects.filter(identifier="company_created").count() == 1 notification = Notification.objects.filter(identifier="company_created").first() assert notification assert data["contact-email"] in notification.message # New save should not add new notifications response, soup = client.response_and_soup(company_edit_url, data, "post") assert response.status_code == 302 assert Notification.objects.filter(identifier="company_created").count() == 1 script.delete() else: response = client.get(company_edit_url) assert reverse("shuup:customer_edit") in response.url assert Notification.objects.filter(identifier="company_created").count() == 0
def get_steps(self): """ :rtype Iterable[Step] """ if getattr(self, "_steps", None) is None: from shuup.notify.script import Step self._steps = [Step.unserialize(data) for data in self._step_data] return self._steps
def create_script(self, shop, form=None): condition = BooleanEqual({ "v1": {"constant": True}, "v2": {"constant": False} }) script = Script(event_identifier=AlertLimitReached.identifier, name="Dummy Alert", enabled=True, shop=shop) script.set_steps([Step(next=StepNext.STOP, conditions=(condition,))]) script.save() return script
def test_load_save(): sc = Script(event_identifier=ATestEvent.identifier, name="Test Script") assert force_text(sc) == "Test Script" sc.set_serialized_steps(TEST_STEP_DATA) sc.save() sc = Script.objects.get(pk=sc.pk) first_step = sc.get_steps()[0] first_step_data = TEST_STEP_DATA[0] step_from_data = Step.unserialize(first_step_data) data_from_step = first_step.serialize() assert data_from_step == first_step_data assert first_step == step_from_data
def test_load_save(): sc = Script(event_identifier=ATestEvent.identifier, name="Test Script", shop=factories.get_default_shop()) assert force_text(sc) == "Test Script" sc.set_serialized_steps(TEST_STEP_DATA) sc.save() sc = Script.objects.get(pk=sc.pk) first_step = sc.get_steps()[0] first_step_data = TEST_STEP_DATA[0] step_from_data = Step.unserialize(first_step_data) data_from_step = first_step.serialize() assert data_from_step == first_step_data assert first_step == step_from_data
def save(self): """ Create and configure the selected objects if needed """ # User wants a order notification and Notify installed and there is no script created previously if (self.is_valid() and self.cleaned_data["order_confirm_notification"] and djangoenv.has_installed("shuup.notify") and not self._get_saved_script()): from shuup.front.notify_events import OrderReceived from shuup.notify.models.script import Script from shuup.notify.script import Step, StepNext send_email_action = self._get_send_email_action() script = Script(event_identifier=OrderReceived.identifier, name="Order Received", enabled=True) script.set_steps([Step(next=StepNext.STOP, actions=(send_email_action,))]) script.save() # save the PK in the configs config.set(self.shop, BEHAVIOR_ORDER_CONFIRM_KEY, script.pk)
def test_conditionless_step_executes(): step = Step(actions=[SetDebugFlag({})]) context = Context() step.execute(context) assert context.get("debug")