Repository: mozmeao/basket

Issue:
Updates to FxA Feed to SalesCloudcCRM 
This request is related to the work that has already been done on the Basket side around turning on the feed for FxA Contacts to come into Salesforce CRM. 

UPDATES FROM LCM:
For Net Net New verified account holder records feeding into CRM:

- Bring over FxA ID
- Flip Double Opt In to True
- Flip Subscriber to True
- Flip Email Format to H
- Flip Firefox Account Tips Subscriber field to True
- Flip any other applicable subscription fields to True that were a part of their signup process (i.e. FxA subscription options upon signup: Knowledge is Power Subscription, New Product Testing Subscriber, Take Action for the Internet Subscription)
- Bring over FxA language and populate Newsletter Language (based on previously discussed & agreed upon mapping - one of our standard 9 locales or “Other”; I believe you wanted to capture the ACTUAL language from FxA in another new field too in case it was ever needed.)


Discussion:
What of this should happen if a user is being updated instead of created? I assume:

- Bring over FxA ID
- Ensure Double Opt In is True
- Ensure Subscriber is True
- Flip Firefox Account Tips Subscriber field to True
- Flip any other applicable subscription fields to True that were a part of their signup process (i.e. FxA subscription options upon signup: Knowledge is Power Subscription, New Product Testing Subscriber, Take Action for the Internet Subscription)
- Bring over FxA language and populate Newsletter Language (based on previously discussed & agreed upon mapping - one of our standard 9 locales or “Other”; I believe you wanted to capture the ACTUAL language from FxA in another new field too in case it was ever needed.) if they don't already have a language specified.

Resolving patch:
diff --git a/basket/news/tasks.py b/basket/news/tasks.py
--- a/basket/news/tasks.py
+++ b/basket/news/tasks.py
@@ -276,7 +276,6 @@ def fxa_verified_sfdc(data):
     fxa_id = data['uid']
     lang = data['lang']
     create_date = data.get('createDate')
-    subscribe = data.get('marketingOptIn')
     newsletters = data.get('newsletters')
     metrics = data.get('metricsContext', {})
     new_data = {
@@ -286,21 +285,15 @@ def fxa_verified_sfdc(data):
         'fxa_lang': data.get('locale'),
         'fxa_service': data.get('service', ''),
         'fxa_id': fxa_id,
+        'optin': True,
+        'format': 'H',
     }
     if create_date:
         new_data['fxa_create_date'] = iso_format_unix_timestamp(create_date)
 
-    add_news = None
-    if newsletters:
-        if settings.FXA_REGISTER_NEWSLETTER not in newsletters:
-            newsletters.append(settings.FXA_REGISTER_NEWSLETTER)
-
-        add_news = ','.join(newsletters)
-    elif subscribe:
-        add_news = settings.FXA_REGISTER_NEWSLETTER
-
-    if add_news:
-        new_data['newsletters'] = add_news
+    newsletters = newsletters or []
+    newsletters.append(settings.FXA_REGISTER_NEWSLETTER)
+    new_data['newsletters'] = ','.join(newsletters)
 
     user_data = get_fxa_user_data(fxa_id, email)
     # don't overwrite the user's language if already set


Test patch:
diff --git a/basket/news/tests/test_tasks.py b/basket/news/tests/test_tasks.py
--- a/basket/news/tests/test_tasks.py
+++ b/basket/news/tests/test_tasks.py
@@ -1260,46 +1260,20 @@ def test_firefox_ios_tablet(self):
         self.assertEqual(record['DEVICE_TYPE'], 'T')
 
 
-@override_settings(FXA_EVENTS_VERIFIED_SFDC_ENABLE=True)
+@override_settings(FXA_EVENTS_VERIFIED_SFDC_ENABLE=True,
+                   FXA_REGISTER_NEWSLETTER='firefox-accounts-journey')
 @patch('basket.news.tasks.get_best_language', Mock(return_value='en-US'))
 @patch('basket.news.tasks.newsletter_languages', Mock(return_value=['en-US']))
 @patch('basket.news.tasks.apply_updates')
 @patch('basket.news.tasks.upsert_contact')
 @patch('basket.news.tasks.get_fxa_user_data')
 class FxAVerifiedTests(TestCase):
