Skip to content

Commit 88c67f5

Browse files
GbArcgbarc-dt
andauthored
[TBT-269] - Plans usage - bug in user credits consumption (#1353)
* added internal field to user * extended internal users list - added login --------- Co-authored-by: GbArc <[email protected]>
1 parent f712ac3 commit 88c67f5

File tree

12 files changed

+188
-12
lines changed

12 files changed

+188
-12
lines changed

lib/travis/api/v3/models/user.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ def installation
8787
@installation = Models::Installation.find_by(owner_type: 'User', owner_id: id, removed_by_id: nil)
8888
end
8989

90+
def internal?
91+
!!get_internal_user
92+
end
93+
94+
def get_internal_user
95+
Travis.config[:internal_users]&.find { |item| item[:id] == id }
96+
end
97+
98+
def login
99+
read_attribute(:login) || get_internal_user&.dig(:login)
100+
end
101+
90102
def github?
91103
vcs_type == 'GithubUser'
92104
end

lib/travis/api/v3/renderer/user.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Travis::API::V3
44
class Renderer::User < Renderer::Owner
5-
representation(:standard, :email, :is_syncing, :synced_at, :recently_signed_up, :secure_user_hash, :ro_mode, :confirmed_at, :custom_keys)
5+
representation(:standard, :email, :is_syncing, :synced_at, :recently_signed_up, :secure_user_hash, :ro_mode, :confirmed_at, :custom_keys, :internal)
66
representation(:additional, :emails, :collaborator)
77

88
def email
@@ -17,6 +17,10 @@ def collaborator
1717
query(:user).collaborator? @model.id
1818
end
1919

20+
def internal
21+
@model.internal?
22+
end
23+
2024
def secure_user_hash
2125
hmac_secret_key = Travis.config.intercom && Travis.config.intercom.hmac_secret_key.to_s
2226
OpenSSL::HMAC.hexdigest('sha256', hmac_secret_key, @model.id.to_s) if @model.id && hmac_secret_key

lib/travis/config/defaults.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ def fallback_logs_api_auth_token
105105
authorizer: { url: 'http://authorizer', auth_key: 'secret' },
106106
recaptcha: { endpoint: 'https://www.google.com', secret: ENV['RECAPTCHA_SECRET_KEY'] || '' },
107107
antifraud: { captcha_max_failed_attempts: 3, captcha_block_duration: 24, credit_card_max_failed_attempts: 3, credit_card_block_duration: 24 },
108-
legacy_roles: false
108+
legacy_roles: false,
109+
internal_users: [{id: 0, login: 'cron'}]
109110

110111
default :_access => [:key]
111112

spec/support/billing_spec_helper.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,5 +362,48 @@ def billing_executions_response_body(attributes = {})
362362
'updated_at' => Time.now.utc.iso8601
363363
}.deep_merge(attributes)
364364
end
365+
366+
def billing_executions_multiple_response_body(attributes = {})
367+
[{
368+
'id' => 1,
369+
'os' => 'linux',
370+
'instance_size' => 'standard-2',
371+
'arch' => 'amd64',
372+
'virtualization_type' => 'vm',
373+
'queue' => 'builds.gce-oss',
374+
'job_id' => 123,
375+
'repository_id' => 1,
376+
'owner_id' => 1,
377+
'owner_type' => 'User',
378+
'plan_id' => 2,
379+
'sender_id' => 1,
380+
'credits_consumed' => 5,
381+
'user_license_credits_consumed' => 4,
382+
'started_at' => Time.now.utc.iso8601,
383+
'finished_at' => (Time.now. + 10.minutes).utc.iso8601,
384+
'created_at' => Time.now.utc.iso8601,
385+
'updated_at' => Time.now.utc.iso8601
386+
}.deep_merge(attributes),
387+
{
388+
'id' => 2,
389+
'os' => 'linux',
390+
'instance_size' => 'standard-2',
391+
'arch' => 'amd64',
392+
'virtualization_type' => 'vm',
393+
'queue' => 'builds.gce-oss',
394+
'job_id' => 123,
395+
'repository_id' => 1,
396+
'owner_id' => 1,
397+
'owner_type' => 'User',
398+
'plan_id' => 2,
399+
'sender_id' => 0,
400+
'credits_consumed' => 5,
401+
'user_license_credits_consumed' => 4,
402+
'started_at' => Time.now.utc.iso8601,
403+
'finished_at' => (Time.now. + 10.minutes).utc.iso8601,
404+
'created_at' => Time.now.utc.iso8601,
405+
'updated_at' => Time.now.utc.iso8601
406+
}]
407+
end
365408
end
366409
end

spec/v3/services/build/find_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99

1010
let(:authorization) { { 'permissions' => ['repository_state_update', 'repository_build_create', 'repository_settings_create', 'repository_settings_update', 'repository_cache_view', 'repository_cache_delete', 'repository_settings_delete', 'repository_log_view', 'repository_log_delete', 'repository_build_cancel', 'repository_build_debug', 'repository_build_restart', 'repository_settings_read', 'repository_scans_view'] } }
1111

12-
before { stub_request(:get, %r((.+)/permissions/repo/(.+))).to_return(status: 200, body: JSON.generate(authorization)) }
1312

