Пример #1
0
    def test_widget_plugin(self):
        form = build_plugin(FormPlugin, name="test")
        field = build_plugin(FormFieldPlugin,
                             form,
                             name="field1",
                             field_type=TypeReference(forms.CharField))
        form.child_plugin_instances = [field]  # Simulate CMS tree
        widget = build_plugin(WidgetPlugin,
                              form,
                              widget_type=TypeReference(widgets.Textarea))
        field.child_plugin_instances = [widget]  # Simulate CMS tree

        form_cls = form.build_form_cls()
        frm = form_cls({"field1": "1"})
        assert isinstance(frm.fields["field1"].widget, forms.Textarea)
        assert frm.is_valid(), frm.errors

        assert WidgetPlugin(WidgetPlugin).render(context={},
                                                 instance=widget,
                                                 placeholder=None)

        self._check_widget_plugin(plugin_cls=WidgetPlugin,
                                  widget_cls=widgets.Widget,
                                  creation_data={},
                                  expected_data={})
Пример #2
0
    def test_submission_view(self):
        form = build_plugin(FormPlugin, name="test")
        build_plugin(FormFieldPlugin, form, name="field1")
        build_plugin(FormFieldPlugin, form, name="field2")
        view = BaseFormSubmissionView.as_view(model=Form)

        # Get rendering
        response = view(self.cms_request(), form_pk=form.pk)
        content = clean_html(response.content.decode("utf-8"))
        assert response.status_code == 200
        assert "<form" not in content
        assert "<input" in content
        assert 'id="id_field1"' in content
        assert 'name="field1"' in content
        assert 'name="field2"' in content

        # Post processing and rendering
        response = view(self.cms_request("post", data={"field1": "data1", "field2": "data2"}), form_pk=form.pk)
        content = clean_html(response.content.decode("utf-8"))
        assert response.status_code == 200
        assert "<form" not in content
        assert "<input" in content
        assert 'id="id_field1"' in content
        assert 'name="field1"' in content
        assert 'name="field2"' in content
        assert 'value="data1"' in content
        assert 'value="data1"' in content
Пример #3
0
    def test_saving_form(self):
        form = build_plugin(
            FormPlugin, name="test", form_type=TypeReference(SavingForm), meta_parameters={"success_content": "Yay"}
        )
        field = build_plugin(FormFieldPlugin, form, name="field1")
        form.child_plugin_instances = [field]  # Simulate CMS tree

        frm = form.build_form_cls()({"field1": "test1", "other": "stuff"})
        assert isinstance(frm, SavingForm)
        assert frm.is_valid(), frm.errors
        response = frm.cms_save(None, form)
        submission = FormSubmission.objects.get()
        assert submission.form_name == "test"
        assert submission.data == {"field1": "test1"}
        assert response.status_code == 200
        assert response.content == b"Yay"
Пример #4
0
 def test_get_plugin(self):
     form = build_plugin(FormPlugin, name="test")
     form2 = Form.objects.create(name="test2")
     assert get_plugin(None, Form, form.pk) == form
     with pytest.raises(Http404):
         get_plugin(self.cms_request(), Form, form2.pk + 1)
     with pytest.raises(Http404):
         get_plugin(self.cms_request(), Form, form2.pk)
Пример #5
0
    def test_submission_view_custom_form(self):
        # redirect response
        form = build_plugin(FormPlugin, name="test", form_type=TypeReference(RedirectingForm))
        view = BaseFormSubmissionView.as_view(model=Form)
        response = view(self.cms_request("post", data={}), form_pk=form.pk)
        assert response.status_code == 302
        assert response.url == "/success/"

        # form not valid
        form = build_plugin(FormPlugin, name="test", form_type=TypeReference(SuccessForm), auto_render_fields=True)
        response = view(self.cms_request("post", data={}), form_pk=form.pk)
        content = clean_html(response.content.decode("utf-8"))
        assert response.status_code == 200
        assert "<input" in content

        # plain response
        response = view(self.cms_request("post", data={"field1": 1}), form_pk=form.pk)
        content = clean_html(response.content.decode("utf-8"))
        assert response.status_code == 200
        assert content == "Success!"
