|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | RSpec.describe ActiveStorage::Service::DBService do |
4 | | - let(:fixture_data) { |
5 | | - (+"\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\020\000\000\000\020\001\003\000\000\000%=m\"\000\000\000\006PLTE\000\000\000\377\377\377\245\331\237\335\000\000\0003IDATx\234c\370\377\237\341\377_\206\377\237\031\016\2603\334?\314p\1772\303\315\315\f7\215\031\356\024\203\320\275\317\f\367\201R\314\f\017\300\350\377\177\000Q\206\027(\316]\233P\000\000\000\000IEND\256B`\202").force_encoding(Encoding::BINARY) # rubocop:disable Layout/LineLength |
6 | | - } |
| 4 | + let(:fixture_data) { build(:active_storage_db_file).data } |
7 | 5 | let(:content_type) { 'image/png' } |
| 6 | + let(:host) { 'http://test.example.com:3001' } |
8 | 7 | let(:checksum) { Digest::MD5.base64digest(fixture_data) } |
9 | 8 | let(:key) { SecureRandom.base58(24) } |
10 | | - let(:tmp_config) { { tmp: { service: 'DB' } } } |
11 | | - let(:service) { described_class.configure(:tmp, tmp_config) } |
| 9 | + let(:service) { described_class.configure(:tmp, tmp: { service: 'DB' }) } |
12 | 10 | let(:upload_options) { {} } |
13 | 11 | let(:upload) { service.upload(key, StringIO.new(fixture_data), upload_options) } |
14 | 12 |
|
|
18 | 16 | end |
19 | 17 |
|
20 | 18 | describe '.delete' do |
| 19 | + subject(:delete) { service.delete(key) } |
| 20 | + |
21 | 21 | before { upload } |
22 | 22 |
|
23 | 23 | it 'deletes the file' do |
24 | | - expect(service.delete(key)).to be_kind_of ActiveStorageDB::File |
25 | | - expect(service.delete(key)).to be_nil |
| 24 | + expect { delete }.to change { ActiveStorageDB::File.count }.from(1).to(0) |
26 | 25 | end |
27 | 26 | end |
28 | 27 |
|
29 | 28 | describe '.delete_prefixed' do |
| 29 | + subject(:delete_prefixed) { service.delete_prefixed(key[0..10]) } |
| 30 | + |
| 31 | + before { upload } |
| 32 | + |
30 | 33 | it 'deletes the files' do |
31 | | - file = upload |
32 | | - prefix = key[0..10] |
33 | | - expect(service.delete_prefixed(prefix)).to eq [file] |
34 | | - expect(service.delete_prefixed(prefix)).to be_empty |
| 34 | + expect { delete_prefixed }.to change { ActiveStorageDB::File.count }.from(1).to(0) |
35 | 35 | end |
36 | 36 | end |
37 | 37 |
|
|
48 | 48 | after { service.delete(key) } |
49 | 49 |
|
50 | 50 | it 'downloads the data' do |
51 | | - is_expected.to eq fixture_data |
| 51 | + expect(download).to eq fixture_data |
52 | 52 | end |
53 | 53 |
|
54 | | - context 'with a block' do |
55 | | - it 'sends the data to the block' do |
| 54 | + context 'with download a block' do |
| 55 | + let(:download_block) do |
56 | 56 | result = nil |
57 | 57 | service.download(key) do |data| |
58 | 58 | if result |
|
61 | 61 | result = data |
62 | 62 | end |
63 | 63 | end |
64 | | - expect(result).to eq fixture_data |
| 64 | + result |
| 65 | + end |
| 66 | + |
| 67 | + it 'sends the data to the block' do |
| 68 | + expect(download_block).to eq fixture_data |
65 | 69 | end |
66 | 70 | end |
67 | 71 | end |
68 | 72 | end |
69 | 73 |
|
70 | 74 | describe '.download_chunk' do |
71 | | - let(:subject) { service.download_chunk(key, range) } |
72 | | - let(:range) { (10..25) } |
| 75 | + subject { service.download_chunk(key, range) } |
| 76 | + |
| 77 | + let(:range) { (10..15) } |
73 | 78 |
|
74 | 79 | before { upload } |
75 | 80 |
|
76 | 81 | after { service.delete(key) } |
77 | 82 |
|
78 | | - it 'download a specific chunk of data' do |
79 | | - is_expected.to eq fixture_data[range] |
80 | | - end |
| 83 | + it { is_expected.to eq fixture_data[range] } |
81 | 84 | end |
82 | 85 |
|
83 | 86 | describe '.exist?' do |
84 | | - subject(:exist) { service.exist?(key) } |
| 87 | + subject { service.exist?(key) } |
85 | 88 |
|
86 | 89 | it { is_expected.to be_falsey } |
87 | 90 |
|
88 | | - context 'after an upload' do |
| 91 | + context 'when a file is uploaded' do |
89 | 92 | before { upload } |
90 | 93 |
|
91 | 94 | after { service.delete(key) } |
|
95 | 98 | end |
96 | 99 |
|
97 | 100 | describe '.headers_for_direct_upload' do |
98 | | - subject(:headers) { service.headers_for_direct_upload(key, content_type: content_type) } |
| 101 | + subject { service.headers_for_direct_upload(key, content_type: content_type) } |
99 | 102 |
|
100 | | - it 'returns the headers' do |
101 | | - is_expected.to eq({ 'Content-Type' => content_type }) |
102 | | - end |
| 103 | + it { is_expected.to eq('Content-Type' => content_type) } |
103 | 104 | end |
104 | 105 |
|
105 | 106 | describe '.upload' do |
|
129 | 130 | end |
130 | 131 |
|
131 | 132 | describe '.url' do |
132 | | - let(:subject) do |
133 | | - filename = ActiveStorage::Filename.new('avatar.png') |
| 133 | + subject do |
134 | 134 | service.url(key, expires_in: 5.minutes, disposition: :inline, filename: filename, content_type: content_type) |
135 | 135 | end |
136 | | - let(:host) { 'http://test.example.com:3001' } |
| 136 | + |
| 137 | + let(:filename) { ActiveStorage::Filename.new('avatar.png') } |
137 | 138 |
|
138 | 139 | before do |
139 | 140 | upload |
|
142 | 143 |
|
143 | 144 | after { service.delete(key) } |
144 | 145 |
|
145 | | - it 'generates the file URL' do |
146 | | - is_expected.to start_with host |
147 | | - end |
| 146 | + it { is_expected.to start_with host } |
148 | 147 | end |
149 | 148 |
|
150 | 149 | describe '.url_for_direct_upload' do |
| 150 | + subject { service.url_for_direct_upload(key, url_options) } |
| 151 | + |
151 | 152 | let(:url_options) do |
152 | 153 | { expires_in: 5.minutes, content_type: content_type, content_length: fixture_data.size, checksum: checksum } |
153 | 154 | end |
154 | | - let(:subject) { service.url_for_direct_upload(key, url_options) } |
155 | | - let(:host) { 'http://test.example.com:3001' } |
156 | 155 |
|
157 | 156 | before do |
158 | 157 | upload |
|
161 | 160 |
|
162 | 161 | after { service.delete(key) } |
163 | 162 |
|
164 | | - it 'generates the file URL' do |
165 | | - is_expected.to start_with host |
166 | | - end |
| 163 | + it { is_expected.to start_with host } |
167 | 164 | end |
168 | 165 | end |
0 commit comments