Ejemplo n.º 1
0
 def test_one(self):
     """
     Split name should have the name as the first tuple item
     """
     first_name, last_name = util.split_name("one")
     assert first_name == "one"
     assert last_name == ""
Ejemplo n.º 2
0
 def test_more_than_two(self):
     """
     Split name should be limited to two names
     """
     first_name, last_name = util.split_name("three names here")
     assert first_name == "three"
     assert last_name == "names here"
Ejemplo n.º 3
0
 def test_two(self):
     """
     Split name with two names
     """
     first_name, last_name = util.split_name("two names")
     assert first_name == "two"
     assert last_name == "names"
Ejemplo n.º 4
0
 def test_empty(self):
     """
     split_name should always return two parts
     """
     first_name, last_name = util.split_name("")
     assert first_name == ""
     assert last_name == ""
Ejemplo n.º 5
0
 def test_one(self):
     """
     Split name should have the name as the first tuple item
     """
     first_name, last_name = util.split_name("one")
     assert first_name == "one"
     assert last_name == ""
Ejemplo n.º 6
0
 def test_two(self):
     """
     Split name with two names
     """
     first_name, last_name = util.split_name("two names")
     assert first_name == "two"
     assert last_name == "names"
Ejemplo n.º 7
0
 def test_none(self):
     """
     None should be treated like an empty string
     """
     first_name, last_name = util.split_name(None)
     assert first_name == ""
     assert last_name == ""
Ejemplo n.º 8
0
 def test_more_than_two(self):
     """
     Split name should be limited to two names
     """
     first_name, last_name = util.split_name("three names here")
     assert first_name == "three"
     assert last_name == "names here"
Ejemplo n.º 9
0
 def test_empty(self):
     """
     split_name should always return two parts
     """
     first_name, last_name = util.split_name("")
     assert first_name == ""
     assert last_name == ""
Ejemplo n.º 10
0
    def test_update_profile(self):
        """
        Happy path
        """
        mocked_content = self.mocked_edx_profile
        pipeline_api.update_profile_from_edx(
            edxorg.EdxOrgOAuth2(strategy=mock.Mock()),
            self.user, {'access_token': 'foo_token'}, True, **{'edx_profile': mocked_content}
        )

        first_name, last_name = split_name(mocked_content['name'])

        all_fields = [
            ('account_privacy', Profile.PUBLIC_TO_MM),
            ('edx_name', mocked_content['name']),
            ('first_name', first_name),
            ('last_name', last_name),
            ('preferred_name', mocked_content['name']),
            ('edx_bio', mocked_content['bio']),
            ('country', mocked_content['country']),
            ('edx_requires_parental_consent', mocked_content['requires_parental_consent']),
            ('edx_level_of_education', mocked_content['level_of_education']),
            ('edx_goals', mocked_content['goals']),
            ('edx_language_proficiencies', mocked_content['language_proficiencies']),
            ('preferred_language', mocked_content['language_proficiencies'][0]['code']),
            ('gender', mocked_content['gender']),
            ('edx_mailing_address', mocked_content['mailing_address']),
        ]

        self.check_profile_fields(self.user_profile, all_fields)

        # We do not set the date_of_birth using year_of_birth
        assert self.user_profile.date_of_birth is None
Ejemplo n.º 11
0
 def test_none(self):
     """
     None should be treated like an empty string
     """
     first_name, last_name = util.split_name(None)
     assert first_name == ""
     assert last_name == ""
def copy_names(apps, schema_editor):
    Profile = apps.get_model("profiles", "Profile")

    for profile in Profile.objects.all():
        name = profile.name
        profile.preferred_name = name
        profile.first_name, profile.last_name = split_name(name)
        profile.save()
Ejemplo n.º 13
0
    def test_update_profile(self, mocked_get_json):
        """
        Happy path
        """
        mocked_content = self.mocked_edx_profile
        mocked_get_json.return_value = mocked_content
        pipeline_api.update_profile_from_edx(
            edxorg.EdxOrgOAuth2, self.user, {'access_token': 'foo_token'}, True)
        mocked_get_json.assert_called_once_with(
            urljoin(
                edxorg.EdxOrgOAuth2.EDXORG_BASE_URL,
                '/api/user/v1/accounts/{0}'.format(get_social_username(self.user))
            ),
            headers={'Authorization': 'Bearer foo_token'}
        )

        first_name, last_name = split_name(mocked_content['name'])

        all_fields = [
            ('account_privacy', Profile.PUBLIC_TO_MM),
            ('edx_name', mocked_content['name']),
            ('first_name', first_name),
            ('last_name', last_name),
            ('preferred_name', None),
            ('edx_bio', mocked_content['bio']),
            ('country', mocked_content['country']),
            ('has_profile_image', mocked_content['profile_image']['has_image']),
            ('edx_requires_parental_consent', mocked_content['requires_parental_consent']),
            ('edx_level_of_education', mocked_content['level_of_education']),
            ('edx_goals', mocked_content['goals']),
            ('edx_language_proficiencies', mocked_content['language_proficiencies']),
            ('preferred_language', mocked_content['language_proficiencies'][0]['code']),
            ('gender', mocked_content['gender']),
            ('edx_mailing_address', mocked_content['mailing_address']),
        ]

        self.check_profile_fields(self.user_profile, all_fields)

        # We do not set the date_of_birth using year_of_birth
        assert self.user_profile.date_of_birth is None