-    def test_no_subscribe(self, fxa_data_mock, upsert_mock, apply_mock):
-        fxa_data_mock.return_value = None
-        data = {
-            'email': 'thedude@example.com',
-            'uid': 'the-fxa-id',
-            'locale': 'en-US,en',
-            'marketingOptIn': False,
-        }
-        fxa_verified(data)
-        upsert_mock.assert_called_with(SUBSCRIBE, {
-            'email': data['email'],
-            'source_url': settings.FXA_REGISTER_SOURCE_URL,
-            'country': '',
-            'lang': 'en-US',
-            'fxa_lang': data['locale'],
-            'fxa_service': '',
-            'fxa_id': 'the-fxa-id',
-        }, None)
-        apply_mock.assert_called_with('Firefox_Account_ID', {
-            'EMAIL_ADDRESS_': data['email'],
-            'CREATED_DATE_': gmttime(None),
-            'FXA_ID': 'the-fxa-id',
-            'FXA_LANGUAGE_ISO2': 'en-US',
-            'SERVICE': '',
-        })
-
-    def test_with_subscribe(self, fxa_data_mock, upsert_mock, apply_mock):
+    def test_success(self, fxa_data_mock, upsert_mock, apply_mock):
         fxa_data_mock.return_value = {'lang': 'en-US'}
         data = {
             'email': 'thedude@example.com',
             'uid': 'the-fxa-id',
             'locale': 'en-US,en',
-            'marketingOptIn': True,
             'service': 'sync',
         }
         fxa_verified(data)
@@ -1311,10 +1285,12 @@ def test_with_subscribe(self, fxa_data_mock, upsert_mock, apply_mock):
             'fxa_lang': data['locale'],
             'fxa_service': 'sync',
             'fxa_id': 'the-fxa-id',
+            'optin': True,
+            'format': 'H',
         }, fxa_data_mock())
         apply_mock.assert_called_with('Firefox_Account_ID', {
             'EMAIL_ADDRESS_': data['email'],
-            'CREATED_DATE_': gmttime(None),
+            'CREATED_DATE_': ANY,
             'FXA_ID': 'the-fxa-id',
             'FXA_LANGUAGE_ISO2': 'en-US',
             'SERVICE': 'sync',
@@ -1326,7 +1302,6 @@ def test_with_newsletters(self, fxa_data_mock, upsert_mock, apply_mock):
             'email': 'thedude@example.com',
             'uid': 'the-fxa-id',
             'locale': 'en-US,en',
-            'marketingOptIn': True,
             'newsletters': ['test-pilot', 'take-action-for-the-internet'],
             'service': 'sync',
         }
@@ -1340,10 +1315,12 @@ def test_with_newsletters(self, fxa_data_mock, upsert_mock, apply_mock):
             'fxa_lang': data['locale'],
             'fxa_service': 'sync',
             'fxa_id': 'the-fxa-id',
+            'optin': True,
+            'format': 'H',
         }, None)
         apply_mock.assert_called_with('Firefox_Account_ID', {
             'EMAIL_ADDRESS_': data['email'],
-            'CREATED_DATE_': gmttime(None),
+            'CREATED_DATE_': ANY,
             'FXA_ID': 'the-fxa-id',
             'FXA_LANGUAGE_ISO2': 'en-US',
             'SERVICE': 'sync',
@@ -1355,7 +1332,6 @@ def test_with_subscribe_and_metrics(self, fxa_data_mock, upsert_mock, apply_mock
             'email': 'thedude@example.com',
             'uid': 'the-fxa-id',
             'locale': 'en-US,en',
-            'marketingOptIn': True,
             'metricsContext': {
                 'utm_campaign': 'bowling',
                 'some_other_thing': 'Donnie',
@@ -1373,10 +1349,12 @@ def test_with_subscribe_and_metrics(self, fxa_data_mock, upsert_mock, apply_mock
             'fxa_lang': data['locale'],
             'fxa_service': 'monitor',
             'fxa_id': 'the-fxa-id',
+            'optin': True,
+            'format': 'H',
         }, None)
         apply_mock.assert_called_with('Firefox_Account_ID', {
             'EMAIL_ADDRESS_': data['email'],
-            'CREATED_DATE_': gmttime(None),
+            'CREATED_DATE_': ANY,
             'FXA_ID': 'the-fxa-id',
             'FXA_LANGUAGE_ISO2': 'en-US',
             'SERVICE': 'monitor',
@@ -1394,13 +1372,16 @@ def test_with_createDate(self, fxa_data_mock, upsert_mock, apply_mock):
         fxa_verified(data)
         upsert_mock.assert_called_with(SUBSCRIBE, {
             'email': data['email'],
+            'newsletters': settings.FXA_REGISTER_NEWSLETTER,
             'source_url': settings.FXA_REGISTER_SOURCE_URL,
             'country': '',
             'lang': 'en-US',
             'fxa_lang': data['locale'],
             'fxa_service': '',
             'fxa_id': 'the-fxa-id',
-            'fxa_create_date': iso_format_unix_timestamp(create_date)
+            'fxa_create_date': iso_format_unix_timestamp(create_date),
+            'optin': True,
+            'format': 'H',
         }, None)
         apply_mock.assert_called_with('Firefox_Account_ID', {
             'EMAIL_ADDRESS_': data['email'],