Exemplo n.º 1
0
def fraud_report(request):
    form = FraudReportForm(auto_id='%s')

    form_submitted = False
    form_error = False

    if request.method == 'POST':
        form = FraudReportForm(request.POST, request.FILES)
        form_results = submit_form(request, form)

        form_submitted = form_results['form_submitted']
        form_error = form_results['form_error']

    template_vars = {
        'form': form,
        'form_submitted': form_submitted,
        'form_error': form_error,
    }

    if request.POST and not form_error:
        # Seeing the form was submitted without error, redirect, do not simply
        # send a response to avoid problem described below.
        # @see https://bugzilla.mozilla.org/show_bug.cgi?id=873476 (3.2)
        response = redirect(reverse('legal.fraud-report'), template_vars)
        response['Location'] += '?submitted=%s' % form_submitted

        return response
    else:
        # If the below is called after a redirect the template_vars will be lost, therefore
        # we need to update the form_submitted state from the submitted url parameter.
        submitted = request.GET.get('submitted') == 'True'
        template_vars['form_submitted'] = submitted
        return l10n_utils.render(request, 'legal/fraud-report.html', template_vars)
Exemplo n.º 2
0
def fraud_report(request):
    form = FraudReportForm(auto_id="%s")

    form_submitted = False
    form_error = False

    if request.method == "POST":
        form = FraudReportForm(request.POST, request.FILES)
        form_results = submit_form(request, form)

        form_submitted = form_results["form_submitted"]
        form_error = form_results["form_error"]

    template_vars = {
        "form": form,
        "form_submitted": form_submitted,
        "form_error": form_error,
    }

    if request.POST and not form_error:
        # Seeing the form was submitted without error, redirect, do not simply
        # send a response to avoid problem described below.
        # @see https://bugzilla.mozilla.org/show_bug.cgi?id=873476 (3.2)
        response = redirect(reverse("legal.fraud-report"), template_vars)
        response["Location"] += f"?submitted={form_submitted}"

        return response
    else:
        # If the below is called after a redirect the template_vars will be lost, therefore
        # we need to update the form_submitted state from the submitted url parameter.
        submitted = request.GET.get("submitted") == "True"
        template_vars["form_submitted"] = submitted
        return l10n_utils.render(request, "legal/fraud-report.html",
                                 template_vars)
Exemplo n.º 3
0
    def test_form_valid_data(self):
        """
        Form should be valid.
        """
        form = FraudReportForm(self.data)

        # make sure form is valid
        ok_(form.is_valid())
Exemplo n.º 4
0
    def test_form_valid_data(self):
        """
        Form should be valid.
        """
        form = FraudReportForm(self.data)

        # make sure form is valid
        ok_(form.is_valid())
Exemplo n.º 5
0
 def test_form_valid_attachement(self):
     """
     Form should be valid when attachment under/at size limit.
     """
     # attachment within size limit
     mock_attachment = self._create_image_file()
     form = FraudReportForm(self.data, {'input_attachment': mock_attachment})
     # make sure form is valid
     assert form.is_valid()
Exemplo n.º 6
0
    def test_form_honeypot(self):
        """
        Form with honeypot checkbox checked should not be valid.
        """
        self.data['superpriority'] = True

        form = FraudReportForm(self.data)

        eq_(False, form.is_valid())
Exemplo n.º 7
0
    def test_form_honeypot(self):
        """
        Form with honeypot text box filled should not be valid.
        """
        self.data['office_fax'] = 'spammer!'

        form = FraudReportForm(self.data)

        eq_(False, form.is_valid())
Exemplo n.º 8
0
    def test_form_honeypot(self):
        """
        Form with honeypot checkbox checked should not be valid.
        """
        self.data["superpriority"] = True

        form = FraudReportForm(self.data)

        eq_(False, form.is_valid())
Exemplo n.º 9
0
    def test_form_honeypot(self):
        """
        Form with honeypot text box filled should not be valid.
        """
        self.data['office_fax'] = 'spammer!'

        form = FraudReportForm(self.data)

        eq_(False, form.is_valid())
