Add test for `Export#finalize` with a profile header attached

This commit is contained in:
Karina Kwiatek 2022-07-10 18:31:51 +02:00 committed by Karina Kwiatek
parent 45a87bf2b4
commit eaf61f1a6a
2 changed files with 29 additions and 5 deletions

View File

@ -93,7 +93,7 @@ class Exporter
target_file = "#{@export_dirname}/pictures/picture_#{s}_#{@user.profile_picture_file_name}" target_file = "#{@export_dirname}/pictures/picture_#{s}_#{@user.profile_picture_file_name}"
File.open target_file, "wb" do |f| File.open target_file, "wb" do |f|
f.binmode f.binmode
data = if url.start_with?("/system") data = if url.start_with?("/")
File.read(Rails.root.join("public", url.sub(%r{\A/+}, ""))) File.read(Rails.root.join("public", url.sub(%r{\A/+}, "")))
else else
HTTParty.get(url).parsed_response HTTParty.get(url).parsed_response
@ -109,7 +109,7 @@ class Exporter
target_file = "#{@export_dirname}/pictures/header_#{s}_#{@user.profile_header_file_name}" target_file = "#{@export_dirname}/pictures/header_#{s}_#{@user.profile_header_file_name}"
File.open target_file, "wb" do |f| File.open target_file, "wb" do |f|
f.binmode f.binmode
data = if url.start_with?("/system") data = if url.start_with?("/")
File.read(Rails.root.join("public", url.sub(%r{\A/+}, ""))) File.read(Rails.root.join("public", url.sub(%r{\A/+}, "")))
else else
HTTParty.get(url).parsed_response HTTParty.get(url).parsed_response

View File

@ -38,6 +38,15 @@ RSpec.describe Exporter do
let(:user) { FactoryBot.create(:user, **user_params) } let(:user) { FactoryBot.create(:user, **user_params) }
let(:instance) { described_class.new(user) } let(:instance) { described_class.new(user) }
before do
stub_const("APP_CONFIG", {
"hostname" => "example.com",
"https" => true,
"items_per_page" => 5,
"fog" => {}
})
end
after do after do
filename = instance.instance_variable_get(:@export_dirname) filename = instance.instance_variable_get(:@export_dirname)
FileUtils.rm_r(filename) if File.exist?(filename) FileUtils.rm_r(filename) if File.exist?(filename)
@ -201,6 +210,8 @@ RSpec.describe Exporter do
describe "#finalize" do describe "#finalize" do
let(:fake_rails_root) { Pathname(Dir.mktmpdir) } let(:fake_rails_root) { Pathname(Dir.mktmpdir) }
let(:dir) { instance.instance_variable_get(:@export_dirname) }
let(:name) { instance.instance_variable_get(:@export_filename) }
before do before do
instance.instance_variable_set(:@obj, { instance.instance_variable_set(:@obj, {
@ -210,15 +221,15 @@ RSpec.describe Exporter do
} }
} }
}) })
Dir.mkdir("#{fake_rails_root}/public")
FileUtils.cp_r(Rails.root.join('public/images'), "#{fake_rails_root}/public/images")
allow(Rails).to receive(:root).and_return(fake_rails_root) allow(Rails).to receive(:root).and_return(fake_rails_root)
end end
subject { instance.send(:finalize) } subject { instance.send(:finalize) }
context "exporting a user without a profile picture or header" do context "exporting a user without a profile picture or header" do
let(:dir) { instance.instance_variable_get(:@export_dirname) }
let(:name) { instance.instance_variable_get(:@export_filename) }
it "prepares files to be archived" do it "prepares files to be archived" do
subject subject
expect(File.directory?(fake_rails_root.join("public/export"))).to eq(true) expect(File.directory?(fake_rails_root.join("public/export"))).to eq(true)
@ -245,5 +256,18 @@ RSpec.describe Exporter do
expect(File.exist?(path)).to eq(true) expect(File.exist?(path)).to eq(true)
end end
end end
context "exporting a user with a profile header" do
before do
user.profile_header = Rack::Test::UploadedFile.new(File.open("#{file_fixture_path}/banana_racc.jpg"))
user.save!
end
it "exports the header image" do
subject
dirname = instance.instance_variable_get(:@export_dirname)
expect(File.exist?("#{dirname}/pictures/header_web_banana_racc.jpg")).to eq(true)
end
end
end end
end end