From e7fcca9b47f8cf0a167a171aeff6a3e64a90d4e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 20 Apr 2020 03:09:44 +0200 Subject: [PATCH 1/2] Check remote translations locales at runtime We were evaluating its value when the server starts. On production enviroments, that could mean weeks or months before the available locales are checked again, and so it would be possible to use a list which is no longer in sync with the list provided by microsoft. --- app/models/remote_translation.rb | 2 +- spec/models/remote_translation_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/remote_translation.rb b/app/models/remote_translation.rb index 85fc77bdd..63b4cfc92 100644 --- a/app/models/remote_translation.rb +++ b/app/models/remote_translation.rb @@ -4,7 +4,7 @@ class RemoteTranslation < ApplicationRecord validates :remote_translatable_id, presence: true validates :remote_translatable_type, presence: true validates :locale, presence: true - validates :locale, inclusion: { in: RemoteTranslations::Microsoft::AvailableLocales.available_locales } + validates :locale, inclusion: { in: ->(_) { RemoteTranslations::Microsoft::AvailableLocales.available_locales }} validate :already_translated_resource after_create :enqueue_remote_translation diff --git a/spec/models/remote_translation_spec.rb b/spec/models/remote_translation_spec.rb index 399952939..e0364033c 100644 --- a/spec/models/remote_translation_spec.rb +++ b/spec/models/remote_translation_spec.rb @@ -37,6 +37,18 @@ describe RemoteTranslation do expect(remote_translation).not_to be_valid end + it "checks available locales dynamically" do + allow(RemoteTranslations::Microsoft::AvailableLocales) + .to receive(:available_locales).and_return(["en"]) + + expect(remote_translation).not_to be_valid + + allow(RemoteTranslations::Microsoft::AvailableLocales) + .to receive(:available_locales).and_return(["es"]) + + expect(remote_translation).to be_valid + end + describe "#enqueue_remote_translation", :delay_jobs do it "after create enqueue Delayed Job" do expect { remote_translation.save }.to change { Delayed::Job.count }.by(1) From a20622e2fa1f3d9eb32a2e83ff410663c9e6c3d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 20 Apr 2020 03:17:39 +0200 Subject: [PATCH 2/2] Allow remote translation tests to be run offline The method `available_locales` in `RemoteTranslations::Microsoft::AvailableLocales` needs to execute a request to an external server in order to work, meaning it will fail if the machine its executed on doesn't have an internet connection. So we're stubbing the method in the tests using it. --- spec/controllers/remote_translation_controller_spec.rb | 2 +- spec/lib/remote_translations/caller_spec.rb | 2 +- spec/models/remote_translation_spec.rb | 2 +- spec/spec_helper.rb | 5 +++++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spec/controllers/remote_translation_controller_spec.rb b/spec/controllers/remote_translation_controller_spec.rb index ab4399f9a..e7685f46e 100644 --- a/spec/controllers/remote_translation_controller_spec.rb +++ b/spec/controllers/remote_translation_controller_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe RemoteTranslationsController do +describe RemoteTranslationsController, :remote_translations do describe "POST create", :delay_jobs do let(:debate) { create(:debate) } diff --git a/spec/lib/remote_translations/caller_spec.rb b/spec/lib/remote_translations/caller_spec.rb index c0f137b11..b24307cb9 100644 --- a/spec/lib/remote_translations/caller_spec.rb +++ b/spec/lib/remote_translations/caller_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe RemoteTranslations::Caller do +describe RemoteTranslations::Caller, :remote_translations do before do RemoteTranslation.skip_callback(:create, :after, :enqueue_remote_translation) end diff --git a/spec/models/remote_translation_spec.rb b/spec/models/remote_translation_spec.rb index e0364033c..ce8508898 100644 --- a/spec/models/remote_translation_spec.rb +++ b/spec/models/remote_translation_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe RemoteTranslation do +describe RemoteTranslation, :remote_translations do let(:remote_translation) { build(:remote_translation, locale: :es) } it "is valid" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ff616c6e9..808707a9b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -69,6 +69,11 @@ RSpec.configure do |config| Delayed::Worker.delay_jobs = false end + config.before(:each, :remote_translations) do + allow(RemoteTranslations::Microsoft::AvailableLocales) + .to receive(:available_locales).and_return(I18n.available_locales.map(&:to_s)) + end + config.before(:each, :with_frozen_time) do travel_to Time.current # TODO: use `freeze_time` after migrating to Rails 5.2. end