Ejemplo n.º 14
0
    def test_update_profile(self):
        """
        Happy path
        """
        mocked_content = self.mocked_edx_profile
        pipeline_api.update_profile_from_edx(
            edxorg.EdxOrgOAuth2(strategy=mock.Mock()), self.user,
            {'access_token': 'foo_token'}, True,
            **{'edx_profile': mocked_content})

        first_name, last_name = split_name(mocked_content['name'])

        all_fields = [
            ('account_privacy', Profile.PUBLIC_TO_MM),
            ('edx_name', mocked_content['name']),
            ('first_name', first_name),
            ('last_name', last_name),
            ('preferred_name', mocked_content['name']),
            ('edx_bio', mocked_content['bio']),
            ('country', mocked_content['country']),
            ('edx_requires_parental_consent',
             mocked_content['requires_parental_consent']),
            ('edx_level_of_education', mocked_content['level_of_education']),
            ('edx_goals', mocked_content['goals']),
            ('edx_language_proficiencies',
             mocked_content['language_proficiencies']),
            ('preferred_language',
             mocked_content['language_proficiencies'][0]['code']),
            ('gender', mocked_content['gender']),
            ('edx_mailing_address', mocked_content['mailing_address']),
        ]

        self.check_profile_fields(self.user_profile, all_fields)

        # We do not set the date_of_birth using year_of_birth
        assert self.user_profile.date_of_birth is None
Ejemplo n.º 15
0
def update_profile_from_edx(backend, user, response, is_new, *args, **kwargs):  # pylint: disable=unused-argument
    """
    Gets profile information from EDX and saves them in the user profile

    Args:
        backend (social.backends.oauth.BaseOAuth2): the python social auth backend
        user (User): user object
        response (dict): dictionary of the user information coming
            from previous functions in the pipeline
        is_new (bool): whether the authenticated user created a new local instance

    Returns:
        None
    """

    # this function is completely skipped if the backend is not edx or
    # the user has not created now
    if backend.name != EdxOrgOAuth2.name or not is_new:
        return

    access_token = response.get('access_token')
    if not access_token:
        # this should never happen for the edx oauth provider, but just in case...
        log.error('Missing access token for the user %s', user.username)
        return

    try:
        user_profile = Profile.objects.get(user=user)
    except Profile.DoesNotExist:
        # this should never happen, since the profile is created with a signal
        # right after the user is created
        log.error('No profile found for the user %s', user.username)
        return

    username = get_social_username(user)
    user_profile_edx = backend.get_json(urljoin(
        backend.EDXORG_BASE_URL, '/api/user/v1/accounts/{0}'.format(username)),
                                        headers={
                                            "Authorization":
                                            "Bearer {}".format(access_token),
                                        })

    name = user_profile_edx.get('name', "")
    user_profile.edx_name = name
    user_profile.first_name, user_profile.last_name = split_name(name)
    user_profile.edx_bio = user_profile_edx.get('bio')
    user_profile.country = user_profile_edx.get('country')
    user_profile.has_profile_image = user_profile_edx.get('profile_image',
                                                          {}).get('has_image')
    user_profile.edx_requires_parental_consent = user_profile_edx.get(
        'requires_parental_consent')
    user_profile.edx_level_of_education = user_profile_edx.get(
        'level_of_education')
    user_profile.edx_goals = user_profile_edx.get('goals')
    user_profile.edx_language_proficiencies = user_profile_edx.get(
        'language_proficiencies')
    try:
        user_profile.preferred_language = user_profile.edx_language_proficiencies[
            0]['code']
    except (IndexError, ValueError, KeyError, TypeError):
        pass
    user_profile.gender = user_profile_edx.get('gender')
    user_profile.edx_mailing_address = user_profile_edx.get('mailing_address')
    user_profile.agreed_to_terms_of_service = True

    user_profile.save()

    log.debug('Profile for user "%s" updated with values from EDX %s',
              user.username, user_profile_edx)
