Skip to content

Commit b22db13

Browse files
committed
atomicly wrap api calls with VCR
1 parent c34ca83 commit b22db13

11 files changed

+387
-231
lines changed

spec/controllers/accounts_controller_spec.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
let(:plan) { Plan.create(amount: 20000, interval: Plan::MONTHLY, name: 'Monthly') }
44
let(:current_user) { Fabricate(:user) }
55

6-
before {
6+
before do
77
team.add_user(current_user)
88
controller.send :sign_in, current_user
9-
}
9+
end
1010

1111
def new_token
1212
Stripe::Token.create(card: { number: 4242424242424242, cvc: 224, exp_month: 12, exp_year: 14 }).try(:id)
@@ -20,9 +20,14 @@ def valid_params
2020
end
2121

2222
it 'should create an account and send email' do
23-
post :create, { team_id: team.id, account: valid_params }
24-
expect(ActionMailer::Base.deliveries.size).to eq(1)
25-
expect(ActionMailer::Base.deliveries.first.body.encoded).to include(team.name)
26-
expect(ActionMailer::Base.deliveries.first.body.encoded).to include(plan.name)
23+
# TODO: Refactor API call to Sidekiq Job
24+
VCR.use_cassette('AccountsController') do
25+
26+
post :create, { team_id: team.id, account: valid_params }
27+
expect(ActionMailer::Base.deliveries.size).to eq(1)
28+
expect(ActionMailer::Base.deliveries.first.body.encoded).to include(team.name)
29+
expect(ActionMailer::Base.deliveries.first.body.encoded).to include(plan.name)
30+
31+
end
2732
end
2833
end

spec/models/account_spec.rb

Lines changed: 169 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
end
1919

2020
def new_token
21-
Stripe::Token.create(card: { number: 4242424242424242, cvc: 224, exp_month: 12, exp_year: 14 }).try(:id)
21+
Stripe::Token.create(card: { number: 4242424242424242, cvc: 224, exp_month: 12, exp_year: 14 }).try(:id)
2222
end
2323

2424
def post_job_for(team)
@@ -28,42 +28,62 @@ def post_job_for(team)
2828
describe 'account creation' do
2929

3030
it 'should create a valid account locally and on stripe' do
31-
expect(team.account).to be_nil
32-
team.build_account(account)
33-
team.account.admin_id = admin.id
34-
team.account.save_with_payment
35-
team.reload
36-
expect(team.account.stripe_card_token).to eq(account[:stripe_card_token])
37-
expect(team.account.stripe_customer_token).not_to be_nil
38-
expect(team.account.plan_ids).to eq([])
31+
# TODO: Refactor api calls to Sidekiq job
32+
VCR.use_cassette("Account") do
33+
34+
expect(team.account).to be_nil
35+
team.build_account(account)
36+
team.account.admin_id = admin.id
37+
team.account.save_with_payment
38+
team.reload
39+
expect(team.account.stripe_card_token).to eq(account[:stripe_card_token])
40+
expect(team.account.stripe_customer_token).not_to be_nil
41+
expect(team.account.plan_ids).to eq([])
42+
43+
end
3944
end
4045

4146
it 'should still create an account if account admin not team admin' do
42-
team.build_account(account)
43-
some_random_user = Fabricate(:user)
44-
team.account.admin_id = some_random_user.id
45-
team.account.save_with_payment
46-
team.reload
47-
expect(team.account).not_to be_nil
47+
# TODO: Refactor api calls to Sidekiq job
48+
VCR.use_cassette("Account") do
49+
50+
team.build_account(account)
51+
some_random_user = Fabricate(:user)
52+
team.account.admin_id = some_random_user.id
53+
team.account.save_with_payment
54+
team.reload
55+
expect(team.account).not_to be_nil
56+
57+
end
4858
end
4959

