diff --git a/Gemfile.lock b/Gemfile.lock index e3463f9..befc613 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,14 +11,19 @@ GEM safe_yaml (~> 1.0.0) diff-lcs (1.2.5) rake (10.1.1) - rspec (2.13.0) - rspec-core (~> 2.13.0) - rspec-expectations (~> 2.13.0) - rspec-mocks (~> 2.13.0) - rspec-core (2.13.1) - rspec-expectations (2.13.0) - diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.13.1) + rspec (3.2.0) + rspec-core (~> 3.2.0) + rspec-expectations (~> 3.2.0) + rspec-mocks (~> 3.2.0) + rspec-core (3.2.2) + rspec-support (~> 3.2.0) + rspec-expectations (3.2.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-mocks (3.2.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-support (3.2.2) safe_yaml (1.0.1) webmock (1.17.4) addressable (>= 2.2.7) @@ -31,5 +36,5 @@ DEPENDENCIES bundler (~> 1.3) mkto_rest! rake - rspec (~> 2.13.0) + rspec (~> 3.2) webmock diff --git a/lib/mkto_rest.rb b/lib/mkto_rest.rb index 28c0010..70908d9 100644 --- a/lib/mkto_rest.rb +++ b/lib/mkto_rest.rb @@ -23,16 +23,11 @@ def debug=(bool) end def authenticated? - !@token.empty? && token_valid? + !token.empty? && token_valid? end def token_valid? - return Time.now < @valid_until - end - - # used for testing only - def __auth(token) - @token = token + valid_until && Time.now < valid_until end # \options: @@ -61,7 +56,7 @@ def get_leads(filtr, values = [], fields = [], &block) # values can be a string or an array values = values.split unless values.is_a? Array args = { - access_token: @token, + access_token: token, filterType: filtr.to_s, filterValues: values.join(',') } @@ -112,13 +107,13 @@ def create_leads(leads, action = 'createOnly', partition = nil) def associate_lead(id, cookie) authenticate unless authenticated? - post(nil, "https://#{@host}/rest/v1/leads/#{id}/associate.json?#{URI.encode_www_form(cookie: cookie, access_token: @token)}") + post(nil, "https://#{@host}/rest/v1/leads/#{id}/associate.json?#{URI.encode_www_form(cookie: cookie, access_token: token)}") end def post(data, url = "https://#{@host}/rest/v1/leads.json") authenticate unless authenticated? fail 'client not authenticated.' unless authenticated? - headers = { 'Authorization' => "Bearer #{@token}" } + headers = { 'Authorization' => "Bearer #{token}" } body = MktoRest::HttpUtils.post(url, headers, data.to_json, @options) data = JSON.parse(body, symbolize_names: true) handle_errors(data[:errors]) if data[:success] == false diff --git a/mkto_rest.gemspec b/mkto_rest.gemspec index a891d93..aa696a5 100644 --- a/mkto_rest.gemspec +++ b/mkto_rest.gemspec @@ -20,6 +20,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" - spec.add_development_dependency 'rspec', '~> 2.13.0' + spec.add_development_dependency 'rspec', '~> 3.2' spec.add_development_dependency "webmock" end diff --git a/spec/mkto_rest_spec.rb b/spec/mkto_rest_spec.rb index dee1aff..0a91155 100644 --- a/spec/mkto_rest_spec.rb +++ b/spec/mkto_rest_spec.rb @@ -4,29 +4,29 @@ describe MktoRest do - before do - @client_id = 'id' - @client_key = 'key' - @hostname = 'dummy.mktorest.com' - @token = 'token' - - @client = MktoRest::Client.new(host: @hostname, client_id: @client_id, client_secret: @client_key) - @authenticated_client = MktoRest::Client.new(host: @hostname, client_id: @client_id, client_secret: @client_key) - @authenticated_client.__auth(@token) + let(:client_id) { 'id' } + let(:client_key) { 'key' } + let(:hostname) { 'dummy.mktorest.com' } + let(:token) { 'token' } + let(:client) { MktoRest::Client.new(host: hostname, client_id: client_id, client_secret: client_key) } + let(:authenticated_client) { MktoRest::Client.new(host: hostname, client_id: client_id, client_secret: client_key) } + let(:lead1) { MktoRest::Lead.new(authenticated_client, name: 'john', email: 'john@acme.com', id: 1) } - @lead1 = MktoRest::Lead.new(@authenticated_client, name: 'john', email: 'john@acme.com', id: 1) + before do + allow(authenticated_client).to receive(:valid_until).and_return(Time.now + (60 * 60 * 24)) + allow(authenticated_client).to receive(:token).and_return(token) end describe 'v1 API' do context '#authenticated?' do context 'before authentication' do it 'should be false' do - expect(@client.authenticated?) == false + expect(client.authenticated?) == false end end context 'after authentication' do it 'should be true' do - expect(@authenticated_client.authenticated?) == true + expect(authenticated_client.authenticated?) == true end end end @@ -35,19 +35,19 @@ # repsonses samples are in responses_samples/*.json describe 'authentication' do it 'parses response' do - set_authentication_stub_request(@hostname, @client_id, @client_key) - expect { @client.authenticate }.not_to raise_error - expect(@client.token).to_not be_nil - expect(@client.expires_in).to_not be_nil - expect(@client.valid_until).to_not be_nil - expect(@client.token_type).to_not be_nil - expect(@client.scope).to_not be_nil + set_authentication_stub_request(hostname, client_id, client_key) + expect { client.authenticate }.not_to raise_error + expect(client.token).to_not be_nil + expect(client.expires_in).to_not be_nil + expect(client.valid_until).to_not be_nil + expect(client.token_type).to_not be_nil + expect(client.scope).to_not be_nil end end describe 'leads operations' do it 'uses correct HTTP GET body and headers' do - set_get_leads_stub_request('email', 'john@acme.com', @hostname, @token) - expect { @authenticated_client.get_leads :email, 'john@acme.com' }.not_to raise_error + set_get_leads_stub_request('email', 'john@acme.com', hostname, token) + expect { authenticated_client.get_leads :email, 'john@acme.com' }.not_to raise_error end end @@ -85,37 +85,37 @@ let(:partition) { 'bizdev' } it 'can be updated by id' do - set_update_lead_stub_request(:id, 1, { 'someFieldX' => 'new_value' }, @hostname, @token) - @lead1.update({ 'someFieldX' => 'new_value' }, :id) + set_update_lead_stub_request(:id, 1, { 'someFieldX' => 'new_value' }, hostname, token) + lead1.update({ 'someFieldX' => 'new_value' }, :id) end it 'can be updated by email' do - set_update_lead_stub_request(:email, @lead1.email, { 'someFieldX' => 'new_value' }, @hostname, @token) - @lead1.update({ 'someFieldX' => 'new_value' }, :email) + set_update_lead_stub_request(:email, lead1.email, { 'someFieldX' => 'new_value' }, hostname, token) + lead1.update({ 'someFieldX' => 'new_value' }, :email) end it 'one can be created with email but w/out partition' do - set_create_leads_stub_request(sample_lead, @hostname, @token) - @authenticated_client.create_leads(sample_lead) + set_create_leads_stub_request(sample_lead, hostname, token) + authenticated_client.create_leads(sample_lead) end it 'multiple leads can be created with emails but w/out partition' do - set_create_leads_stub_request(sample_leads, @hostname, @token) - @authenticated_client.create_leads(sample_leads) + set_create_leads_stub_request(sample_leads, hostname, token) + authenticated_client.create_leads(sample_leads) end it 'multiple can be created with email and partition' do - set_create_leads_stub_request(sample_leads, @hostname, @token, partition: partition) - @authenticated_client.create_leads(sample_leads, 'createOnly', partition) + set_create_leads_stub_request(sample_leads, hostname, token, partition: partition) + authenticated_client.create_leads(sample_leads, 'createOnly', partition) end end describe 'client' do - let(:lead) { MktoRest::Lead.new(@authenticated_client, id: 1, email: 'joe@acme.com') } - before { set_get_leads_stub_request('email', lead.email, @hostname, @token) } + let(:lead) { MktoRest::Lead.new(authenticated_client, id: 1, email: 'joe@acme.com') } + before { set_get_leads_stub_request('email', lead.email, hostname, token) } describe '#get_leads' do let(:blk) { nil } - let(:leads) { @authenticated_client.get_leads(:email, lead.email, &blk) } + let(:leads) { authenticated_client.get_leads(:email, lead.email, &blk) } it 'should return leads' do expect(leads).to_not be_empty @@ -135,16 +135,16 @@ describe '#associate_lead' do let(:id) { lead.id } let(:cookie) { 'id:287-GTJ-838&token:_mch-marketo.com-1396310362214-46169' } - let(:association) { @authenticated_client.associate_lead(id, cookie) } + let(:association) { authenticated_client.associate_lead(id, cookie) } - before { stub_associate_lead_request(@hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', @token) } + before { stub_associate_lead_request(hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', token) } it 'associates the lead' do expect(association).to include(success: true) end context 'with an unathorized request' do - before { stub_failed_associate_lead_request(@hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', @token) } + before { stub_failed_associate_lead_request(hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', token) } it 'raises a MktoRest::Errors::AccessTokenInvalid exception with the message' do expect { association }.to raise_error(MktoRest::Errors::AccessTokenInvalid, 'Unauthorized')