Skip to content

Commit 11fc13f

Browse files
committed
Some specs improvements
1 parent 02308f8 commit 11fc13f

6 files changed

Lines changed: 63 additions & 53 deletions

File tree

.rubocop.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
inherit_from:
22
- https://relaxed.ruby.style/rubocop.yml
33

4+
require: rubocop-rspec
5+
46
AllCops:
57
TargetRubyVersion: 2.6
68
Exclude:
@@ -12,6 +14,17 @@ AllCops:
1214
Gemspec/RequiredRubyVersion:
1315
Enabled: false
1416

15-
Layout/LineLength:
17+
Metrics/LineLength:
1618
Enabled: true
1719
Max: 120
20+
21+
RSpec/MultipleExpectations:
22+
# Default is 1
23+
Max: 5
24+
25+
RSpec/NestedGroups:
26+
# Default is 3
27+
Max: 5
28+
29+
RSpec/ExampleLength:
30+
Max: 6

active_storage_db.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
3939
spec.add_development_dependency 'rspec_junit_formatter', '~> 0.4.1'
4040
spec.add_development_dependency 'rspec-rails', '~> 4.0.1'
4141
spec.add_development_dependency 'rubocop', '~> 0.89.0'
42+
spec.add_development_dependency 'rubocop-rspec', '~> 1.42.0'
4243
spec.add_development_dependency 'selenium-webdriver', '~> 3.142.7'
4344
spec.add_development_dependency 'simplecov', '~> 0.18.5'
4445
end

spec/factories/active_storage_db_files.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
factory :active_storage_db_file, class: 'ActiveStorageDB::File' do
55
ref { ('a'..'z').to_a.shuffle[0, 32].join } # rubocop:disable Style/Sample
66