5060
it 'should not create an account if stripe_card_token invalid' do
51-
account[:stripe_card_token] = "invalid"
52-
team.build_account(account)
53-
team.account.admin_id = admin.id
54-
team.account.save_with_payment
55-
team.reload
56-
expect(team.account).to be_nil
61+
# TODO: Refactor api calls to Sidekiq job
62+
VCR.use_cassette("Account") do
63+
64+
account[:stripe_card_token] = "invalid"
65+
team.build_account(account)
66+
team.account.admin_id = admin.id
67+
team.account.save_with_payment
68+
team.reload
69+
expect(team.account).to be_nil
70+
71+
end
5772
end
5873

5974
it 'should not allow stripe_customer_token or admin to be set/updated' do
60-
some_random_user = Fabricate(:user)
61-
account[:stripe_customer_token] = "invalid_customer_token"
62-
account[:admin_id] = some_random_user.id
63-
team.build_account(account)
64-
team.account.save_with_payment
65-
team.reload
66-
expect(team.account).to be_nil
75+
# TODO: Refactor api calls to Sidekiq job
76+
VCR.use_cassette("Account") do
77+
78+
some_random_user = Fabricate(:user)
79+
account[:stripe_customer_token] = "invalid_customer_token"
80+
account[:admin_id] = some_random_user.id
81+
team.build_account(account)
82+
team.account.save_with_payment
83+
team.reload
84+
expect(team.account).to be_nil
85+
86+
end
6787
end
6888
end
6989

@@ -74,12 +94,17 @@ def post_job_for(team)
7494

7595
describe 'free subscription' do
7696
before(:each) do
77-
expect(team.account).to be_nil
78-
team.build_account(account)
79-
team.account.admin_id = admin.id
80-
team.account.save_with_payment
81-
team.account.subscribe_to!(free_plan)
82-
team.reload
97+
# TODO: Refactor api calls to Sidekiq job
98+
VCR.use_cassette("Account") do
99+
100+
expect(team.account).to be_nil
101+
team.build_account(account)
102+
team.account.admin_id = admin.id
103+
team.account.save_with_payment
104+
team.account.subscribe_to!(free_plan)
105+
team.reload
106+
107+
end
83108
end
84109

85110
it 'should add a free subscription' do
@@ -88,41 +113,61 @@ def post_job_for(team)
88113
end
89114

90115
it 'should not allow any job posts' do
91-
expect(team.can_post_job?).to eq(false)
92-
expect(team.premium?).to eq(false)
93-
expect(team.valid_jobs?).to eq(false)
94-
expect { Fabricate(:opportunity, team_document_id: team.id) }.to raise_error(ActiveRecord::RecordNotSaved)
116+
# TODO: Refactor api calls to Sidekiq job
117+
VCR.use_cassette("Account") do
118+
119+
expect(team.can_post_job?).to eq(false)
120+
expect(team.premium?).to eq(false)
121+
expect(team.valid_jobs?).to eq(false)
122+
expect { Fabricate(:opportunity, team_document_id: team.id) }.to raise_error(ActiveRecord::RecordNotSaved)
123+
124+
end
95125
end
96126

97127
it 'should allow upgrade to monthly subscription' do
98-
team.account.save_with_payment(monthly_plan)
99-
team.reload
100-
expect(team.can_post_job?).to eq(true)
101-
expect(team.paid_job_posts).to eq(0)
102-
expect(team.valid_jobs?).to eq(true)
103-
expect(team.has_monthly_subscription?).to eq(true)
104-
expect(team.premium?).to eq(true)
128+
# TODO: Refactor api calls to Sidekiq job
129+
VCR.use_cassette("Account") do
130+
131+
team.account.save_with_payment(monthly_plan)
132+
team.reload
133+
expect(team.can_post_job?).to eq(true)
134+
expect(team.paid_job_posts).to eq(0)
135+
expect(team.valid_jobs?).to eq(true)
136+
expect(team.has_monthly_subscription?).to eq(true)
137+
expect(team.premium?).to eq(true)
138+
139+
end
105140
end
106141

