Skip to content

Commit 4de3fce

Browse files
authored
Initial v2 subscription for new users (#1131)
Create initial subscription on user creation
1 parent bca4cce commit 4de3fce

File tree

7 files changed

+33
-4
lines changed

7 files changed

+33
-4
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: ruby
22

33
import:
4-
- travis-ci/build-configs:db-setup.yml
4+
- travis-ci/build-configs:db-setup.yml@sb-remove-user-travis
55

66
rvm: 2.6.5
77

lib/travis/api/app/endpoint/authorization.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class Authorization < Endpoint
134134
def update_first_login(user)
135135
unless user.first_logged_in_at
136136
user.update_attributes(first_logged_in_at: Time.now)
137+
user.create_initial_subscription unless Travis.config.org?
137138
end
138139
end
139140

@@ -209,7 +210,7 @@ def vcs_handshake
209210
redirect to(vcs_data['redirect_uri'])
210211
return
211212
end
212-
213+
update_first_login(user)
213214
yield serialize_user(User.find(vcs_data['user']['id'])), vcs_data['token'], payload(params[:provider])
214215
else
215216
state = vcs_create_state(params[:origin] || params[:redirect_uri])

lib/travis/api/v3/billing_client.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def create_subscription(subscription_data)
8989
handle_subscription_response(response)
9090
end
9191

92+
def create_initial_v2_subscription
93+
response = connection.post('/v2/initial_subscription')
94+
handle_v2_subscription_response(response)
95+
end
96+
9297
def create_v2_subscription(subscription_data)
9398
response = connection.post('/v2/subscriptions', subscription_data)
9499
handle_v2_subscription_response(response)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,10 @@ def installation
8282
def github?
8383
vcs_type == 'GithubUser'
8484
end
85+
86+
def create_initial_subscription
87+
client = BillingClient.new(id)
88+
client.create_initial_v2_subscription
89+
end
8590
end
8691
end

lib/travis/model/user.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ def github?
170170
vcs_type == 'GithubUser'
171171
end
172172

173+
def create_initial_subscription
174+
client = Travis::API::V3::BillingClient.new(id)
175+
client.create_initial_v2_subscription
176+
end
177+
173178
protected
174179

175180
def track_previous_changes

spec/unit/endpoint/authorization/user_manager_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe Travis::Api::App::Endpoint::Authorization::UserManager do
1+
describe Travis::Api::App::Endpoint::Authorization::UserManager, billing_spec_helper: true do
22
let(:manager) { described_class.new(data, 'abc123') }
33

44
before do
@@ -51,9 +51,15 @@
5151
context 'with existing user' do
5252
let!(:user) { FactoryBot.create(:user, login: 'drogus', github_id: 456, github_oauth_token: token) }
5353
let(:token) { nil }
54+
let(:billing_url) { 'http://billingfake.travis-ci.com' }
55+
let(:billing_auth_key) { 'secret' }
5456

5557
before do
5658
allow(manager).to receive(:education).and_return(false)
59+
Travis.config.billing.url = billing_url
60+
Travis.config.billing.auth_key = billing_auth_key
61+
stubbed_request = stub_billing_request(:post, "/v2/initial_subscription", auth_key: billing_auth_key, user_id: user.id)
62+
.to_return(status: 201, body: JSON.dump(billing_v2_subscription_response_body('id' => 456, 'owner' => { 'type' => 'User', 'id' => user.id })))
5763
end
5864

5965
context 'without any User#tokens record' do

spec/unit/endpoint/authorization_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
describe Travis::Api::App::Endpoint::Authorization do
1+
describe Travis::Api::App::Endpoint::Authorization, billing_spec_helper: true do
22
include Travis::Testing::Stubs
33

4+
let(:billing_url) { 'http://billingfake.travis-ci.com' }
5+
let(:billing_auth_key) { 'secret' }
6+
47
before do
58
add_endpoint '/info' do
69
get '/login', scope: :private do
@@ -21,6 +24,10 @@
2124
scope: 'public_repo,user:email,new_scope',
2225
insufficient_access_redirect_url: 'https://travis-ci.org/insufficient_access'
2326
}
27+
Travis.config.billing.url = billing_url
28+
Travis.config.billing.auth_key = billing_auth_key
29+
WebMock.stub_request(:post, 'http://billingfake.travis-ci.com/v2/initial_subscription')
30+
.to_return(status: 200, body: JSON.dump(billing_v2_subscription_response_body('id' => 456, 'owner' => { 'type' => 'User', 'id' => user.id })))
2431
end
2532

2633
after do

0 commit comments

Comments
 (0)