Ejemplo n.º 16
0
def update_profile_from_edx(backend, user, response, is_new, *args, **kwargs):
    # pylint: disable=unused-argument
    """
    Gets profile information from EDX and saves them in the user profile

    Args:
        backend (social.backends.oauth.BaseOAuth2): the python social auth backend
        user (User): user object
        response (dict): dictionary of the user information coming
            from previous functions in the pipeline
        is_new (bool): whether the authenticated user created a new local instance

    Returns:
        None
    """
    # this function is completely skipped if the backend is not edx or
    # the user has not created now
    if backend.name != EdxOrgOAuth2.name:
        return

    if has_role(user, [Staff.ROLE_ID, Instructor.ROLE_ID]):
        next_relative_url = "/learners"
    else:
        next_relative_url = "/dashboard"

    next_url = backend.strategy.session.load().get('next') or backend.strategy.session.get('next')
    if not next_url:
        next_url = next_relative_url

    backend.strategy.session_set('next', next_url)

    user_profile_edx = kwargs.get('edx_profile')
    update_email(user_profile_edx, user)
    if not is_new:
        return

    try:
        user_profile = Profile.objects.get(user=user)
    except Profile.DoesNotExist:
        # this should never happen, since the profile is created with a signal
        # right after the user is created
        log.error('No profile found for the user %s', user.username)
        return

    name = user_profile_edx.get('name', "")
    user_profile.edx_name = name
    user_profile.first_name, user_profile.last_name = split_name(name)
    user_profile.preferred_name = name
    user_profile.edx_bio = user_profile_edx.get('bio')
    user_profile.country = user_profile_edx.get('country')
    user_profile.edx_requires_parental_consent = user_profile_edx.get('requires_parental_consent')
    user_profile.edx_level_of_education = user_profile_edx.get('level_of_education')
    user_profile.edx_goals = user_profile_edx.get('goals')
    user_profile.edx_language_proficiencies = user_profile_edx.get('language_proficiencies')
    try:
        user_profile.preferred_language = user_profile.edx_language_proficiencies[0]['code']
    except (IndexError, ValueError, KeyError, TypeError):
        pass
    user_profile.gender = user_profile_edx.get('gender')
    user_profile.edx_mailing_address = user_profile_edx.get('mailing_address')
    user_profile.agreed_to_terms_of_service = True

    user_profile.save()

    log.debug(
        'Profile for user "%s" updated with values from EDX %s',
        user.username,
        user_profile_edx
    )
Ejemplo n.º 17
0
def update_profile_from_edx(backend, user, response, is_new, *args, **kwargs):
    # pylint: disable=unused-argument
    """
    Gets profile information from EDX and saves them in the user profile

    Args:
        backend (social.backends.oauth.BaseOAuth2): the python social auth backend
        user (User): user object
        response (dict): dictionary of the user information coming
            from previous functions in the pipeline
        is_new (bool): whether the authenticated user created a new local instance

    Returns:
        None
    """
    # this function is completely skipped if the backend is not edx or
    # the user has not created now
    if backend.name != EdxOrgOAuth2.name:
        return

    if has_role(user, [Staff.ROLE_ID, Instructor.ROLE_ID]):
        next_relative_url = "/learners"
    else:
        next_relative_url = "/dashboard"

    next_url = backend.strategy.session.load().get(
        'next') or backend.strategy.session.get('next')
    if not next_url:
        next_url = next_relative_url

    backend.strategy.session_set('next', next_url)

    user_profile_edx = kwargs.get('edx_profile')
    update_email(user_profile_edx, user)
    if not is_new:
        return

    try:
        user_profile = Profile.objects.get(user=user)
    except Profile.DoesNotExist:
        # this should never happen, since the profile is created with a signal
        # right after the user is created
        log.error('No profile found for the user %s', user.username)
        return

    name = user_profile_edx.get('name', "")
    user_profile.edx_name = name
    user_profile.first_name, user_profile.last_name = split_name(name)
    user_profile.preferred_name = name
    user_profile.edx_bio = user_profile_edx.get('bio')
    user_profile.country = user_profile_edx.get('country')
    user_profile.edx_requires_parental_consent = user_profile_edx.get(
        'requires_parental_consent')
    user_profile.edx_level_of_education = user_profile_edx.get(
        'level_of_education')
    user_profile.edx_goals = user_profile_edx.get('goals')
    user_profile.edx_language_proficiencies = user_profile_edx.get(
        'language_proficiencies')
    try:
        user_profile.preferred_language = user_profile.edx_language_proficiencies[
            0]['code']
    except (IndexError, ValueError, KeyError, TypeError):
        pass
    user_profile.gender = user_profile_edx.get('gender')
    user_profile.edx_mailing_address = user_profile_edx.get('mailing_address')
    user_profile.agreed_to_terms_of_service = True

    user_profile.save()

    log.debug('Profile for user "%s" updated with values from EDX %s',
              user.username, user_profile_edx)