107142
it 'should allow upgrade to one-time job post charge' do
108-
team.account.update_attributes({stripe_card_token: new_token})
109-
team.account.save_with_payment(onetime_plan)
110-
team.reload
111-
expect(team.can_post_job?).to eq(true)
112-
expect(team.valid_jobs?).to eq(true)
113-
expect(team.paid_job_posts).to eq(1)
114-
expect(team.premium?).to eq(true)
143+
# TODO: Refactor api calls to Sidekiq job
144+
VCR.use_cassette("Account") do
145+
146+
team.account.update_attributes({stripe_card_token: new_token})
147+
team.account.save_with_payment(onetime_plan)
148+
team.reload
149+
expect(team.can_post_job?).to eq(true)
150+
expect(team.valid_jobs?).to eq(true)
151+
expect(team.paid_job_posts).to eq(1)
152+
expect(team.premium?).to eq(true)
153+
154+
end
115155
end
116156
end
117157

118158
describe 'monthly paid subscription' do
119159
before(:each) do
120-
expect(team.account).to be_nil
121-
team.build_account(account)
122-
team.account.admin_id = admin.id
123-
team.account.save_with_payment
124-
team.account.subscribe_to!(monthly_plan)
125-
team.reload
160+
# TODO: Refactor api calls to Sidekiq job
161+
VCR.use_cassette("Account") do
162+
163+
expect(team.account).to be_nil
164+
team.build_account(account)
165+
team.account.admin_id = admin.id
166+
team.account.save_with_payment
167+
team.account.subscribe_to!(monthly_plan)
168+
team.reload
169+
170+
end
126171
end
127172

128173
it 'should add a paid monthly subscription' do
@@ -134,21 +179,31 @@ def post_job_for(team)
134179
end
135180

136181
it 'should allow unlimited job posts' do
137-
expect(team.can_post_job?).to eq(true)
138-
5.times do
139-
Fabricate(:opportunity, team_document_id: team.id)
140-
end
141-
expect(team.can_post_job?).to eq(true)
182+
# TODO: Refactor api calls to Sidekiq job
183+
VCR.use_cassette("Account") do
184+
185+
expect(team.can_post_job?).to eq(true)
186+
5.times do
187+
Fabricate(:opportunity, team_document_id: team.id)
188+
end
189+
expect(team.can_post_job?).to eq(true)
190+
191+
end
142192
end
143193
end
144194

145195
describe 'one-time job post charge' do
146196
before(:each) do
147-
expect(team.account).to be_nil
148-
team.build_account(account)
149-
team.account.admin_id = admin.id
150-
team.account.save_with_payment(onetime_plan)
151-
team.reload
197+
# TODO: Refactor api calls to Sidekiq job
198+
VCR.use_cassette("Account") do
199+
200+
expect(team.account).to be_nil
201+
team.build_account(account)
202+
team.account.admin_id = admin.id
203+
team.account.save_with_payment(onetime_plan)
204+
team.reload
205+
206+
end
152207
end
153208
it 'should add a one-time job post charge' do
154209
expect(team.account.plan_ids).to include(onetime_plan.id)
@@ -159,44 +214,59 @@ def post_job_for(team)
159214
end
160215

161216
it 'should allow only one job-post' do
162-
expect(team.can_post_job?).to eq(true)
163-
Fabricate(:opportunity, team_document_id: team.id)
164-
team.reload
165-
expect(team.paid_job_posts).to eq(0)
166-
expect(team.can_post_job?).to eq(false)
167-
expect { Fabricate(:opportunity, team_document_id: team.id) }.to raise_error(ActiveRecord::RecordNotSaved)
217+
# TODO: Refactor api calls to Sidekiq job
218+
VCR.use_cassette("Account") do
219+
220+
expect(team.can_post_job?).to eq(true)
221+
Fabricate(:opportunity, team_document_id: team.id)
222+
team.reload
223+
expect(team.paid_job_posts).to eq(0)
224+
expect(team.can_post_job?).to eq(false)
225+
expect { Fabricate(:opportunity, team_document_id: team.id) }.to raise_error(ActiveRecord::RecordNotSaved)
226+
227+
end
168228
end
169229

