Esempio n. 1
0
    def test_language_in_session(self):
        # language set in both the user preferences and session,
        # session should get precedence
        self.request.session['django_language'] = 'en'
        UserPreference.set_preference(self.user, LANGUAGE_KEY, 'eo')
        self.middleware.process_request(self.request)

        self.assertEquals(self.request.session['django_language'], 'en')
Esempio n. 2
0
    def test_language_in_session(self):
        # language set in both the user preferences and session,
        # session should get precedence
        self.request.session['django_language'] = 'en'
        UserPreference.set_preference(self.user, LANGUAGE_KEY, 'eo')
        self.middleware.process_request(self.request)

        self.assertEquals(self.request.session['django_language'], 'en')
Esempio n. 3
0
    def test_user_with_locale_claim(self):
        language = "en"
        UserPreference.set_preference(self.user, LANGUAGE_KEY, language)
        scopes, claims = self.get_id_token_values("openid profile")

        self.assertIn("profile", scopes)

        locale = claims["locale"]
        self.assertEqual(language, locale)
Esempio n. 4
0
    def test_user_with_locale_claim(self):
        language = 'en'
        UserPreference.set_preference(self.user, LANGUAGE_KEY, language)
        scopes, claims = self.get_id_token_values('openid profile')

        self.assertIn('profile', scopes)

        locale = claims['locale']
        self.assertEqual(language, locale)
Esempio n. 5
0
    def test_user_with_locale_claim(self):
        language = 'en'
        UserPreference.set_preference(self.user, LANGUAGE_KEY, language)
        scopes, claims = self.get_id_token_values('openid profile')

        self.assertIn('profile', scopes)

        locale = claims['locale']
        self.assertEqual(language, locale)
    def setUp(self):
        # Platform language is English, instructor's language is Chinese,
        # student's language is French, so the emails should all be sent in
        # French.
        self.course = CourseFactory.create()
        self.instructor = InstructorFactory(course_key=self.course.id)
        UserPreference.set_preference(self.instructor, LANGUAGE_KEY, 'zh-cn')
        self.client.login(username=self.instructor.username, password='******')

        self.student = UserFactory.create()
        UserPreference.set_preference(self.student, LANGUAGE_KEY, 'fr')
    def setUp(self):
        # Platform language is English, instructor's language is Chinese,
        # student's language is French, so the emails should all be sent in
        # French.
        self.course = CourseFactory.create()
        self.instructor = InstructorFactory(course_key=self.course.id)
        UserPreference.set_preference(self.instructor, LANGUAGE_KEY, 'zh-cn')
        self.client.login(username=self.instructor.username, password='******')

        self.student = UserFactory.create()
        UserPreference.set_preference(self.student, LANGUAGE_KEY, 'fr')
Esempio n. 8
0
def set_language(request):
    """
    This view is called when the user would like to set a language preference
    """
    user = request.user
    lang_pref = request.POST.get('language', None)

    if lang_pref:
        UserPreference.set_preference(user, LANGUAGE_KEY, lang_pref)
        return HttpResponse('{"success": true}')

    return HttpResponseBadRequest('no language provided')
Esempio n. 9
0
 def _verify_full_account_response(self,
                                   response,
                                   requires_parental_consent=False):
     """
     Verify that all account fields are returned (even those that are not shareable).
     """
     data = response.data
     self.assertEqual(17, len(data))
     self.assertEqual(self.user.username, data["username"])
     self.assertEqual(self.user.first_name + " " + self.user.last_name,
                      data["name"])
     self.assertEqual("US", data["country"])
     self.assertEqual("f", data["gender"])
     self.assertEqual(2000, data["year_of_birth"])
     self.assertEqual("m", data["level_of_education"])
     self.assertEqual("world peace", data["goals"])
     self.assertEqual("Park Ave", data['mailing_address'])
     self.assertEqual(self.user.email, data["email"])
     self.assertTrue(data["is_active"])
     self.assertIsNotNone(data["date_joined"])
     self.assertEqual("Tired mother of twins", data["bio"])
     self._verify_profile_image_data(data, not requires_parental_consent)
     self.assertEquals(requires_parental_consent,
                       data["requires_parental_consent"])
     self.assertEqual([{"code": "en"}], data["language_proficiencies"])
     self.assertEqual(
         UserPreference.get_value(self.user, 'account_privacy'),
         data["account_privacy"])
