Exemple #1
0
    def test_url_encoded_chars_in_text_raises_exception(self):
        """
        Test that a URLEncodedCharsFoundInText is raised because the text contains encoded characters after trying to
        decode for x (5) times.
        """
        fake = Faker()
        fake_text = fake.text(max_nb_chars=250)
        position_to_insert = fake_text.find('.')

        signal = SignalFactory.create()
        unquoted_url = 'https://*****:*****@test-domain.com/?query=param&extra=param'

        quoted_url = unquoted_url
        # Let's encode the URL 10 times
        for _ in range(10):
            quoted_url = quote(quoted_url)

        faked_text = f'{fake_text[:position_to_insert + 1]} {quoted_url} {fake_text[position_to_insert + 2:]}'

        self.assertIn(quoted_url, faked_text)

        signal.text = faked_text
        signal.text_extra = faked_text
        signal.save()

        with self.assertRaises(URLEncodedCharsFoundInText):
            make_email_context(signal=signal)
Exemple #2
0
    def test_make_email_context_for_category_with_or_without_public_name(self):
        # Check categories with public names.
        main_cat_with_public_name = ParentCategoryFactory.create(
            parent=None, name='PRIVATE_MAIN_I', public_name='PUBLIC_MAIN_I')
        sub_cat_with_public_name = CategoryFactory.create(
            parent=main_cat_with_public_name,
            name='PRIVATE_SUB',
            public_name='PUBLIC_SUB_I')
        signal_with_public_name = SignalFactory.create(
            category_assignment__category=sub_cat_with_public_name)

        context_with = make_email_context(signal=signal_with_public_name)
        self.assertEqual(context_with['main_category_public_name'],
                         'PUBLIC_MAIN_I')
        self.assertEqual(context_with['sub_category_public_name'],
                         'PUBLIC_SUB_I')

        # Check categories without public names.
        main_cat_no_public_name = ParentCategoryFactory.create(
            parent=None, name='PRIVATE_MAIN_II', public_name=None)
        sub_cat_no_public_name = CategoryFactory.create(
            parent=main_cat_no_public_name,
            name='PRIVATE_SUB_II',
            public_name=None)
        signal_no_public_name = SignalFactory.create(
            category_assignment__category=sub_cat_no_public_name)

        context_without = make_email_context(signal=signal_no_public_name)
        self.assertEqual(context_without['main_category_public_name'],
                         'PRIVATE_MAIN_II')
        self.assertEqual(context_without['sub_category_public_name'],
                         'PRIVATE_SUB_II')
Exemple #3
0
    def test_make_email_context_with_encoded_urls(self):
        fake = Faker()
        fake_text = fake.text(max_nb_chars=250)
        position_to_insert = fake_text.find('.')

        signal = SignalFactory.create()
        for schema in self.test_schemas:
            for uri in self.test_uris:
                unquoted_url = f'{schema}{uri}'
                quoted_url = quote(unquoted_url)
                faked_text = f'{fake_text[:position_to_insert + 1]} {quoted_url} {fake_text[position_to_insert + 2:]}'

                if schema:
                    self.assertNotIn(unquoted_url, faked_text)
                self.assertIn(quoted_url, faked_text)

                signal.text = faked_text
                signal.text_extra = faked_text
                signal.save()

                if schema:
                    self.assertNotIn(unquoted_url, signal.text)
                    self.assertNotIn(unquoted_url, signal.text_extra)

                self.assertIn(quoted_url, signal.text)
                self.assertIn(quoted_url, signal.text_extra)

                context = make_email_context(signal=signal)

                self.assertNotIn(unquoted_url, context['text'])
                self.assertNotIn(unquoted_url, context['text_extra'])

                self.assertNotIn(quoted_url, context['text'])
                self.assertNotIn(quoted_url, context['text_extra'])
Exemple #4
0
 def get_context(self, signal, dry_run=False):
     """
     Email context
     """
     context = make_email_context(
         signal, self.get_additional_context(signal, dry_run), dry_run)
     return context
Exemple #5
0
    def test_make_email_context_additional_context_should_not_override_default_variables(
            self):
        signal = SignalFactory.create()
        context = make_email_context(signal=signal,
                                     additional_context={'signal_id': 'test'})

        self.assertEqual(context['signal_id'], signal.id)
        self.assertNotEqual(context['signal_id'], 'test')
Exemple #6
0
    def test_make_email_context_backwards_compatibility(self):
        """
        TODO: should be removed a.s.a.p.
        """
        signal = SignalFactory.create()
        context = make_email_context(signal=signal)

        self.assertEqual(context['signal'].id, signal.id)
        self.assertEqual(context['status']._signal_id, signal.id)
        self.assertEqual(context['status'].state, signal.status.state)
Exemple #7
0
    def test_make_email_context(self):
        signal = SignalFactory.create()
        context = make_email_context(signal=signal)

        self.assertEqual(context['signal_id'], signal.id)
        self.assertEqual(context['formatted_signal_id'], signal.sia_id)
        self.assertEqual(context['created_at'], signal.created_at)
        self.assertEqual(context['text'], signal.text)
        self.assertEqual(context['text_extra'], signal.text_extra)
        self.assertEqual(context['address'], signal.location.address)
        self.assertEqual(context['status_text'], signal.status.text)
        self.assertEqual(context['status_state'], signal.status.state)
        self.assertEqual(context['handling_message'], signal.category_assignment.stored_handling_message)
        self.assertEqual(context['ORGANIZATION_NAME'], settings.ORGANIZATION_NAME)
Exemple #8
0
    def test_double_url_encoded_chars_in_text(self):
        """
        Test that double encoded chars are decoded and URLs have been removed from the text.
        """
        fake = Faker()
        fake_text = fake.text(max_nb_chars=250)
        position_to_insert = fake_text.find('.')

        signal = SignalFactory.create()
        for schema in self.test_schemas:
            for uri in self.test_uris:
                unquoted_url = f'{schema}{uri}'
                quoted_url = quote(quote(unquoted_url))  # double quote
                faked_text = f'{fake_text[:position_to_insert + 1]} {quoted_url} {fake_text[position_to_insert + 2:]}'

                if schema:
                    self.assertNotIn(unquoted_url, faked_text)
                self.assertIn(quoted_url, faked_text)

                signal.text = faked_text
                signal.text_extra = faked_text
                signal.save()

                if schema:
                    self.assertNotIn(unquoted_url, signal.text)
                    self.assertNotIn(unquoted_url, signal.text_extra)

                self.assertIn(quoted_url, signal.text)
                self.assertIn(quoted_url, signal.text_extra)

                context = make_email_context(signal=signal)

                self.assertNotIn(unquoted_url, context['text'])
                self.assertNotIn(unquoted_url, context['text_extra'])

                self.assertNotIn(quoted_url, context['text'])
                self.assertNotIn(quoted_url, context['text_extra'])
Exemple #9
0
    def _get_mail_context(self, signal: Signal, mail_kwargs: dict):
        additional_context = {}
        if 'context' in mail_kwargs and callable(mail_kwargs['context']):
            additional_context.update(mail_kwargs['context'](signal))

        return make_email_context(signal=signal, additional_context=additional_context)