FIX: Validate interpolation keys used in translation overrides.

https://meta.discourse.org/t/discobot-translation-missing-error/64429/6?u=tgxworld
This commit is contained in:
Guo Xiang Tan
2017-06-15 17:08:23 +09:00
parent 7366f334b0
commit b5ec241716
7 changed files with 159 additions and 16 deletions

View File

@ -40,11 +40,36 @@ describe Admin::SiteTextsController do
end
end
context '.update and .revert' do
context '#update and #revert' do
after do
TranslationOverride.delete_all
I18n.reload!
end
describe 'failure' do
before do
TranslationOverride.any_instance.expects(:lookup_original_text)
.returns('%{first} %{second}')
end
it 'returns the right error message' do
xhr :put, :update, id: 'title', site_text: { value: 'hello %{key}' }
expect(response.status).to eq(422)
body = JSON.parse(response.body)
expect(body['message']).to eq(I18n.t(
'activerecord.errors.models.translation_overrides.attributes.value.missing_interpolation_keys',
keys: 'first, second'
))
end
end
it 'updates and reverts the key' do
orig_title = I18n.t(:title)
xhr :put, :update, id: 'title', site_text: {value: 'hello'}
xhr :put, :update, id: 'title', site_text: { value: 'hello' }
expect(response).to be_success
json = ::JSON.parse(response.body)
@ -56,7 +81,6 @@ describe Admin::SiteTextsController do
expect(site_text['id']).to eq('title')
expect(site_text['value']).to eq('hello')
# Revert
xhr :put, :revert, id: 'title'
expect(response).to be_success
@ -72,13 +96,28 @@ describe Admin::SiteTextsController do
end
it 'returns not found for missing keys' do
xhr :put, :update, id: 'made_up_no_key_exists', site_text: {value: 'hello'}
xhr :put, :update, id: 'made_up_no_key_exists', site_text: { value: 'hello' }
expect(response).not_to be_success
end
it 'logs the change' do
StaffActionLogger.any_instance.expects(:log_site_text_change).once
xhr :put, :update, id: 'title', site_text: {value: 'hello'}
original_title = I18n.t(:title)
xhr :put, :update, id: 'title', site_text: { value: 'yay' }
log = UserHistory.last
expect(log.previous_value).to eq(original_title)
expect(log.new_value).to eq('yay')
expect(log.action).to eq(UserHistory.actions[:change_site_text])
xhr :put, :revert, id: 'title'
log = UserHistory.last
expect(log.previous_value).to eq('yay')
expect(log.new_value).to eq(original_title)
expect(log.action).to eq(UserHistory.actions[:change_site_text])
end
end
end