DEV: Remove use of fake external store in optimized images spec.

Testing against fake stuff is bad because we're not testing against reality when
reality changes.
This commit is contained in:
Guo Xiang Tan 2019-05-29 20:10:46 +08:00
parent 630e9814bc
commit 20e783337d
1 changed files with 35 additions and 34 deletions

View File

@ -292,38 +292,60 @@ describe OptimizedImage do
end end
describe "external store" do describe "external store" do
let(:s3_upload) { Fabricate(:upload_s3) }
let(:store) { FakeExternalStore.new } before do
before { Discourse.stubs(:store).returns(store) } SiteSetting.enable_s3_uploads = true
SiteSetting.s3_upload_bucket = "s3-upload-bucket"
SiteSetting.s3_access_key_id = "some key"
SiteSetting.s3_secret_access_key = "some secret key"
tempfile = Tempfile.new(["discourse-external", ".png"])
%i{head get}.each do |method|
stub_request(method, "http://#{s3_upload.url}")
.to_return(
status: 200,
body: tempfile.read
)
end
end
context "when an error happened while generatign the thumbnail" do context "when an error happened while generatign the thumbnail" do
it "returns nil" do it "returns nil" do
OptimizedImage.expects(:resize).returns(false) OptimizedImage.expects(:resize).returns(false)
expect(OptimizedImage.create_for(upload, 100, 200)).to eq(nil) expect(OptimizedImage.create_for(s3_upload, 100, 200)).to eq(nil)
end end
end end
context "when the thumbnail is properly generated" do context "when the thumbnail is properly generated" do
before do before do
OptimizedImage.expects(:resize).returns(true) OptimizedImage.expects(:resize).returns(true)
end end
it "downloads a copy of the original image" do it "downloads a copy of the original image" do
Tempfile.any_instance.expects(:close!) optimized_path = "/optimized/1X/#{s3_upload.sha1}_2_100x200.png"
store.expects(:download).with(upload).returns(Tempfile.new(["discourse-external", ".png"]))
OptimizedImage.create_for(upload, 100, 200) stub_request(
end :head,
"https://#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com/"
)
stub_request(
:put,
"https://#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com#{optimized_path}"
).to_return(
status: 200,
headers: { "ETag" => "someetag" }
)
oi = OptimizedImage.create_for(s3_upload, 100, 200)
it "works" do
oi = OptimizedImage.create_for(upload, 100, 200)
expect(oi.sha1).to eq("da39a3ee5e6b4b0d3255bfef95601890afd80709") expect(oi.sha1).to eq("da39a3ee5e6b4b0d3255bfef95601890afd80709")
expect(oi.extension).to eq(".png") expect(oi.extension).to eq(".png")
expect(oi.width).to eq(100) expect(oi.width).to eq(100)
expect(oi.height).to eq(200) expect(oi.height).to eq(200)
expect(oi.url).to eq("/externally/stored/optimized/image.png") expect(oi.url).to eq("//#{SiteSetting.s3_upload_bucket}.s3.dualstack.us-east-1.amazonaws.com#{optimized_path}")
end end
end end
@ -361,24 +383,3 @@ class FakeInternalStore
end end
end end
class FakeExternalStore
def path_for(upload)
nil
end
def external?
true
end
def store_optimized_image(file, optimized_image)
"/externally/stored/optimized/image#{optimized_image.extension}"
end
def download(upload)
extension = File.extname(upload.original_filename)
Tempfile.new(["discourse-s3", extension])
end
end