def test_no_referral(self):
        """Verify that no referral is recorded when a cookie is not present."""
        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

            self.assertIsNone(
                self.client.cookies.get(settings.AFFILIATE_COOKIE_NAME))  # pylint: disable=no-member
            self.assertIsNone(self.client.cookies.get(utm_cookie_name))  # pylint: disable=no-member
            user = self.create_account_and_fetch_profile().user
            self.assertIsNone(
                UserAttribute.get_user_attribute(user,
                                                 REGISTRATION_AFFILIATE_ID))
            self.assertIsNone(
                UserAttribute.get_user_attribute(
                    user, REGISTRATION_UTM_PARAMETERS.get('utm_source')))
            self.assertIsNone(
                UserAttribute.get_user_attribute(
                    user, REGISTRATION_UTM_PARAMETERS.get('utm_medium')))
            self.assertIsNone(
                UserAttribute.get_user_attribute(
                    user, REGISTRATION_UTM_PARAMETERS.get('utm_campaign')))
            self.assertIsNone(
                UserAttribute.get_user_attribute(
                    user, REGISTRATION_UTM_PARAMETERS.get('utm_term')))
            self.assertIsNone(
                UserAttribute.get_user_attribute(
                    user, REGISTRATION_UTM_PARAMETERS.get('utm_content')))
            self.assertIsNone(
                UserAttribute.get_user_attribute(user,
                                                 REGISTRATION_UTM_CREATED_AT))
    def test_utm_referral_attribution(self):
        """
        Verify that a referral attribution is recorded if an affiliate
        cookie is present upon a new user's registration.
        """
        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))
    def test_utm_referral_attribution(self):
        """
        Verify that a referral attribution is recorded if an affiliate
        cookie is present upon a new user's registration.
        """
        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)
            )
    def test_no_referral(self):
        """Verify that no referral is recorded when a cookie is not present."""
        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

            self.assertIsNone(self.client.cookies.get(settings.AFFILIATE_COOKIE_NAME))
            self.assertIsNone(self.client.cookies.get(utm_cookie_name))
            user = self.create_account_and_fetch_profile().user
            self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_AFFILIATE_ID))
            self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_source')))
            self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_medium')))
            self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_campaign')))
            self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_term')))
            self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_content')))
            self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_CREATED_AT))
    def test_incomplete_utm_referral(self):
        """Verify that no referral is recorded when a cookie is not present."""
        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

            utm_cookie = {
                'utm_source': 'test-source',
                'utm_medium': 'test-medium',
                # No campaign
                'utm_term': 'test-term',
                'utm_content': 'test-content',
                # No created at
            }

            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_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.assertIsNone(
                UserAttribute.get_user_attribute(
                    user, REGISTRATION_UTM_PARAMETERS.get('utm_campaign')))
            self.assertIsNone(
                UserAttribute.get_user_attribute(user,
                                                 REGISTRATION_UTM_CREATED_AT))
    def test_incomplete_utm_referral(self):
        """Verify that no referral is recorded when a cookie is not present."""
        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

            utm_cookie = {
                'utm_source': 'test-source',
                'utm_medium': 'test-medium',
                # No campaign
                'utm_term': 'test-term',
                'utm_content': 'test-content',
                # No created at
            }

            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_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.assertIsNone(
                UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_campaign'))
            )
            self.assertIsNone(
                UserAttribute.get_user_attribute(user, REGISTRATION_UTM_CREATED_AT)
            )