Esempio n. 10
0
    def render_to_fragment(self, request, course_id=None, **kwargs):
        """
        Renders the user's course badges as a fragment.
        """
        course_key = CourseKey.from_string(course_id)
        course = get_course_with_access(request.user,
                                        'load',
                                        course_key,
                                        check_if_enrolled=True)

        language = UserPreference.get_value(request.user,
                                            'pref-lang',
                                            default='en')

        context = {
            'csrf':
            csrf(request)['csrf_token'],
            'course':
            course,
            'badges_api_url':
            reverse("badges_api:user_assertions",
                    kwargs={'username': request.user}),
            'language_preference':
            language,
            'user':
            User.objects.get(id=request.user.id)
        }
        html = render_to_string(
            'course_badges/course-badges-progress-fragment.html', context)
        fragment = Fragment(html)
        self.add_fragment_resource_urls(fragment)
        return fragment
Esempio n. 11
0
    def test_custom_visibility_over_age(self, api_client, requesting_username):
        self.create_mock_profile(self.user)
        # set user's custom visibility preferences
        set_user_preference(self.user, ACCOUNT_VISIBILITY_PREF_KEY, CUSTOM_VISIBILITY)
        shared_fields = ("bio", "language_proficiencies", "name")
        for field_name in shared_fields:
            set_user_preference(self.user, "visibility.{}".format(field_name), ALL_USERS_VISIBILITY)

        # make API request
        client = self.login_client(api_client, requesting_username)
        response = self.send_get(client)

        # verify response
        if requesting_username == "different_user":
            data = response.data
            assert 6 == len(data)

            # public fields
            assert self.user.username == data['username']
            assert UserPreference.get_value(self.user, 'account_privacy') == data['account_privacy']
            self._verify_profile_image_data(data, has_profile_image=True)

            # custom shared fields
            assert TEST_BIO_VALUE == data['bio']
            assert [{'code': TEST_LANGUAGE_PROFICIENCY_CODE}] == data['language_proficiencies']
            assert ((self.user.first_name + ' ') + self.user.last_name) == data['name']
        else:
            self._verify_full_account_response(response)
Esempio n. 12
0
    def test_custom_visibility_over_age(self, api_client, requesting_username):
        self.create_mock_profile(self.user)
        # set user's custom visibility preferences
        set_user_preference(self.user, ACCOUNT_VISIBILITY_PREF_KEY,
                            CUSTOM_VISIBILITY)
        shared_fields = ("bio", "language_proficiencies", "name")
        for field_name in shared_fields:
            set_user_preference(self.user, "visibility.{}".format(field_name),
                                ALL_USERS_VISIBILITY)

        # make API request
        client = self.login_client(api_client, requesting_username)
        response = self.send_get(client)

        # verify response
        if requesting_username == "different_user":
            data = response.data
            self.assertEqual(6, len(data))

            # public fields
            self.assertEqual(self.user.username, data["username"])
            self.assertEqual(
                UserPreference.get_value(self.user, 'account_privacy'),
                data["account_privacy"])
            self._verify_profile_image_data(data, has_profile_image=True)

            # custom shared fields
            self.assertEqual(TEST_BIO_VALUE, data["bio"])
            self.assertEqual([{
                "code": TEST_LANGUAGE_PROFICIENCY_CODE
            }], data["language_proficiencies"])
            self.assertEqual(self.user.first_name + " " + self.user.last_name,
                             data["name"])
        else:
            self._verify_full_account_response(response)
Esempio n. 13
0
 def test_default_lang_pref_saved(self, lang):
     with mock.patch("django.conf.settings.LANGUAGE_CODE", lang):
         response = self.client.post(self.url, self.params)
         self.assertEqual(response.status_code, 200)
         user = User.objects.get(username=self.username)
         self.assertEqual(UserPreference.get_preference(user, LANGUAGE_KEY),
                          lang)
