Example #1
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')
        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')
        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')