diff --git a/.rubocop.yml b/.rubocop.yml index 8df0472..99a8963 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,6 @@ -require: rubocop-rake +require: + - rubocop-rake + - rubocop-rspec AllCops: TargetRubyVersion: 2.5 @@ -24,6 +26,7 @@ Metrics/AbcSize: Metrics/BlockLength: Exclude: - 'spec/**/*' + - '**/*.gemspec' Metrics/MethodLength: Max: 20 Metrics/ParameterLists: @@ -32,6 +35,16 @@ Metrics/ParameterLists: Naming/AccessorMethodName: Enabled: false +RSpec/FilePath: + Enabled: false +RSpec/NestedGroups: + Enabled: false +RSpec/MultipleMemoizedHelpers: + Enabled: false +RSpec/MultipleExpectations: + Exclude: + - 'spec/concerns/authentication_spec.rb' + Style/ClassAndModuleChildren: Enabled: false Style/Documentation: diff --git a/Gemfile.lock b/Gemfile.lock index 2b9e192..37e4d28 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,7 +16,7 @@ GEM crack (0.4.5) rexml diff-lcs (1.4.4) - docile (1.3.5) + docile (1.4.0) faraday (1.8.0) faraday-em_http (~> 1.0) faraday-em_synchrony (~> 1.0) @@ -43,7 +43,7 @@ GEM method_source (1.0.0) multipart-post (2.1.1) parallel (1.21.0) - parser (3.0.2.0) + parser (3.0.3.2) ast (~> 2.4.1) pry (0.13.1) coderay (~> 1.1) @@ -54,7 +54,7 @@ GEM public_suffix (4.0.6) rainbow (3.0.0) rake (13.0.6) - regexp_parser (2.1.1) + regexp_parser (2.2.0) rexml (3.2.5) rspec (3.10.0) rspec-core (~> 3.10.0) @@ -68,7 +68,7 @@ GEM rspec-mocks (3.10.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.10.0) - rspec-support (3.10.2) + rspec-support (3.10.3) rubocop (1.23.0) parallel (~> 1.10) parser (>= 3.0.0.0) @@ -78,10 +78,12 @@ GEM rubocop-ast (>= 1.12.0, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.13.0) + rubocop-ast (1.15.0) parser (>= 3.0.1.1) rubocop-rake (0.6.0) rubocop (~> 1.0) + rubocop-rspec (2.6.0) + rubocop (~> 1.19) ruby-progressbar (1.11.0) ruby2_keywords (0.0.5) simplecov (0.21.2) @@ -108,6 +110,7 @@ DEPENDENCIES rspec (~> 3.2) rubocop (~> 1.23.0) rubocop-rake (~> 0.6.0) + rubocop-rspec (~> 2.6) simplecov (~> 0.21.2) webmock (~> 3.1) diff --git a/lib/mrkt/concerns/crud_program_members.rb b/lib/mrkt/concerns/crud_program_members.rb index 982daaa..7b73cec 100644 --- a/lib/mrkt/concerns/crud_program_members.rb +++ b/lib/mrkt/concerns/crud_program_members.rb @@ -6,7 +6,7 @@ def describe_program_members def createupdate_program_members(program_id, lead_ids, status) post_json("/rest/v1/programs/#{program_id}/members/status.json") do - params = { + { statusName: status, input: lead_ids.map { |lead_id| { leadId: lead_id } } } diff --git a/mrkt.gemspec b/mrkt.gemspec index 6812910..9d85e6f 100644 --- a/mrkt.gemspec +++ b/mrkt.gemspec @@ -17,6 +17,8 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = %w[lib] + spec.metadata['rubygems_mfa_required'] = 'true' + spec.required_ruby_version = '>= 2.5' spec.add_dependency 'faraday', '~> 1.0' @@ -29,6 +31,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rspec', '~> 3.2' spec.add_development_dependency 'rubocop', '~> 1.23.0' spec.add_development_dependency 'rubocop-rake', '~> 0.6.0' + spec.add_development_dependency 'rubocop-rspec', '~> 2.6' spec.add_development_dependency 'simplecov', '~> 0.21.2' spec.add_development_dependency 'webmock', '~> 3.1' end diff --git a/spec/concerns/authentication_spec.rb b/spec/concerns/authentication_spec.rb index 5608fab..93cce07 100644 --- a/spec/concerns/authentication_spec.rb +++ b/spec/concerns/authentication_spec.rb @@ -1,10 +1,10 @@ describe Mrkt::Authentication do - include_context 'initialized client' + include_context 'with an initialized client' describe '#authenticate' do - subject { client.authenticate } + subject(:action) { client.authenticate } - context 'on a successful response' do + context 'with on a successful response' do it { is_expected.to be_success } end @@ -16,21 +16,28 @@ } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::Error, 'Bad client credentials') + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::Error, 'Bad client credentials') end end end describe '#authenticate!' do - it 'should authenticate and then be authenticated?' do - expect(client.authenticated?).to_not be true + it 'authenticates the client' do + expect(client.authenticated?).not_to be true + client.authenticate! expect(client.authenticated?).to be true end context 'with optional partner_id client option' do - before { remove_request_stub(@authentication_request_stub) } + subject(:client) { Mrkt::Client.new(client_options) } + + before do + stub_request(:get, "https://#{host}/identity/oauth/token") + .with(query: query) + .to_return(json_stub(authentication_stub)) + end let(:partner_id) { SecureRandom.uuid } @@ -52,23 +59,22 @@ } end - subject(:client) { Mrkt::Client.new(client_options) } - - before do - stub_request(:get, "https://#{host}/identity/oauth/token") - .with(query: query) - .to_return(json_stub(authentication_stub)) - end - - it 'should authenticate and then be authenticated?' do - expect(client.authenticated?).to_not be true + it 'authenticates the client' do + expect(client.authenticated?).not_to be true client.authenticate! expect(client.authenticated?).to be true end end context 'when the token has expired and @retry_authentication = true' do - before { remove_request_stub(@authentication_request_stub) } + subject(:client) { Mrkt::Client.new(client_options) } + + before do + stub_request(:get, "https://#{host}/identity/oauth/token") + .with(query: { client_id: client_id, client_secret: client_secret, grant_type: 'client_credentials' }) + .to_return(json_stub(expired_authentication_stub)).times(3).then + .to_return(json_stub(valid_authentication_stub)) + end let(:retry_count) { 3 } @@ -90,17 +96,8 @@ } end - subject(:client) { Mrkt::Client.new(client_options) } - - before do - stub_request(:get, "https://#{host}/identity/oauth/token") - .with(query: { client_id: client_id, client_secret: client_secret, grant_type: 'client_credentials' }) - .to_return(json_stub(expired_authentication_stub)).times(3).then - .to_return(json_stub(valid_authentication_stub)) - end - - it 'should retry until getting valid token and then be authenticated?' do - expect(client.authenticated?).to_not be true + it 'retries until it gets a valid token' do + expect(client.authenticated?).not_to be true client.authenticate! expect(client.authenticated?).to be true end @@ -108,8 +105,8 @@ context 'when retry_authentication_count is low' do let(:retry_count) { 2 } - it 'should stop retrying after @retry_authentication_count tries and then raise an error' do - expect(client.authenticated?).to_not be true + it 'stops retrying after a while' do + expect(client.authenticated?).not_to be true expect { client.authenticate! }.to raise_error(Mrkt::Errors::Error, 'Client not authenticated') end @@ -120,11 +117,11 @@ describe '#authenticated?' do subject { client.authenticated? } - context 'before authentication' do + context 'when authentication has not been done' do it { is_expected.to be_falsey } end - context 'after authentication' do + context 'when authentication has been done' do before { client.authenticate } it { is_expected.to be_truthy } diff --git a/spec/concerns/crud_activities_spec.rb b/spec/concerns/crud_activities_spec.rb index 6cb0fcb..79897cb 100644 --- a/spec/concerns/crud_activities_spec.rb +++ b/spec/concerns/crud_activities_spec.rb @@ -1,7 +1,9 @@ describe Mrkt::CrudActivities do - include_context 'initialized client' + include_context 'with an initialized client' describe '#get_activity_types' do + subject { client.get_activity_types } + let(:response_stub) do { requestId: 'c245#14cd6830ae2', @@ -29,7 +31,6 @@ success: true } end - subject { client.get_activity_types } before do stub_request(:get, "https://#{host}/rest/v1/activities/types.json") @@ -40,6 +41,8 @@ end describe '#get_paging_token' do + subject { client.get_paging_token(since_datetime) } + let(:since_datetime) { Time.utc(2017, 1, 1, 4, 30) } let(:response_stub) do { @@ -48,7 +51,6 @@ nextPageToken: '4GAX7YNCIJKO2VAED5LH5PQIYPUM7WCVKTQWEDMP2L24AXZT54LA====' } end - subject { client.get_paging_token(since_datetime) } before do stub_request(:get, "https://#{host}/rest/v1/activities/pagingtoken.json") @@ -60,6 +62,8 @@ end describe '#get_activities' do + subject { client.get_activities(token) } + let(:activity_type_ids) { [1, 2] } let(:lead_ids) { [100, 102] } let(:token) { '4GAX7YNCIJKO2VAED5LH5PQIYPUM7WCVKTQWEDMP2L24AXZT54LA====' } @@ -106,7 +110,6 @@ moreResult: false } end - subject { client.get_activities(token) } before do stub_request(:get, "https://#{host}/rest/v1/activities.json") @@ -116,7 +119,9 @@ it { is_expected.to eq(response_stub) } - context 'specifying activity type ids' do + context 'when specifying activity type ids' do + subject { client.get_activities(token, activity_type_ids: activity_type_ids) } + let(:response_stub) do { data: { @@ -147,7 +152,6 @@ moreResult: false } end - subject { client.get_activities(token, activity_type_ids: activity_type_ids) } before do stub_request(:get, "https://#{host}/rest/v1/activities.json") @@ -161,7 +165,9 @@ it { is_expected.to eq(response_stub) } end - context 'specifying lead ids' do + context 'when specifying lead ids' do + subject { client.get_activities(token, lead_ids: lead_ids) } + let(:response_stub) do { data: { @@ -192,7 +198,6 @@ moreResult: false } end - subject { client.get_activities(token, lead_ids: lead_ids) } before do stub_request(:get, "https://#{host}/rest/v1/activities.json") @@ -203,13 +208,14 @@ it { is_expected.to eq(response_stub) } end - context 'specifying arrays values as empty strings' do - let(:activity_type_ids) { '' } - let(:lead_ids) { '' } + context 'when specifying arrays values as empty strings' do subject do client.get_activities(token, activity_type_ids: activity_type_ids, lead_ids: lead_ids) end + let(:activity_type_ids) { '' } + let(:lead_ids) { '' } + before do stub_request(:get, "https://#{host}/rest/v1/activities.json") .with(query: { nextPageToken: token }) @@ -219,7 +225,7 @@ it { is_expected.to eq(response_stub) } end - context 'specifying all options' do + context 'when specifying all options' do subject do client.get_activities(token, activity_type_ids: activity_type_ids, lead_ids: lead_ids) end @@ -239,6 +245,8 @@ end describe '#get_deleted_leads' do + subject { client.get_deleted_leads(token) } + let(:token) { '4GAX7YNCIJKO2VAED5LH5PQIYPUM7WCVKTQWEDMP2L24AXZT54LA====' } let(:response_stub) do { @@ -266,7 +274,6 @@ moreResult: false } end - subject { client.get_deleted_leads(token) } before do stub_request(:get, "https://#{host}/rest/v1/activities/deletedleads.json") diff --git a/spec/concerns/crud_asset_folders_spec.rb b/spec/concerns/crud_asset_folders_spec.rb index 9141889..1d52166 100644 --- a/spec/concerns/crud_asset_folders_spec.rb +++ b/spec/concerns/crud_asset_folders_spec.rb @@ -1,5 +1,5 @@ describe Mrkt::CrudAssetFolders do - include_context 'initialized client' + include_context 'with an initialized client' let(:response_folder_result) do { @@ -65,7 +65,7 @@ end describe '#get_folder_by_id' do - subject { client.get_folder_by_id(id, type: type) } + subject(:action) { client.get_folder_by_id(id, type: type) } let(:id) { 77 } @@ -123,14 +123,14 @@ } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::TypeMismatch) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::TypeMismatch) end end end describe '#get_folder_by_name' do - subject { client.get_folder_by_name(name, type: type, root: root, work_space: work_space) } + subject(:action) { client.get_folder_by_name(name, type: type, root: root, work_space: work_space) } let(:name) { 'Test Folder Name' } let(:type) { 'Folder' } @@ -196,8 +196,8 @@ } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::System) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::System) end end @@ -216,14 +216,14 @@ } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::UnspecifiedAction) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::UnspecifiedAction) end end end describe '#delete_folder' do - subject { client.delete_folder(id) } + subject(:action) { client.delete_folder(id) } let(:id) { 75 } @@ -265,8 +265,8 @@ } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::RecordNotFound) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::RecordNotFound) end end end diff --git a/spec/concerns/crud_asset_static_lists_spec.rb b/spec/concerns/crud_asset_static_lists_spec.rb index 66f58f2..5b9d209 100644 --- a/spec/concerns/crud_asset_static_lists_spec.rb +++ b/spec/concerns/crud_asset_static_lists_spec.rb @@ -1,5 +1,5 @@ describe Mrkt::CrudAssetStaticLists do - include_context 'initialized client' + include_context 'with an initialized client' let(:response_static_list_result) do { @@ -55,7 +55,7 @@ end describe '#get_static_list_by_id' do - subject { client.get_static_list_by_id(id) } + subject(:action) { client.get_static_list_by_id(id) } let(:id) { response_static_list_result[:id] } let(:response_stub) do @@ -106,8 +106,8 @@ } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::RecordNotFound) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::RecordNotFound) end end end diff --git a/spec/concerns/crud_campaigns_spec.rb b/spec/concerns/crud_campaigns_spec.rb index 113c026..63c909a 100644 --- a/spec/concerns/crud_campaigns_spec.rb +++ b/spec/concerns/crud_campaigns_spec.rb @@ -1,7 +1,9 @@ describe Mrkt::CrudCampaigns do - include_context 'initialized client' + include_context 'with an initialized client' describe '#request_campaign' do + subject(:action) { client.request_campaign(id, lead_ids, tokens) } + let(:id) { 42 } let(:lead_ids) { [1234, 5678] } let(:tokens) do @@ -13,7 +15,6 @@ value: 'Value for other token' }] end - subject { client.request_campaign(id, lead_ids, tokens) } before do stub_request(:post, "https://#{host}/rest/v1/campaigns/#{id}/trigger.json") @@ -25,16 +26,16 @@ let(:response_stub) do { requestId: 'a9b#14eb6771358', - success: false, - errors: [{ - code: '1013', - message: 'Campaign not found' - }] + success: false, + errors: [{ + code: '1013', + message: 'Campaign not found' + }] } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::ObjectNotFound) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::ObjectNotFound) end end @@ -43,20 +44,20 @@ let(:response_stub) do { requestId: '7cdc#14eb6ae8a86', - success: false, - errors: [{ - code: '1004', - message: 'Lead [1234] not found' - }] + success: false, + errors: [{ + code: '1004', + message: 'Lead [1234] not found' + }] } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::LeadNotFound) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::LeadNotFound) end end - context 'for valid lead ids' do + context 'with valid lead ids' do let(:response_stub) do { requestId: 'e42b#14272d07d78', diff --git a/spec/concerns/crud_custom_activities_spec.rb b/spec/concerns/crud_custom_activities_spec.rb index 438f2ed..bb044cd 100644 --- a/spec/concerns/crud_custom_activities_spec.rb +++ b/spec/concerns/crud_custom_activities_spec.rb @@ -1,5 +1,5 @@ describe Mrkt::CrudCustomObjects do - include_context 'initialized client' + include_context 'with an initialized client' describe '#get_list_of_custom_activity_types' do let(:response_stub) do @@ -49,14 +49,14 @@ } end - context 'all activities' do + context 'with all activities' do + subject { client.get_list_of_custom_activity_types } + before do stub_request(:get, "https://#{host}/rest/v1/activities/types.json") .to_return(json_stub(response_stub)) end - subject { client.get_list_of_custom_activity_types } - it { is_expected.to eq(response_stub) } end end @@ -104,6 +104,8 @@ end context 'with no additional attributes' do + subject { client.create_custom_activity(lead_id, activity_type_id, primary_attribute_value, date: date) } + let(:request_body) do { input: [{ @@ -122,12 +124,14 @@ .to_return(json_stub(response_stub)) end - subject { client.create_custom_activity(lead_id, activity_type_id, primary_attribute_value, date: date) } - it { is_expected.to eq(response_stub) } end context 'with additional attributes' do + subject do + client.create_custom_activity(lead_id, activity_type_id, primary_attribute_value, attributes: attributes, date: date) + end + let(:request_body) do { input: [{ @@ -148,13 +152,6 @@ }] } end - - before do - stub_request(:post, "https://#{host}/rest/v1/activities/external.json") - .with(json_stub(request_body)) - .to_return(json_stub(response_stub)) - end - let(:attributes) do { percent: '0.20', @@ -162,8 +159,10 @@ } end - subject do - client.create_custom_activity(lead_id, activity_type_id, primary_attribute_value, attributes: attributes, date: date) + before do + stub_request(:post, "https://#{host}/rest/v1/activities/external.json") + .with(json_stub(request_body)) + .to_return(json_stub(response_stub)) end it { is_expected.to eq(response_stub) } diff --git a/spec/concerns/crud_custom_objects_spec.rb b/spec/concerns/crud_custom_objects_spec.rb index e999701..acae728 100644 --- a/spec/concerns/crud_custom_objects_spec.rb +++ b/spec/concerns/crud_custom_objects_spec.rb @@ -1,5 +1,5 @@ describe Mrkt::CrudCustomObjects do - include_context 'initialized client' + include_context 'with an initialized client' describe '#get_list_of_custom_objects' do let(:response_stub) do @@ -44,17 +44,19 @@ end context 'with no object names' do + subject { client.get_list_of_custom_objects } + before do stub_request(:get, "https://#{host}/rest/v1/customobjects.json") .to_return(json_stub(response_stub)) end - subject { client.get_list_of_custom_objects } - it { is_expected.to eq(response_stub) } end context 'with object names' do + subject { client.get_list_of_custom_objects(object_names) } + let(:object_names) { %w[device_c manufacturer_c] } before do @@ -63,13 +65,13 @@ .to_return(json_stub(response_stub)) end - subject { client.get_list_of_custom_objects(object_names) } - it { is_expected.to eq(response_stub) } end end describe '#describe_custom_object' do + subject(:action) { client.describe_custom_object(object_name) } + let(:response_stub) do { requestId: 'eeef#152858b17d2', @@ -129,8 +131,6 @@ .to_return(json_stub(response_stub)) end - subject { client.describe_custom_object(object_name) } - context 'when the object name is valid' do let(:object_name) { :device_c } @@ -140,13 +140,15 @@ context 'when the object name is invalid' do let(:object_name) { nil } - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::Unknown) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::Unknown) end end end describe '#createupdate_custom_objects' do + subject { client.createupdate_custom_objects(object_name, devices) } + let(:object_name) { 'device' } let(:devices) do @@ -178,8 +180,6 @@ } end - subject { client.createupdate_custom_objects(object_name, devices) } - before do stub_request(:post, "https://#{host}/rest/v1/customobjects/#{object_name}.json") .with(json_stub(request_body)) @@ -190,6 +190,8 @@ end describe '#delete_custom_objects' do + subject { client.delete_custom_objects(object_name, search_fields) } + let(:object_name) { 'device' } let(:search_fields) do @@ -215,8 +217,6 @@ } end - subject { client.delete_custom_objects(object_name, search_fields) } - before do stub_request(:post, "https://#{host}/rest/v1/customobjects/#{object_name}/delete.json") .with(json_stub(request_body)) @@ -227,6 +227,8 @@ end describe '#get_custom_objects' do + subject { client.get_custom_objects(object_name, filter_values) } + let(:object_name) { 'device' } let(:filter_values) do @@ -256,8 +258,6 @@ } end - subject { client.get_custom_objects(object_name, filter_values) } - before do stub_request(:post, "https://#{host}/rest/v1/customobjects/#{object_name}.json?_method=GET") .with(json_stub(request_body)) diff --git a/spec/concerns/crud_leads_spec.rb b/spec/concerns/crud_leads_spec.rb index 3f8bd43..5a124ca 100644 --- a/spec/concerns/crud_leads_spec.rb +++ b/spec/concerns/crud_leads_spec.rb @@ -1,5 +1,5 @@ describe Mrkt::CrudLeads do - include_context 'initialized client' + include_context 'with an initialized client' describe 'get_lead_by_id' do subject { client.get_lead_by_id(id, fields: fields) } @@ -56,6 +56,8 @@ end describe '#get_leads' do + subject { client.get_leads(filter_type, filter_values) } + let(:filter_type) { 'email' } let(:filter_values) { %w[user@example.com] } let(:response_stub) do @@ -74,7 +76,6 @@ success: true } end - subject { client.get_leads(filter_type, filter_values) } before do stub_request(:get, "https://#{host}/rest/v1/leads.json") @@ -86,6 +87,8 @@ end describe '#createupdate_leads' do + subject { client.createupdate_leads(leads, lookup_field: :email) } + let(:leads) do [ firstName: 'John', @@ -118,7 +121,6 @@ ] } end - subject { client.createupdate_leads(leads, lookup_field: :email) } before do stub_request(:post, "https://#{host}/rest/v1/leads.json") @@ -130,6 +132,8 @@ end describe '#delete_leads' do + subject { client.delete_leads(leads) } + let(:leads) { [1] } let(:request_body) do { @@ -147,7 +151,6 @@ success: true } end - subject { client.delete_leads(leads) } before do stub_request(:delete, "https://#{host}/rest/v1/leads.json") @@ -159,12 +162,12 @@ end describe '#associate_lead' do + subject(:action) { client.associate_lead(id, cookie) } + let(:id) { 1 } let(:cookie) { 'id:561-HYG-937&token:_mch-marketo.com-1427205775289-40768' } let(:request_stub) { {} } - subject { client.associate_lead(id, cookie) } - before do stub_request(:post, "https://#{host}/rest/v1/leads/#{id}/associate.json?#{URI.encode_www_form(cookie: cookie)}") .with(json_stub(request_stub)) @@ -197,19 +200,19 @@ } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::LeadNotFound) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::LeadNotFound) end end end describe '#merge_leads' do + subject(:action) { client.merge_leads(id, losing_lead_ids) } + let(:id) { 1 } let(:losing_lead_ids) { [2, 3, 4] } let(:request_stub) { {} } - subject { client.merge_leads(id, losing_lead_ids) } - before do params = ::Faraday::Utils::ParamsHash.new params[:mergeInCRM] = false @@ -245,13 +248,15 @@ } end - it 'should raise an Error' do - expect { subject }.to raise_error(Mrkt::Errors::LeadNotFound) + it 'raises an Error' do + expect { action }.to raise_error(Mrkt::Errors::LeadNotFound) end end end describe '#describe_lead' do + subject { client.describe_lead } + let(:response_stub) do { requestId: '5c9e#169a68fa806', @@ -289,8 +294,6 @@ } end - subject { client.describe_lead } - before do stub_request(:get, "https://#{host}/rest/v1/leads/describe.json") .to_return(json_stub(response_stub)) diff --git a/spec/concerns/crud_lists_spec.rb b/spec/concerns/crud_lists_spec.rb index 054f251..a64394a 100644 --- a/spec/concerns/crud_lists_spec.rb +++ b/spec/concerns/crud_lists_spec.rb @@ -1,7 +1,9 @@ describe Mrkt::CrudLists do - include_context 'initialized client' + include_context 'with an initialized client' describe '#get_leads_by_list' do + subject { client.get_leads_by_list(list_id) } + let(:list_id) { '1001' } let(:response_stub) do { @@ -20,8 +22,6 @@ } end - subject { client.get_leads_by_list(list_id) } - before do stub_request(:get, "https://#{host}/rest/v1/list/#{list_id}/leads.json") .with(query: {}) @@ -32,6 +32,8 @@ end describe '#add_leads_to_list' do + subject { client.add_leads_to_list(list_id, lead_ids) } + let(:list_id) { '1001' } let(:lead_ids) { ['1'] } let(:request_body) do @@ -53,7 +55,6 @@ success: true } end - subject { client.add_leads_to_list(list_id, lead_ids) } before do stub_request(:post, "https://#{host}/rest/v1/lists/#{list_id}/leads.json") @@ -65,6 +66,8 @@ end describe '#remove_leads_from_list' do + subject { client.remove_leads_from_list(list_id, lead_ids) } + let(:list_id) { '1001' } let(:lead_ids) { ['1'] } let(:request_body) do @@ -86,7 +89,6 @@ success: true } end - subject { client.remove_leads_from_list(list_id, lead_ids) } before do stub_request(:delete, "https://#{host}/rest/v1/lists/#{list_id}/leads.json") diff --git a/spec/concerns/crud_program_members_spec.rb b/spec/concerns/crud_program_members_spec.rb index d212c8e..a86666f 100644 --- a/spec/concerns/crud_program_members_spec.rb +++ b/spec/concerns/crud_program_members_spec.rb @@ -1,7 +1,9 @@ describe Mrkt::CrudProgramMembers do - include_context 'initialized client' + include_context 'with an initialized client' describe '#describe_program_members' do + subject { client.describe_program_members } + let(:response_stub) do { requestId: 'c245#14cd6830ae2', @@ -38,7 +40,6 @@ success: true } end - subject { client.describe_program_members } before do stub_request(:get, "https://#{host}/rest/v1/programs/members/describe.json") @@ -49,6 +50,8 @@ end describe '#createupdate_program_members' do + subject { client.createupdate_program_members(program_id, lead_ids, status) } + let(:program_id) { 123 } let(:lead_ids) { [1, 2, 3] } let(:status) { 'Registered' } @@ -85,7 +88,6 @@ success: true } end - subject { client.createupdate_program_members(program_id, lead_ids, status) } before do stub_request(:post, "https://#{host}/rest/v1/programs/#{program_id}/members/status.json") @@ -97,6 +99,8 @@ end describe '#get_program_members' do + subject { client.get_program_members(program_id, filter_type, filter_values) } + let(:filter_type) { 'leadId' } let(:filter_values) { [1, 2] } let(:program_id) { 1014 } @@ -125,7 +129,6 @@ moreResult: false } end - subject { client.get_program_members(program_id, filter_type, filter_values) } before do stub_request(:get, "https://#{host}/rest/v1/programs/#{program_id}/members.json") diff --git a/spec/concerns/crud_programs_spec.rb b/spec/concerns/crud_programs_spec.rb index ee11d8b..3b06ce5 100644 --- a/spec/concerns/crud_programs_spec.rb +++ b/spec/concerns/crud_programs_spec.rb @@ -1,7 +1,9 @@ describe Mrkt::CrudPrograms do - include_context 'initialized client' + include_context 'with an initialized client' describe '#browse_programs' do + subject { client.browse_programs } + let(:response_stub) do { success: true, @@ -30,8 +32,6 @@ } end - subject { client.browse_programs } - before do stub_request(:get, "https://#{host}/rest/asset/v1/programs.json") .to_return(json_stub(response_stub)) @@ -41,6 +41,8 @@ end describe '#get_program_by_id' do + subject { client.get_program_by_id(1107) } + let(:response_stub) do { success: true, @@ -76,8 +78,6 @@ } end - subject { client.get_program_by_id(1107) } - before do stub_request(:get, "https://#{host}/rest/asset/v1/program/1107.json") .to_return(json_stub(response_stub)) diff --git a/spec/concerns/import_custom_objects_spec.rb b/spec/concerns/import_custom_objects_spec.rb index 4075a81..7655796 100644 --- a/spec/concerns/import_custom_objects_spec.rb +++ b/spec/concerns/import_custom_objects_spec.rb @@ -1,10 +1,12 @@ require 'tempfile' describe Mrkt::ImportCustomObjects do - include_context 'initialized client' + include_context 'with an initialized client' let(:custom_object) { 'car_c' } describe '#import_custom_object' do + subject { client.import_custom_object(file, custom_object) } + let(:file) { StringIO.new } let(:response_stub) do { @@ -19,7 +21,6 @@ ] } end - subject { client.import_custom_object(file, custom_object) } before do stub_request(:post, "https://#{host}/bulk/v1/customobjects/#{custom_object}/import.json") @@ -31,6 +32,8 @@ end describe '#import_custom_object_status' do + subject { client.import_custom_object_status(1, custom_object) } + let(:id) { 1 } let(:response_stub) do { @@ -51,7 +54,6 @@ success: true } end - subject { client.import_custom_object_status(1, custom_object) } before do stub_request(:get, "https://#{host}/bulk/v1/customobjects/#{custom_object}/import/#{id}/status.json") @@ -62,9 +64,10 @@ end describe '#import_custom_object_failures' do + subject { client.import_custom_object_failures(1, custom_object) } + let(:id) { 1 } let(:response_stub) { '' } - subject { client.import_custom_object_failures(1, custom_object) } before do stub_request(:get, "https://#{host}/bulk/v1/customobjects/#{custom_object}/import/#{id}/failures.json") @@ -75,9 +78,10 @@ end describe '#import_custom_object_warnings' do + subject { client.import_custom_object_warnings(1, custom_object) } + let(:id) { 1 } let(:response_stub) { '' } - subject { client.import_custom_object_warnings(1, custom_object) } before do stub_request(:get, "https://#{host}/bulk/v1/customobjects/#{custom_object}/import/#{id}/warnings.json") diff --git a/spec/concerns/import_leads_spec.rb b/spec/concerns/import_leads_spec.rb index b8c222a..91fe0f1 100644 --- a/spec/concerns/import_leads_spec.rb +++ b/spec/concerns/import_leads_spec.rb @@ -2,9 +2,11 @@ require 'tempfile' describe Mrkt::ImportLeads do - include_context 'initialized client' + include_context 'with an initialized client' describe '#import_lead' do + subject { client.import_lead(tempfile) } + let(:tempfile) { Tempfile.new(%w[import-leads csv]) } let(:response_stub) do { @@ -18,7 +20,6 @@ ] } end - subject { client.import_lead(tempfile) } before do CSV.open(tempfile, 'wb') do |csv| @@ -39,6 +40,8 @@ end describe '#import_lead_status' do + subject { client.import_lead_status(1) } + let(:id) { 1 } let(:response_stub) do { @@ -56,7 +59,6 @@ success: true } end - subject { client.import_lead_status(1) } before do stub_request(:get, "https://#{host}/bulk/v1/leads/batch/#{id}.json") @@ -67,9 +69,10 @@ end describe '#import_lead_failures' do + subject { client.import_lead_failures(1) } + let(:id) { 1 } let(:response_stub) { '' } - subject { client.import_lead_failures(1) } before do stub_request(:get, "https://#{host}/bulk/v1/leads/batch/#{id}/failures.json") @@ -80,9 +83,10 @@ end describe '#import_lead_warnings' do + subject { client.import_lead_warnings(1) } + let(:id) { 1 } let(:response_stub) { '' } - subject { client.import_lead_warnings(1) } before do stub_request(:get, "https://#{host}/bulk/v1/leads/batch/#{id}/warnings.json") diff --git a/spec/errors_spec.rb b/spec/errors_spec.rb index a23e0bc..c7b85c6 100644 --- a/spec/errors_spec.rb +++ b/spec/errors_spec.rb @@ -1,21 +1,19 @@ describe Mrkt::Errors do describe '.find_by_response_code' do - subject { described_class.find_by_response_code(code) } + subject(:actual) { described_class.find_by_response_code(code) } - context 'when the code is' do - context 'known' do - let(:code) { 413 } + context 'when the code is known' do + let(:code) { 413 } - it 'should return the mapped error class' do - is_expected.to eq(Mrkt::Errors::RequestEntityTooLarge) - end + it 'returns the mapped error class' do + expect(actual).to eq(Mrkt::Errors::RequestEntityTooLarge) end + end - context 'unknown' do - let(:code) { 7331 } + context 'when the code is unknown' do + let(:code) { 7331 } - it { is_expected.to eq(Mrkt::Errors::Error) } - end + it { is_expected.to eq(Mrkt::Errors::Error) } end end end diff --git a/spec/faraday/params_encoder_spec.rb b/spec/faraday/params_encoder_spec.rb index 5d192d7..081cadf 100644 --- a/spec/faraday/params_encoder_spec.rb +++ b/spec/faraday/params_encoder_spec.rb @@ -1,5 +1,7 @@ describe Mrkt::Faraday::ParamsEncoder do describe '.encode' do + subject { described_class.encode(params) } + let(:params) do { string: 'foobar', @@ -9,16 +11,14 @@ } end - subject { described_class.encode(params) } - it { is_expected.to eq(Faraday::Utils::ParamsHash.new.merge(params.merge(array: '1,2,3')).to_query) } end describe '.decode' do - let(:value) { 'foo=foo&bar=bar' } - subject { described_class.decode(value) } + let(:value) { 'foo=foo&bar=bar' } + it { is_expected.to eq('foo' => 'foo', 'bar' => 'bar') } end end diff --git a/spec/mkto_rest_spec.rb b/spec/mkto_rest_spec.rb index 4b7527a..7a4ca44 100644 --- a/spec/mkto_rest_spec.rb +++ b/spec/mkto_rest_spec.rb @@ -1,5 +1,5 @@ describe Mrkt do - include_context 'initialized client' + include_context 'with an initialized client' it { is_expected.to respond_to(:get, :post, :delete) } end diff --git a/spec/support/initialized_client.rb b/spec/support/initialized_client.rb index 3aa8e93..891e0f8 100644 --- a/spec/support/initialized_client.rb +++ b/spec/support/initialized_client.rb @@ -1,7 +1,9 @@ require 'securerandom' require 'json' -shared_context 'initialized client' do +shared_context 'with an initialized client' do + subject(:client) { Mrkt::Client.new(host: host, client_id: client_id, client_secret: client_secret) } + let(:host) { '0-KBZ-0.mktorest.com' } let(:client_id) { SecureRandom.uuid } let(:client_secret) { SecureRandom.hex } @@ -9,8 +11,6 @@ { access_token: SecureRandom.uuid, token_type: 'bearer', expires_in: 2241, scope: 'RestClient' } end - subject(:client) { Mrkt::Client.new(host: host, client_id: client_id, client_secret: client_secret) } - before do @authentication_request_stub = stub_request(:get, "https://#{host}/identity/oauth/token") .with(query: { client_id: client_id, client_secret: client_secret, grant_type: 'client_credentials' })