Skip to content

Commit feec1cf

Browse files
Merge branch 'master' into deploy
2 parents 4d41016 + 3d285b9 commit feec1cf

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ def get_coupon(code)
9090
handle_coupon_response(response)
9191
end
9292

93+
def update_organization_billing_permission(organization_id, billing_admin_only)
94+
response = connection.patch("/organization/permission_update/#{organization_id}", billing_admin_only)
95+
handle_subscription_response(response)
96+
end
97+
9398
private
9499

95100
def handle_subscription_response(response)

lib/travis/api/v3/queries/organization.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ def find
1313
raise WrongParams, 'missing organization.id or organization.login'.freeze
1414
end
1515

16+
def update_billing_permission(user_id)
17+
data = params.dup.tap { |h| h.delete('organization.id') }
18+
client = BillingClient.new(user_id)
19+
client.update_organization_billing_permission(params['organization.id'], data)
20+
end
21+
1622
private
1723

1824
def provider

lib/travis/api/v3/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ module Routes
8383
capture id: :digit
8484
route '/org/{organization.id}'
8585
get :find
86+
patch :update_billing_permission, '/update_billing_permission'
8687

8788
resource :preferences do
8889
route '/preferences'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Travis::API::V3
2+
class Services::Organization::UpdateBillingPermission < Service
3+
params :billing_admin_only
4+
5+
def run!
6+
raise LoginRequired unless access_control.full_access_or_logged_in?
7+
query.update_billing_permission(access_control.user.id)
8+
no_content
9+
end
10+
end
11+
end

spec/v3/billing_client_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,18 @@
230230
end
231231
end
232232
end
233+
234+
describe '#update_organization_billing_permission' do
235+
let(:billing_admin_only) { { billing_admin_only: true } }
236+
subject { billing.update_organization_billing_permission(organization.id, billing_admin_only) }
237+
238+
it 'requests the update' do
239+
stubbed_request = stub_billing_request(:patch, "/organization/permission_update/#{organization.id}", auth_key: auth_key, user_id: user_id)
240+
.with(body: JSON.dump(billing_admin_only))
241+
.to_return(status: 204)
242+
243+
expect { subject }.to_not raise_error
244+
expect(stubbed_request).to have_been_made
245+
end
246+
end
233247
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
describe Travis::API::V3::Services::Organization::UpdateBillingPermission, set_app: true, billing_spec_helper: true do
2+
let(:billing_url) { 'http://billingfake.travis-ci.com' }
3+
let(:billing_auth_key) { 'secret' }
4+
let(:organization_id) { rand(999) }
5+
6+
before do
7+
Travis.config.billing.url = billing_url
8+
Travis.config.billing.auth_key = billing_auth_key
9+
end
10+
11+
context 'unauthenticated' do
12+
it 'responds 403' do
13+
patch("/v3/org/#{organization_id}/update_billing_permission", { })
14+
15+
expect(last_response.status).to eq(403)
16+
end
17+
end
18+
19+
context 'authenticated' do
20+
let(:user) { FactoryBot.create(:user) }
21+
let(:token) { Travis::Api::App::AccessToken.create(user: user, app_id: 1) }
22+
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}",
23+
'CONTENT_TYPE' => 'application/json' }}
24+
let(:billing_admin_only) { { billing_admin_only: true } }
25+
26+
27+
let!(:stubbed_request) do
28+
stub_billing_request(:patch, "/organization/permission_update/#{organization_id}", auth_key: billing_auth_key, user_id: user.id)
29+
.with(body: JSON.dump(billing_admin_only))
30+
.to_return(status: 204)
31+
end
32+
33+
it 'updates the billing permission on organization' do
34+
patch("/v3/org/#{organization_id}/update_billing_permission", JSON.generate(billing_admin_only), headers)
35+
36+
expect(last_response.status).to eq(204)
37+
expect(stubbed_request).to have_been_made.once
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)