Esempio n. 1
0
    def test_generate(self):
        """
        bundle.generate should render the snippets, save them to the
        filesystem, and mark the bundle as not-expired in the cache.
        """
        bundle = SnippetBundle(self._client(locale='fr'))
        bundle.storage = Mock()
        bundle._snippets = [self.snippet1, self.snippet2]

        with patch('snippets.base.models.cache') as cache:
            with patch('snippets.base.models.render_to_string') as render_to_string:
                with patch('snippets.base.models.default_storage') as default_storage:
                    with self.settings(SNIPPET_BUNDLE_TIMEOUT=10):
                        with patch('snippets.base.models.version_list') as version_list:
                            version_list.return_value = ['45.0']
                            render_to_string.return_value = 'rendered snippet'
                            bundle.generate()

        render_to_string.assert_called_with('base/fetch_snippets.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_version': '45',
            'metrics_url': settings.METRICS_URL,
        })
        default_storage.save.assert_called_with(bundle.filename, ANY)
        cache.set.assert_called_with(bundle.cache_key, True, 10)

        # Check content of saved file.
        content_file = default_storage.save.call_args[0][1]
        self.assertEqual(content_file.read(), 'rendered snippet')
Esempio n. 2
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)
Esempio n. 3
0
    def test_generate(self):
        """
        bundle.generate should render the snippets, save them to the
        filesystem, and mark the bundle as not-expired in the cache.
        """
        bundle = SnippetBundle(self._client(locale='fr'))
        bundle.storage = Mock()
        bundle._snippets = [self.snippet1, self.snippet2]

        with patch('snippets.base.models.cache') as cache:
            with patch('snippets.base.models.render_to_string') as render_to_string:
                with self.settings(SNIPPET_BUNDLE_TIMEOUT=10):
                    render_to_string.return_value = 'rendered snippet'
                    bundle.generate()

        render_to_string.assert_called_with('base/fetch_snippets.html', {
            'snippets': [self.snippet1, self.snippet2],
            'client': bundle.client,
            'locale': 'fr',
        })
        bundle.storage.save.assert_called_with(bundle.filename, ANY)
        cache.set.assert_called_with(bundle.cache_key, True, 10)

        # Check content of saved file.
        content_file = bundle.storage.save.call_args[0][1]
        eq_(content_file.read(), 'rendered snippet')
Esempio n. 4
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)
Esempio n. 5
0
def fetch_pregenerated_snippets(request, **kwargs):
    """
    Return a redirect to a pre-generated bundle of snippets for the
    client. If the bundle in question is expired, re-generate it.
    """
    client = Client(**kwargs)
    bundle = SnippetBundle(client)
    if bundle.expired:
        bundle.generate()

    return HttpResponseRedirect(bundle.url)
Esempio n. 6
0
def fetch_pregenerated_snippets(request, **kwargs):
    """
    Return a redirect to a pre-generated bundle of snippets for the
    client. If the bundle in question is expired, re-generate it.
    """
    client = Client(**kwargs)
    bundle = SnippetBundle(client)
    if bundle.expired:
        bundle.generate()

    return HttpResponseRedirect(bundle.url)
Esempio n. 7
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)
Esempio n. 8
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)
Esempio n. 9
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)
Esempio n. 10
0
 def test_key_funny_characters(self):
     """
     bundle.key should generate even when client contains strange unicode
     characters
     """
     client = self._client(
         channel=u'release-cck- \xe2\x80\x9cubuntu\xe2\x80\x9d')
     SnippetBundle(client).key
Esempio n. 11
0
 def test_not_cached(self):
     bundle = SnippetBundle(self._client(locale='fr',
                                         startpage_version='5'))
     with patch('snippets.base.models.cache') as cache:
         cache.get.return_value = False
         with patch(
                 'snippets.base.models.default_storage') as default_storage:
             default_storage.exists.return_value = False
             self.assertFalse(bundle.cached)
Esempio n. 12
0
 def test_cached_remote(self):
     bundle = SnippetBundle(self._client(locale='fr',
                                         startpage_version='5'))
     with patch('snippets.base.models.cache') as cache:
         cache.get.return_value = False
         with patch(
                 'snippets.base.models.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)
Esempio n. 13
0
def fetch_snippets(request, **kwargs):
    """
    Return one of the following responses:
    - 204 with the bundle is empty
    - 302 to a bundle URL after generating it if not cached.
    """
    statsd.incr('serve.snippets')

    client = Client(**kwargs)
    bundle = SnippetBundle(client)
    if bundle.empty:
        statsd.incr('bundle.empty')
        return HttpResponse(status=204)
    elif bundle.cached:
        statsd.incr('bundle.cached')
    else:
        statsd.incr('bundle.generate')
        bundle.generate()

    return HttpResponseRedirect(bundle.url)
Esempio n. 14
0
    def test_generate_brotli(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.models.cache') as cache:
            with patch('snippets.base.models.render_to_string'
                       ) as render_to_string:
                with patch('snippets.base.models.default_storage'
                           ) as default_storage:
                    with patch('snippets.base.models.brotli',
                               wraps=brotli) as brotli_mock:
                        render_to_string.return_value = 'rendered snippet'
                        bundle.generate()

        brotli_mock.compress.assert_called_with('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(),
                         '\x8b\x07\x80rendered snippet\x03')
Esempio n. 15
0
    def test_generate(self):
        """
        bundle.generate should render the snippets, save them to the
        filesystem, and mark the bundle as not-expired in the cache.
        """
        bundle = SnippetBundle(self._client(locale='fr'))
        bundle.storage = Mock()
        bundle._snippets = [self.snippet1, self.snippet2]

        with patch('snippets.base.models.cache') as cache:
            with patch('snippets.base.models.render_to_string') as render_to_string:
                with self.settings(SNIPPET_BUNDLE_TIMEOUT=10):
                    render_to_string.return_value = 'rendered snippet'
                    bundle.generate()

        render_to_string.assert_called_with('base/fetch_snippets.html', {
            'snippets': [self.snippet1, self.snippet2],
            'client': bundle.client,
            'locale': 'fr',
        })
        bundle.storage.save.assert_called_with(bundle.filename, ANY)
        cache.set.assert_called_with(bundle.cache_key, True, 10)

        # Check content of saved file.
        content_file = bundle.storage.save.call_args[0][1]
        eq_(content_file.read(), 'rendered snippet')
Esempio n. 16
0
def fetch_snippets(request, **kwargs):
    """
    Return one of the following responses:
    - 204 with the bundle is empty
    - 302 to a bundle URL after generating it if not cached.
    """
    statsd.incr('serve.snippets')

    client = Client(**kwargs)
    bundle = SnippetBundle(client)
    if bundle.empty:
        statsd.incr('bundle.empty')
        # 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)
Esempio n. 17
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)
Esempio n. 18
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)
Esempio n. 19
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)
Esempio n. 20
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)
Esempio n. 21
0
    def test_generate(self):
        """
        bundle.generate should render the snippets, save them to the
        filesystem, and mark the bundle as not-expired in the cache.
        """
        bundle = SnippetBundle(self._client(locale='fr'))
        bundle.storage = Mock()
        bundle.snippets = [self.snippet1, self.snippet2]

        with patch('snippets.base.models.cache') as cache:
            with patch('snippets.base.models.render_to_string'
                       ) as render_to_string:
                with patch('snippets.base.models.default_storage'
                           ) as default_storage:
                    with self.settings(SNIPPET_BUNDLE_TIMEOUT=10):
                        with patch('snippets.base.models.brotli',
                                   wraps=brotli) as brotli_mock:
                            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.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)

        # Brotli must not be used in non-AS requests.
        self.assertFalse(brotli_mock.called)

        # Check content of saved file.
        content_file = default_storage.save.call_args[0][1]
        self.assertEqual(content_file.read(), 'rendered snippet')
Esempio n. 22
0
 def test_cached_local(self):
     bundle = SnippetBundle(self._client(locale='fr',
                                         startpage_version='5'))
     with patch('snippets.base.models.cache') as cache:
         cache.get.return_value = True
         self.assertTrue(bundle.cached)