|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe CamaleonCmsAwsUploader do |
| 6 | + init_site |
| 7 | + |
| 8 | + let(:current_site) { Cama::Site.first.decorate } |
| 9 | + let(:hook_instance) { instance_double('UploaderHookInstance', hooks_run: nil) } |
| 10 | + let(:uploader) { described_class.new({ current_site: current_site, aws_settings: {} }, hook_instance) } |
| 11 | + let(:bucket) { instance_double('Aws::S3::Bucket') } |
| 12 | + |
| 13 | + before { allow(uploader).to receive(:bucket).and_return(bucket) } |
| 14 | + |
| 15 | + context 'with an invalid path containing path traversal characters' do |
| 16 | + describe '#add_file' do |
| 17 | + it 'returns an error' do |
| 18 | + expect(bucket).not_to receive(:object) |
| 19 | + |
| 20 | + expect(uploader.add_file('/tmp/test.png', '../tmp/test.png')).to eql(error: 'Invalid file path') |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + describe '#delete_folder' do |
| 25 | + it 'returns an error' do |
| 26 | + expect(bucket).not_to receive(:objects) |
| 27 | + |
| 28 | + expect(uploader.delete_folder('../tmp')).to eql(error: 'Invalid folder path') |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + describe '#delete_file' do |
| 33 | + it 'returns an error' do |
| 34 | + expect(bucket).not_to receive(:object) |
| 35 | + expect(hook_instance).not_to receive(:hooks_run) |
| 36 | + |
| 37 | + expect(uploader.delete_file('../tmp/test.png')).to eql(error: 'Invalid file path') |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context 'with an invalid URI-like path' do |
| 43 | + describe '#add_file' do |
| 44 | + it 'returns an error' do |
| 45 | + expect(bucket).not_to receive(:object) |
| 46 | + |
| 47 | + expect(uploader.add_file('/tmp/test.png', 'file:///tmp/test.png')).to eql(error: 'Invalid file path') |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + describe '#delete_folder' do |
| 52 | + it 'returns an error' do |
| 53 | + expect(bucket).not_to receive(:objects) |
| 54 | + |
| 55 | + expect(uploader.delete_folder('s3://bucket/folder')).to eql(error: 'Invalid folder path') |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe '#delete_file' do |
| 60 | + it 'returns an error' do |
| 61 | + expect(bucket).not_to receive(:object) |
| 62 | + expect(hook_instance).not_to receive(:hooks_run) |
| 63 | + |
| 64 | + expect(uploader.delete_file('https://example.com/file.txt')).to eql(error: 'Invalid file path') |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context 'with a valid file path' do |
| 70 | + describe '#add_file' do |
| 71 | + let(:s3_file) { instance_double('Aws::S3::Object') } |
| 72 | + let(:parsed_file) do |
| 73 | + { |
| 74 | + 'name' => 'test.png', |
| 75 | + 'folder_path' => '/safe', |
| 76 | + 'url' => 'https://cdn.example.com/safe/test.png', |
| 77 | + 'is_folder' => false, |
| 78 | + 'file_size' => 123.45, |
| 79 | + 'thumb' => '/safe/thumb/test-png.png', |
| 80 | + 'file_type' => 'image', |
| 81 | + 'created_at' => '2026-03-09T00:00:00Z', |
| 82 | + 'dimension' => '100x100', |
| 83 | + 'key' => '/safe/test.png' |
| 84 | + } |
| 85 | + end |
| 86 | + |
| 87 | + before do |
| 88 | + allow(bucket).to receive(:object).and_return(s3_file) |
| 89 | + allow(s3_file).to receive(:upload_file).and_return(true) |
| 90 | + allow(uploader).to receive(:search_new_key).and_return('/safe/test.png') |
| 91 | + allow(uploader).to receive(:file_parse).with(s3_file).and_return(parsed_file) |
| 92 | + allow(uploader).to receive(:cache_item).with(parsed_file).and_return(parsed_file) |
| 93 | + end |
| 94 | + |
| 95 | + it 'uploads the file and returns cached metadata' do |
| 96 | + file_path = "#{CAMALEON_CMS_ROOT}/spec/support/fixtures/rails.png" |
| 97 | + expect(hook_instance).to receive(:hooks_run).with( |
| 98 | + 'uploader_aws_before_upload', |
| 99 | + hash_including( |
| 100 | + file: file_path, key: '/safe/test.png', args: hash_including(same_name: false, is_thumb: false) |
| 101 | + ) |
| 102 | + ) |
| 103 | + |
| 104 | + result = uploader.add_file(file_path, 'safe/test.png') |
| 105 | + |
| 106 | + expect(bucket).to have_received(:object).with('safe/test.png') |
| 107 | + expect(s3_file).to have_received(:upload_file).with( |
| 108 | + file_path, { acl: 'public-read' } |
| 109 | + ) |
| 110 | + expect(result).to eql(parsed_file) |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments