From ad1a59a14c86eef4181844bb8c7add3d9a9381d3 Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Tue, 15 Dec 2020 14:44:11 -0500 Subject: [PATCH] Fix html variant image prefix logic (#11917) * Fix html variant image prefix logic * Update app/models/html_variant.rb Co-authored-by: Mac Siri * Update spec/models/html_variant_spec.rb Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> * Use helper to simplify url code and update spec * Provide Imgproxy default endpoint Co-authored-by: Mac Siri Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> Co-authored-by: Mac Siri --- app/models/html_variant.rb | 2 +- app/services/images/optimizer.rb | 2 +- spec/models/html_variant_spec.rb | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/models/html_variant.rb b/app/models/html_variant.rb index 0ee6a86ee..0d383fd1d 100644 --- a/app/models/html_variant.rb +++ b/app/models/html_variant.rb @@ -75,6 +75,6 @@ class HtmlVariant < ApplicationRecord end def allowed_image_host?(src) - src.start_with?("https://res.cloudinary.com/") + src.start_with?("https://res.cloudinary.com/") || src.start_with?(Images::Optimizer.get_imgproxy_endpoint) end end diff --git a/app/services/images/optimizer.rb b/app/services/images/optimizer.rb index 9ef1ae1aa..c06ace3d8 100644 --- a/app/services/images/optimizer.rb +++ b/app/services/images/optimizer.rb @@ -66,7 +66,7 @@ module Images # On other environments, rely on ApplicationConfig for a # more flexible configuration # ie. default imgproxy endpoint is localhost:8080 - ApplicationConfig["IMGPROXY_ENDPOINT"] + ApplicationConfig["IMGPROXY_ENDPOINT"] || "http://localhost:8080" end end end diff --git a/spec/models/html_variant_spec.rb b/spec/models/html_variant_spec.rb index 0a4f605be..00be54dad 100644 --- a/spec/models/html_variant_spec.rb +++ b/spec/models/html_variant_spec.rb @@ -55,4 +55,19 @@ RSpec.describe HtmlVariant, type: :model do cloudinary_string = "/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_420/https://devimages.com/image.jpg" expect(html_variant.html).to include(cloudinary_string) end + + it "does not add prefix if it already starts with cloudinary" do + allow(Images::Optimizer).to receive(:call) + html = %() + html_variant.update(approved: false, html: html) + expect(Images::Optimizer).not_to have_received(:call) + end + + it "does not add prefix if already on site root" do + allow(Images::Optimizer).to receive(:call) + html = "" + html_variant.update(approved: false, html: html) + html_variant.save + expect(Images::Optimizer).not_to have_received(:call) + end end