Repository: mozmeao/snippets-service

Issue:
Use OAuth client ids instead of names in Firefox App Targeting.
List of client ids: https://docs.telemetry.mozilla.org/datasets/fxa_metrics/attribution.html#service-attribution

Related bug https://bugzilla.mozilla.org/show_bug.cgi?id=1596514#c5

 - [ ] After pushing to prod, generate JEXL for related Targets
 - [ ] ping mindy when is complete

<!-- probot = {"419912":{"who":"glogiotatidis","what":"","when":"2020-01-08T09:00:00.000Z"}} -->


Discussion:
Will go ahead and remove this targeting from Admin until Fx72 has been shipped. This JEXL has been causing performance issues on Fx. They will add caching in 72 to fix this.

We will re-add the targeting with Client IDs in 72.


Resolving patch:
diff --git a/snippets/base/admin/fields.py b/snippets/base/admin/fields.py
--- a/snippets/base/admin/fields.py
+++ b/snippets/base/admin/fields.py
@@ -188,14 +188,19 @@ def __init__(self, **kwargs):
             ('no_account', "User hasn't signed up for"),
             ('has_account', 'User has signed up for'),
         )
+        # Verify IDs using
+        # curl -s https://oauth.stage.mozaws.net/v1/client/<ID> | jq .
+        # Incomplete list of IDs
+        # https://docs.telemetry.mozilla.org/datasets/fxa_metrics/attribution.html#service-attribution  # noqa
         service_choices = (
             (None, '---------'),
-            ('Firefox Lockwise', 'Firefox Lockwise'),
-            ('Firefox Monitor', 'Firefox Monitor'),
-            ('Firefox Send', 'Firefox Send'),
-            ('Firefox Private Network', 'Firefox Private Network'),
-            ('Notes', 'Notes'),
-            ('Pocket', 'Pocket'),
+            ('e7ce535d93522896|98adfa37698f255b', 'Firefox Lockwise'),
+            ('802d56ef2a9af9fa', 'Firefox Monitor'),
+            ('1f30e32975ae5112|20f7931c9054d833', 'Firefox Send'),
+            ('a8c528140153d1c6|565585c1745a144d|e6eb0d1e856335fc', 'Firefox Private Network'),
+            ('7ad9917f6c55fb77', 'Firefox Reality'),
+            ('7377719276ad44ee|749818d3f2e7857f', 'Pocket'),
+
         )
         fields = (
             ChoiceField(choices=check_choices),
@@ -210,14 +215,21 @@ def compress(self, data_list):
         return ''
 
     def to_jexl(self, value):
-        check, service_name = value.split(',')
-        if not check or not service_name:
+        check, ids = value.split(',')
+        ids = ids.split('|') if ids else ''
+
+        if not check or not ids:
             return ''
 
+        jexl = '('
+        for id in ids:
+            jexl += f'("{id}" in attachedFxAOAuthClients|mapToProperty("id")) || '
+        jexl = jexl[:-4]
+
         if check == 'no_account':
-            jexl = f'("{service_name}" in attachedFxAOAuthClients|mapToProperty("name")) == false'
+            jexl += ') == false'
         elif check == 'has_account':
-            jexl = f'("{service_name}" in attachedFxAOAuthClients|mapToProperty("name")) == true'
+            jexl += ') == true'
 
         return jexl
 


Test patch:
diff --git a/snippets/base/tests/test_fields.py b/snippets/base/tests/test_fields.py
--- a/snippets/base/tests/test_fields.py
+++ b/snippets/base/tests/test_fields.py
@@ -137,15 +137,18 @@ def test_compress(self):
                          'has_account,Pocket')
 
     def test_to_jexl(self):
-        value = 'has_account,Firefox Lockwise'
+        value = 'has_account,e7ce535d93522896|98adfa37698f255b'
         self.assertEqual(
             self.field.to_jexl(value),
-            '("Firefox Lockwise" in attachedFxAOAuthClients|mapToProperty("name")) == true')
+            '(("e7ce535d93522896" in attachedFxAOAuthClients|mapToProperty("id")) '
+            '|| ("98adfa37698f255b" in attachedFxAOAuthClients|mapToProperty("id"))) == true'
+        )
 
-        value = 'no_account,Firefox Lockwise'
+        value = 'no_account,e7ce535d93522896'
         self.assertEqual(
             self.field.to_jexl(value),
-            '("Firefox Lockwise" in attachedFxAOAuthClients|mapToProperty("name")) == false')
+            '(("e7ce535d93522896" in attachedFxAOAuthClients|mapToProperty("id"))) == false'
+        )
 
         value = ','
         self.assertEqual(self.field.to_jexl(value), '')