diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2bbb2fe..cf7afe0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,8 +54,6 @@ jobs: - name: Validate Base Ref id: validate_base_ref - env: - JSON_ENV_VARS: ${{ inputs.credentials_json }} shell: bash run: | if [ "${{ github.event.base_ref }}" != "refs/heads/main" ]; then diff --git a/.rubocop.yml b/.rubocop.yml index b75ffb3..3e9e13f 100755 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,7 @@ AllCops: NewCops: enable SuggestExtensions: false - TargetRubyVersion: 3.2 + TargetRubyVersion: 3.3 Exclude: - vendor/**/* - spec/**/* diff --git a/fat_zebra.gemspec b/fat_zebra.gemspec index 492e70e..80b24d4 100644 --- a/fat_zebra.gemspec +++ b/fat_zebra.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |s| s.files = Dir.glob('lib/**/*') + %w[README.md fat_zebra.gemspec] s.require_paths = ['lib'] - s.required_ruby_version = '>= 3.2' + s.required_ruby_version = '>= 3.3' s.add_development_dependency 'byebug' s.add_development_dependency 'rake' diff --git a/lib/fat_zebra/api_operation/save.rb b/lib/fat_zebra/api_operation/save.rb index cd3dc5f..c072510 100644 --- a/lib/fat_zebra/api_operation/save.rb +++ b/lib/fat_zebra/api_operation/save.rb @@ -50,10 +50,12 @@ def save(params = {}, options = {}) path = singleton_methods.include?(:id) ? "#{resource_path}/#{id}" : resource_path method = singleton_methods.include?(:id) ? :put : :post - params_for_save = to_hash.merge(params) - - # Remove the id from the params to save, it should not be updated. - params_for_save.delete('id') + # Send each attribute once, whether it's keyed by a string or a symbol: the caller's + # value over the stored one, and the last of any the caller repeats, under that key. + # The id is never sent: it can't be updated. + params_for_save = [*to_hash, *params].reject { |key, _| key.to_s == 'id' } + .group_by { |key, _| key.to_s } + .to_h { |_, pairs| pairs.last } response = request(method, path, params_for_save, options) update_from(response) diff --git a/lib/fat_zebra/api_resource.rb b/lib/fat_zebra/api_resource.rb index e50b7bf..1bf7539 100644 --- a/lib/fat_zebra/api_resource.rb +++ b/lib/fat_zebra/api_resource.rb @@ -26,7 +26,10 @@ def base_path # @raise [FatZebra::RequestError] if the request is invalid # rubocop:disable-next Metrics/AbcSize def request(method, path, payload = {}, options = {}) - payload[:test] = true if configurations.test_mode + if configurations.test_mode + payload.delete('test') + payload[:test] = true + end payload = Util.format_dates_in_hash(payload) url_params = Util.encode_parameters(payload) if method == :get uri = build_endpoint_url(configurations.gateway, path, url_params, http_secure: configurations.http_secure) diff --git a/lib/fat_zebra/validation.rb b/lib/fat_zebra/validation.rb index 2dc9853..21fb9f9 100644 --- a/lib/fat_zebra/validation.rb +++ b/lib/fat_zebra/validation.rb @@ -56,7 +56,7 @@ def validate_type(field, options, params) when :file_type /\A\# true, 'response' => { 'id' => '071-PP-1', 'status' => 'Active' }) + end + + before do + allow(FatZebra::PaymentPlan).to receive(:request).and_return('successful' => true, 'response' => {}) + payment_plan.suspend!('new_status' => 'Cancelled') + end + + it "sends the caller's new_status in place of the default" do + expect(FatZebra::PaymentPlan).to have_received(:request) + .with(:put, anything, { 'status' => 'Active', 'new_status' => 'Cancelled' }, {}) + end + end + end diff --git a/spec/lib/fat_zebra/validation_spec.rb b/spec/lib/fat_zebra/validation_spec.rb index 4a436c1..5cda6b7 100644 --- a/spec/lib/fat_zebra/validation_spec.rb +++ b/spec/lib/fat_zebra/validation_spec.rb @@ -19,4 +19,13 @@ it { expect { klass.valid!({}, :create) }.to raise_error(FatZebra::RequestValidationError) } end + describe 'type: :boolean' do + before { klass.validates(:flag, type: :boolean, on: :create) } + + it { expect(klass.valid?({ flag: true }, :create)).to be_truthy } + it { expect(klass.valid?({ flag: false }, :create)).to be_truthy } + it { expect(klass.valid?({ flag: 'trueish' }, :create)).to be_falsey } + it { expect(klass.valid?({ flag: 'notfalse' }, :create)).to be_falsey } + end + end diff --git a/spec/lib/fat_zebra/web_hook_spec.rb b/spec/lib/fat_zebra/web_hook_spec.rb index 7560262..40e0903 100644 --- a/spec/lib/fat_zebra/web_hook_spec.rb +++ b/spec/lib/fat_zebra/web_hook_spec.rb @@ -47,4 +47,60 @@ it { is_expected.to be_accepted } end + describe '#update payload' do + subject(:web_hook) { FatZebra::WebHook.initialize_from(api_response('test hook')) } + + def api_response(name) + { 'successful' => true, 'response' => { 'id' => '071-WH-1', 'name' => name, 'mode' => 'Test' } } + end + + context 'with a new value for an existing attribute and an id' do + before do + allow(FatZebra::WebHook).to receive(:request).and_return(api_response('New Test hook')) + web_hook.update(name: 'New Test hook', id: '071-WH-2') + end + + it 'sends each attribute once, with the new value and no id' do + expect(FatZebra::WebHook).to have_received(:request) + .with(:put, anything, { name: 'New Test hook', 'mode' => 'Test' }, {}) + end + end + + context 'with test passed in test mode' do + before do + allow(FatZebra::Request).to receive(:execute).and_return(double(body: api_response('New Test hook'))) + web_hook.update(name: 'New Test hook', test: true) + end + + it 'sends test once' do + expect(FatZebra::Request).to have_received(:execute) + .with(hash_including(payload: { name: 'New Test hook', 'mode' => 'Test', test: true })) + end + end + + context 'with test passed as a string in test mode' do + before do + allow(FatZebra::Request).to receive(:execute).and_return(double(body: api_response('New Test hook'))) + web_hook.update(name: 'New Test hook', 'test' => true) + end + + it 'sends test once' do + expect(FatZebra::Request).to have_received(:execute) + .with(hash_including(payload: { name: 'New Test hook', 'mode' => 'Test', test: true })) + end + end + + context 'with one attribute passed as both a symbol and a string' do + before do + allow(FatZebra::WebHook).to receive(:request).and_return(api_response('Second name')) + web_hook.update(:name => 'First name', 'name' => 'Second name') + end + + it 'sends it once, with the last value' do + expect(FatZebra::WebHook).to have_received(:request) + .with(:put, anything, { 'name' => 'Second name', 'mode' => 'Test' }, {}) + end + end + end + end