1413
before do
14+
stub_request(:get, %r((.+)/permissions/repo/(.+))).to_return(status: 200, body: JSON.generate(authorization))
15+
stub_request(:post, 'http://billingfake.travis-ci.com/usage/stats')
16+
.with(body: "{\"owners\":[{\"id\":1,\"type\":\"User\"}],\"query\":\"trial_allowed\"}")
17+
.to_return(status: 200, body: "{\"trial_allowed\": false }", headers: {})
1518
build.update(sender_id: repo.owner.id, sender_type: 'User')
1619
test = build.stages.create(number: 1, name: 'test')
1720
deploy = build.stages.create(number: 2, name: 'deploy')

spec/v3/services/installation/find_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"recently_signed_up" => false,
7373
"secure_user_hash" => nil,
7474
"trial_allowed" => false,
75+
"internal" => false,
7576
"ro_mode" => true,
7677
"confirmed_at" => nil,
7778
}

spec/v3/services/owner/find_spec.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@
345345
"github_id" => 1234,
346346
"vcs_id" => org.vcs_id,
347347
"vcs_type" => org.vcs_type,
348-
"trial_allowed" => false,
348+
"trial_allowed" => false,
349349
"ro_mode" => true,
350350
"avatar_url" => nil,
351351
"education" => false,
@@ -392,7 +392,7 @@
392392
"github_id" => 1234,
393393
"vcs_id" => org.vcs_id,
394394
"vcs_type" => org.vcs_type,
395-
"trial_allowed" => false,
395+
"trial_allowed" => false,
396396
"ro_mode" => true,
397397
"avatar_url" => nil,
398398
"education" => false,
@@ -445,7 +445,8 @@
445445
"custom_keys" => [],
446446
"recently_signed_up"=>false,
447447
"secure_user_hash" => nil,
448-
"trial_allowed" => false,
448+
"trial_allowed" => false,
449+
"internal" => false,
449450
"ro_mode" => false,
450451
"confirmed_at" => nil,
451452
}}
@@ -479,7 +480,8 @@
479480
"custom_keys" => [],
480481
"recently_signed_up"=>false,
481482
"secure_user_hash" => nil,
482-
"trial_allowed" => false,
483+
"trial_allowed" => false,
484+
"internal" => false,
483485
"ro_mode" => false,
484486
"confirmed_at" => nil,
485487
}}
@@ -513,7 +515,8 @@
513515
"custom_keys" => [],
514516
"recently_signed_up"=>false,
515517
"secure_user_hash" => nil,
516-
"trial_allowed" => false,
518+
"trial_allowed" => false,
519+
"internal" => false,
517520
"ro_mode" => false,
518521
"confirmed_at" => nil,
519522
}}
@@ -554,7 +557,8 @@
554557
"custom_keys" => [],
555558
"recently_signed_up"=>false,
556559
"secure_user_hash" => nil,
557-
"trial_allowed" => false,
560+
"trial_allowed" => false,
561+
"internal" => false,
558562
"ro_mode" => false,
559563
"confirmed_at" => nil,
560564
"@warnings" => [{

spec/v3/services/repository/find_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99

1010
let(:authorization_role) { { 'roles' => ['repository_admin'] } }
1111

12-
before { stub_request(:get, %r((.+)/permissions/repo/(.+))).to_return(status: 200, body: JSON.generate(authorization)) }
13-
before { stub_request(:get, %r((.+)/roles/repo/(.+))).to_return(status: 200, body: JSON.generate(authorization_role)) }
14-
12+
before do
13+
stub_request(:get, %r((.+)/permissions/repo/(.+))).to_return(status: 200, body: JSON.generate(authorization))
14+
stub_request(:get, %r((.+)/roles/repo/(.+))).to_return(status: 200, body: JSON.generate(authorization_role))
15+
stub_request(:post, 'http://billingfake.travis-ci.com/usage/stats')
16+
.with(body: "{\"owners\":[{\"id\":1,\"type\":\"User\"}],\"query\":\"trial_allowed\"}")
17+
.to_return(status: 200, body: "{\"trial_allowed\": false }", headers: {})
18+
end
1519

1620
let(:permissions) do
1721
{

spec/v3/services/user/current_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"education" => nil,
2727
"allow_migration" => false,
2828
"trial_allowed" => false,
29+
"internal" => false,
2930
"allowance" => {
3031
"@type" => "allowance",
3132
"@representation" => "minimal",

spec/v3/services/user/find_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"education" => true,
4242
"allow_migration" => false,
4343
"trial_allowed" => false,
44+
"internal" => false,
4445
"allowance" => {
4546
"@type" => "allowance",
4647
"@representation" => "minimal",
@@ -82,6 +83,7 @@
8283
"education" => true,
8384
"allow_migration" => false,
8485
"trial_allowed" => false,
86+
"internal" => false,
8587
"allowance" => {
8688
"@type" => "allowance",
8789
"@representation" => "minimal",
@@ -123,6 +125,7 @@
123125
"education" => true,
124126
"allow_migration" => false,
125127
"trial_allowed" => false,
128+
"internal" => false,
126129
"allowance" => {
127130
"@type" => "allowance",
128131
"@representation" => "minimal",

0 commit comments

Comments
 (0)