Ejemplo n.º 1
0
    def test_key_equal(self):
        client1 = self._client(locale='en-US', startpage_version=4)
        client2 = self._client(locale='en-US', startpage_version=4)
        bundle1 = SnippetBundle(client1)
        bundle1.snippets = [self.snippet1, self.snippet2]
        bundle2 = SnippetBundle(client2)
        bundle2.snippets = [self.snippet1, self.snippet2]

        self.assertEqual(bundle1.key, bundle2.key)
Ejemplo n.º 2
0
    def test_key_template_modified(self):
        client1 = self._client(locale='en-US', startpage_version=4)
        bundle = SnippetBundle(client1)
        bundle.snippets = [self.snippet1]
        key_1 = bundle.key

        # save template, touch modified
        self.snippet1.template.save()
        bundle = SnippetBundle(client1)
        bundle.snippets = [self.snippet1]
        key_2 = bundle.key
        self.assertNotEqual(key_1, key_2)
Ejemplo n.º 3
0
    def test_key_snippets(self):
        """
        bundle.key must be different between bundles if they have
        different snippets.
        """
        client = self._client()
        bundle1 = SnippetBundle(client)
        bundle1.snippets = [self.snippet1, self.snippet2]
        bundle2 = SnippetBundle(client)
        bundle2.snippets = [self.snippet2]

        self.assertNotEqual(bundle1.key, bundle2.key)
Ejemplo n.º 4
0
    def test_key_current_firefox_version(self):
        client1 = self._client(locale='en-US', startpage_version=4)
        bundle = SnippetBundle(client1)
        bundle.snippets = [self.snippet1]
        key_1 = bundle.key

        with patch('snippets.base.util.current_firefox_major_version') as cfmv:
            cfmv.return_value = 'xx'
            bundle = SnippetBundle(client1)
            bundle.snippets = [self.snippet1]
            key_2 = bundle.key
        self.assertNotEqual(key_1, key_2)
Ejemplo n.º 5
0
    def test_generate_activity_stream(self):
        """
        bundle.generate should render the snippets, save them to the
        filesystem, and mark the bundle as not-expired in the cache for
        activity stream!
        """
        bundle = SnippetBundle(self._client(locale='fr', startpage_version=5))
        bundle.storage = Mock()
        bundle.snippets = [self.snippet1, self.snippet2]

        with patch('snippets.base.bundles.cache') as cache:
            with patch('snippets.base.bundles.render_to_string') as render_to_string:
                with patch('snippets.base.bundles.default_storage') as default_storage:
                    with self.settings(SNIPPET_BUNDLE_TIMEOUT=10):
                        with patch('snippets.base.util.current_firefox_major_version') as cfmv:
                            cfmv.return_value = '45'
                            render_to_string.return_value = 'rendered snippet'
                            bundle.generate()

        render_to_string.assert_called_with('base/fetch_snippets_as.jinja', {
            'snippet_ids': [s.id for s in [self.snippet1, self.snippet2]],
            'snippets_json': json.dumps([s.to_dict() for s in [self.snippet1, self.snippet2]]),
            'client': bundle.client,
            'locale': 'fr',
            'settings': settings,
            'current_firefox_major_version': '45',
        })
        default_storage.save.assert_called_with(bundle.filename, ANY)
        cache.set.assert_called_with(bundle.cache_key, True, ONE_DAY)

        # Check content of saved file.
        content_file = default_storage.save.call_args[0][1]
        self.assertEqual(content_file.read(), b'rendered snippet')
Ejemplo n.º 6
0
        def _test(client):
            bundle = SnippetBundle(
                self._client(locale='fr', startpage_version=5))
            bundle.storage = Mock()
            bundle.snippets = [self.snippet1, self.snippet2]

            with patch('snippets.base.bundles.cache') as cache:
                with patch('snippets.base.bundles.render_to_string'
                           ) as render_to_string:
                    with patch('snippets.base.bundles.default_storage'
                               ) as default_storage:
                        with patch('snippets.base.bundles.brotli',
                                   wraps=brotli) as brotli_mock:
                            render_to_string.return_value = 'rendered snippet'
                            bundle.generate()

            brotli_mock.compress.assert_called_with(b'rendered snippet')
            default_storage.save.assert_called_with(bundle.filename, ANY)
            cache.set.assert_called_with(bundle.cache_key, True, ONE_DAY)

            # Check content of saved file.
            content_file = default_storage.save.call_args[0][1]
            self.assertEqual(content_file.content_encoding, 'br')
            self.assertEqual(content_file.read(),
                             b'\x8b\x07\x80rendered snippet\x03')
Ejemplo n.º 7
0
        def _test(client):
            bundle = SnippetBundle(self._client(locale='fr', startpage_version=5))
            bundle.storage = Mock()
            bundle.snippets = [self.snippet1, self.snippet2]

            with patch('snippets.base.bundles.cache') as cache:
                with patch('snippets.base.bundles.render_to_string') as render_to_string:
                    with patch('snippets.base.bundles.default_storage') as default_storage:
                        with patch('snippets.base.bundles.brotli', wraps=brotli) as brotli_mock:
                            render_to_string.return_value = 'rendered snippet'
                            bundle.generate()

            brotli_mock.compress.assert_called_with(b'rendered snippet')
            default_storage.save.assert_called_with(bundle.filename, ANY)
            cache.set.assert_called_with(bundle.cache_key, True, ONE_DAY)

            # Check content of saved file.
            content_file = default_storage.save.call_args[0][1]
            self.assertEqual(content_file.content_encoding, 'br')
            self.assertEqual(content_file.read(), b'\x8b\x07\x80rendered snippet\x03')
Ejemplo n.º 8
0
    def test_empty(self):
        bundle = SnippetBundle(self._client(locale='fr', startpage_version=5))
        self.assertTrue(bundle.empty)

        bundle.snippets = [self.snippet1, self.snippet2]
        self.assertFalse(bundle.empty)