'Verify that a referral attribution is recorded if an affiliate
cookie is present upon a new user\'s registration.'

@unittest.skipUnless((settings.ROOT_URLCONF == 'lms.urls'), 'Test    only    valid    in    lms')
def test_utm_referral_attribution(self):

    utm_cookie_name = 'edx.test.utm'
    with mock.patch('student.models.RegistrationCookieConfiguration.current') as config:
        instance = config.return_value
        instance.utm_cookie_name = utm_cookie_name
        timestamp = 1475521816879
        utm_cookie = {'utm_source': 'test-source', 'utm_medium': 'test-medium', 'utm_campaign': 'test-campaign', 'utm_term': 'test-term', 'utm_content': 'test-content', 'created_at': timestamp}
        created_at = datetime.fromtimestamp((timestamp / float(1000)), tz=pytz.UTC)
        self.client.cookies[utm_cookie_name] = json.dumps(utm_cookie)
        user = self.create_account_and_fetch_profile().user
        self.assertEqual(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_source')), utm_cookie.get('utm_source'))
        self.assertEqual(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_medium')), utm_cookie.get('utm_medium'))
        self.assertEqual(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_campaign')), utm_cookie.get('utm_campaign'))
        self.assertEqual(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_term')), utm_cookie.get('utm_term'))
        self.assertEqual(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_content')), utm_cookie.get('utm_content'))
        self.assertEqual(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_CREATED_AT), str(created_at))