Esempio n. 14
0
    def render_to_fragment(self, request, course_id=None, **kwargs):  # lint-amnesty, pylint: disable=arguments-differ
        """
        Renders the user's course bookmarks as a fragment.
        """
        course_key = CourseKey.from_string(course_id)
        course = get_course_with_access(request.user,
                                        'load',
                                        course_key,
                                        check_if_enrolled=True)

        language = UserPreference.get_value(request.user,
                                            'pref-lang',
                                            default='en')

        context = {
            'csrf': csrf(request)['csrf_token'],
            'course': course,
            'bookmarks_api_url': reverse('bookmarks'),
            'language_preference': language,
        }
        html = render_to_string(
            'course_bookmarks/course-bookmarks-fragment.html', context)
        inline_js = render_to_string(
            'course_bookmarks/course_bookmarks_js.template', context)
        fragment = Fragment(html)
        self.add_fragment_resource_urls(fragment)
        fragment.add_javascript(inline_js)
        return fragment
Esempio n. 15
0
    def test_custom_visibility_over_age(self, api_client, requesting_username):
        self.create_mock_profile(self.user)
        # set user's custom visibility preferences
        set_user_preference(self.user, ACCOUNT_VISIBILITY_PREF_KEY, CUSTOM_VISIBILITY)
        shared_fields = ("bio", "language_proficiencies", "name")
        for field_name in shared_fields:
            set_user_preference(self.user, "visibility.{}".format(field_name), ALL_USERS_VISIBILITY)

        # make API request
        client = self.login_client(api_client, requesting_username)
        response = self.send_get(client)

        # verify response
        if requesting_username == "different_user":
            data = response.data
            self.assertEqual(6, len(data))

            # public fields
            self.assertEqual(self.user.username, data["username"])
            self.assertEqual(UserPreference.get_value(self.user, 'account_privacy'), data["account_privacy"])
            self._verify_profile_image_data(data, has_profile_image=True)

            # custom shared fields
            self.assertEqual(TEST_BIO_VALUE, data["bio"])
            self.assertEqual([{"code": TEST_LANGUAGE_PROFICIENCY_CODE}], data["language_proficiencies"])
            self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"])
        else:
            self._verify_full_account_response(response)
Esempio n. 16
0
    def _verify_full_account_response(self,
                                      response,
                                      requires_parental_consent=False,
                                      year_of_birth=2000):
        """
        Verify that all account fields are returned (even those that are not shareable).
        """
        data = response.data
        self.assertEqual(26, len(data))

        # public fields (3)
        expected_account_privacy = (
            PRIVATE_VISIBILITY if requires_parental_consent else
            UserPreference.get_value(self.user, 'account_privacy'))
        self.assertEqual(expected_account_privacy, data["account_privacy"])
        self._verify_profile_image_data(data, not requires_parental_consent)
        self.assertEqual(self.user.username, data["username"])

        # additional shareable fields (8)
        self.assertEqual(TEST_BIO_VALUE, data["bio"])
        self.assertEqual("US", data["country"])
        self.assertIsNotNone(data["date_joined"])
        self.assertIsNotNone(data["last_login"])
        self.assertEqual([{
            "code": TEST_LANGUAGE_PROFICIENCY_CODE
        }], data["language_proficiencies"])
        self.assertEqual("m", data["level_of_education"])
        self.assertIsNotNone(data["social_links"])
        self.assertEqual(UserPreference.get_value(self.user, 'time_zone'),
                         data["time_zone"])
        self.assertIsNotNone(data["accomplishments_shared"])
        self.assertEqual(self.user.first_name + " " + self.user.last_name,
                         data["name"])

        # additional admin fields (10)
        self.assertEqual(self.user.email, data["email"])
        self.assertIsNotNone(data["extended_profile"])
        self.assertEqual("MA", data["state"])
        self.assertEqual("f", data["gender"])
        self.assertEqual("world peace", data["goals"])
        self.assertTrue(data["is_active"])
        self.assertEqual("Park Ave", data['mailing_address'])
        self.assertEqual(requires_parental_consent,
                         data["requires_parental_consent"])
        self.assertIsNone(data["secondary_email"])
        self.assertIsNone(data["secondary_email_enabled"])
        self.assertEqual(year_of_birth, data["year_of_birth"])
