Changeset fd6da93 in OpenWorkouts-current for ow/tests/views/test_user.py


Ignore:
Timestamp:
Feb 26, 2019, 11:11:43 AM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
a4e4799
Parents:
d411dae
Message:

(#56) Add support for different locale/language:

  • Let users choose their lang/locale in the edit profile page
  • Set the currently selected locale as a cookie (following pyramid docs on how to set the locale using the default locale negotiator)
  • Save the locale setting for each user as an attribute on the User model
  • Set the proper locale as a cookie on login
  • Unset the locale cookie on logout

Default available locales for now are en (english) and es (spanish)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/tests/views/test_user.py

    rd411dae rfd6da93  
    2020from PIL import Image
    2121
     22from pytz import common_timezones
     23
    2224from ow.models.root import OpenWorkouts
    2325from ow.models.user import User
    2426from ow.models.workout import Workout
    2527from ow.views.renderers import OWFormRenderer
     28from ow.utilities import get_available_locale_names
    2629import ow.views.user as user_views
    2730
     
    5154    def dummy_request(self, root):
    5255        request = DummyRequest()
     56        request.registry.settings = {
     57            'pyramid.default_locale_name': 'en'
     58        }
    5359        request.root = root
    5460        return request
     
    561567        assert rem.called
    562568        assert response.location == request.resource_url(john)
     569        # the response headers contain the proper set_cookie for the default
     570        # locale
     571        default_locale_name = request.registry.settings[
     572            'pyramid.default_locale_name']
     573        expected_locale_header = '_LOCALE_=' + default_locale_name + '; Path=/'
     574        assert response.headers['Set-Cookie'] == expected_locale_header
     575
     576    @patch('ow.views.user.remember')
     577    def test_login_post_ok_set_locale(self, rem, dummy_request, john):
     578        # same as the previous test, but this time the user has set a
     579        # locale different than the default one
     580        request = dummy_request
     581        request.method = 'POST'
     582        request.POST['submit'] = True
     583        request.POST['email'] = 'john.doe@example.net'
     584        request.POST['password'] = 's3cr3t'
     585        # verify the user first
     586        john.verified = True
     587        # set the locale
     588        john.locale = 'es'
     589        response = user_views.login(request.root, request)
     590        assert isinstance(response, HTTPFound)
     591        assert rem.called
     592        assert response.location == request.resource_url(john)
     593        # the response headers contain the proper set_cookie for the user
     594        # locale setting
     595        expected_locale_header = '_LOCALE_=es; Path=/'
     596        assert response.headers['Set-Cookie'] == expected_locale_header
    563597
    564598    @patch('ow.views.user.forget')
     
    569603        assert forg.called
    570604        assert response.location == request.resource_url(request.root)
     605        # the response headers contain the needed Set-Cookie header that
     606        # invalidates the _LOCALE_ cookie, preventing problems with users
     607        # sharing the same web browser (one locale setting being set for
     608        # another user)
     609        expected_locale_header = '_LOCALE_=; Max-Age=0; Path=/; expires='
     610        assert expected_locale_header in response.headers['Set-Cookie']
    571611
    572612    extensions = ('png', 'jpg', 'jpeg', 'gif')
     
    639679        # loaded user profile
    640680        data = ['firstname', 'lastname', 'email', 'nickname', 'bio',
    641                 'birth_date', 'height', 'weight', 'gender', 'timezone']
     681                'birth_date', 'height', 'weight', 'gender', 'timezone',
     682                'locale']
    642683        assert list(response['form'].data.keys()) == data
    643684        # and check the email to see data is properly loaded
    644685        assert response['form'].data['email'] == 'john.doe@example.net'
     686        assert response['timezones'] == common_timezones
     687        assert response[
     688            'available_locale_names'] == get_available_locale_names()
     689        assert response['current_locale'] == request.registry.settings[
     690            'pyramid.default_locale_name']
    645691
    646692    def test_edit_profile_post_ok(self, profile_post_request, john):
     
    654700        assert response.location == request.resource_url(user, 'profile')
    655701        assert user.bio == bio
     702
     703    def test_edit_profile_post_ok_change_locale(
     704            self, profile_post_request, john):
     705        request = profile_post_request
     706        user = john
     707        # Update the locale
     708        request.POST['locale'] = 'es'
     709        response = user_views.edit_profile(user, request)
     710        assert isinstance(response, HTTPFound)
     711        assert response.location == request.resource_url(user, 'profile')
     712        assert user.locale == 'es'
     713
     714    def test_edit_profile_post_ok_invalid_locale(
     715            self, profile_post_request, john):
     716        request = profile_post_request
     717        user = john
     718        # Update the locale with an invalid option
     719        request.POST['locale'] = 'XX'
     720        response = user_views.edit_profile(user, request)
     721        assert isinstance(response['form'], OWFormRenderer)
     722        # as an error happened, the current_locale had not changed
     723        assert response['form'].errors == {
     724            'locale': "Value must be one of: en; es (not 'XX')"}
     725        assert user.locale == 'en'
    656726
    657727    def test_edit_profile_post_missing_required(
Note: See TracChangeset for help on using the changeset viewer.