|
5 | 5 | require 'logger' |
6 | 6 | require 'base64' |
7 | 7 | require 'digest' |
| 8 | +require 'active_storage/errors' |
8 | 9 | require 'active_storage/service/uploadcare_service' |
9 | 10 |
|
10 | 11 | RSpec.describe ActiveStorage::Service::UploadcareService do |
11 | 12 | let(:service) { described_class.new(public_key: 'demopublickey', secret_key: 'demosecretkey') } |
| 13 | + let(:public_service) { described_class.new(public_key: 'demopublickey', secret_key: 'demosecretkey', public: true) } |
12 | 14 | let(:uuid) { '2d33999d-c74a-4ff9-99ea-abc23496b052' } |
13 | 15 |
|
14 | 16 | before do |
15 | 17 | stub_const('ActiveStorage::Blob', Class.new) |
16 | 18 | allow(ActiveStorage).to receive(:logger).and_return(Logger.new(nil)) |
17 | 19 | allow(ActiveStorage::Blob).to receive(:where).and_return(double(pluck: [])) |
| 20 | + allow(ActiveStorage::Blob).to receive(:find_by).and_return(nil) |
18 | 21 | end |
19 | 22 |
|
20 | 23 | it 'uploads a file and persists uploadcare uuid into blob metadata' do |
|
32 | 35 | service.upload('blob-key', io, checksum: checksum) |
33 | 36 | end |
34 | 37 |
|
| 38 | + it 'raises integrity error for invalid checksum' do |
| 39 | + io = StringIO.new('test payload') |
| 40 | + bad_checksum = Base64.strict_encode64(Digest::MD5.digest('different payload')) |
| 41 | + |
| 42 | + uploadcare_file = double(uuid: uuid) |
| 43 | + allow(Uploadcare::Uploader).to receive(:upload_file).and_return(uploadcare_file) |
| 44 | + |
| 45 | + expect { service.upload('blob-key', io, checksum: bad_checksum) }.to raise_error(ActiveStorage::IntegrityError) |
| 46 | + end |
| 47 | + |
| 48 | + it 'downloads file body for mapped key' do |
| 49 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }) |
| 50 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 51 | + allow(Uploadcare::File).to receive(:info).with(uuid: uuid, config: kind_of(Uploadcare::Configuration)) |
| 52 | + .and_return(double(original_file_url: 'https://ucarecdn.com/file.bin', cdn_url: nil)) |
| 53 | + allow(service).to receive(:request).with('https://ucarecdn.com/file.bin').and_return('file-body') |
| 54 | + |
| 55 | + expect(service.download('blob-key')).to eq('file-body') |
| 56 | + end |
| 57 | + |
| 58 | + it 'streams download when block is given' do |
| 59 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }) |
| 60 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 61 | + allow(Uploadcare::File).to receive(:info).with(uuid: uuid, config: kind_of(Uploadcare::Configuration)) |
| 62 | + .and_return(double(original_file_url: nil, cdn_url: 'https://ucarecdn.com/file.bin')) |
| 63 | + response = double |
| 64 | + allow(response).to receive(:read_body).and_yield('ab').and_yield('cd') |
| 65 | + allow(service).to receive(:request).with('https://ucarecdn.com/file.bin').and_yield(response) |
| 66 | + |
| 67 | + chunks = [] |
| 68 | + service.download('blob-key') { |chunk| chunks << chunk } |
| 69 | + |
| 70 | + expect(chunks).to eq(%w[ab cd]) |
| 71 | + end |
| 72 | + |
| 73 | + it 'raises file not found on download for missing blob mapping' do |
| 74 | + expect { service.download('missing-key') }.to raise_error(ActiveStorage::FileNotFoundError) |
| 75 | + end |
| 76 | + |
| 77 | + it 'raises file not found on download when uploadcare does not know uuid' do |
| 78 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }) |
| 79 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 80 | + allow(Uploadcare::File).to receive(:info).with(uuid: uuid, config: kind_of(Uploadcare::Configuration)) |
| 81 | + .and_raise(Uploadcare::Exception::NotFoundError) |
| 82 | + |
| 83 | + expect { service.download('blob-key') }.to raise_error(ActiveStorage::FileNotFoundError) |
| 84 | + end |
| 85 | + |
| 86 | + it 'downloads chunk with requested range' do |
| 87 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }) |
| 88 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 89 | + allow(Uploadcare::File).to receive(:info).with(uuid: uuid, config: kind_of(Uploadcare::Configuration)) |
| 90 | + .and_return(double(original_file_url: nil, cdn_url: 'https://ucarecdn.com/file.bin')) |
| 91 | + allow(service).to receive(:request).with('https://ucarecdn.com/file.bin', range: 0..3).and_return('data') |
| 92 | + |
| 93 | + expect(service.download_chunk('blob-key', 0..3)).to eq('data') |
| 94 | + end |
| 95 | + |
| 96 | + it 'raises file not found on download_chunk when uploadcare does not know uuid' do |
| 97 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }) |
| 98 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 99 | + allow(Uploadcare::File).to receive(:info).with(uuid: uuid, config: kind_of(Uploadcare::Configuration)) |
| 100 | + .and_raise(Uploadcare::Exception::NotFoundError) |
| 101 | + |
| 102 | + expect { service.download_chunk('blob-key', 0..3) }.to raise_error(ActiveStorage::FileNotFoundError) |
| 103 | + end |
| 104 | + |
| 105 | + it 'deletes a mapped file' do |
| 106 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }) |
| 107 | + uploadcare_file = double(delete: true) |
| 108 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 109 | + expect(Uploadcare::File).to receive(:new).with({ uuid: uuid }, kind_of(Uploadcare::Configuration)).and_return(uploadcare_file) |
| 110 | + |
| 111 | + service.delete('blob-key') |
| 112 | + end |
| 113 | + |
| 114 | + it 'does not delete when mapping is missing' do |
| 115 | + expect(Uploadcare::File).not_to receive(:new) |
| 116 | + |
| 117 | + service.delete('missing-key') |
| 118 | + end |
| 119 | + |
| 120 | + it 'returns nil when delete gets not found error' do |
| 121 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }) |
| 122 | + uploadcare_file = double |
| 123 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 124 | + allow(Uploadcare::File).to receive(:new).with({ uuid: uuid }, kind_of(Uploadcare::Configuration)).and_return(uploadcare_file) |
| 125 | + allow(uploadcare_file).to receive(:delete).and_raise(Uploadcare::Exception::NotFoundError) |
| 126 | + |
| 127 | + expect(service.delete('blob-key')).to be_nil |
| 128 | + end |
| 129 | + |
| 130 | + it 'deletes all keys with prefix' do |
| 131 | + allow(ActiveStorage::Blob).to receive(:where).with('key LIKE ?', 'prefix%').and_return(double(pluck: %w[prefix-1 prefix-2])) |
| 132 | + allow(service).to receive(:delete) |
| 133 | + |
| 134 | + service.delete_prefixed('prefix') |
| 135 | + |
| 136 | + expect(service).to have_received(:delete).with('prefix-1') |
| 137 | + expect(service).to have_received(:delete).with('prefix-2') |
| 138 | + end |
| 139 | + |
35 | 140 | it 'supports existence check using mapped uuid' do |
36 | 141 | blob = double(metadata: { 'uploadcare_uuid' => uuid }, update!: true) |
37 | 142 | allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
|
40 | 145 | expect(service.exist?('blob-key')).to eq(true) |
41 | 146 | end |
42 | 147 |
|
| 148 | + it 'returns false from exist? when file is not found in uploadcare' do |
| 149 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }, update!: true) |
| 150 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 151 | + allow(Uploadcare::File).to receive(:info).with(uuid: uuid, config: kind_of(Uploadcare::Configuration)) |
| 152 | + .and_raise(Uploadcare::Exception::NotFoundError) |
| 153 | + |
| 154 | + expect(service.exist?('blob-key')).to eq(false) |
| 155 | + end |
| 156 | + |
| 157 | + it 'returns false from exist? when key is unknown' do |
| 158 | + expect(service.exist?('missing-key')).to eq(false) |
| 159 | + end |
| 160 | + |
| 161 | + it 'uses instrumentation payload for exist? true path' do |
| 162 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }, update!: true) |
| 163 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 164 | + allow(Uploadcare::File).to receive(:info).with(uuid: uuid, config: kind_of(Uploadcare::Configuration)).and_return(double) |
| 165 | + |
| 166 | + payload = {} |
| 167 | + expect(service).to receive(:instrument).with(:exist, key: 'blob-key').and_yield(payload) |
| 168 | + |
| 169 | + expect(service.exist?('blob-key')).to eq(true) |
| 170 | + expect(payload[:exist]).to eq(true) |
| 171 | + end |
| 172 | + |
| 173 | + it 'uses instrumentation payload for exist? false path' do |
| 174 | + payload = {} |
| 175 | + expect(service).to receive(:instrument).with(:exist, key: 'missing-key').and_yield(payload) |
| 176 | + |
| 177 | + expect(service.exist?('missing-key')).to eq(false) |
| 178 | + expect(payload[:exist]).to eq(false) |
| 179 | + end |
| 180 | + |
| 181 | + it 'uses uuid key directly when key is a uuid' do |
| 182 | + allow(Uploadcare::File).to receive(:info).with(uuid: uuid, config: kind_of(Uploadcare::Configuration)).and_return(double) |
| 183 | + |
| 184 | + expect(service.exist?(uuid)).to eq(true) |
| 185 | + end |
| 186 | + |
| 187 | + it 'returns empty headers for direct upload' do |
| 188 | + expect(service.headers_for_direct_upload('key', checksum: 'x')).to eq({}) |
| 189 | + end |
| 190 | + |
43 | 191 | it 'raises for direct upload support' do |
44 | 192 | expect { service.url_for_direct_upload('key', expires_in: 10, content_type: 'text/plain', content_length: 1, checksum: 'x') } |
45 | 193 | .to raise_error(NotImplementedError) |
46 | 194 | end |
| 195 | + |
| 196 | + it 'generates url for private service' do |
| 197 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }) |
| 198 | + file = double(cdn_url: 'https://ucarecdn.com/private') |
| 199 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 200 | + allow(Uploadcare::File).to receive(:new).with({ uuid: uuid }, kind_of(Uploadcare::Configuration)).and_return(file) |
| 201 | + |
| 202 | + expect(service.url('blob-key', expires_in: 60, filename: 'a.txt', disposition: :inline, content_type: 'text/plain')) |
| 203 | + .to eq('https://ucarecdn.com/private') |
| 204 | + end |
| 205 | + |
| 206 | + it 'generates url for public service' do |
| 207 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }) |
| 208 | + file = double(cdn_url: 'https://ucarecdn.com/public') |
| 209 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 210 | + allow(Uploadcare::File).to receive(:new).with({ uuid: uuid }, kind_of(Uploadcare::Configuration)).and_return(file) |
| 211 | + |
| 212 | + expect(public_service.url('blob-key', expires_in: 60, filename: 'a.txt', disposition: :inline, content_type: 'text/plain')) |
| 213 | + .to eq('https://ucarecdn.com/public') |
| 214 | + end |
| 215 | + |
| 216 | + it 'uses blob metadata mapping for download after upload mapping persisted' do |
| 217 | + blob = double(metadata: { 'uploadcare_uuid' => uuid }, update!: true) |
| 218 | + allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob) |
| 219 | + allow(Uploadcare::File).to receive(:info).with(uuid: uuid, config: kind_of(Uploadcare::Configuration)) |
| 220 | + .and_return(double(original_file_url: 'https://ucarecdn.com/file.bin', cdn_url: nil)) |
| 221 | + allow(service).to receive(:request).with('https://ucarecdn.com/file.bin').and_return('file-body') |
| 222 | + |
| 223 | + expect(service.download('blob-key')).to eq('file-body') |
| 224 | + end |
47 | 225 | end |
0 commit comments