Esempio n. 17
0
    def _verify_full_account_response(self,
                                      response,
                                      requires_parental_consent=False,
                                      year_of_birth=2000):
        """
        Verify that all account fields are returned (even those that are not shareable).
        """
        data = response.data
        assert 27 == len(data)

        # public fields (3)
        expected_account_privacy = (
            PRIVATE_VISIBILITY if requires_parental_consent else
            UserPreference.get_value(self.user, 'account_privacy'))
        assert expected_account_privacy == data['account_privacy']
        self._verify_profile_image_data(data, not requires_parental_consent)
        assert self.user.username == data['username']

        # additional shareable fields (8)
        assert TEST_BIO_VALUE == data['bio']
        assert 'US' == data['country']
        assert data['date_joined'] is not None
        assert data['last_login'] is not None
        assert [{
            'code': TEST_LANGUAGE_PROFICIENCY_CODE
        }] == data['language_proficiencies']
        assert 'm' == data['level_of_education']
        assert data['social_links'] is not None
        assert UserPreference.get_value(self.user,
                                        'time_zone') == data['time_zone']
        assert data['accomplishments_shared'] is not None
        assert ((self.user.first_name + ' ') +
                self.user.last_name) == data['name']

        # additional admin fields (11)
        assert self.user.email == data['email']
        assert self.user.id == data['id']
        assert data['extended_profile'] is not None
        assert 'MA' == data['state']
        assert 'f' == data['gender']
        assert 'world peace' == data['goals']
        assert data['is_active']
        assert 'Park Ave' == data['mailing_address']
        assert requires_parental_consent == data['requires_parental_consent']
        assert data['secondary_email'] is None
        assert data['secondary_email_enabled'] is None
        assert year_of_birth == data['year_of_birth']
Esempio n. 18
0
 def test_header_lang_pref_saved(self, lang):
     response = self.client.post(self.url,
                                 self.params,
                                 HTTP_ACCEPT_LANGUAGE=lang)
     self.assertEqual(response.status_code, 200)
     user = User.objects.get(username=self.username)
     self.assertEqual(UserPreference.get_preference(user, LANGUAGE_KEY),
                      lang)
Esempio n. 19
0
def get_user_email_language(user):
    """
    Return the language most appropriate for writing emails to user. Returns
    None if the preference has not been set, or if the user does not exist.
    """
    # Calling UserPreference directly instead of get_user_preference because the user requesting the
    # information is not "user" and also may not have is_staff access.
    return UserPreference.get_value(user, LANGUAGE_KEY)
Esempio n. 20
0
def get_user_email_language(user):
    """
    Return the language most appropriate for writing emails to user. Returns
    None if the preference has not been set, or if the user does not exist.
    """
    # Calling UserPreference directly instead of get_user_preference because the user requesting the
    # information is not "user" and also may not have is_staff access.
    return UserPreference.get_value(user, LANGUAGE_KEY)
Esempio n. 21
0
 def process_request(self, request):
     """
     If a user's UserPreference contains a language preference and there is
     no language set on the session (i.e. from dark language overrides), use the user's preference.
     """
     if request.user.is_authenticated() and 'django_language' not in request.session:
         user_pref = UserPreference.get_preference(request.user, LANGUAGE_KEY)
         if user_pref:
             request.session['django_language'] = user_pref
Esempio n. 22
0
    def test_set_preference_missing_lang(self):
        user = UserFactory.create()
        self.client.login(username=user.username, password='******')

        response = self.client.post(reverse('lang_pref_set_language'))

        self.assertEquals(response.status_code, 400)

        self.assertIsNone(UserPreference.get_preference(user, LANGUAGE_KEY))
Esempio n. 23
0
    def test_set_preference_missing_lang(self):
        user = UserFactory.create()
        self.client.login(username=user.username, password='******')

        response = self.client.post(reverse('lang_pref_set_language'))

        self.assertEquals(response.status_code, 400)

        self.assertIsNone(UserPreference.get_preference(user, LANGUAGE_KEY))
    def has_permission(self, request, view):
        requested_profile_username = view.kwargs.get('username')
        # check whether requesting user is the owner of certs or not
        if request.user.username == requested_profile_username:
            return True

        user = User.objects.get(username=requested_profile_username)
        cert_privacy = UserPreference.get_value(user, 'visibility.course_certificates')

        return cert_privacy == 'all_users'
