Skip to content

Commit 5fba5a4

Browse files
authored
Confirm user account endpoints
1 parent f79bd83 commit 5fba5a4

File tree

12 files changed

+141
-4
lines changed

12 files changed

+141
-4
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ class Authorization < Endpoint
127127
halt 401, 'could not resolve github token'
128128
end
129129

130+
get '/confirm_user/:token' do
131+
content_type :json
132+
Travis::RemoteVCS::User.new.confirm_user(token: params[:token])
133+
{ status: 200 }.to_json
134+
rescue Travis::RemoteVCS::ResponseError
135+
halt 404, 'The token is expired or not found.'
136+
end
137+
138+
get '/request_confirmation/:id' do
139+
content_type :json
140+
Travis::RemoteVCS::User
141+
.new.request_confirmation(id: current_user.id)
142+
{ status: 200 }.to_json
143+
end
144+
130145
private
131146

132147
# update first login date if not set

lib/travis/api/serialize/v2/http/user.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'travis/api/serialize/formats'
2-
require 'travis/github/oauth'
32
require 'travis/remote_vcs/user'
43
require 'travis/remote_vcs/response_error'
54

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

Lines changed: 1 addition & 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)
5+
representation(:standard, :email, :is_syncing, :synced_at, :recently_signed_up, :secure_user_hash, :confirmed_at)
66
representation(:additional, :emails)
77

88
def email

lib/travis/remote_vcs/user.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ def check_scopes(user_id:)
4444
req.url "users/#{user_id}/check_scopes"
4545
end && true
4646
end
47+
48+
def confirm_user(token:)
49+
request(:post, __method__) do |req|
50+
req.url 'users/confirm'
51+
req.params['token'] = token
52+
end
53+
end
54+
55+
def request_confirmation(id:)
56+
request(:post, __method__) do |req|
57+
req.url 'users/request_confirmation'
58+
req.params['id'] = id
59+
end
60+
end
4761
end
4862
end
4963
end

spec/auth/v2/users_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
let(:repo) { Repository.by_slug('svenfuchs/minimal').first }
44

55
before { allow_any_instance_of(Travis::RemoteVCS::User).to receive(:check_scopes) }
6+
67
# TODO put /users/
78
# TODO put /users/:id ?
89
# TODO post /users/sync

spec/travis/remote_vcs/user_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require 'rspec'
4+
5+
describe Travis::RemoteVCS::User do
6+
describe '#confirm_user' do
7+
let(:token) { double(:token) }
8+
let(:instance) { described_class.new }
9+
let(:req) { double(:request) }
10+
let(:params) { double(:params) }
11+
12+
subject { instance.confirm_user(token: token) }
13+
14+
before do
15+
allow(req).to receive(:url)
16+
allow(req).to receive(:params).and_return(params)
17+
allow(params).to receive(:[]=)
18+
end
19+
20+
it 'performs POST to VCS with proper params' do
21+
expect(instance).to receive(:request).with(:post, :confirm_user).and_yield(req)
22+
expect(req).to receive(:url).with('users/confirm')
23+
expect(params).to receive(:[]=).with('token', token)
24+
25+
subject
26+
end
27+
end
28+
29+
describe '#request_confirmation' do
30+
let(:id) { double(:id) }
31+
let(:instance) { described_class.new }
32+
let(:req) { double(:request) }
33+
let(:params) { double(:params) }
34+
35+
subject { instance.request_confirmation(id: id) }
36+
37+
before do
38+
allow(req).to receive(:url)
39+
allow(req).to receive(:params).and_return(params)
40+
allow(params).to receive(:[]=)
41+
end
42+
43+
it 'performs POST to VCS with proper params' do
44+
expect(instance).to receive(:request).with(:post, :request_confirmation).and_yield(req)
45+
expect(req).to receive(:url).with('users/request_confirmation')
46+
expect(params).to receive(:[]=).with('id', id)
47+
48+
subject
49+
end
50+
end
51+
end

spec/unit/endpoint/authorization_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,53 @@ def user_for(github_token)
304304
expect(post('/auth/github', github_token: 'public repos')).to be_ok
305305
end
306306
end
307+
308+
describe 'GET /confirm_user/:token' do
309+
context 'when response is ok' do
310+
before { allow_any_instance_of(Travis::RemoteVCS::User).to receive(:confirm_user) }
311+
312+
it 'returns ok' do
313+
expect(get('/auth/confirm_user/mytokentopass')).to be_ok
314+
end
315+
316+
it 'calls VCS service with proper params' do
317+
expect_any_instance_of(Travis::RemoteVCS::User)
318+
.to receive(:confirm_user).with(token: 'mytokentopass')
319+
320+
get('/auth/confirm_user/mytokentopass')
321+
end
322+
end
323+
324+
context 'when response is not ok' do
325+
before do
326+
allow_any_instance_of(Travis::RemoteVCS::User)
327+
.to receive(:confirm_user).and_raise(Travis::RemoteVCS::ResponseError)
328+
end
329+
330+
it 'returns 404 with a message' do
331+
expect(get('/auth/confirm_user/mytokentopass')).not_to be_ok
332+
expect(last_response.status).to eq(404)
333+
expect(body).to include('The token is expired or not found.')
334+
end
335+
end
336+
end
337+
338+
describe 'GET /request_confirmation/:session_token/:id' do
339+
let(:current_user) { double(:user, id: 123) }
340+
before do
341+
allow_any_instance_of(described_class).to receive(:current_user).and_return(current_user)
342+
allow_any_instance_of(Travis::RemoteVCS::User).to receive(:request_confirmation)
343+
end
344+
345+
it 'returns ok' do
346+
expect(get('/auth/request_confirmation/123')).to be_ok
347+
end
348+
349+
it 'calls VCS service with proper params' do
350+
expect_any_instance_of(Travis::RemoteVCS::User)
351+
.to receive(:request_confirmation).with(id: 123)
352+
353+
get('/auth/request_confirmation/123')
354+
end
355+
end
307356
end

spec/v3/services/installation/find_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"allow_migration" => false,
6565
"recently_signed_up" => false,
6666
"secure_user_hash" => nil,
67+
"confirmed_at" => nil,
6768
}
6869
}}
6970
end

spec/v3/services/owner/find_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@
296296
},
297297
"recently_signed_up"=>false,
298298
"secure_user_hash" => nil,
299+
"confirmed_at" => nil,
299300
}}
300301
end
301302

@@ -326,6 +327,7 @@
326327
},
327328
"recently_signed_up"=>false,
328329
"secure_user_hash" => nil,
330+
"confirmed_at" => nil,
329331
}}
330332
end
331333

@@ -356,6 +358,7 @@
356358
},
357359
"recently_signed_up"=>false,
358360
"secure_user_hash" => nil,
361+
"confirmed_at" => nil,
359362
}}
360363
end
361364

@@ -390,6 +393,7 @@
390393
},
391394
"recently_signed_up"=>false,
392395
"secure_user_hash" => nil,
396+
"confirmed_at" => nil,
393397
"@warnings" => [{
394398
"@type" => "warning",
395399
"message" => "query parameter user.id not safelisted, ignored",

spec/v3/services/user/current_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"id" => user.id
3131
},
3232
"recently_signed_up"=>false,
33-
"secure_user_hash" => nil
33+
"secure_user_hash" => nil,
34+
"confirmed_at" => nil,
3435
}}
3536
end
3637
end

0 commit comments

Comments
 (0)