Skip to content

Commit 46e825b

Browse files
authored
Merge pull request #1143 from AndriiMysko/am-get-user-usage
Retrieve user license usage api point
2 parents c7d95a0 + 733db44 commit 46e825b

File tree

7 files changed

+127
-0
lines changed

7 files changed

+127
-0
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ def purchase_addon(subscription_id, addon_config_id)
125125
handle_v2_subscription_response(response)
126126
end
127127

128+
def v2_subscription_user_usages(subscription_id)
129+
connection.get("/v2/subscriptions/#{subscription_id}/user_usage").body.map do |usage_data|
130+
Travis::API::V3::Models::V2AddonUsage.new(usage_data)
131+
end
132+
end
133+
128134
def v2_plans_for_organization(organization_id)
129135
connection.get("/v2/plans_for/organization/#{organization_id}").body.map do |plan_data|
130136
Travis::API::V3::Models::V2PlanConfig.new(plan_data)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Travis::API::V3
2+
class Queries::V2AddonUsages < Query
3+
def all(user_id)
4+
return unless params['subscription.id']
5+
6+
client = BillingClient.new(user_id)
7+
client.v2_subscription_user_usages(params['subscription.id'])
8+
end
9+
end
10+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Travis::API::V3
2+
class Renderer::V2AddonUsages < CollectionRenderer
3+
type :v2_addon_usages
4+
collection_key :v2_addon_usages
5+
end
6+
end

lib/travis/api/v3/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ module Routes
326326
patch :update_plan, '/plan'
327327
post :pay, '/pay'
328328
post :buy_addon, '/addon/{addon.id}'
329+
get :user_usages, '/user_usages'
329330
get :invoices, '/invoices'
330331
end
331332

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::V2Subscription::UserUsages < Service
3+
params :subscription_id
4+
result_type :v2_addon_usages
5+
6+
def run!
7+
raise LoginRequired unless access_control.full_access_or_logged_in?
8+
result query(:v2_addon_usages).all(access_control.user.id)
9+
end
10+
end
11+
end

spec/support/billing_spec_helper.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,22 @@ def billing_addons_response_body
160160
]
161161
end
162162

163+
def billing_addon_usage_response_body(attributes = {})
164+
{
165+
'id' => 1,
166+
'addon_id' => 1,
167+
'addon_quantity' => 100,
168+
'addon_usage' => 0,
169+
'remaining' => 100,
170+
'purchase_date' => '2020-09-14T11:25:02.612Z',
171+
'valid_to' => '2020-10-14T11:25:02.612Z',
172+
'status' => 'subscribed',
173+
'active' => true,
174+
'created_at' => '2020-09-14T11:25:02.614Z',
175+
'updated_at' => '2020-09-14T11:25:02.614Z'
176+
}.deep_merge(attributes)
177+
end
178+
163179
def billing_plan_response_body(attributes={})
164180
{
165181
"id" => "travis-ci-ten-builds",
@@ -171,6 +187,42 @@ def billing_plan_response_body(attributes={})
171187
}.deep_merge(attributes)
172188
end
173189

190+
def billing_v2_plan_response_body(attributes = {})
191+
{
192+
'id' => 'free_tier_plan',
193+
'name' => 'Free Tier Plan',
194+
'private_repos' => true,
195+
'addon_configs' => [
196+
{
197+
'id' => 'oss_tier_credits',
198+
'name' => 'Free 40 000 credits (renewed monthly)',
199+
'price' => 0,
200+
'quantity' => 40_000,
201+
'type' => 'credit_public'
202+
},
203+
{
204+
'id' => 'free_tier_credits',
205+
'name' => 'Free 10 000 credits (renewed monthly)',
206+
'price' => 0,
207+
'quantity' => 10_000,
208+
'type' => 'credit_private'
209+
},
210+
{
211+
'id' => 'users_free',
212+
'name' => 'Unlimited users',
213+
'price' => 0,
214+
'quantity' => 999_999,
215+
'type' => 'user_license'
216+
}
217+
],
218+
'starting_price' => 0,
219+
'starting_users' => 999_999,
220+
'private_credits' => 10_000,
221+
'public_credits' => 40_000,
222+
'available_standalone_addons' => []
223+
}.deep_merge(attributes)
224+
end
225+
174226
def billing_coupon_response_body(attributes = {})
175227
{
176228
"id" => "10_BUCKS_OFF",

spec/v3/billing_client_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,47 @@
360360
end
361361
end
362362

363+
describe '#v2_plans_for' do
364+
describe '#organization' do
365+
subject { billing.v2_plans_for_organization(organization.id) }
366+
367+
it 'returns the list of v2 plans for an organization' do
368+
stub_request(:get, "#{billing_url}v2/plans_for/organization/#{organization.id}").with(basic_auth: ['_', auth_key], headers: { 'X-Travis-User-Id' => user_id })
369+
.to_return(body: JSON.dump([billing_v2_plan_response_body('id' => 'plan-id')]))
370+
371+
expect(subject.size).to eq 1
372+
expect(subject.first.id).to eq('plan-id')
373+
expect(subject.first.addon_configs.first['id']).to eq('oss_tier_credits')
374+
end
375+
end
376+
377+
describe '#user' do
378+
subject { billing.v2_plans_for_user }
379+
380+
it 'returns the list of v2 plans for an user' do
381+
stub_request(:get, "#{billing_url}v2/plans_for/user").with(basic_auth: ['_', auth_key], headers: { 'X-Travis-User-Id' => user_id })
382+
.to_return(body: JSON.dump([billing_v2_plan_response_body('id' => 'plan-id')]))
383+
384+
expect(subject.size).to eq 1
385+
expect(subject.first.id).to eq('plan-id')
386+
expect(subject.first.addon_configs.first['id']).to eq('oss_tier_credits')
387+
end
388+
end
389+
end
390+
391+
describe '#v2_subscription_user_usages' do
392+
subject { billing.v2_subscription_user_usages(subscription_id)}
393+
394+
it 'returns the list of user license usages for the subscription' do
395+
stub_request(:get, "#{billing_url}v2/subscriptions/#{subscription_id}/user_usage").with(basic_auth: ['_', auth_key], headers: { 'X-Travis-User-Id' => user_id })
396+
.to_return(body: JSON.dump([billing_addon_usage_response_body]))
397+
398+
expect(subject.size).to eq 1
399+
expect(subject.first.addon_quantity).to eq 100
400+
expect(subject.first.addon_usage).to eq 0
401+
end
402+
end
403+
363404
describe '#update_organization_billing_permission' do
364405
let(:billing_admin_only) { { billing_admin_only: true } }
365406
subject { billing.update_organization_billing_permission(organization.id, billing_admin_only) }

0 commit comments

Comments
 (0)