1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
require 'spec_helper'
RSpec.shared_examples 'site has valid domain url' do |url|
it { expect(subject.site).to eq(url) }
end
describe OmniAuth::Strategies::Auth0 do
let(:client_id) { 'CLIENT_ID' }
let(:client_secret) { 'CLIENT_SECRET' }
let(:domain_url) { 'https://samples.auth0.com' }
let(:application) do
lambda do
[200, {}, ['Hello.']]
end
end
let(:auth0) do
OmniAuth::Strategies::Auth0.new(
application,
client_id,
client_secret,
domain_url
)
end
describe 'client_options' do
let(:subject) { auth0.client }
context 'domain with https' do
let(:domain_url) { 'https://samples.auth0.com' }
it_behaves_like 'site has valid domain url', 'https://samples.auth0.com'
end
context 'domain with http' do
let(:domain_url) { 'http://mydomain.com' }
it_behaves_like 'site has valid domain url', 'http://mydomain.com'
end
context 'domain with host only' do
let(:domain_url) { 'samples.auth0.com' }
it_behaves_like 'site has valid domain url', 'https://samples.auth0.com'
end
it 'should have correct authorize path' do
expect(subject.options[:authorize_url]).to eq('/authorize')
end
it 'should have the correct userinfo path' do
expect(subject.options[:userinfo_url]).to eq('/userinfo')
end
it 'should have the correct token path' do
expect(subject.options[:token_url]).to eq('/oauth/token')
end
end
describe 'options' do
let(:subject) { auth0.options }
it 'should have the correct client_id' do
expect(subject[:client_id]).to eq(client_id)
end
it 'should have the correct client secret' do
expect(subject[:client_secret]).to eq(client_secret)
end
it 'should have correct domain' do
expect(subject[:domain]).to eq(domain_url)
end
end
describe 'oauth' do
it 'redirects to hosted login page' do
get 'auth/auth0'
expect(last_response.status).to eq(302)
redirect_url = last_response.headers['Location']
expect(redirect_url).to start_with('https://samples.auth0.com/authorize')
expect(redirect_url).to have_query('response_type', 'code')
expect(redirect_url).to have_query('state')
expect(redirect_url).to have_query('client_id')
expect(redirect_url).to have_query('redirect_uri')
end
describe 'callback' do
let(:access_token) { 'access token' }
let(:expires_in) { 2000 }
let(:token_type) { 'bearer' }
let(:refresh_token) { 'refresh token' }
let(:id_token) { 'id token' }
let(:user_id) { 'user identifier' }
let(:state) { SecureRandom.hex(8) }
let(:name) { 'John' }
let(:nickname) { 'J' }
let(:picture) { 'some picture url' }
let(:email) { 'mail@mail.com' }
let(:email_verified) { true }
let(:oauth_response) do
{
access_token: access_token,
expires_in: expires_in,
token_type: token_type
}
end
let(:oidc_response) do
{
id_token: id_token,
access_token: access_token,
expires_in: expires_in,
token_type: token_type
}
end
let(:basic_user_info) { { sub: user_id } }
let(:oidc_user_info) do
{
sub: user_id,
name: name,
nickname: nickname,
email: email,
picture: picture,
email_verified: email_verified
}
end
def stub_auth(body)
stub_request(:post, 'https://samples.auth0.com/oauth/token')
.to_return(
headers: { 'Content-Type' => 'application/json' },
body: MultiJson.encode(body)
)
end
def stub_userinfo(body)
stub_request(:get, 'https://samples.auth0.com/userinfo')
.to_return(
headers: { 'Content-Type' => 'application/json' },
body: MultiJson.encode(body)
)
end
def trigger_callback
get '/auth/auth0/callback', { 'state' => state },
'rack.session' => { 'omniauth.state' => state }
end
before(:each) do
WebMock.reset!
end
let(:subject) { MultiJson.decode(last_response.body) }
context 'basic oauth' do
before do
stub_auth(oauth_response)
stub_userinfo(basic_user_info)
trigger_callback
end
it 'to succeed' do
expect(last_response.status).to eq(200)
end
it 'has credentials' do
expect(subject['credentials']['token']).to eq(access_token)
expect(subject['credentials']['expires']).to be true
expect(subject['credentials']['expires_at']).to_not be_nil
end
it 'has basic values' do
expect(subject['provider']).to eq('auth0')
expect(subject['uid']).to eq(user_id)
expect(subject['info']['name']).to eq(user_id)
end
end
context 'basic oauth w/refresh token' do
before do
stub_auth(oauth_response.merge(refresh_token: refresh_token))
stub_userinfo(basic_user_info)
trigger_callback
end
it 'to succeed' do
expect(last_response.status).to eq(200)
end
it 'has credentials' do
expect(subject['credentials']['token']).to eq(access_token)
expect(subject['credentials']['refresh_token']).to eq(refresh_token)
expect(subject['credentials']['expires']).to be true
expect(subject['credentials']['expires_at']).to_not be_nil
end
end
context 'oidc' do
before do
stub_auth(oidc_response)
stub_userinfo(oidc_user_info)
trigger_callback
end
it 'to succeed' do
expect(last_response.status).to eq(200)
end
it 'has credentials' do
expect(subject['credentials']['token']).to eq(access_token)
expect(subject['credentials']['expires']).to be true
expect(subject['credentials']['expires_at']).to_not be_nil
expect(subject['credentials']['id_token']).to eq(id_token)
end
it 'has basic values' do
expect(subject['provider']).to eq('auth0')
expect(subject['uid']).to eq(user_id)
end
it 'has info' do
expect(subject['info']['name']).to eq(name)
expect(subject['info']['nickname']).to eq(nickname)
expect(subject['info']['image']).to eq(picture)
expect(subject['info']['email']).to eq(email)
end
it 'has extra' do
expect(subject['extra']['raw_info']['email_verified']).to be true
end
end
end
end
describe 'error_handling' do
it 'fails when missing client_id' do
@app = make_application(client_id: nil)
get 'auth/auth0'
expect(last_response.status).to eq(302)
redirect_url = last_response.headers['Location']
expect(redirect_url).to fail_auth_with('missing_client_id')
end
it 'fails when missing client_secret' do
@app = make_application(client_secret: nil)
get 'auth/auth0'
expect(last_response.status).to eq(302)
redirect_url = last_response.headers['Location']
expect(redirect_url).to fail_auth_with('missing_client_secret')
end
it 'fails when missing domain' do
@app = make_application(domain: nil)
get 'auth/auth0'
expect(last_response.status).to eq(302)
redirect_url = last_response.headers['Location']
expect(redirect_url).to fail_auth_with('missing_domain')
end
end
end
RSpec::Matchers.define :fail_auth_with do |message|
match do |actual|
uri = URI(actual)
query = CGI.parse(uri.query)
(uri.path == '/auth/failure') &&
(query['message'] == [message]) &&
(query['strategy'] == ['auth0'])
end
end
RSpec::Matchers.define :have_query do |key, value|
match do |actual|
uri = redirect_uri(actual)
query = query(uri)
if value.nil?
query[key].length == 1
else
query[key] == [value]
end
end
def redirect_uri(string)
URI(string)
end
def query(uri)
CGI.parse(uri.query)
end
end
|