Skip to content

Commit 54d05d7

Browse files
committed
Add specs
1 parent 6e8f8da commit 54d05d7

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def purchase_addon(subscription_id, addon_config_id)
126126
end
127127

128128
def v2_subscription_user_usages(subscription_id)
129-
connection.get("/v2/subscription/#{subscription_id}/user_usage").body.map do |usage_data|
129+
connection.get("/v2/subscriptions/#{subscription_id}/user_usage").body.map do |usage_data|
130130
Travis::API::V3::Models::V2AddonUsage.new(usage_data)
131131
end
132132
end
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+
client = BillingClient.new(user_id)
5+
if params['subscription.id']
6+
client.v2_subscription_user_usages(params['subscription.id'])
7+
end
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
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Travis::API::V3
22
class Services::V2Subscription::UserUsages < Service
3-
result_type :invoices
3+
params :subscription_id
4+
result_type :v2_addon_usages
45

56
def run!
67
raise LoginRequired unless access_control.full_access_or_logged_in?
7-
result query.user_usages(access_control.user.id)
8+
result query(:v2_addon_usages).all(access_control.user.id)
89
end
910
end
1011
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,49 @@
360360
end
361361
end
362362

363+
describe '#v2_plans_for' do
364+
describe '#organization' do
365+
subject { billing.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).to exist
374+
expect(subject.first.available_standalone_addons).to exist
375+
end
376+
end
377+
378+
describe '#user' do
379+
subject { billing.plans_for_user }
380+
381+
it 'returns the list of v2 plans for an user' do
382+
stub_request(:get, "#{billing_url}v2/plans_for/user").with(basic_auth: ['_', auth_key], headers: { 'X-Travis-User-Id' => user_id })
383+
.to_return(body: JSON.dump([billing_v2_plan_response_body('id' => 'plan-id')]))
384+
385+
expect(subject.size).to eq 1
386+
expect(subject.first.id).to eq('plan-id')
387+
expect(subject.first.addon_configs).to exist
388+
expect(subject.first.available_standalone_addons).to exist
389+
end
390+
end
391+
end
392+
393+
describe '#v2_subscription_user_usages' do
394+
subject { billing.v2_subscription_user_usages(subscription_id)}
395+
396+
it 'returns the list of user license usages for the subscription' do
397+
stub_request(:get, "#{billing_url}v2/subscription/#{subscription_id}/user_usage").with(basic_auth: ['_', auth_key], headers: { 'X-Travis-User-Id' => user_id })
398+
.to_return(body: JSON.dump([[billing_addon_usage_response_body]]))
399+
400+
expect(subject.size).to eq 1
401+
expect(subject.addon_quantity).to eq 100
402+
expect(subject.addon_usage).to eq 0
403+
end
404+
end
405+
363406
describe '#update_organization_billing_permission' do
364407
let(:billing_admin_only) { { billing_admin_only: true } }
365408
subject { billing.update_organization_billing_permission(organization.id, billing_admin_only) }

0 commit comments

Comments
 (0)