Skip to content

Commit 808d549

Browse files
author
Shairyar Baig
authored
Merge pull request #1172 from travis-ci/ta-fix-restart-build-breach
Fix build restart subscription/plan check
2 parents 597bb9d + d924aca commit 808d549

File tree

4 files changed

+99
-36
lines changed

4 files changed

+99
-36
lines changed

lib/travis/api/enqueue/services/restart_model.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ def billing?
4646
false
4747
rescue Travis::API::V3::NotFound
4848
# Owner is on a legacy plan
49-
true
49+
if subscription&.active?
50+
true
51+
else
52+
@cause_of_denial = 'You do not seem to have active subscription.'
53+
false
54+
end
5055
end
5156
end
5257

@@ -77,6 +82,10 @@ def target
7782

7883
private
7984

85+
def subscription
86+
Subscription.where(owner: repository.owner)&.first
87+
end
88+
8089
def permission?
8190
current_user && current_user.permission?(required_role, repository_id: target.repository_id) && !abusive? && build_permission?
8291
end

lib/travis/testing/factories.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@
2525
compare_url { 'https://github.com/svenfuchs/minimal/compare/master...develop' }
2626
end
2727

28+
factory :subscription do
29+
association :owner, factory: :user
30+
valid_to { Time.now.utc + 1.week }
31+
customer_id { 'cus_123' }
32+
billing_email { '[email protected]' }
33+
cc_last_digits { 111 }
34+
status { 'subscribed'}
35+
source { 'stripe' }
36+
cc_token { 'token_123' }
37+
selected_plan { 'travis-ci-one-build' }
38+
country { 'Germany' }
39+
40+
factory :valid_stripe_subs do
41+
status { 'subscribed' }
42+
source { 'stripe' }
43+
end
44+
45+
factory :canceled_stripe_subs do
46+
status { 'canceled' }
47+
source { 'stripe' }
48+
end
49+
end
50+
2851
factory :test, :class => 'Job::Test', aliases: [:job] do
2952
owner { User.first || FactoryBot.create(:user) }
3053
repository { Repository.first || FactoryBot.create(:repository) }

spec/lib/travis/api/enqueue/services/restart_model_spec.rb

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
let(:repository) { FactoryBot.create(:repository, owner: owner) }
44
let(:job) { FactoryBot.create(:job, repository: repository, state: 'canceled') }
55
let(:user) { FactoryBot.create(:user) }
6+
let(:subscription) { nil }
67

78
let(:service) { Travis::Enqueue::Services::RestartModel.new(user, { job_id: job.id }) }
89

910
before do
1011
Travis.config.billing.url = 'http://localhost:9292/'
1112
Travis.config.billing.auth_key = 'secret'
12-
13-
stub_request(:post, /http:\/\/localhost:9292\/(users|organizations)\/(.+)\/authorize_build/).to_return(
14-
body: MultiJson.dump(allowed: true, rejection_code: nil)
15-
)
1613
end
1714

1815
after do
@@ -39,54 +36,88 @@
3936
end
4037
end
4138

42-
context 'build permissions' do
43-
context 'when owner is a user' do
44-
context 'on repo level' do
45-
context 'when value is nil' do
46-
before { repository.permissions.create(user: user, build: nil) }
39+
context 'when owner active plan' do
40+
before do
41+
stub_request(:post, /http:\/\/localhost:9292\/(users|organizations)\/(.+)\/authorize_build/).to_return(
42+
body: MultiJson.dump(allowed: true, rejection_code: nil)
43+
)
44+
end
45+
context 'build permissions' do
46+
context 'when owner is a user' do
47+
context 'on repo level' do
48+
context 'when value is nil' do
49+
before { repository.permissions.create(user: user, build: nil) }
4750

48-
include_examples 'restarts the job'
49-
end
51+
include_examples 'restarts the job'
52+
end
5053

51-
context 'when value is true' do
52-
before { repository.permissions.create(user: user, build: true) }
54+
context 'when value is true' do
55+
before { repository.permissions.create(user: user, build: true) }
5356

54-
include_examples 'restarts the job'
55-
end
57+
include_examples 'restarts the job'
58+
end
5659

57-
context 'when value is false' do
58-
before { repository.permissions.create(user: user, build: false) }
60+
context 'when value is false' do
61+
before { repository.permissions.create(user: user, build: false) }
5962

60-
include_examples 'does not restart the job'
63+
include_examples 'does not restart the job'
64+
end
6165
end
6266
end
63-
end
6467

65-
context 'when owner is an organization' do
66-
let(:owner) { FactoryBot.create(:org) }
68+
context 'when owner is an organization' do
69+
let(:owner) { FactoryBot.create(:org) }
6770

68-
before { repository.permissions.create(user: user, build: true) }
71+
before { repository.permissions.create(user: user, build: true) }
6972

70-
context 'on organization level' do
71-
context 'when value is nil' do
72-
before { owner.memberships.create(user: user, build_permission: nil) }
73+
context 'on organization level' do
74+
context 'when value is nil' do
75+
before { owner.memberships.create(user: user, build_permission: nil) }
7376

74-
include_examples 'restarts the job'
75-
end
77+
include_examples 'restarts the job'
78+
end
7679

77-
context 'when value is true' do
78-
before { owner.memberships.create(user: user, build_permission: true) }
80+
context 'when value is true' do
81+
before { owner.memberships.create(user: user, build_permission: true) }
7982

80-
include_examples 'restarts the job'
81-
end
83+
include_examples 'restarts the job'
84+
end
8285

83-
context 'when value is false' do
84-
before { owner.memberships.create(user: user, build_permission: false) }
86+
context 'when value is false' do
87+
before { owner.memberships.create(user: user, build_permission: false) }
8588

86-
include_examples 'does not restart the job'
89+
include_examples 'does not restart the job'
90+
end
8791
end
8892
end
8993
end
9094
end
95+
96+
context 'when customer does not have active plan' do
97+
before do
98+
stub_request(:post, /http:\/\/localhost:9292\/(users|organizations)\/(.+)\/authorize_build/)
99+
.to_return(status: 404, body: JSON.dump(error: 'Not Found'))
100+
end
101+
102+
context 'when customer has no old subscription' do
103+
include_examples 'does not restart the job'
104+
end
105+
106+
context 'when customer has an old active subscription' do
107+
before do
108+
repository.permissions.create(user: user, build: true)
109+
FactoryBot.create(:valid_stripe_subs, owner: owner)
110+
end
111+
112+
include_examples 'restarts the job'
113+
end
114+
115+
context 'when customer has an old canceled subscription' do
116+
let(:subscription) { FactoryBot.create(:canceled_stripe_subs, owner: owner) }
117+
118+
include_examples 'does not restart the job'
119+
end
120+
121+
end
91122
end
92123
end

spec/v3/services/job/restart_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242

243243
it 'restarts the job' do
244244
post("/v3/job/#{job.id}/restart", params, headers)
245-
expect(last_response.status).to eq(202)
245+
expect(last_response.status).to eq(403)
246246
end
247247
end
248248

0 commit comments

Comments
 (0)