Exemplo n.º 10
0
    def test_form_honeypot(self):
        """
        Form with honeypot text box filled should not be valid.
        """
        self.data["office_fax"] = "spammer!"

        form = FraudReportForm(self.data)

        assert not form.is_valid()
Exemplo n.º 11
0
 def test_form_valid_attachement(self):
     """
     Form should be valid when attachment under/at size limit.
     """
     # attachment within size limit
     mock_attachment = self._create_image_file()
     form = FraudReportForm(self.data,
                            {'input_attachment': mock_attachment})
     # make sure form is valid
     assert form.is_valid()
Exemplo n.º 12
0
    def test_form_valid_attachement(self):
        """
        Form should be valid when attachment under/at size limit.
        """
        # attachment within size limit
        mock_attachment = Mock(_size=legal_forms.FRAUD_REPORT_FILE_SIZE_LIMIT)

        form = FraudReportForm(self.data, {'input_attachment': mock_attachment})

        # make sure form is valid
        ok_(form.is_valid())
Exemplo n.º 13
0
    def test_form_valid_attachement(self):
        """
        Form should be valid when attachment under/at size limit.
        """
        # attachment within size limit
        mock_attachment = Mock(_size=legal_forms.FRAUD_REPORT_FILE_SIZE_LIMIT)

        form = FraudReportForm(self.data, {'input_attachment': mock_attachment})

        # make sure form is valid
        ok_(form.is_valid())
Exemplo n.º 14
0
 def test_form_invalid_attachement_type(self):
     """
     Form should be invalid and contain attachment errors when attachment
     is not an image.
     """
     # attachment within size limit
     mock_attachment = self._create_text_file()
     form = FraudReportForm(self.data, {'input_attachment': mock_attachment})
     # make sure form is not valid
     assert not form.is_valid()
     # make sure attachment errors are in form
     self.assertIn('input_attachment', form.errors)
Exemplo n.º 15
0
 def test_form_invalid_attachement_type(self):
     """
     Form should be invalid and contain attachment errors when attachment
     is not an image.
     """
     # attachment within size limit
     mock_attachment = self._create_text_file()
     form = FraudReportForm(self.data,
                            {'input_attachment': mock_attachment})
     # make sure form is not valid
     assert not form.is_valid()
     # make sure attachment errors are in form
     self.assertIn('input_attachment', form.errors)
Exemplo n.º 16
0
    def test_form_invalid_attachement_size(self):
        """
        Form should be invalid and contain attachment errors when attachment
        over size limit.
        """
        # attachment within size limit
        mock_attachment = self._create_image_file()
        form = FraudReportForm(self.data, {'input_attachment': mock_attachment})
        with patch.object(legal_forms, 'FRAUD_REPORT_FILE_SIZE_LIMIT', 100):
            # make sure form is not valid
            assert not form.is_valid()

        # make sure attachment errors are in form
        self.assertIn('input_attachment', form.errors)
Exemplo n.º 17
0
    def test_form_invalid_data(self):
        """
        With incorrect data (missing url), form should not be valid and should
        have url in the errors hash.
        """
        self.data.update(input_url='')  # remove required url

        form = FraudReportForm(self.data)

        # make sure form is invalid
        eq_(False, form.is_valid())

        # make sure url errors are in form
        self.assertIn('input_url', form.errors)
Exemplo n.º 18
0
    def test_form_invalid_data(self):
        """
        With incorrect data (missing url), form should not be valid and should
        have url in the errors hash.
        """
        self.data.update(input_url='')  # remove required url

        form = FraudReportForm(self.data)

        # make sure form is invalid
        eq_(False, form.is_valid())

        # make sure url errors are in form
        self.assertIn('input_url', form.errors)
Exemplo n.º 19
0
    def test_form_invalid_attachement_size(self):
        """
        Form should be invalid and contain attachment errors when attachment
        over size limit.
        """
        # attachment within size limit
        mock_attachment = self._create_image_file()
        form = FraudReportForm(self.data,
                               {'input_attachment': mock_attachment})
        with patch.object(legal_forms, 'FRAUD_REPORT_FILE_SIZE_LIMIT', 100):
            # make sure form is not valid
            assert not form.is_valid()

        # make sure attachment errors are in form
        self.assertIn('input_attachment', form.errors)
