|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# |
| 4 | +require 'rails_helper' |
| 5 | +require 'scratch_asset_importer' |
| 6 | + |
| 7 | +RSpec.describe ScratchAssetImporter do |
| 8 | + describe '.import' do |
| 9 | + it 'imports assets from the config' do |
| 10 | + image = Rails.root.join('spec/fixtures/files/test_image_1.png').read |
| 11 | + stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 200, body: image) |
| 12 | + |
| 13 | + described_class.import(['123abc.png'], 'https://example.net/internalapi/asset/') |
| 14 | + |
| 15 | + scratch_asset = ScratchAsset.find_by(filename: '123abc.png') |
| 16 | + expect(scratch_asset).to be_present |
| 17 | + expect(scratch_asset.file.download).to eq(image) |
| 18 | + end |
| 19 | + |
| 20 | + it 'does nothing if asset already exists' do |
| 21 | + create(:scratch_asset, :with_file, filename: '123abc.png') |
| 22 | + |
| 23 | + expect do |
| 24 | + described_class.import(['123abc.png'], 'https://example.net/internalapi/asset/') |
| 25 | + end.not_to change(ScratchAsset, :count) |
| 26 | + end |
| 27 | + |
| 28 | + it 'can import multiple assets' do |
| 29 | + image = Rails.root.join('spec/fixtures/files/test_image_1.png').read |
| 30 | + |
| 31 | + stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 200, body: image) |
| 32 | + stub_request(:get, 'https://example.net/internalapi/asset/456xyz.png/get/').to_return(status: 200, body: image) |
| 33 | + |
| 34 | + described_class.import(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/') |
| 35 | + expect(ScratchAsset.find_by(filename: '123abc.png')).to be_present |
| 36 | + expect(ScratchAsset.find_by(filename: '456xyz.png')).to be_present |
| 37 | + end |
| 38 | + |
| 39 | + it 'skips assets that fail to import' do |
| 40 | + image = Rails.root.join('spec/fixtures/files/test_image_1.png').read |
| 41 | + |
| 42 | + stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 500, body: 'error') |
| 43 | + stub_request(:get, 'https://example.net/internalapi/asset/456xyz.png/get/').to_return(status: 200, body: image) |
| 44 | + |
| 45 | + described_class.import(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/') |
| 46 | + expect(ScratchAsset.find_by(filename: '123abc.png')).not_to be_present |
| 47 | + expect(ScratchAsset.find_by(filename: '456xyz.png')).to be_present |
| 48 | + end |
| 49 | + end |
| 50 | +end |
0 commit comments