Repository: mozmeao/snippets-service

Issue:
Default to `default` distribution if client is not part of an experiment.
We want all Firefox clients to get the default bundle for the locale / channel combination, unless they are part of an experiment when using pregen bundles in https://github.com/mozmeao/snippets-service/blob/master/snippets/base/views.py#L63

This is because non-Mozilla distributors of Firefox (e.g. Linux Distributions) override the distribution field with their identification. 

We 'll only use client's distribution if it starts with `experiment-`


Discussion:

Resolving patch:
diff --git a/snippets/base/views.py b/snippets/base/views.py
--- a/snippets/base/views.py
+++ b/snippets/base/views.py
@@ -60,7 +60,20 @@ def fetch_snippet_pregen_bundle(request, **kwargs):
     channel = client.channel.lower()
     channel = next((item for item in CHANNELS if channel.startswith(item)), None) or 'release'
     locale = client.locale.lower()
+
+    # Distribution populated by client's distribution if it starts with
+    # `experiment-`. Otherwise default to `default`.
+    #
+    # This is because non-Mozilla distributors of Firefox (e.g. Linux
+    # Distributions) override the distribution field with their identification.
+    # We want all Firefox clients to get the default bundle for the locale /
+    # channel combination, unless they are part of an experiment.
     distribution = client.distribution.lower()
+    if distribution.startswith('experiment-'):
+        distribution = distribution[11:]
+    else:
+        distribution = 'default'
+
     filename = (
         f'{settings.MEDIA_BUNDLES_PREGEN_ROOT}/{product}/{channel}/'
         f'{locale}/{distribution}.json'


Test patch:
diff --git a/snippets/base/tests/test_views.py b/snippets/base/tests/test_views.py
--- a/snippets/base/tests/test_views.py
+++ b/snippets/base/tests/test_views.py
@@ -169,43 +169,44 @@ def setUp(self):
         ])
 
     def test_base(self):
-        with patch('snippets.base.views.default_storage.exists') as exists_mock:
-            exists_mock.return_value = True
-            response = views.fetch_snippet_pregen_bundle(self.request, **self.asrclient_kwargs)
+        response = views.fetch_snippet_pregen_bundle(self.request, **self.asrclient_kwargs)
         expected_url = (
-            'http://example.org/media/bundles/pregen/Firefox/release/el-gr/other-than-default.json'
+            'http://example.org/media/bundles/pregen/Firefox/release/el-gr/default.json'
         )
         self.assertEqual(response.url, expected_url)
 
     @override_settings(CDN_URL='https://cdn.com')
     def test_cdn(self):
-        with patch('snippets.base.views.default_storage.exists') as exists_mock:
-            exists_mock.return_value = True
-            response = views.fetch_snippet_pregen_bundle(self.request, **self.asrclient_kwargs)
+        response = views.fetch_snippet_pregen_bundle(self.request, **self.asrclient_kwargs)
         expected_url = (
-            'https://cdn.com/media/bundles/pregen/Firefox/release/el-gr/other-than-default.json'
+            'https://cdn.com/media/bundles/pregen/Firefox/release/el-gr/default.json'
         )
         self.assertEqual(response.url, expected_url)
 
     def test_complicated_channel(self):
         asrclient_kwargs = self.asrclient_kwargs.copy()
         asrclient_kwargs['channel'] = 'nightly-cck-δφια'
-        with patch('snippets.base.views.default_storage.exists') as exists_mock:
-            exists_mock.return_value = True
-            response = views.fetch_snippet_pregen_bundle(self.request, **asrclient_kwargs)
+        response = views.fetch_snippet_pregen_bundle(self.request, **asrclient_kwargs)
         expected_url = (
-            'http://example.org/media/bundles/pregen/Firefox/nightly/el-gr/other-than-default.json'
+            'http://example.org/media/bundles/pregen/Firefox/nightly/el-gr/default.json'
         )
         self.assertEqual(response.url, expected_url)
 
     def test_other_product(self):
         asrclient_kwargs = self.asrclient_kwargs.copy()
         asrclient_kwargs['name'] = 'Edge'
-        with patch('snippets.base.views.default_storage.exists') as exists_mock:
-            exists_mock.return_value = True
-            response = views.fetch_snippet_pregen_bundle(self.request, **asrclient_kwargs)
+        response = views.fetch_snippet_pregen_bundle(self.request, **asrclient_kwargs)
         expected_url = (
-            'http://example.org/media/bundles/pregen/Firefox/release/el-gr/other-than-default.json'
+            'http://example.org/media/bundles/pregen/Firefox/release/el-gr/default.json'
+        )
+        self.assertEqual(response.url, expected_url)
+
+    def test_distribution(self):
+        asrclient_kwargs = self.asrclient_kwargs.copy()
+        asrclient_kwargs['distribution'] = 'experiment-foo-bar'
+        response = views.fetch_snippet_pregen_bundle(self.request, **asrclient_kwargs)
+        expected_url = (
+            'http://example.org/media/bundles/pregen/Firefox/release/el-gr/foo-bar.json'
         )
         self.assertEqual(response.url, expected_url)