7-
data { "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\" viewBox=\"0 0 299.429 299.429\" style=\"enable-background:new 0 0 299.429 299.429;\" xml:space=\"preserve\">\n<g>\n<path style=\"fill:#010002;\" d=\"M245.185,44.209H54.245L0,116.533l149.715,138.688l149.715-138.682L245.185,44.209z\nM206.746,121.778l-57.007,112.1l-56.53-112.1H206.746z M98.483,109.844l51.232-51.232l51.232,51.232H98.483z M164.119,56.142\nh69.323L213.876,105.9L164.119,56.142z M86.311,105.142l-16.331-49h65.331L86.311,105.142z M79.849,121.778l49.632,98.429\nL23.223,121.778H79.849z M220.136,121.778h56.071l-106.013,98.203L220.136,121.778z M225.148,109.844l18.694-47.538l35.652,47.538\nH225.148z M58.266,58.738l17.035,51.112H19.929L58.266,58.738z\"\n/>\n</g>\n</svg>\n" } # rubocop:disable Layout/LineLength
7+
data do
8+
(+"\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
9+
end
810
end
911
end
File renamed without changes.

spec/requests/file_controller_spec.rb

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ def create_blob_before_direct_upload(filename: 'hello.txt', byte_size:, checksum
99
ActiveStorage::Blob.create_before_direct_upload! filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type # rubocop:disable Layout/LineLength
1010
end
1111

12-
let(:blob) { create_blob(filename: 'hello.jpg', content_type: 'image/jpg') }
12+
let(:blob) { create_blob(filename: 'img.jpg', content_type: 'image/jpg') }
1313
let(:host) { 'http://test.example.com:3001' }
14+
let(:engine_url_helpers) { ::ActiveStorageDB::Engine.routes.url_helpers }
1415

1516
before { allow(ActiveStorage::Current).to receive(:host).and_return(host) }
1617

@@ -20,9 +21,8 @@ def create_blob_before_direct_upload(filename: 'hello.txt', byte_size:, checksum
2021

2122
expect(response).to have_http_status(:ok)
2223
expect(response.content_type).to eq('image/jpg')
23-
expect(response.headers['Content-Disposition']).to eq(
24-
"inline; filename=\"hello.jpg\"; filename*=UTF-8''hello.jpg"
25-
)
24+
content_disposition = response.headers['Content-Disposition']
25+
expect(content_disposition).to eq("inline; filename=\"img.jpg\"; filename*=UTF-8''img.jpg")
2626
expect(response.body).to eq 'Hello world!'
2727
end
2828

@@ -31,9 +31,8 @@ def create_blob_before_direct_upload(filename: 'hello.txt', byte_size:, checksum
3131

3232
expect(response).to have_http_status(:ok)
3333
expect(response.content_type).to eq('image/jpg')
34-
expect(response.headers['Content-Disposition']).to eq(
35-
"attachment; filename=\"hello.jpg\"; filename*=UTF-8''hello.jpg"
36-
)
34+
content_disposition = response.headers['Content-Disposition']
35+
expect(content_disposition).to eq("attachment; filename=\"img.jpg\"; filename*=UTF-8''img.jpg")
3736
expect(response.body).to eq 'Hello world!'
3837
end
3938

@@ -50,10 +49,9 @@ def create_blob_before_direct_upload(filename: 'hello.txt', byte_size:, checksum
5049
end
5150

5251
context 'with an invalid key' do
53-
let(:engine_url_helpers) { ::ActiveStorageDB::Engine.routes.url_helpers }
54-
5552
it 'returns not found' do
5653
get engine_url_helpers.service_path(encoded_key: 'Invalid key', filename: 'hello.txt')
54+
5755
expect(response).to have_http_status(:not_found)
5856
end
5957
end
@@ -74,7 +72,7 @@ def create_blob_before_direct_upload(filename: 'hello.txt', byte_size:, checksum
7472
put blob.service_url_for_direct_upload, params: data, headers: { 'Content-Type' => 'application/octet-stream' }
7573

7674
expect(response).to have_http_status(:unprocessable_entity)
77-
expect(blob.service.exist?(blob.key)).to be_falsey
75+
expect(blob.service).not_to exist(blob.key)
7876
end
7977

8078
context 'with an invalid checksum' do
@@ -86,7 +84,7 @@ def create_blob_before_direct_upload(filename: 'hello.txt', byte_size:, checksum
8684
put blob.service_url_for_direct_upload, params: data
8785

8886
expect(response).to have_http_status(:unprocessable_entity)
89-
expect(blob.service.exist?(blob.key)).to be_falsey
87+
expect(blob.service).not_to exist(blob.key)
9088
end
9189
end
9290

@@ -97,15 +95,14 @@ def create_blob_before_direct_upload(filename: 'hello.txt', byte_size:, checksum
9795
put blob.service_url_for_direct_upload, params: data, headers: { 'Content-Type' => 'text/plain' }
9896

9997
expect(response).to have_http_status(:unprocessable_entity)
100-
expect(blob.service.exist?(blob.key)).to be_falsey
98+
expect(blob.service).not_to exist(blob.key)
10199
end
102100
end
103101

104102
context 'with an invalid token' do
105-
let(:engine_url_helpers) { ::ActiveStorageDB::Engine.routes.url_helpers }
106-
107103
it 'returns not found' do
108104
put engine_url_helpers.update_service_path(encoded_token: 'Invalid token')
105+
109106
expect(response).to have_http_status(:not_found)
110107
end
111108
end

spec/service/db_service_spec.rb renamed to spec/service/active_storage/service/db_service_spec.rb

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# frozen_string_literal: true
22

33
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 }
75
let(:content_type) { 'image/png' }
6+
let(:host) { 'http://test.example.com:3001' }
87
let(:checksum) { Digest::MD5.base64digest(fixture_data) }
98
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' }) }
1210
let(:upload_options) { {} }
1311
let(:upload) { service.upload(key, StringIO.new(fixture_data), upload_options) }
1412

@@ -18,20 +16,22 @@
1816
end
1917

2018
describe '.delete' do
19+
subject(:delete) { service.delete(key) }
20+
2121
before { upload }
2222

2323
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)
2625
end
2726
end
2827

2928
describe '.delete_prefixed' do
29+
subject(:delete_prefixed) { service.delete_prefixed(key[0..10]) }
30+
31+
before { upload }
32+
3033
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)
3535
end
3636
end
3737

@@ -48,11 +48,11 @@
4848
after { service.delete(key) }
4949

5050
it 'downloads the data' do
51-
is_expected.to eq fixture_data
51+
expect(download).to eq fixture_data
5252
end
5353

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
5656
result = nil
5757
service.download(key) do |data|
5858
if result
@@ -61,31 +61,34 @@
6161
result = data
6262
end
6363
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
6569
end
6670
end
6771
end
6872
end
6973

7074
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) }
7378

7479
before { upload }
7580

7681
after { service.delete(key) }
7782

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] }
8184
end
8285

8386
describe '.exist?' do
84-
subject(:exist) { service.exist?(key) }
87+
subject { service.exist?(key) }
8588

8689
it { is_expected.to be_falsey }
8790

88-
context 'after an upload' do
91+
context 'when a file is uploaded' do
8992
before { upload }
9093

9194
after { service.delete(key) }
@@ -95,11 +98,9 @@
9598
end
9699

97100
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) }
99102

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) }
103104
end
104105

105106
describe '.upload' do
@@ -129,11 +130,11 @@
129130
end
130131

131132
describe '.url' do
132-
let(:subject) do
133-
filename = ActiveStorage::Filename.new('avatar.png')
133+
subject do
134134
service.url(key, expires_in: 5.minutes, disposition: :inline, filename: filename, content_type: content_type)
135135
end
136-
let(:host) { 'http://test.example.com:3001' }
136+
137+
let(:filename) { ActiveStorage::Filename.new('avatar.png') }
137138

138139
before do
139140
upload
@@ -142,17 +143,15 @@
142143

143144
after { service.delete(key) }
144145

145-
it 'generates the file URL' do
146-
is_expected.to start_with host
147-
end
146+
it { is_expected.to start_with host }
148147
end
149148

150149
describe '.url_for_direct_upload' do
150+
subject { service.url_for_direct_upload(key, url_options) }
151+
151152
let(:url_options) do
152153
{ expires_in: 5.minutes, content_type: content_type, content_length: fixture_data.size, checksum: checksum }
153154
end
154-
let(:subject) { service.url_for_direct_upload(key, url_options) }
155-
let(:host) { 'http://test.example.com:3001' }
156155

157156
before do
158157
upload
@@ -161,8 +160,6 @@
161160

162161
after { service.delete(key) }
163162

164-
it 'generates the file URL' do
165-
is_expected.to start_with host
166-
end
163+
it { is_expected.to start_with host }
167164
end
168165
end

0 commit comments

Comments
 (0)