|
4 | 4 |
|
5 | 5 | describe MktoRest do
|
6 | 6 |
|
7 |
| - before do |
8 |
| - @client_id = 'id' |
9 |
| - @client_key = 'key' |
10 |
| - @hostname = 'dummy.mktorest.com' |
11 |
| - @token = 'token' |
12 |
| - |
13 |
| - @client = MktoRest::Client.new(host: @hostname, client_id: @client_id, client_secret: @client_key) |
14 |
| - @authenticated_client = MktoRest::Client.new(host: @hostname, client_id: @client_id, client_secret: @client_key) |
15 |
| - @authenticated_client.__auth(@token) |
| 7 | + let(:client_id) { 'id' } |
| 8 | + let(:client_key) { 'key' } |
| 9 | + let(:hostname) { 'dummy.mktorest.com' } |
| 10 | + let(:token) { 'token' } |
| 11 | + let(:client) { MktoRest::Client.new(host: hostname, client_id: client_id, client_secret: client_key) } |
| 12 | + let(:authenticated_client) { MktoRest::Client.new(host: hostname, client_id: client_id, client_secret: client_key) } |
| 13 | + let(:lead1) { MktoRest::Lead.new(authenticated_client, name: 'john', email: 'john@acme.com', id: 1) } |
16 | 14 |
|
17 |
| - @lead1 = MktoRest::Lead.new(@authenticated_client, name: 'john', email: 'john@acme.com', id: 1) |
| 15 | + before do |
| 16 | + allow(authenticated_client).to receive(:valid_until).and_return(Time.now + (60 * 60 * 24)) |
| 17 | + allow(authenticated_client).to receive(:token).and_return(token) |
18 | 18 | end
|
19 | 19 |
|
20 | 20 | describe 'v1 API' do
|
21 | 21 | context '#authenticated?' do
|
22 | 22 | context 'before authentication' do
|
23 | 23 | it 'should be false' do
|
24 |
| - expect(@client.authenticated?) == false |
| 24 | + expect(client.authenticated?) == false |
25 | 25 | end
|
26 | 26 | end
|
27 | 27 | context 'after authentication' do
|
28 | 28 | it 'should be true' do
|
29 |
| - expect(@authenticated_client.authenticated?) == true |
| 29 | + expect(authenticated_client.authenticated?) == true |
30 | 30 | end
|
31 | 31 | end
|
32 | 32 | end
|
|
35 | 35 | # repsonses samples are in responses_samples/*.json
|
36 | 36 | describe 'authentication' do
|
37 | 37 | it 'parses response' do
|
38 |
| - set_authentication_stub_request(@hostname, @client_id, @client_key) |
39 |
| - expect { @client.authenticate }.not_to raise_error |
40 |
| - expect(@client.token).to_not be_nil |
41 |
| - expect(@client.expires_in).to_not be_nil |
42 |
| - expect(@client.valid_until).to_not be_nil |
43 |
| - expect(@client.token_type).to_not be_nil |
44 |
| - expect(@client.scope).to_not be_nil |
| 38 | + set_authentication_stub_request(hostname, client_id, client_key) |
| 39 | + expect { client.authenticate }.not_to raise_error |
| 40 | + expect(client.token).to_not be_nil |
| 41 | + expect(client.expires_in).to_not be_nil |
| 42 | + expect(client.valid_until).to_not be_nil |
| 43 | + expect(client.token_type).to_not be_nil |
| 44 | + expect(client.scope).to_not be_nil |
45 | 45 | end
|
46 | 46 | end
|
47 | 47 | describe 'leads operations' do
|
48 | 48 | it 'uses correct HTTP GET body and headers' do
|
49 |
| - set_get_leads_stub_request('email', 'john@acme.com', @hostname, @token) |
50 |
| - expect { @authenticated_client.get_leads :email, 'john@acme.com' }.not_to raise_error |
| 49 | + set_get_leads_stub_request('email', 'john@acme.com', hostname, token) |
| 50 | + expect { authenticated_client.get_leads :email, 'john@acme.com' }.not_to raise_error |
51 | 51 | end
|
52 | 52 | end
|
53 | 53 |
|
|
85 | 85 | let(:partition) { 'bizdev' }
|
86 | 86 |
|
87 | 87 | it 'can be updated by id' do
|
88 |
| - set_update_lead_stub_request(:id, 1, { 'someFieldX' => 'new_value' }, @hostname, @token) |
89 |
| - @lead1.update({ 'someFieldX' => 'new_value' }, :id) |
| 88 | + set_update_lead_stub_request(:id, 1, { 'someFieldX' => 'new_value' }, hostname, token) |
| 89 | + lead1.update({ 'someFieldX' => 'new_value' }, :id) |
90 | 90 | end
|
91 | 91 | it 'can be updated by email' do
|
92 |
| - set_update_lead_stub_request(:email, @lead1.email, { 'someFieldX' => 'new_value' }, @hostname, @token) |
93 |
| - @lead1.update({ 'someFieldX' => 'new_value' }, :email) |
| 92 | + set_update_lead_stub_request(:email, lead1.email, { 'someFieldX' => 'new_value' }, hostname, token) |
| 93 | + lead1.update({ 'someFieldX' => 'new_value' }, :email) |
94 | 94 | end
|
95 | 95 |
|
96 | 96 | it 'one can be created with email but w/out partition' do
|
97 |
| - set_create_leads_stub_request(sample_lead, @hostname, @token) |
98 |
| - @authenticated_client.create_leads(sample_lead) |
| 97 | + set_create_leads_stub_request(sample_lead, hostname, token) |
| 98 | + authenticated_client.create_leads(sample_lead) |
99 | 99 | end
|
100 | 100 |
|
101 | 101 | it 'multiple leads can be created with emails but w/out partition' do
|
102 |
| - set_create_leads_stub_request(sample_leads, @hostname, @token) |
103 |
| - @authenticated_client.create_leads(sample_leads) |
| 102 | + set_create_leads_stub_request(sample_leads, hostname, token) |
| 103 | + authenticated_client.create_leads(sample_leads) |
104 | 104 | end
|
105 | 105 |
|
106 | 106 | it 'multiple can be created with email and partition' do
|
107 |
| - set_create_leads_stub_request(sample_leads, @hostname, @token, partition: partition) |
108 |
| - @authenticated_client.create_leads(sample_leads, 'createOnly', partition) |
| 107 | + set_create_leads_stub_request(sample_leads, hostname, token, partition: partition) |
| 108 | + authenticated_client.create_leads(sample_leads, 'createOnly', partition) |
109 | 109 | end
|
110 | 110 | end
|
111 | 111 |
|
112 | 112 | describe 'client' do
|
113 |
| - let(:lead) { MktoRest::Lead.new(@authenticated_client, id: 1, email: 'joe@acme.com') } |
114 |
| - before { set_get_leads_stub_request('email', lead.email, @hostname, @token) } |
| 113 | + let(:lead) { MktoRest::Lead.new(authenticated_client, id: 1, email: 'joe@acme.com') } |
| 114 | + before { set_get_leads_stub_request('email', lead.email, hostname, token) } |
115 | 115 |
|
116 | 116 | describe '#get_leads' do
|
117 | 117 | let(:blk) { nil }
|
118 |
| - let(:leads) { @authenticated_client.get_leads(:email, lead.email, &blk) } |
| 118 | + let(:leads) { authenticated_client.get_leads(:email, lead.email, &blk) } |
119 | 119 |
|
120 | 120 | it 'should return leads' do
|
121 | 121 | expect(leads).to_not be_empty
|
|
135 | 135 | describe '#associate_lead' do
|
136 | 136 | let(:id) { lead.id }
|
137 | 137 | let(:cookie) { 'id:287-GTJ-838&token:_mch-marketo.com-1396310362214-46169' }
|
138 |
| - let(:association) { @authenticated_client.associate_lead(id, cookie) } |
| 138 | + let(:association) { authenticated_client.associate_lead(id, cookie) } |
139 | 139 |
|
140 |
| - before { stub_associate_lead_request(@hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', @token) } |
| 140 | + before { stub_associate_lead_request(hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', token) } |
141 | 141 |
|
142 | 142 | it 'associates the lead' do
|
143 | 143 | expect(association).to include(success: true)
|
144 | 144 | end
|
145 | 145 |
|
146 | 146 | context 'with an unathorized request' do
|
147 |
| - before { stub_failed_associate_lead_request(@hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', @token) } |
| 147 | + before { stub_failed_associate_lead_request(hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', token) } |
148 | 148 |
|
149 | 149 | it 'raises a MktoRest::Errors::AccessTokenInvalid exception with the message' do
|
150 | 150 | expect { association }.to raise_error(MktoRest::Errors::AccessTokenInvalid, 'Unauthorized')
|
|
0 commit comments