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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
|
# frozen_string_literal: true
RSpec.describe OAuth2::AccessToken do
subject { described_class.new(client, token) }
let(:base_options) { {site: 'https://api.example.com'} }
let(:options) { {} }
let(:token) { 'monkey' }
let(:refresh_body) { JSON.dump(access_token: 'refreshed_foo', expires_in: 600, refresh_token: 'refresh_bar') }
let(:client) do
OAuth2::Client.new('abc', 'def', options.merge(base_options)) do |builder|
builder.request :url_encoded
builder.adapter :test do |stub|
VERBS.each do |verb|
stub.send(verb, '/token/header') { |env| [200, {}, env[:request_headers]['Authorization']] }
stub.send(verb, "/token/query?access_token=#{token}") { |env| [200, {}, Addressable::URI.parse(env[:url]).query_values['access_token']] }
stub.send(verb, '/token/query_string') { |env| [200, {}, CGI.unescape(Addressable::URI.parse(env[:url]).query)] }
stub.send(verb, '/token/body') { |env| [200, {}, env[:body]] }
end
stub.post('/oauth/token') { |_env| [200, {'Content-Type' => 'application/json'}, refresh_body] }
end
end
end
describe '.from_hash' do
subject(:target) { described_class.from_hash(client, hash) }
let(:hash) do
{
:access_token => token,
:id_token => 'confusing bug here',
:refresh_token => 'foobar',
:expires_at => Time.now.to_i + 200,
'foo' => 'bar',
}
end
it 'return a hash equals to the hash used to initialize access token' do
expect(target.to_hash).to eq(hash)
end
context 'with warning for too many tokens' do
subject(:printed) do
capture(:stderr) do
target
end
end
it 'warns on STDERR' do
msg = <<-MSG.lstrip
OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key ([:access_token, :id_token]); using :access_token.
MSG
expect(printed).to eq(msg)
end
context 'when silenced' do
subject(:printed) do
capture(:stderr) do
target
end
end
before do
OAuth2.configure do |config|
config.silence_extra_tokens_warning = true
end
end
after do
OAuth2.configure do |config|
config.silence_extra_tokens_warning = false
end
end
it 'does not warn on STDERR' do
expect(printed).to eq('')
end
end
end
context 'with keys in a different order to the lookup' do
subject(:printed) do
capture(:stderr) do
target
end
end
let(:hash) do
{
id_token: 'confusing bug here',
access_token: token,
}
end
it 'warns on STDERR and selects the correct key' do
msg = <<-MSG.lstrip
OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key ([:access_token, :id_token]); using :access_token.
MSG
expect(printed).to eq(msg)
end
end
end
describe '#initialize' do
it 'assigns client and token' do
expect(subject.client).to eq(client)
expect(subject.token).to eq(token)
end
it 'assigns extra params' do
target = described_class.new(client, token, 'foo' => 'bar')
expect(target.params).to include('foo')
expect(target.params['foo']).to eq('bar')
end
def assert_initialized_token(target)
expect(target.token).to eq(token)
expect(target).to be_expires
expect(target.params.keys).to include('foo')
expect(target.params['foo']).to eq('bar')
end
it 'initializes with a Hash' do
hash = {:access_token => token, :expires_at => Time.now.to_i + 200, 'foo' => 'bar'}
target = described_class.from_hash(client, hash)
assert_initialized_token(target)
end
it 'from_hash does not modify opts hash' do
hash = {access_token: token, expires_at: Time.now.to_i}
hash_before = hash.dup
described_class.from_hash(client, hash)
expect(hash).to eq(hash_before)
end
it 'initializes with a form-urlencoded key/value string' do
kvform = "access_token=#{token}&expires_at=#{Time.now.to_i + 200}&foo=bar"
target = described_class.from_kvform(client, kvform)
assert_initialized_token(target)
end
context 'with options' do
subject(:target) { described_class.new(client, token, **options) }
context 'with body mode' do
let(:mode) { :body }
let(:options) { {param_name: 'foo', header_format: 'Bearer %', mode: mode} }
it 'sets options' do
expect(target.options[:param_name]).to eq('foo')
expect(target.options[:header_format]).to eq('Bearer %')
expect(target.options[:mode]).to eq(mode)
end
end
context 'with header mode' do
let(:mode) { :header }
let(:options) { {headers: {}, mode: mode} }
it 'sets options' do
expect(target.options[:headers]).to be_nil
expect(target.options[:mode]).to eq(mode)
end
end
context 'with query mode' do
let(:mode) { :query }
let(:options) { {params: {}, param_name: 'foo', mode: mode} }
it 'sets options' do
expect(target.options[:param_name]).to eq('foo')
expect(target.options[:params]).to be_nil
expect(target.options[:mode]).to eq(mode)
end
end
context 'with invalid mode' do
let(:mode) { :this_is_bad }
let(:options) { {mode: mode} }
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
context 'with request' do
subject(:request) { target.post('/token/header') }
it 'raises' do
block_is_expected.to raise_error("invalid :mode option of #{mode}")
end
end
context 'with client.options[:raise_errors] = true' do
let(:mode) { :this_is_bad }
let(:options) { {mode: mode, raise_errors: true} }
before do
expect(client.options[:raise_errors]).to be(true)
end
context 'when there is a token' do
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
context 'with request' do
subject(:request) { target.post('/token/header') }
it 'raises' do
block_is_expected.to raise_error("invalid :mode option of #{mode}")
end
end
end
context 'when there is empty token' do
let(:token) { '' }
it 'raises on initialize' do
block_is_expected.to raise_error(OAuth2::Error, '{:mode=>:this_is_bad, :raise_errors=>true}')
end
end
context 'when there is nil token' do
let(:token) { nil }
it 'raises on initialize' do
block_is_expected.to raise_error(OAuth2::Error, '{:mode=>:this_is_bad, :raise_errors=>true}')
end
end
end
end
context 'with client.options[:raise_errors] = false' do
let(:options) { {raise_errors: false} }
before do
expect(client.options[:raise_errors]).to be(false)
end
context 'when there is a token' do
let(:token) { 'hurdygurdy' }
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has token' do
expect(target.token).to eq(token)
end
it 'has no refresh_token' do
expect(target.refresh_token).to be_nil
end
context 'when there is refresh_token' do
let(:options) { {raise_errors: false, refresh_token: 'zxcv'} }
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has token' do
expect(target.token).to eq(token)
end
it 'has refresh_token' do
expect(target.refresh_token).to eq('zxcv')
end
end
end
context 'when there is empty token' do
let(:token) { '' }
context 'when there is no refresh_token' do
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has no token' do
expect(target.token).to eq('')
end
it 'has no refresh_token' do
expect(target.refresh_token).to be_nil
end
context 'with warning for no token' do
subject(:printed) do
capture(:stderr) do
target
end
end
it 'warns on STDERR' do
msg = <<-MSG.lstrip
OAuth2::AccessToken has no token
MSG
expect(printed).to eq(msg)
end
end
end
context 'when there is refresh_token' do
let(:options) { {raise_errors: false, refresh_token: 'qwer'} }
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has no token' do
expect(target.token).to eq('')
end
it 'has refresh_token' do
expect(target.refresh_token).to eq('qwer')
end
end
end
context 'when there is nil token' do
let(:token) { nil }
context 'when there is no refresh_token' do
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has no token' do
expect(target.token).to eq('')
end
it 'has no refresh_token' do
expect(target.refresh_token).to be_nil
end
context 'with warning for no token' do
subject(:printed) do
capture(:stderr) do
target
end
end
it 'warns on STDERR' do
msg = <<-MSG.lstrip
OAuth2::AccessToken has no token
MSG
expect(printed).to eq(msg)
end
end
end
context 'when there is refresh_token' do
let(:options) { {raise_errors: false, refresh_token: 'asdf'} }
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has no token' do
expect(target.token).to eq('')
end
it 'has refresh_token' do
expect(target.refresh_token).to eq('asdf')
end
end
end
end
context 'with client.options[:raise_errors] = true' do
let(:options) { {raise_errors: true} }
before do
expect(client.options[:raise_errors]).to be(true)
end
context 'when there is a token' do
let(:token) { 'hurdygurdy' }
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has token' do
expect(target.token).to eq(token)
end
it 'has no refresh_token' do
expect(target.refresh_token).to be_nil
end
context 'when there is refresh_token' do
let(:options) { {raise_errors: true, refresh_token: 'zxcv'} }
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has token' do
expect(target.token).to eq(token)
end
it 'has refresh_token' do
expect(target.refresh_token).to eq('zxcv')
end
end
end
context 'when there is empty token' do
let(:token) { '' }
context 'when there is no refresh_token' do
it 'raises on initialize' do
block_is_expected.to raise_error(OAuth2::Error, '{:raise_errors=>true}')
end
end
context 'when there is refresh_token' do
let(:options) { {raise_errors: true, refresh_token: 'qwer'} }
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has no token' do
expect(target.token).to eq('')
end
it 'has refresh_token' do
expect(target.refresh_token).to eq('qwer')
end
end
end
context 'when there is nil token' do
let(:token) { nil }
context 'when there is no refresh_token' do
it 'raises on initialize' do
block_is_expected.to raise_error(OAuth2::Error, '{:raise_errors=>true}')
end
end
context 'when there is refresh_token' do
let(:options) { {raise_errors: true, refresh_token: 'asdf'} }
it 'does not raise on initialize' do
block_is_expected.not_to raise_error
end
it 'has no token' do
expect(target.token).to eq('')
end
it 'has refresh_token' do
expect(target.refresh_token).to eq('asdf')
end
end
end
end
end
it 'does not modify opts hash' do
opts = {param_name: 'foo', header_format: 'Bearer %', mode: :body}
opts_before = opts.dup
described_class.new(client, token, opts)
expect(opts).to eq(opts_before)
end
describe 'expires_at' do
let(:expires_at) { 1_361_396_829 }
let(:hash) do
{
:access_token => token,
:expires_at => expires_at.to_s,
'foo' => 'bar',
}
end
it 'initializes with an integer timestamp expires_at' do
target = described_class.from_hash(client, hash.merge(expires_at: expires_at))
assert_initialized_token(target)
expect(target.expires_at).to eql(expires_at)
end
it 'initializes with a string timestamp expires_at' do
target = described_class.from_hash(client, hash)
assert_initialized_token(target)
expect(target.expires_at).to eql(expires_at)
end
it 'initializes with a string time expires_at' do
target = described_class.from_hash(client, hash.merge(expires_at: Time.at(expires_at).iso8601))
assert_initialized_token(target)
expect(target.expires_at).to eql(expires_at)
end
end
describe 'expires_latency' do
let(:expires_at) { 1_530_000_000 }
let(:expires_in) { 100 }
let(:expires_latency) { 10 }
let(:hash) do
{
access_token: token,
expires_latency: expires_latency,
expires_in: expires_in,
}
end
it 'sets it via options' do
target = described_class.from_hash(client, hash.merge(expires_latency: expires_latency.to_s))
expect(target.expires_latency).to eq expires_latency
end
it 'sets it nil by default' do
hash.delete(:expires_latency)
target = described_class.from_hash(client, hash)
expect(target.expires_latency).to be_nil
end
it 'reduces expires_at by the given amount' do
allow(Time).to receive(:now).and_return(expires_at)
target = described_class.from_hash(client, hash)
expect(target.expires_at).to eq(expires_at + expires_in - expires_latency)
end
it 'reduces expires_at by the given amount if expires_at is provided as option' do
target = described_class.from_hash(client, hash.merge(expires_at: expires_at))
expect(target.expires_at).to eq(expires_at - expires_latency)
end
end
end
describe '#request' do
context 'with :mode => :header' do
before do
subject.options[:mode] = :header
end
VERBS.each do |verb|
it "sends the token in the Authorization header for a #{verb.to_s.upcase} request" do
expect(subject.post('/token/header').body).to include(token)
end
end
end
context 'with :mode => :query' do
before do
subject.options[:mode] = :query
end
VERBS.each do |verb|
it "sends the token in the body for a #{verb.to_s.upcase} request" do
expect(subject.post('/token/query').body).to eq(token)
end
it "sends a #{verb.to_s.upcase} request and options[:param_name] include [number]." do
subject.options[:param_name] = 'auth[1]'
expect(subject.__send__(verb, '/token/query_string').body).to include("auth[1]=#{token}")
end
end
end
context 'with :mode => :body' do
before do
subject.options[:mode] = :body
end
VERBS.each do |verb|
it "sends the token in the body for a #{verb.to_s.upcase} request" do
expect(subject.post('/token/body').body.split('=').last).to eq(token)
end
context 'when options[:param_name] include [number]' do
it "sends a #{verb.to_s.upcase} request when body is a hash" do
subject.options[:param_name] = 'auth[1]'
expect(subject.__send__(verb, '/token/body', body: {hi: 'there'}).body).to include("auth%5B1%5D=#{token}")
end
it "sends a #{verb.to_s.upcase} request when body is overridden as string" do
subject.options[:param_name] = 'snoo[1]'
expect(subject.__send__(verb, '/token/body', body: 'hi_there').body).to include("hi_there&snoo[1]=#{token}")
end
end
end
end
context 'params include [number]' do
VERBS.each do |verb|
it "sends #{verb.to_s.upcase} correct query" do
expect(subject.__send__(verb, '/token/query_string', params: {'foo[bar][1]' => 'val'}).body).to include('foo[bar][1]=val')
end
end
end
end
describe '#expires?' do
it 'is false if there is no expires_at' do
expect(described_class.new(client, token)).not_to be_expires
end
it 'is true if there is an expires_in' do
expect(described_class.new(client, token, refresh_token: 'abaca', expires_in: 600)).to be_expires
end
it 'is true if there is an expires_at' do
expect(described_class.new(client, token, refresh_token: 'abaca', expires_in: Time.now.getutc.to_i + 600)).to be_expires
end
end
describe '#expired?' do
it 'is false if there is no expires_in or expires_at' do
expect(described_class.new(client, token)).not_to be_expired
end
it 'is false if expires_in is in the future' do
expect(described_class.new(client, token, refresh_token: 'abaca', expires_in: 10_800)).not_to be_expired
end
it 'is true if expires_at is in the past' do
access = described_class.new(client, token, refresh_token: 'abaca', expires_in: 600)
@now = Time.now + 10_800
allow(Time).to receive(:now).and_return(@now)
expect(access).to be_expired
end
it 'is true if expires_at is now' do
@now = Time.now
access = described_class.new(client, token, refresh_token: 'abaca', expires_at: @now.to_i)
allow(Time).to receive(:now).and_return(@now)
expect(access).to be_expired
end
end
describe '#refresh' do
let(:options) { {access_token_class: access_token_class} }
let(:access_token_class) { NewAccessToken }
let(:access) do
described_class.new(client, token, refresh_token: 'abaca',
expires_in: 600,
param_name: 'o_param',
access_token_class: access_token_class)
end
let(:new_access) do
NewAccessToken.new(client, token, refresh_token: 'abaca')
end
before do
custom_class = Class.new(described_class) do
def self.from_hash(client, hash)
new(client, hash.delete('access_token'), hash)
end
def self.contains_token?(hash)
hash.key?('refresh_token')
end
end
stub_const('NewAccessToken', custom_class)
end
context 'without refresh_token' do
subject(:no_refresh) { no_access.refresh }
let(:no_access) do
described_class.new(client, token, refresh_token: nil,
expires_in: 600,
param_name: 'o_param',
access_token_class: access_token_class)
end
it 'raises when no refresh_token' do
block_is_expected.to raise_error('A refresh_token is not available')
end
end
it 'returns a refresh token with appropriate values carried over' do
refreshed = access.refresh
expect(access.client).to eq(refreshed.client)
expect(access.options[:param_name]).to eq(refreshed.options[:param_name])
end
it 'returns a refresh token of the same access token class' do
refreshed = new_access.refresh!
expect(new_access.class).to eq(refreshed.class)
end
context 'with a nil refresh_token in the response' do
let(:refresh_body) { JSON.dump(access_token: 'refreshed_foo', expires_in: 600, refresh_token: nil) }
it 'copies the refresh_token from the original token' do
refreshed = access.refresh
expect(refreshed.refresh_token).to eq(access.refresh_token)
end
end
context 'with a not-nil refresh_token in the response' do
let(:refresh_body) { JSON.dump(access_token: 'refreshed_foo', expires_in: 600, refresh_token: 'qerwer') }
it 'copies the refresh_token from the original token' do
refreshed = access.refresh
expect(refreshed.token).to eq('refreshed_foo')
expect(refreshed.refresh_token).to eq('qerwer')
end
end
context 'with a not-nil, not camel case, refresh_token in the response' do
let(:refresh_body) { JSON.dump(accessToken: 'refreshed_foo', expires_in: 600, refreshToken: 'qerwer') }
it 'copies the refresh_token from the original token' do
refreshed = access.refresh
expect(refreshed.token).to eq('refreshed_foo')
expect(refreshed.refresh_token).to eq('qerwer')
end
end
context 'with a custom access_token_class' do
let(:access_token_class) { NewAccessToken }
it 'returns a refresh token of NewAccessToken' do
refreshed = access.refresh!
expect(new_access.class).to eq(refreshed.class)
end
end
end
describe '#to_hash' do
it 'return a hash equal to the hash used to initialize access token' do
hash = {:access_token => token, :refresh_token => 'foobar', :expires_at => Time.now.to_i + 200, 'foo' => 'bar'}
access_token = described_class.from_hash(client, hash.clone)
expect(access_token.to_hash).to eq(hash)
end
end
end
|