|
| 1 | +require_relative './spec_helper' |
| 2 | + |
| 3 | +describe Fastlane::Actions::UploadToS3Action do |
| 4 | + let(:client) { instance_double(Aws::S3::Client) } |
| 5 | + let(:test_bucket) { 'a8c-wpmrt-unit-tests-bucket' } |
| 6 | + |
| 7 | + before do |
| 8 | + allow(Aws::S3::Client).to receive(:new).and_return(client) |
| 9 | + end |
| 10 | + |
| 11 | + # Stub head_object to return a specific content_length |
| 12 | + def stub_s3_response_for_file(key, exists: true) |
| 13 | + content_length = exists == true ? 1 : 0 |
| 14 | + allow(client).to(receive(:head_object)) |
| 15 | + .with(bucket: test_bucket, key: key) |
| 16 | + .and_return(Aws::S3::Types::HeadObjectOutput.new(content_length: content_length)) |
| 17 | + end |
| 18 | + |
| 19 | + describe 'uploading a file with valid parameters' do |
| 20 | + it 'generates a prefix for the key by default' do |
| 21 | + expected_key = '939c39398db2405e791e205778ff70f85dff620e/a8c-key1' |
| 22 | + stub_s3_response_for_file(expected_key, exists: false) |
| 23 | + |
| 24 | + with_tmp_file(named: 'input_file_1') do |file_path| |
| 25 | + expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key) |
| 26 | + |
| 27 | + return_value = run_described_fastlane_action( |
| 28 | + bucket: test_bucket, |
| 29 | + key: 'a8c-key1', |
| 30 | + file: file_path |
| 31 | + ) |
| 32 | + |
| 33 | + expect(return_value).to eq(expected_key) |
| 34 | + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::S3_UPLOADED_FILE_PATH]).to eq(expected_key) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + it 'generates a prefix for the key when using auto_prefix:true' do |
| 39 | + expected_key = '8bde1a7a04300df27b52f4383dc997e5fbbff180/a8c-key2' |
| 40 | + stub_s3_response_for_file(expected_key, exists: false) |
| 41 | + |
| 42 | + with_tmp_file(named: 'input_file_2') do |file_path| |
| 43 | + expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key) |
| 44 | + |
| 45 | + return_value = run_described_fastlane_action( |
| 46 | + bucket: test_bucket, |
| 47 | + key: 'a8c-key2', |
| 48 | + file: file_path, |
| 49 | + auto_prefix: true |
| 50 | + ) |
| 51 | + |
| 52 | + expect(return_value).to eq(expected_key) |
| 53 | + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::S3_UPLOADED_FILE_PATH]).to eq(expected_key) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + it 'uses the provided key verbatim when using auto_prefix:false' do |
| 58 | + expected_key = 'a8c-key1' |
| 59 | + stub_s3_response_for_file(expected_key, exists: false) |
| 60 | + |
| 61 | + with_tmp_file do |file_path| |
| 62 | + expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key) |
| 63 | + |
| 64 | + return_value = run_described_fastlane_action( |
| 65 | + bucket: test_bucket, |
| 66 | + key: 'a8c-key1', |
| 67 | + file: file_path, |
| 68 | + auto_prefix: false |
| 69 | + ) |
| 70 | + |
| 71 | + expect(return_value).to eq(expected_key) |
| 72 | + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::S3_UPLOADED_FILE_PATH]).to eq(expected_key) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + it 'correctly appends the key if it contains subdirectories' do |
| 77 | + expected_key = '939c39398db2405e791e205778ff70f85dff620e/subdir/a8c-key1' |
| 78 | + stub_s3_response_for_file(expected_key, exists: false) |
| 79 | + |
| 80 | + with_tmp_file(named: 'input_file_1') do |file_path| |
| 81 | + expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key) |
| 82 | + |
| 83 | + return_value = run_described_fastlane_action( |
| 84 | + bucket: test_bucket, |
| 85 | + key: 'subdir/a8c-key1', |
| 86 | + file: file_path |
| 87 | + ) |
| 88 | + |
| 89 | + expect(return_value).to eq(expected_key) |
| 90 | + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::S3_UPLOADED_FILE_PATH]).to eq(expected_key) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + it 'uses the filename as the key if one is not provided' do |
| 95 | + expected_key = 'c125bd799c6aad31092b02e440a8fae25b45a2ad/test_file_1' |
| 96 | + stub_s3_response_for_file(expected_key, exists: false) |
| 97 | + |
| 98 | + with_tmp_file(named: 'test_file_1') do |file_path| |
| 99 | + expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key) |
| 100 | + |
| 101 | + return_value = run_described_fastlane_action( |
| 102 | + bucket: test_bucket, |
| 103 | + file: file_path |
| 104 | + ) |
| 105 | + |
| 106 | + expect(return_value).to eq(expected_key) |
| 107 | + expect(Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::S3_UPLOADED_FILE_PATH]).to eq(expected_key) |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + describe 'uploading a file with invalid parameters' do |
| 113 | + it 'fails if bucket is empty or nil' do |
| 114 | + expect do |
| 115 | + with_tmp_file do |file_path| |
| 116 | + run_described_fastlane_action( |
| 117 | + bucket: '', |
| 118 | + key: 'key', |
| 119 | + file: file_path |
| 120 | + ) |
| 121 | + end |
| 122 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, 'You must provide a valid bucket name') |
| 123 | + end |
| 124 | + |
| 125 | + it 'fails if an empty key is provided' do |
| 126 | + expect do |
| 127 | + with_tmp_file do |file_path| |
| 128 | + run_described_fastlane_action( |
| 129 | + bucket: test_bucket, |
| 130 | + key: '', |
| 131 | + file: file_path |
| 132 | + ) |
| 133 | + end |
| 134 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, 'The provided key must not be empty. Use nil instead if you want to default to the file basename') |
| 135 | + end |
| 136 | + |
| 137 | + it 'fails if local file does not exist' do |
| 138 | + expect do |
| 139 | + run_described_fastlane_action( |
| 140 | + bucket: test_bucket, |
| 141 | + key: 'key', |
| 142 | + file: 'this-file-does-not-exist.txt' |
| 143 | + ) |
| 144 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, 'Path `this-file-does-not-exist.txt` does not exist.') |
| 145 | + end |
| 146 | + |
| 147 | + it 'fails if the file already exists on S3' do |
| 148 | + expected_key = 'a62f2225bf70bfaccbc7f1ef2a397836717377de/key' |
| 149 | + stub_s3_response_for_file(expected_key) |
| 150 | + |
| 151 | + with_tmp_file(named: 'key') do |file_path| |
| 152 | + expect do |
| 153 | + run_described_fastlane_action( |
| 154 | + bucket: test_bucket, |
| 155 | + key: 'key', |
| 156 | + file: file_path |
| 157 | + ) |
| 158 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, "File already exists in S3 bucket #{test_bucket} at #{expected_key}") |
| 159 | + end |
| 160 | + end |
| 161 | + end |
| 162 | +end |
0 commit comments