def test_flag_on(self): Flag.objects.create(name='serve_pregenerated_snippets', everyone=True) with patch.object(views, 'fetch_pregenerated_snippets') as fetch_pregenerated_snippets: eq_(views.fetch_snippets(self.request, foo='bar'), fetch_pregenerated_snippets.return_value) fetch_pregenerated_snippets.assert_called_with(self.request, foo='bar')
def test_flag_off(self): Flag.objects.create(name='serve_pregenerated_snippets', everyone=False) with patch.object(views, 'fetch_render_snippets') as fetch_render_snippets: eq_(views.fetch_snippets(self.request, foo='bar'), fetch_render_snippets.return_value) fetch_render_snippets.assert_called_with(self.request, foo='bar')
def test_empty(self): """If the bundle is empty return 204. """ with patch.object(views, 'SnippetBundle') as SnippetBundle: bundle = SnippetBundle.return_value bundle.empty = True response = views.fetch_snippets(self.request, **self.client_kwargs) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, '')
def test_empty(self): """If the bundle is empty return 200 and empty string.""" with patch.object(views, 'SnippetBundle') as SnippetBundle: bundle = SnippetBundle.return_value bundle.empty = True response = views.fetch_snippets(self.request, **self.client_kwargs) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, b'')
def test_empty_asr(self): """If the bundle is empty return 200 and valid JSON for ASR.""" with patch.object(views, 'ASRSnippetBundle') as ASRSnippetBundle: bundle = ASRSnippetBundle.return_value bundle.empty = True response = views.fetch_snippets(self.request, **self.asrclient_kwargs) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, b'{}') self.assertEqual(response['content-type'], 'application/json')
def test_etag(self): """ The response returned by fetch_snippets should have a ETag set to the sha256 hash of the response content. """ request = self.factory.get('/') with patch.object(views, 'render') as mock_render: mock_render.return_value = HttpResponse('asdf') response = views.fetch_snippets(request, **self.client_kwargs) # sha256 of 'asdf' expected = 'f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b' eq_(response['ETag'], expected)
def test_regenerate(self): """If the bundle has expired, re-generate it.""" with patch.object(views, 'SnippetBundle') as SnippetBundle: bundle = SnippetBundle.return_value bundle.url = '/foo/bar' bundle.empty = False bundle.cached = False response = views.fetch_snippets(self.request, **self.client_kwargs) self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], '/foo/bar') # Since the bundle was expired, ensure it was re-generated. self.assertTrue(SnippetBundle.return_value.generate.called)
def test_normal(self): with patch.object(views, 'SnippetBundle') as SnippetBundle: bundle = SnippetBundle.return_value bundle.url = '/foo/bar' bundle.empty = False bundle.cached = True response = views.fetch_snippets(self.request, **self.client_kwargs) self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], '/foo/bar') # Check for correct client. self.assertEqual(SnippetBundle.call_args[0][0].locale, 'en-US') # Do not generate bundle when not expired. self.assertTrue(not SnippetBundle.return_value.generate.called)
def test_normal_asr(self): with patch.object(views, 'ASRSnippetBundle') as ASRSnippetBundle: bundle = ASRSnippetBundle.return_value bundle.url = '/foo/bar' bundle.empty = False bundle.cached = True response = views.fetch_snippets(self.request, **self.asrclient_kwargs) self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], '/foo/bar') # Check for correct client. self.assertEqual(ASRSnippetBundle.call_args[0][0].locale, 'en-US') # Do not generate bundle when not expired. self.assertTrue(not ASRSnippetBundle.return_value.generate.called)
def test_base(self): asrclient_kwargs = dict([ ('startpage_version', 6), ('name', 'Firefox'), ('version', '64.0'), ('appbuildid', '20190110041606'), ('build_target', 'Darwin_Universal-gcc3'), ('locale', 'en-US'), ('channel', 'release'), ('os_version', 'Darwin 10.8.0'), ('distribution', 'default'), ('distribution_version', 'default_version'), ]) request = RequestFactory().get('/') with patch.multiple('snippets.base.views', fetch_snippet_pregen_bundle=DEFAULT, fetch_snippet_bundle=DEFAULT) as patches: with override_settings(USE_PREGEN_BUNDLES=False): views.fetch_snippets(request, **asrclient_kwargs) self.assertFalse(patches['fetch_snippet_pregen_bundle'].called) self.assertTrue(patches['fetch_snippet_bundle'].called) with patch.multiple('snippets.base.views', fetch_snippet_pregen_bundle=DEFAULT, fetch_snippet_bundle=DEFAULT) as patches: with override_settings(USE_PREGEN_BUNDLES=True): views.fetch_snippets(request, **asrclient_kwargs) self.assertTrue(patches['fetch_snippet_pregen_bundle'].called) self.assertFalse(patches['fetch_snippet_bundle'].called) # Pregen Enabled but old client. with patch.multiple('snippets.base.views', fetch_snippet_pregen_bundle=DEFAULT, fetch_snippet_bundle=DEFAULT) as patches: with override_settings(USE_PREGEN_BUNDLES=True): asrclient_kwargs['startpage_version'] = 5 views.fetch_snippets(request, **asrclient_kwargs) self.assertFalse(patches['fetch_snippet_pregen_bundle'].called) self.assertTrue(patches['fetch_snippet_bundle'].called)
def process_request(self, request): result = resolve(request.path) if result.func == fetch_snippets: return fetch_snippets(request, *result.args, **result.kwargs)
def test_flag_on(self): with patch.object(views, 'fetch_pregenerated_snippets') as fetch_pregenerated_snippets: self.assertEqual(views.fetch_snippets(self.request, foo='bar'), fetch_pregenerated_snippets.return_value) fetch_pregenerated_snippets.assert_called_with(self.request, foo='bar')