diff --git a/app/controllers/concerns/remotely_translatable.rb b/app/controllers/concerns/remotely_translatable.rb index b21f6c39b..7f47f0c8f 100644 --- a/app/controllers/concerns/remotely_translatable.rb +++ b/app/controllers/concerns/remotely_translatable.rb @@ -2,7 +2,7 @@ module RemotelyTranslatable private def detect_remote_translations(*args) - return [] unless Setting["feature.remote_translations"].present? + return [] unless Setting["feature.remote_translations"].present? && api_key_has_been_set_in_secrets? resources_groups(*args).flatten.select { |resource| translation_empty?(resource) }.map do |resource| remote_translation_for(resource) @@ -24,4 +24,8 @@ module RemotelyTranslatable args.compact - [feeds] + feeds.map(&:items) end + + def api_key_has_been_set_in_secrets? + Rails.application.secrets.microsoft_api_key.present? + end end diff --git a/spec/controllers/concerns/remotely_translatable_spec.rb b/spec/controllers/concerns/remotely_translatable_spec.rb index 149ce0c4f..8320e778c 100644 --- a/spec/controllers/concerns/remotely_translatable_spec.rb +++ b/spec/controllers/concerns/remotely_translatable_spec.rb @@ -4,6 +4,7 @@ include RemotelyTranslatable describe RemotelyTranslatable do before do Setting["feature.remote_translations"] = true + allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return("123") end describe "#detect_remote_translations" do @@ -48,6 +49,16 @@ describe RemotelyTranslatable do end end + it "When api key has not been set in secrets should not detect remote_translations" do + allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return(nil) + proposal = create(:proposal) + comment = create(:comment, commentable: proposal) + + I18n.with_locale(:es) do + expect(detect_remote_translations([proposal, comment])).to eq [] + end + end + it "When defined in current locale should not detect remote_translations" do proposal = create(:proposal) comment = create(:comment, commentable: proposal) diff --git a/spec/features/remote_translations_spec.rb b/spec/features/remote_translations_spec.rb index 980920243..c144a406a 100644 --- a/spec/features/remote_translations_spec.rb +++ b/spec/features/remote_translations_spec.rb @@ -7,6 +7,7 @@ describe "Remote Translations" do available_locales_response = %w[de en es fr pt zh-Hans] expect(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales). and_return(available_locales_response) + allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return("123") end describe "Display remote translation button when locale is included in microsoft translate client" do diff --git a/spec/shared/features/remotely_translatable.rb b/spec/shared/features/remotely_translatable.rb index 38c491e12..d7acff59f 100644 --- a/spec/shared/features/remotely_translatable.rb +++ b/spec/shared/features/remotely_translatable.rb @@ -12,6 +12,7 @@ shared_examples "remotely_translatable" do |factory_name, path_name, path_argume available_locales_response = %w[de en es fr pt zh-Hans] expect(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales).at_most(3).times. and_return(available_locales_response) + allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return("123") end context "Button to request remote translation" do