Пример #6
0
    def test_choice_plugin(self):
        form = build_plugin(FormPlugin, name="test")
        field = build_plugin(ChoiceFieldPlugin,
                             form,
                             name="field1",
                             field_type=TypeReference(forms.ChoiceField))
        form.child_plugin_instances = [field]  # Simulate CMS tree
        choice1 = build_plugin(ChoiceOptionPlugin,
                               form,
                               value="1",
                               display="One")
        choice2 = build_plugin(ChoiceOptionPlugin,
                               form,
                               value="2",
                               display="Two")
        field.child_plugin_instances = [choice1, choice2]  # Simulate CMS tree

        choices = list(ChoiceOption.objects.values_list("value", "display"))
        assert choices == [("1", "One"), ("2", "Two")]

        form_cls = form.build_form_cls()
        frm = form_cls({"field1": "1"})
        assert frm.fields["field1"].choices == choices
        assert frm.is_valid(), frm.errors
        frm = form_cls({"field1": "3"})
        assert not frm.is_valid(), frm.errors

        plugin = self._check_plugin(
            plugin_cls=ChoiceOptionPlugin,
            plugin_model=ChoiceOption,
            creation_data={
                "value": "1",
                "display": "One"
            },
            expected_data={
                "value": "1",
                "display": "One"
            },
        )
        content = self.render_plugin_instance(ChoiceOptionPlugin, plugin)
        assert content == ""
Пример #7
0
    def test_saving_form_files(self):
        form = build_plugin(
            FormPlugin, name="test", form_type=TypeReference(SavingForm), meta_parameters={"success_url": "/thank-you/"}
        )
        field1 = build_plugin(FormFieldPlugin, form, name="field1")
        field2 = build_plugin(
            FormFieldPlugin, form, name="field2", field_type=TypeReference("django.forms.fields.FileField")
        )
        form.child_plugin_instances = [field1, field2]  # Simulate CMS tree

        frm = form.build_form_cls()({"field1": "test1"}, {"field2": SimpleUploadedFile("text.txt", b"content")})
        assert isinstance(frm, SavingForm)
        assert frm.is_valid(), frm.errors
        response = frm.cms_save(None, form)
        submission = FormSubmission.objects.get()
        assert submission.form_name == "test"
        assert submission.data == {"field1": "test1"}
        assert response.status_code == 302
        assert response.url == "/thank-you/"
        submission_file = FormSubmissionFile.objects.get()
        assert submission_file.submission == submission
        assert submission_file.field_name == "field2"
        assert submission_file.file.read() == b"content"
Пример #8
0
    def test_field_plugin(self):
        form = build_plugin(FormPlugin, name="test")
        field = build_plugin(
            FormFieldPlugin,
            form,
            name="field1",
            field_type=TypeReference(forms.CharField),
            field_parameters={
                "disabled": True,
                "help_text": "help",
                "initial": "5",
                "label": "lab",
                "label_suffix": "suf",
                "localize": True,
                "required": True,
                "show_hidden_initial": True,
            },
        )
        form.child_plugin_instances = [field]  # Simulate CMS tree

        form_cls = form.build_form_cls()
        frm = form_cls({"field1": "1"})
        assert isinstance(frm.fields["field1"], forms.CharField)
        assert frm.fields["field1"].disabled is True
        assert frm.fields["field1"].help_text == "help"
        assert frm.fields["field1"].initial == "5"
        assert frm.fields["field1"].label == "lab"
        assert frm.fields["field1"].label_suffix == "suf"
        assert frm.fields["field1"].localize is True
        assert frm.fields["field1"].required is True
        assert frm.fields["field1"].show_hidden_initial is True
        assert frm.is_valid(), frm.errors

        self._check_field_plugin(plugin_cls=FormFieldPlugin,
                                 field_cls=fields.Field,
                                 creation_data={},
                                 expected_data={})
Пример #9
0
 def test_render_plugin(self):
     form = build_plugin(FormPlugin, name="test")
     content = render_plugin(self.cms_request(), form)
     assert "<form" in content