170230
it 'should allow upgrade to monthly subscription' do
171-
team.account.update_attributes({stripe_card_token: new_token})
172-
team.account.save_with_payment(monthly_plan)
173-
team.reload
174-
expect(team.can_post_job?).to eq(true)
175-
expect(team.valid_jobs?).to eq(true)
176-
expect(team.paid_job_posts).to eq(1)
177-
expect(team.has_monthly_subscription?).to eq(true)
178-
5.times do
179-
Fabricate(:opportunity, team_document_id: team.id)
231+
# TODO: Refactor api calls to Sidekiq job
232+
VCR.use_cassette("Account") do
233+
234+
team.account.update_attributes({stripe_card_token: new_token})
235+
team.account.save_with_payment(monthly_plan)
236+
team.reload
237+
expect(team.can_post_job?).to eq(true)
238+
expect(team.valid_jobs?).to eq(true)
239+
expect(team.paid_job_posts).to eq(1)
240+
expect(team.has_monthly_subscription?).to eq(true)
241+
5.times do
242+
Fabricate(:opportunity, team_document_id: team.id)
243+
end
244+
expect(team.can_post_job?).to eq(true)
245+
expect(team.paid_job_posts).to eq(1)
246+
expect(team.premium?).to eq(true)
247+
180248
end
181-
expect(team.can_post_job?).to eq(true)
182-
expect(team.paid_job_posts).to eq(1)
183-
expect(team.premium?).to eq(true)
184249
end
185250

186251
it 'should allow additional one time job post charges' do
187-
team.account.update_attributes({stripe_card_token: new_token})
188-
team.account.save_with_payment(onetime_plan)
189-
team.reload
190-
expect(team.paid_job_posts).to eq(2)
191-
expect(team.can_post_job?).to eq(true)
192-
2.times do
193-
Fabricate(:opportunity, team_document_id: team.id)
194-
end
195-
team.reload
196-
expect(team.paid_job_posts).to eq(0)
197-
expect(team.has_monthly_subscription?).to eq(false)
198-
expect(team.premium?).to eq(true)
199-
expect(team.valid_jobs?).to eq(true)
252+
# TODO: Refactor api calls to Sidekiq job
253+
VCR.use_cassette("Account") do
254+
255+
team.account.update_attributes({stripe_card_token: new_token})
256+
team.account.save_with_payment(onetime_plan)
257+
team.reload
258+
expect(team.paid_job_posts).to eq(2)
259+
expect(team.can_post_job?).to eq(true)
260+
2.times do
261+
Fabricate(:opportunity, team_document_id: team.id)
262+
end
263+
team.reload
264+
expect(team.paid_job_posts).to eq(0)
265+
expect(team.has_monthly_subscription?).to eq(false)
266+
expect(team.premium?).to eq(true)
267+
expect(team.valid_jobs?).to eq(true)
268+
269+
end
200270
end
201271
end
202272
end

spec/models/badges/ashcat_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
it 'creates facts for each contributor' do
66
VCR.use_cassette('ashcat_creates_facts_for_each_contributor') do
7+
# TODO: Refactor to utilize sidekiq job
78
Ashcat.perform
89

910
contributor.build_github_facts

spec/models/github_profile_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'vcr_helper'
22

3+
# TODO: Deprecate GithubOld, and related testing
34
RSpec.describe GithubProfile, :type => :model, skip: ENV['TRAVIS'] do
45
let(:languages) {
56
{

0 commit comments

Comments
 (0)