docbrown/spec/lib/html_css_to_image_spec.rb
Michael Kohl 6dfabd578f
Rename SiteConfig to Settings::General (#13573)
* Rename SiteConfig

* More renaming

* Update spec

* Update mandatory settings mapping

* More renaming

* e2e test fixes

* You have a rename, and you have a rename

* Spec fix

* More changes

* Temporarily disable specs

* After-merge update

* Undo rename for migration

* undo rename of DUS

* Fix DUS

* Fix merge problem

* Remove redundant DUS

* Fix specs

* Remove unused code

* Change wrong class name

* More cleanup

* Re-add missing values to constant

* Fix constant

* Fix spec

* Remove obsolete fields

* Add accidentally removed field

* Update spec

* Move methods from Settings::General to ForemInstance

* Remove unneeded model

* Change mentions of 'site config'
2021-05-21 14:45:37 +02:00

65 lines
2.6 KiB
Ruby

require "rails_helper"
RSpec.describe HtmlCssToImage, type: :lib do
describe ".url" do
it "returns the url to the created image" do
stub_request(:post, /hcti.io/)
.to_return(status: 200,
body: '{ "url": "https://hcti.io/v1/image/6c52de9d-4d37-4008-80f8-67155589e1a1" }',
headers: { "Content-Type" => "application/json" })
expect(described_class.url(html: "test")).to eq("https://hcti.io/v1/image/6c52de9d-4d37-4008-80f8-67155589e1a1")
end
it "returns fallback image if the request fails" do
stub_request(:post, /hcti.io/)
.to_return(status: 429,
body: '{ "error": "Plan limit exceeded" }',
headers: { "Content-Type" => "application/json" })
expect(described_class.url(html: "test")).to eq described_class.fallback_image
end
end
describe ".fetch_url" do
before do
allow(Settings::General).to receive(:main_social_image).and_return("https://testimage.com/image.png")
allow(Rails.cache).to receive(:write)
allow(Rails.cache).to receive(:read)
end
it "caches the image url when successful" do
stub_request(:post, /hcti.io/)
.to_return(status: 200,
body: '{ "url": "https://hcti.io/v1/image/6c52de9d-4d37-4008-80f8-67155589e1a1" }',
headers: { "Content-Type" => "application/json" })
expected_url = "https://hcti.io/v1/image/6c52de9d-4d37-4008-80f8-67155589e1a1"
expect(described_class.fetch_url(html: "test")).to eq(expected_url)
expect(Rails.cache).to have_received(:write).once
end
it "cache has a long expiration" do
# Images are expensive to generate, make sure we don't expire them too quickly.
stub_request(:post, /hcti.io/)
.to_return(status: 200,
body: '{ "url": "https://hcti.io/v1/image/6c52de9d-4d37-4008-80f8-67155589e1a1" }',
headers: { "Content-Type" => "application/json" })
expected_url = "https://hcti.io/v1/image/6c52de9d-4d37-4008-80f8-67155589e1a1"
expect(described_class.fetch_url(html: "test")).to eq(expected_url)
expect(Rails.cache).to have_received(:write)
.with(anything, anything, expires_in: HtmlCssToImage::CACHE_EXPIRATION).once
end
it "does not cache errors" do
stub_request(:post, /hcti.io/)
.to_return(status: 429,
body: '{ "error": "Plan limit exceeded" }',
headers: { "Content-Type" => "application/json" })
expect(described_class.fetch_url(html: "test")).to eq described_class.fallback_image
expect(Rails.cache).not_to have_received(:write)
end
end
end