Esempio n. 25
0
 def test_discussions_email_digest_pref(self, digest_enabled):
     with mock.patch.dict("student.models.settings.FEATURES", {"ENABLE_DISCUSSION_EMAIL_DIGEST": digest_enabled}):
         response = self.client.post(self.url, self.params)
         self.assertEqual(response.status_code, 200)
         user = User.objects.get(username=self.username)
         preference = UserPreference.get_preference(user, NOTIFICATION_PREF_KEY)
         if digest_enabled:
             self.assertIsNotNone(preference)
         else:
             self.assertIsNone(preference)
Esempio n. 26
0
    def test_set_preference_happy(self):
        user = UserFactory.create()
        self.client.login(username=user.username, password='******')

        lang = 'en'
        response = self.client.post(reverse('lang_pref_set_language'), {'language': lang})

        self.assertEquals(response.status_code, 200)
        user_pref = UserPreference.get_preference(user, LANGUAGE_KEY)
        self.assertEqual(user_pref, lang)
Esempio n. 27
0
def _visible_fields_from_custom_preferences(user, configuration):
    """
    Returns all fields that are marked to be shared with other users in the
    given user's preferences. Includes fields that are always public.
    """
    preferences = UserPreference.get_all_preferences(user)
    fields_shared_with_all_users = [
        field_name for field_name in configuration.get('custom_shareable_fields')
        if preferences.get('{}{}'.format(VISIBILITY_PREFIX, field_name)) == 'all_users'
    ]
    return set(fields_shared_with_all_users + configuration.get('public_fields'))
Esempio n. 28
0
    def test_set_preference_happy(self):
        user = UserFactory.create()
        self.client.login(username=user.username, password='******')

        lang = 'en'
        response = self.client.post(reverse('lang_pref_set_language'),
                                    {'language': lang})

        self.assertEquals(response.status_code, 200)
        user_pref = UserPreference.get_preference(user, LANGUAGE_KEY)
        self.assertEqual(user_pref, lang)
Esempio n. 29
0
def _visible_fields_from_custom_preferences(user, configuration):
    """
    Returns all fields that are marked to be shared with other users in the
    given user's preferences. Includes fields that are always public.
    """
    preferences = UserPreference.get_all_preferences(user)
    fields_shared_with_all_users = [
        field_name for field_name in configuration.get('custom_shareable_fields')
        if preferences.get('{}{}'.format(VISIBILITY_PREFIX, field_name)) == 'all_users'
    ]
    return set(fields_shared_with_all_users + configuration.get('public_fields'))
Esempio n. 30
0
def get_profile_visibility(user_profile, user, configuration=None):
    """Returns the visibility level for the specified user profile."""
    if user_profile.requires_parental_consent():
        return PRIVATE_VISIBILITY

    if not configuration:
        configuration = settings.ACCOUNT_VISIBILITY_CONFIGURATION

    # Calling UserPreference directly because the requesting user may be different from existing_user
    # (and does not have to be is_staff).
    profile_privacy = UserPreference.get_value(user, ACCOUNT_VISIBILITY_PREF_KEY)
    return profile_privacy if profile_privacy else configuration.get('default_visibility')
Esempio n. 31
0
def get_profile_visibility(user_profile, user, configuration=None):
    """Returns the visibility level for the specified user profile."""
    if user_profile.requires_parental_consent():
        return PRIVATE_VISIBILITY

    if not configuration:
        configuration = settings.ACCOUNT_VISIBILITY_CONFIGURATION

    # Calling UserPreference directly because the requesting user may be different from existing_user
    # (and does not have to be is_staff).
    profile_privacy = UserPreference.get_value(user, ACCOUNT_VISIBILITY_PREF_KEY)
    return profile_privacy if profile_privacy else configuration.get('default_visibility')
Esempio n. 32
0
def share_with_facebook_friends(friend):
    """
    Return true if the user's share_with_facebook_friends preference is set to true.
    """

    # Calling UserPreference directly because the requesting user may be different (and not is_staff).
    try:
        existing_user = User.objects.get(username=friend['edX_username'])
    except ObjectDoesNotExist:
        return False

    return UserPreference.get_value(existing_user, 'share_with_facebook_friends') == 'True'
