set application locale from custom LTI launch parameter

This commit is contained in:
Hauke Klement
2015-02-23 11:33:43 +01:00
parent 6d592472e7
commit 5bfe03c426
3 changed files with 22 additions and 5 deletions

View File

@ -23,7 +23,7 @@ class ApplicationController < ActionController::Base
private :render_not_authorized private :render_not_authorized
def set_locale def set_locale
session[:locale] = params[:locale] if params[:locale] session[:locale] = params[:custom_locale] || params[:locale] || session[:locale]
I18n.locale = session[:locale] || I18n.default_locale I18n.locale = session[:locale] || I18n.default_locale
end end
private :set_locale private :set_locale

View File

@ -40,9 +40,18 @@ describe ApplicationController do
let(:locale) { :de } let(:locale) { :de }
context 'when specifying a locale' do context 'when specifying a locale' do
it 'overwrites the session' do context "using the 'custom_locale' parameter" do
expect(session).to receive(:[]=).with(:locale, locale.to_s) it 'overwrites the session' do
get :welcome, locale: locale expect(session).to receive(:[]=).with(:locale, locale.to_s)
get :welcome, custom_locale: locale
end
end
context "using the 'locale' parameter" do
it 'overwrites the session' do
expect(session).to receive(:[]=).with(:locale, locale.to_s)
get :welcome, locale: locale
end
end end
end end

View File

@ -29,6 +29,7 @@ describe SessionsController do
describe 'POST #create_through_lti' do describe 'POST #create_through_lti' do
let(:exercise) { FactoryGirl.create(:dummy) } let(:exercise) { FactoryGirl.create(:dummy) }
let(:nonce) { SecureRandom.hex } let(:nonce) { SecureRandom.hex }
before(:each) { I18n.locale = I18n.default_locale }
context 'without OAuth parameters' do context 'without OAuth parameters' do
it 'refuses the LTI launch' do it 'refuses the LTI launch' do
@ -69,7 +70,8 @@ describe SessionsController do
end end
context 'with valid launch parameters' do context 'with valid launch parameters' do
let(:request) { post :create_through_lti, custom_token: exercise.token, oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: user.external_id } let(:locale) { :de }
let(:request) { post :create_through_lti, custom_locale: locale, custom_token: exercise.token, oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: user.external_id }
let(:user) { FactoryGirl.create(:external_user, consumer_id: consumer.id) } let(:user) { FactoryGirl.create(:external_user, consumer_id: consumer.id) }
before(:each) { expect_any_instance_of(IMS::LTI::ToolProvider).to receive(:valid_request?).and_return(true) } before(:each) { expect_any_instance_of(IMS::LTI::ToolProvider).to receive(:valid_request?).and_return(true) }
@ -79,6 +81,12 @@ describe SessionsController do
expect(session[:external_user_id]).to eq(user.id) expect(session[:external_user_id]).to eq(user.id)
end end
it 'sets the specified locale' do
expect(controller).to receive(:set_locale).and_call_original
request
expect(I18n.locale).to eq(locale)
end
it 'assigns the exercise' do it 'assigns the exercise' do
request request
expect(assigns(:exercise)).to eq(exercise) expect(assigns(:exercise)).to eq(exercise)