From e3d7697aa080a06fb30915920ddfc258ed3c1159 Mon Sep 17 00:00:00 2001 From: Lukasz Mlodzik Date: Fri, 4 Jan 2019 15:32:06 +0100 Subject: [PATCH 1/3] ignore rubymine files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2c5ef592f..ffd824377 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ test/tmp test/version_tmp tmp vendor/ +/.idea From 3065b4847389c74cb98e9ef18c5a7b1a87caa75f Mon Sep 17 00:00:00 2001 From: Lukasz Mlodzik Date: Fri, 4 Jan 2019 16:00:26 +0100 Subject: [PATCH 2/3] Apply errors to class after `get_all` fail --- lib/netsuite/actions/get_all.rb | 18 +++- spec/netsuite/actions/get_all_spec.rb | 59 +++++++++++++ .../fixtures/get_all/get_all_currencies.xml | 83 +++++++++++++++++++ .../get_all_insufficient_permissions.xml | 21 +++++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 spec/netsuite/actions/get_all_spec.rb create mode 100644 spec/support/fixtures/get_all/get_all_currencies.xml create mode 100644 spec/support/fixtures/get_all/get_all_insufficient_permissions.xml diff --git a/lib/netsuite/actions/get_all.rb b/lib/netsuite/actions/get_all.rb index dddf32dbd..6aad74c1c 100644 --- a/lib/netsuite/actions/get_all.rb +++ b/lib/netsuite/actions/get_all.rb @@ -49,6 +49,20 @@ def response_hash @response_hash ||= @response.body[:get_all_response][:get_all_result] end + def response_errors + if response_hash.dig(:status, :status_detail) + @response_errors ||= errors + end + end + + def errors + error_obj = response_hash[:status][:status_detail] + error_obj = [error_obj] if error_obj.class == Hash + error_obj.map do |error| + NetSuite::Error.new(error) + end + end + module Support def self.included(base) @@ -56,10 +70,12 @@ def self.included(base) end module ClassMethods + attr_reader :errors + def get_all(credentials = {}) response = NetSuite::Actions::GetAll.call([self], credentials) - # TODO expose errors to the user + @errors = response.errors if response.success? response.body.map { |attr| new(attr) } diff --git a/spec/netsuite/actions/get_all_spec.rb b/spec/netsuite/actions/get_all_spec.rb new file mode 100644 index 000000000..8cb3e2536 --- /dev/null +++ b/spec/netsuite/actions/get_all_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe NetSuite::Actions::GetAll do + before(:all) { savon.mock! } + after(:all) { savon.unmock! } + + describe 'Currency' do + context 'retrieving all' do + let(:message) { { record: [ { record_type: "currency" } ] } } + + context 'when successful' do + before do + savon.expects(:get_all).with(message: message).returns( + File.read('spec/support/fixtures/get_all/get_all_currencies.xml') + ) + end + + it 'makes a valid request to the NetSuite API' do + NetSuite::Actions::GetAll.call([NetSuite::Records::Currency]) + end + + it 'returns a valid Response object' do + response = NetSuite::Actions::GetAll.call([NetSuite::Records::Currency]) + expect(response).to be_kind_of(NetSuite::Response) + end + end + + context 'when insufficient permissions' do + let(:currency) { NetSuite::Records::Currency } + let(:response) { currency.get_all({}) } + + before do + savon.expects(:get_all).with(message: message).returns( + File.read('spec/support/fixtures/get_all/get_all_insufficient_permissions.xml') + ) + end + + it 'provides an error method on the object with details about the error' do + response + error = currency.errors.first + + expect(error).to be_kind_of(NetSuite::Error) + expect(error.type).to eq('ERROR') + expect(error.code).to eq('INSUFFICIENT_PERMISSION') + expect(error.message).to eq( + "Permission Violation: You need the 'Lists -> Currency' permission to access this page. Please contact your account administrator." + ) + end + + it 'provides an error method on the response' do + response + + expect(currency.errors.first).to be_kind_of(NetSuite::Error) + expect(response).to eq(false) + end + end + end + end +end diff --git a/spec/support/fixtures/get_all/get_all_currencies.xml b/spec/support/fixtures/get_all/get_all_currencies.xml new file mode 100644 index 000000000..e013ff53f --- /dev/null +++ b/spec/support/fixtures/get_all/get_all_currencies.xml @@ -0,0 +1,83 @@ + + + + + WEBSERVICES_TSTDRV1115270_010420194222336661788757021_365d42 + + + + + + + 5 + + + US Dollar + USD + true + false + false + $ + _beforeNumber + _unitedStatesEnglish + $1,234.56 + 1.0 + _two + + + British pound + GBP + false + false + false + £ + _beforeNumber + _unitedKingdomEnglish + £1,234.56 + 2.365 + _two + + + Canadian Dollar + CAD + false + false + false + $ + _beforeNumber + _canadaEnglish + $1,234.56 + 1.559 + _two + + + Euro + EUR + false + false + false + + _beforeNumber + _franceFrenchEuro + €1 234,56 + 1.509 + _two + + + Polish Zloty + PLN + true + false + false + + _beforeNumber + _polandPolish + zł1 234,56 + 0.27 + _two + + + + + + diff --git a/spec/support/fixtures/get_all/get_all_insufficient_permissions.xml b/spec/support/fixtures/get_all/get_all_insufficient_permissions.xml new file mode 100644 index 000000000..efb3c7456 --- /dev/null +++ b/spec/support/fixtures/get_all/get_all_insufficient_permissions.xml @@ -0,0 +1,21 @@ + + + + + WEBSERVICES_TSTDRV1115270_01042019421775958535724851_3779906 + + + + + + + + INSUFFICIENT_PERMISSION + Permission Violation: You need the 'Lists -> Currency' permission to access this page. Please contact your account administrator. + + + 5 + + + + From db3da3de700e2c82f8d5dfb0301ea2702d4d678b Mon Sep 17 00:00:00 2001 From: Lukasz Mlodzik Date: Fri, 4 Jan 2019 16:10:05 +0100 Subject: [PATCH 3/3] Add more specs --- spec/netsuite/actions/get_all_spec.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/spec/netsuite/actions/get_all_spec.rb b/spec/netsuite/actions/get_all_spec.rb index 8cb3e2536..efcf6e422 100644 --- a/spec/netsuite/actions/get_all_spec.rb +++ b/spec/netsuite/actions/get_all_spec.rb @@ -7,6 +7,8 @@ describe 'Currency' do context 'retrieving all' do let(:message) { { record: [ { record_type: "currency" } ] } } + let(:currency) { NetSuite::Records::Currency } + let(:response) { currency.get_all({}) } context 'when successful' do before do @@ -23,12 +25,18 @@ response = NetSuite::Actions::GetAll.call([NetSuite::Records::Currency]) expect(response).to be_kind_of(NetSuite::Response) end + + it 'returns valid currency list' do + expect(response).to be_kind_of(Array) + expect(response.count).to eq(5) + + usd = response.first + expect(usd).to be_kind_of(NetSuite::Records::Currency) + expect(usd.attributes[:name]).to eq('US Dollar') + end end context 'when insufficient permissions' do - let(:currency) { NetSuite::Records::Currency } - let(:response) { currency.get_all({}) } - before do savon.expects(:get_all).with(message: message).returns( File.read('spec/support/fixtures/get_all/get_all_insufficient_permissions.xml')