Esempio n. 33
0
def share_with_facebook_friends(friend):
    """
    Return true if the user's share_with_facebook_friends preference is set to true.
    """

    # Calling UserPreference directly because the requesting user may be different (and not is_staff).
    try:
        existing_user = User.objects.get(username=friend['edX_username'])
    except ObjectDoesNotExist:
        return False

    return UserPreference.get_value(existing_user,
                                    'share_with_facebook_friends') == 'True'
Esempio n. 34
0
    def claim_locale(self, data):
        """
        Return the locale for the users based on their preferences.
        Does not return a value if the users have not set their locale preferences.

        """

        language = UserPreference.get_preference(data['user'], LANGUAGE_KEY)

        # If the user has no language specified, return the default one.
        if not language:
            language = getattr(settings, 'LANGUAGE_CODE')

        return language
Esempio n. 35
0
    def claim_locale(self, data):
        """
        Return the locale for the users based on their preferences.
        Does not return a value if the users have not set their locale preferences.

        """

        language = UserPreference.get_preference(data['user'], LANGUAGE_KEY)

        # If the user has no language specified, return the default one.
        if not language:
            language = getattr(settings, 'LANGUAGE_CODE')

        return language
Esempio n. 36
0
    def claim_locale(self, data):
        """
        Return the locale for the users based on their preferences.
        Does not return a value if the users have not set their locale preferences.
        """

        # Calling UserPreference directly because it is not clear which user made the request.
        language = UserPreference.get_value(data['user'], LANGUAGE_KEY)

        # If the user has no language specified, return the default one.
        if not language:
            language = settings.LANGUAGE_CODE

        return language
Esempio n. 37
0
    def claim_locale(self, data):
        """
        Return the locale for the users based on their preferences.
        Does not return a value if the users have not set their locale preferences.
        """

        # Calling UserPreference directly because it is not clear which user made the request.
        language = UserPreference.get_value(data['user'], LANGUAGE_KEY)

        # If the user has no language specified, return the default one.
        if not language:
            language = settings.LANGUAGE_CODE

        return language
Esempio n. 38
0
    def _verify_full_account_response(self, response, requires_parental_consent=False, year_of_birth=2000):
        """
        Verify that all account fields are returned (even those that are not shareable).
        """
        data = response.data
        self.assertEqual(22, len(data))

        # public fields (3)
        expected_account_privacy = (
            PRIVATE_VISIBILITY if requires_parental_consent else
            UserPreference.get_value(self.user, 'account_privacy')
        )
        self.assertEqual(expected_account_privacy, data["account_privacy"])
        self._verify_profile_image_data(data, not requires_parental_consent)
        self.assertEqual(self.user.username, data["username"])

        # additional shareable fields (8)
        self.assertEqual(TEST_BIO_VALUE, data["bio"])
        self.assertEqual("US", data["country"])
        self.assertIsNotNone(data["date_joined"])
        self.assertEqual([{"code": TEST_LANGUAGE_PROFICIENCY_CODE}], data["language_proficiencies"])
        self.assertEqual("m", data["level_of_education"])
        self.assertIsNotNone(data["social_links"])
        self.assertEqual(UserPreference.get_value(self.user, 'time_zone'), data["time_zone"])
        self.assertIsNotNone(data["accomplishments_shared"])
        self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"])

        # additional admin fields (10)
        self.assertEqual(self.user.email, data["email"])
        self.assertIsNotNone(data["extended_profile"])
        self.assertEqual("f", data["gender"])
        self.assertEqual("world peace", data["goals"])
        self.assertTrue(data["is_active"])
        self.assertEqual("Park Ave", data['mailing_address'])
        self.assertEquals(requires_parental_consent, data["requires_parental_consent"])
        self.assertIsNone(data["secondary_email"])
        self.assertEqual(year_of_birth, data["year_of_birth"])
