2 |
2 |
|
3 |
3 |
require File.expand_path('../application_system_test_case', __dir__)
|
4 |
4 |
require 'oauth2'
|
5 |
|
require 'webrick'
|
|
5 |
require 'rack'
|
|
6 |
require 'puma'
|
6 |
7 |
|
7 |
8 |
class OauthProviderSystemTest < ApplicationSystemTestCase
|
8 |
9 |
fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
|
... | ... | |
58 |
59 |
# launches webrick, listening for the redirect with the auth code.
|
59 |
60 |
launch_client_app(port: port) do |req, res|
|
60 |
61 |
# get access code from code url param
|
61 |
|
if code = req.query['code'].presence
|
|
62 |
if code = req.params['code'].presence
|
62 |
63 |
# exchange it for token
|
63 |
64 |
token = client.auth_code.get_token(code, redirect_uri: redirect_uri)
|
64 |
|
res.body = "<html><body><p>Authorization succeeded, you may close this window now.</p></body></html>"
|
|
65 |
res.body = ["<html><body><p>Authorization succeeded, you may close this window now.</p></body></html>"]
|
65 |
66 |
end
|
66 |
67 |
end
|
67 |
68 |
|
... | ... | |
115 |
116 |
private
|
116 |
117 |
|
117 |
118 |
def launch_client_app(port: 12345, path: '/', &block)
|
118 |
|
server = WEBrick::HTTPServer.new Port: port
|
119 |
|
trap('INT') { server.shutdown }
|
120 |
|
server.mount_proc(path, block)
|
121 |
|
Thread.new { server.start }
|
122 |
|
port
|
|
119 |
app = ->(env) do
|
|
120 |
req = Rack::Request.new(env)
|
|
121 |
res = Rack::Response.new
|
|
122 |
yield(req, res)
|
|
123 |
res.finish
|
|
124 |
end
|
|
125 |
|
|
126 |
server = Puma::Server.new app
|
|
127 |
server.add_tcp_listener '127.0.0.1', port
|
|
128 |
Thread.new { server.run }
|
123 |
129 |
end
|
124 |
130 |
|
125 |
131 |
def test_port
|