Exemplo n.º 20
0
    def test_form_invalid_attachement(self):
        """
        Form should be invalid and contain attachment errors when attachment
        over size limit.
        """
        # attachment within size limit
        mock_attachment = Mock(_size=(legal_forms.FRAUD_REPORT_FILE_SIZE_LIMIT + 1))

        form = FraudReportForm(self.data, {"attachment": mock_attachment})

        # make sure form is not valid
        eq_(False, form.is_valid())

        # make sure attachment errors are in form
        self.assertIn("attachment", form.errors)
Exemplo n.º 21
0
    def test_email_with_attachement(self, mock_email_message,
                                    mock_render_to_string):
        """
        Make sure email is sent with attachment.
        """
        mock_attachment = self._create_image_file()

        form = FraudReportForm(self.data,
                               {'input_attachment': mock_attachment})

        # submit form
        request = self.factory.get('/')
        ret = submit_form(request, form)
        self.assertFalse(ret['form_error'])

        # make sure attachment was attached
        mock_attachment.seek(0)
        mock_email_message.return_value.attach.assert_called_once_with(
            mock_attachment.name, mock_attachment.read(),
            mock_attachment.content_type)

        # make sure email was sent
        mock_email_message.return_value.send.assert_called_once()

        # make sure email values are correct
        mock_email_message.assert_called_once_with(
            legal_views.FRAUD_REPORT_EMAIL_SUBJECT %
            (self.data['input_url'], self.data['input_category']), 'rendered',
            legal_views.FRAUD_REPORT_EMAIL_FROM,
            legal_views.FRAUD_REPORT_EMAIL_TO)
Exemplo n.º 22
0
    def test_form_invalid_attachement(self):
        """
        Form should be invalid and contain attachment errors when attachment
        over size limit.
        """
        # attachment within size limit
        mock_attachment = Mock(
            _size=(legal_forms.FRAUD_REPORT_FILE_SIZE_LIMIT + 1))

        form = FraudReportForm(self.data, {'input_attachment': mock_attachment})

        # make sure form is not valid
        eq_(False, form.is_valid())

        # make sure attachment errors are in form
        self.assertIn('input_attachment', form.errors)
Exemplo n.º 23
0
    def test_email_with_attachement(self, mock_email_message,
                                    mock_render_to_string):
        """
        Make sure email is sent with attachment.
        """
        mock_attachment = Mock(
            content_type='text/plain',
            _size=(legal_forms.FRAUD_REPORT_FILE_SIZE_LIMIT))

        # make sure name attribute is treated as string
        mock_attachment.name = 'img.jpg'

        form = FraudReportForm(self.data,
                               {'input_attachment': mock_attachment})

        # submit form
        request = self.factory.get('/')
        submit_form(request, form)

        # make sure attachment was attached
        mock_email_message.return_value.attach.assert_called_once_with(
            'img.jpg', mock_attachment.read.return_value, 'text/plain')

        mock_attachment.read.assert_called_once_with()

        # make sure email was sent
        mock_email_message.return_value.send.assert_called_once_with()

        # make sure email values are correct
        mock_email_message.assert_called_once_with(
            legal_views.FRAUD_REPORT_EMAIL_SUBJECT %
            (self.data['input_url'], self.data['input_category']), 'rendered',
            legal_views.FRAUD_REPORT_EMAIL_FROM,
            legal_views.FRAUD_REPORT_EMAIL_TO)
Exemplo n.º 24
0
    def test_email(self, mock_email_message, mock_render_to_string):
        """
        Make sure email is sent with expected values.
        """
        mock_send = Mock()
        mock_email_message.return_value = Mock(send=mock_send)

        form = FraudReportForm(self.data)

        # submit form
        request = self.factory.get('/')
        submit_form(request, form)

        # make sure email was sent
        mock_send.assert_called_once_with()

        # make sure email values are correct
        mock_email_message.assert_called_once_with(
            legal_views.FRAUD_REPORT_EMAIL_SUBJECT, 'jingo rendered',
            legal_views.FRAUD_REPORT_EMAIL_FROM,
            legal_views.FRAUD_REPORT_EMAIL_TO)