1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
require File.dirname(__FILE__) + '/../spec_helper'
describe AssetSync::Storage do
include_context "mock Rails without_yml"
let(:file_like_object) do
double("file like object").as_null_object
end
describe '#upload_files' do
before(:each) do
@local_files = ["local_image2.jpg", "local_image1.jpg", "local_stylesheet1.css", "local_stylesheet2.css"]
@remote_files = ["local_image.jpg", "local_image3.svg", "local_image4.svg", "local_stylesheet1.css"]
@config = AssetSync::Config.new
end
it 'should overwrite all remote files if set to ignore' do
@config.existing_remote_files = 'ignore'
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist
@local_files.each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end
it 'should allow force overwriting of specific files' do
@config.always_upload = ['local_image.jpg', /local_image\d\.svg/]
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist
(@local_files - @remote_files + storage.always_upload_files).each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end
it 'should allow to ignore files' do
@config.ignored_files = ['local_image1.jpg', /local_stylesheet\d\.css/]
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist
(@local_files - @remote_files - storage.ignored_files + storage.always_upload_files).each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end
it 'should upload files concurrently if enabled' do
@config.concurrent_uploads = true
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist
expect(Thread).to receive(:new).exactly(3).times.and_call_original
(@local_files - @remote_files + storage.always_upload_files).each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end
it 'should allow custom number of threads' do
@config.concurrent_uploads = true
@config.concurrent_uploads_max_threads = 2
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist
expect(Thread).to receive(:new).exactly(2).times.and_call_original
(@local_files - @remote_files + storage.always_upload_files).each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end
it 'should allow remote_file_list_cache_file_path configuration' do
file_path = './foo.json'
@config.remote_file_list_cache_file_path = file_path
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
File.write(file_path, @remote_files.to_json)
expect(storage).not_to receive(:get_remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist
(@local_files - @remote_files + storage.always_upload_files).each do |file|
expect(storage).to receive(:upload_file).with(file)
end
expect(storage).not_to receive(:warn)
storage.upload_files
# update remote_file_list_cache corretly
updated = JSON.parse(File.read(file_path))
expect(updated.sort.uniq).to eq (@remote_files + @local_files + storage.always_upload_files).sort.uniq
File.delete(file_path)
end
it 'should work with broken cache' do
file_path = './foo.json'
@config.remote_file_list_cache_file_path = file_path
storage = AssetSync::Storage.new(@config)
File.write(file_path, 'some non-json text file content')
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist
(@local_files - @remote_files + storage.always_upload_files).each do |file|
expect(storage).to receive(:upload_file).with(file)
end
# when broken, warning message should be prompted
expect(storage).to receive(:warn)
storage.upload_files
File.delete(file_path)
end
it 'should upload updated non-fingerprinted files' do
@local_files = [
'public/image.png',
'public/image-82389298328.png',
'public/image-a8389f9h324.png',
'public/application.js',
'public/application-b3389d983k1.js',
'public/application-ac387d53f31.js',
'public',
]
@remote_files = [
'public/image.png',
'public/image-a8389f9h324.png',
'public/application.js',
'public/application-b3389d983k1.js',
]
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist
updated_nonfingerprinted_files = [
'public/image.png',
'public/application.js',
]
(@local_files - @remote_files + updated_nonfingerprinted_files).each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end
context "when config #add_local_file_paths is called" do
let(:additional_local_file_paths) do
["webpack/example_asset.jpg"]
end
before(:each) do
@config.add_local_file_paths do
additional_local_file_paths
end
end
let(:storage) do
AssetSync::Storage.new(@config)
end
let(:file_paths_should_be_uploaded) do
@local_files -
@remote_files -
storage.ignored_files +
storage.always_upload_files +
additional_local_file_paths
end
before do
# Stubbing
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
# Pretend the files all exist
allow(File).to receive(:file?).and_return(true)
end
it "uploads additional files in additional to local files" do
file_paths_should_be_uploaded.each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end
end
it 'should upload additonal files' do
@local_files = [
'public/image.png',
'public/image-82389298328.png',
'public/image-a8389f9h324.png',
'public/application.js',
'public/application-b3389d983k1.js',
'public/application-ac387d53f31.js',
'public',
]
@remote_files = [
'public/image.png',
'public/image-a8389f9h324.png',
'public/application.js',
'public/application-b3389d983k1.js',
]
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist
updated_nonfingerprinted_files = [
'public/image.png',
'public/application.js',
]
(@local_files - @remote_files + updated_nonfingerprinted_files).each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end
it 'should correctly set expire date' do
local_files = [
'file1.jpg',
'file1-1234567890abcdef1234567890abcdef.jpg',
'file1-1234567890abcdef1234567890abcdef.jpg.gz',
'file1-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg',
'file1-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz'
]
local_files += [
'dir1/dir2/file2.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg.gz',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz'
]
local_files += [
'file3.png',
'file3.zabcde.png',
'file3.zab1cde2.png',
'file3.abcdef.jpg',
'file3.abc1def2.jpg',
'dir3/file3.abc123.jpg',
'dir3/file3.abcdf123.jpg'
]
remote_files = []
@config.cache_asset_regexps = [/\.[a-f0-9]{6}$/i, /\.[a-f0-9]{8}$/i]
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(local_files)
allow(storage).to receive(:get_remote_files).and_return(remote_files)
allow(File).to receive(:file?).and_return(true)
allow(File).to receive(:open).and_return(file_like_object)
def check_file(file)
case file[:key]
when 'file1.jpg',
'dir1/dir2/file2.jpg',
'file3.png',
'file3.zabcde.png',
'file3.zab1cde2.png'
!expect(file).not_to include(:cache_control, :expires)
when 'file1-1234567890abcdef1234567890abcdef.jpg',
'file1-1234567890abcdef1234567890abcdef.jpg.gz',
'file1-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg',
'file1-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg.gz',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz',
'file3.abcdef.jpg',
'file3.abc1def2.jpg',
'dir3/file3.abc123.jpg',
'dir3/file3.abcdf123.jpg'
expect(file).to include(:cache_control, :expires)
else
fail
end
end
files = double()
local_files.count.times do
expect(files).to receive(:create) { |file| check_file(file) }
end
allow(storage).to receive_message_chain(:bucket, :files).and_return(files)
storage.upload_files
end
it "should invalidate files" do
@config.cdn_distribution_id = "1234"
@config.invalidate = ['local_image1.jpg']
@config.fog_provider = 'AWS'
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(storage).to receive(:upload_file).and_return(true)
mock_cdn = double
expect(Fog::CDN).to receive(:new).and_return(mock_cdn)
expect(mock_cdn).to receive(:post_invalidation).with("1234", ["/assets/local_image1.jpg"]).and_return(double({:body => {:id => '1234'}}))
storage.upload_files
end
end
describe '#upload_file' do
before(:each) do
# Object#remove_const does not remove the loaded
# file from the $" variable
#
# So we need do both
#
# 1. Remove constant(s) to avoid warning messages
# 2. Remove loaded file(s)
Object.send(:remove_const, :MIME) if defined?(MIME)
$".grep(/mime\//).each do |file_path|
$".delete(file_path)
end
require 'mime/types'
@config = AssetSync::Config.new
end
it 'accepts custom headers per file' do
@config.custom_headers = {
"local_image2.jpg" => {
:cache_control => 'max-age=0'
}
}
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
# Pretend they all exist
allow(File).to receive(:open).and_return(file_like_object)
bucket = double
files = double
allow(storage).to receive(:bucket).and_return(bucket)
allow(bucket).to receive(:files).and_return(files)
expect(files).to receive(:create) do |argument|
expect(argument[:cache_control]).to eq('max-age=0')
end
storage.upload_file('assets/local_image2.jpg')
end
it 'accepts custom headers with a regular expression' do
@config.custom_headers = {
".*\.jpg" => {
:cache_control => 'max-age=0'
}
}
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
# Pretend they all exist
allow(File).to receive(:open).and_return(file_like_object)
bucket = double
files = double
allow(storage).to receive(:bucket).and_return(bucket)
allow(bucket).to receive(:files).and_return(files)
expect(files).to receive(:create) do |argument|
expect(argument[:cache_control]).to eq('max-age=0')
end
storage.upload_file('assets/some_longer_path/local_image2.jpg')
end
end
describe '#delete_extra_remote_files' do
it 'should delete the files in bulk' do
remote_files = ['public/image.png']
connection = double
config = double
storage = AssetSync::Storage.new(@config)
[:local_files, :ignored_files, :always_upload_files].each do |method|
expect(storage).to receive(method).and_return([])
end
allow(storage).to receive(:get_remote_files).and_return(remote_files)
allow(storage).to receive(:connection).and_return(connection).twice
allow(storage).to receive(:config).and_return(config).twice
allow(config).to receive(:aws?).and_return(true)
allow(config).to receive(:fog_directory).and_return('foo')
expect(connection).to receive(:delete_multiple_objects).with('foo', remote_files)
storage.delete_extra_remote_files
end
context 'when not aws' do
it 'deletes files sequentially' do
remote_files = ['public/image.png']
connection = double
config = double
directories = double
directory = double
file = double
storage = AssetSync::Storage.new(@config)
[:local_files, :ignored_files, :always_upload_files].each do |method|
expect(storage).to receive(method).and_return([])
end
allow(storage).to receive(:get_remote_files).and_return(remote_files)
allow(storage).to receive(:connection).and_return(connection).twice
allow(storage).to receive(:config).and_return(config)
allow(config).to receive(:aws?).and_return(false)
allow(config).to receive(:fog_directory).and_return('foo')
allow(config).to receive(:assets_prefix).and_return('foo')
allow(directories).to receive(:get).and_return(directory)
allow(directory).to receive(:files).and_return([file])
allow(file).to receive(:key).and_return('public/image.png')
allow(connection).to receive(:directories).and_return(directories)
expect(connection).not_to receive(:delete_multiple_objects)
expect(file).to receive(:destroy)
storage.delete_extra_remote_files
end
end
end
end
|