Example #1
0
    def test_key_locale(self):
        """
        bundle.key must be different between bundles if they have
        different locales.
        """
        client1 = self._client(locale='en-US')
        client2 = self._client(locale='fr')
        bundle1 = SnippetBundle(client1)
        bundle2 = SnippetBundle(client2)

        self.assertNotEqual(bundle1.key, bundle2.key)
Example #2
0
    def test_key_startpage_version(self):
        """
        bundle.key must be different between bundles if they have
        different startpage versions.
        """
        client1 = self._client(startpage_version=1)
        client2 = self._client(startpage_version=2)
        bundle1 = SnippetBundle(client1)
        bundle2 = SnippetBundle(client2)

        self.assertNotEqual(bundle1.key, bundle2.key)
Example #3
0
def fetch_snippet_bundle(request, **kwargs):
    """
    Return one of the following responses:
    - 200 with empty body when the bundle is empty
    - 302 to a bundle URL after generating it if not cached.
    """
    statsd.incr('serve.snippets')

    client = Client(**kwargs)
    if client.startpage_version == 6:
        bundle = ASRSnippetBundle(client)
    else:
        bundle = SnippetBundle(client)
    if bundle.empty:
        statsd.incr('bundle.empty')

        if client.startpage_version == 6:
            # Return valid JSON for Activity Stream Router
            return HttpResponse(status=200,
                                content='{}',
                                content_type='application/json')

        # This is not a 204 because Activity Stream expects content, even if
        # it's empty.
        return HttpResponse(status=200, content='')
    elif bundle.cached:
        statsd.incr('bundle.cached')
    else:
        statsd.incr('bundle.generate')
        bundle.generate()

    return HttpResponseRedirect(bundle.url)
Example #4
0
 def test_key_funny_characters(self):
     """
     bundle.key should generate even when client contains strange unicode
     characters
     """
     client = self._client(channel='release-cck- \xe2\x80\x9cubuntu\xe2\x80\x9d')
     SnippetBundle(client).key
Example #5
0
 def test_not_cached(self):
     bundle = SnippetBundle(self._client(locale='fr', startpage_version=5))
     with patch('snippets.base.bundles.cache') as cache:
         cache.get.return_value = False
         with patch('snippets.base.bundles.default_storage') as default_storage:
             default_storage.exists.return_value = False
             self.assertFalse(bundle.cached)
Example #6
0
 def test_cached_remote(self):
     bundle = SnippetBundle(self._client(locale='fr', startpage_version=5))
     with patch('snippets.base.bundles.cache') as cache:
         cache.get.return_value = False
         with patch('snippets.base.bundles.default_storage') as default_storage:
             default_storage.exists.return_value = True
             self.assertTrue(bundle.cached)
             cache.set.assert_called_with(bundle.cache_key, True, ONE_DAY)
        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')
Example #8
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')
Example #10
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)
Example #11
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)
Example #12
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)
Example #13
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)
Example #14
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)
Example #15
0
 def test_cached_local(self):
     bundle = SnippetBundle(self._client(locale='fr', startpage_version=5))
     with patch('snippets.base.bundles.cache') as cache:
         cache.get.return_value = True
         self.assertTrue(bundle.cached)