Esempio n. 39
0
    def test_get_private_profile(self, api_client, requesting_username):
        """
        Test that private profile information is only available to the test user themselves.
        """
        client = self.login_client(api_client, requesting_username)

        # Verify that a user with a private profile only returns the public fields
        UserPreference.set_preference(self.user, PROFILE_VISIBILITY_PREF_KEY,
                                      PRIVATE_VISIBILITY)
        self.create_mock_profile(self.user)
        response = self.send_get(client)
        self._verify_private_profile_response(response)

        # Verify that only the public fields are returned if 'include_all' parameter is specified as false
        response = self.send_get(client, query_parameters='include_all=false')
        self._verify_private_profile_response(response)

        # Verify that all fields are returned for the user themselves if
        # the 'include_all' parameter is specified as true.
        response = self.send_get(client, query_parameters='include_all=true')
        if requesting_username == "user":
            self._verify_full_profile_response(response)
        else:
            self._verify_private_profile_response(response)
Esempio n. 40
0
    def render_to_fragment(self, request, course_id=None, **kwargs):
        """
        Renders the user's course bookmarks as a fragment.
        """
        course_key = CourseKey.from_string(course_id)
        course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)

        language = UserPreference.get_value(request.user, 'pref-lang', default='en')

        context = {
            'csrf': csrf(request)['csrf_token'],
            'course': course,
            'bookmarks_api_url': reverse('bookmarks'),
            'language_preference': language,
        }
        html = render_to_string('course_bookmarks/course-bookmarks-fragment.html', context)
        inline_js = render_to_string('course_bookmarks/course_bookmarks_js.template', context)
        fragment = Fragment(html)
        self.add_fragment_resource_urls(fragment)
        fragment.add_javascript(inline_js)
        return fragment
Esempio n. 41
0
 def _verify_full_account_response(self, response, requires_parental_consent=False):
     """
     Verify that all account fields are returned (even those that are not shareable).
     """
     data = response.data
     self.assertEqual(17, len(data))
     self.assertEqual(self.user.username, data["username"])
     self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"])
     self.assertEqual("US", data["country"])
     self.assertEqual("f", data["gender"])
     self.assertEqual(2000, data["year_of_birth"])
     self.assertEqual("m", data["level_of_education"])
     self.assertEqual("world peace", data["goals"])
     self.assertEqual("Park Ave", data['mailing_address'])
     self.assertEqual(self.user.email, data["email"])
     self.assertTrue(data["is_active"])
     self.assertIsNotNone(data["date_joined"])
     self.assertEqual("Tired mother of twins", data["bio"])
     self._verify_profile_image_data(data, not requires_parental_consent)
     self.assertEquals(requires_parental_consent, data["requires_parental_consent"])
     self.assertEqual([{"code": "en"}], data["language_proficiencies"])
     self.assertEqual(UserPreference.get_value(self.user, 'account_privacy'), data["account_privacy"])
Esempio n. 42
0
 def test_default_lang_pref_saved(self, lang):
     with mock.patch("django.conf.settings.LANGUAGE_CODE", lang):
         response = self.client.post(self.url, self.params)
         self.assertEqual(response.status_code, 200)
         user = User.objects.get(username=self.username)
         self.assertEqual(UserPreference.get_preference(user, LANGUAGE_KEY), lang)
Esempio n. 43
0
 def test_header_lang_pref_saved(self, lang):
     response = self.client.post(self.url, self.params, HTTP_ACCEPT_LANGUAGE=lang)
     user = User.objects.get(username=self.username)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(UserPreference.get_preference(user, LANGUAGE_KEY), lang)
Esempio n. 44
0
def get_user_email_language(user):
    """
    Return the language most appropriate for writing emails to user. Returns
    None if the preference has not been set, or if the user does not exist.
    """
    return UserPreference.get_preference(user, LANGUAGE_KEY)
Esempio n. 45
0
 def test_language_in_user_prefs(self):
     # language set in the user preferences and not the session
     UserPreference.set_preference(self.user, LANGUAGE_KEY, 'eo')
     self.middleware.process_request(self.request)
     self.assertEquals(self.request.session['django_language'], 'eo')
Esempio n. 46
0
def get_user_email_language(user):
    """
    Return the language most appropriate for writing emails to user. Returns
    None if the preference has not been set, or if the user does not exist.
    """
    return UserPreference.get_preference(user, LANGUAGE_KEY)
Esempio n. 47
0
 def test_language_in_user_prefs(self):
     # language set in the user preferences and not the session
     UserPreference.set_preference(self.user, LANGUAGE_KEY, 'eo')
     self.middleware.process_request(self.request)
     self.assertEquals(self.request.session['django_language'], 'eo')