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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
|
# frozen_string_literal: true
RSpec.describe OAuth2::AccessToken do
subject { described_class.new(client, token, token_options) }
let(:base_options) { {site: "https://api.example.com"} }
let(:token_options) { {} }
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] }
stub.post("/oauth/revoke") { |env| [200, {"Content-type" => "application/json"}, env[: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",
header_format: "Bearer %",
mode: :header,
param_name: "access_token",
}
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
before do
@original_setw = OAuth2.config.silence_extra_tokens_warning
OAuth2.config.silence_extra_tokens_warning = false
end
after do
OAuth2.config.silence_extra_tokens_warning = @original_setw
end
it "warns on STDERR" do
skip("Warning output we spit on Hashie without VERSION constant makes this test invalid") unless defined?(Hashie::VERSION)
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 one token" do
subject(:printed) do
capture(:stderr) do
target
end
end
let(:hash) do
{
access_token: token,
}
end
before do
@original_setw = OAuth2.config.silence_extra_tokens_warning
OAuth2.config.silence_extra_tokens_warning = false
end
after do
OAuth2.config.silence_extra_tokens_warning = @original_setw
end
it "does not warn on STDERR" do
skip("Warning output we spit on Hashie without VERSION constant makes this test invalid") unless defined?(Hashie::VERSION)
expect(printed).to eq("")
end
end
context "when silenced" do
subject(:printed) do
capture(:stderr) do
target
end
end
before do
@original_setw = OAuth2.config.silence_extra_tokens_warning
OAuth2.config.silence_extra_tokens_warning = true
end
after do
OAuth2.config.silence_extra_tokens_warning = @original_setw
end
it "does not warn on STDERR" do
skip("Warning output we spit on Hashie without VERSION constant makes this test invalid") unless defined?(Hashie::VERSION)
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
before do
@original_setw = OAuth2.config.silence_extra_tokens_warning
OAuth2.config.silence_extra_tokens_warning = false
end
after do
OAuth2.config.silence_extra_tokens_warning = @original_setw
end
let(:hash) do
{
id_token: "confusing bug here",
access_token: token,
}
end
it "warns on STDERR and selects the correct key" do
skip("Warning output we spit on Hashie without VERSION constant makes this test invalid") unless defined?(Hashie::VERSION)
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
context "with warning for no token keys" do
subject(:printed) do
capture(:stderr) do
target
end
end
before do
@original_sntw = OAuth2.config.silence_no_tokens_warning
OAuth2.config.silence_no_tokens_warning = false
end
after do
OAuth2.config.silence_no_tokens_warning = @original_sntw
end
let(:options) { {raise_errors: true} }
let(:hash) do
{
blather: "confusing bug here",
rather: token,
}
end
it "raises an error" do
block_is_expected.to raise_error(OAuth2::Error)
end
context "when not raising errors" do
let(:options) { {raise_errors: false} }
it "warns on STDERR" do
skip("Warning output we spit on Hashie without VERSION constant makes this test invalid") unless defined?(Hashie::VERSION)
msg = <<-MSG.lstrip
OAuth2::AccessToken has no token
MSG
expect(printed).to eq(msg)
end
context "when custom token_name valid" do
let(:options) { {raise_errors: false} }
let(:hash) do
{
"lollipop" => token,
:expires_at => Time.now.to_i + 200,
:foo => "bar",
:header_format => "Bearer %",
:mode => :header,
:param_name => "lollipop",
:token_name => "lollipop",
}
end
it "finds token" do
expect(target.token).to eq("monkey")
end
it "does not warn when token is found" do
skip("Warning output we spit on Hashie without VERSION constant makes this test invalid") unless defined?(Hashie::VERSION)
expect(printed).to eq("")
end
end
context "when custom token_name invalid" do
let(:options) { {raise_errors: false} }
let(:hash) do
{
"babyshark" => token,
:expires_at => Time.now.to_i + 200,
:foo => "bar",
:header_format => "Bearer %",
:mode => :header,
:param_name => "lollipop",
:token_name => "lollipop",
}
end
context "when silence_no_tokens_warning is false" do
before do
@original_sntw = OAuth2.config.silence_no_tokens_warning
OAuth2.config.silence_no_tokens_warning = false
end
after do
OAuth2.config.silence_no_tokens_warning = @original_sntw
end
it "finds no token" do
expect(target.token).to eq("")
end
it "warns when no token is found" do
skip("Warning output we spit on Hashie without VERSION constant makes this test invalid") unless defined?(Hashie::VERSION)
expect(printed.each_line.to_a).to eq([
"\n",
"OAuth2::AccessToken#from_hash key mismatch.\n",
%{Custom token_name (lollipop) is not found in (["babyshark", :expires_at, :foo, :header_format, :mode, :param_name, :token_name])\n},
"You may need to set `snaky: false`. See inline documentation for more info.\n",
" \n",
"OAuth2::AccessToken has no token\n",
])
end
end
context "when silence_no_tokens_warning is true" do
before do
@original_sntw = OAuth2.config.silence_no_tokens_warning
OAuth2.config.silence_no_tokens_warning = true
end
after do
OAuth2.config.silence_no_tokens_warning = @original_sntw
end
it "finds no token" do
expect(target.token).to eq("")
end
it "does not warn when no token is found" do
skip("Warning output we spit on Hashie without VERSION constant makes this test invalid") unless defined?(Hashie::VERSION)
expect(printed.each_line.to_a).to eq([])
end
end
end
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, {error: "OAuth2::AccessToken has no token", error_description: "Options are: #{{mode: :this_is_bad, raise_errors: true}}"}.to_s)
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, {error: "OAuth2::AccessToken has no token", error_description: "Options are: #{{mode: :this_is_bad, raise_errors: true}}"}.to_s)
end
end
end
end
context "with verb-dependent mode" do
let(:mode) do
lambda do |verb|
case verb
when :get then :query
when :post, :delete then :header
when :put, :patch then :body
end
end
end
let(:options) { {mode: mode} }
VERBS.each do |verb|
it "correctly handles a #{verb.to_s.upcase}" do
expect(subject.__send__(verb, "/token/#{mode.call(verb)}").body).to include(token)
end
end
context "when invalid" do
subject(:invalid_target) { target.__send__(http_verb, "/token/#{mode.call(http_verb)}") }
let(:http_verb) { :get }
let(:mode) do
lambda do |_verb|
"foobar"
end
end
it "correctly handles an invalid mode by raising an error" do
block_is_expected.to raise_error("invalid :mode option of foobar")
end
end
end
context "with verb-dependent Hash mode" do
let(:mode_hash) do
{get: :query, post: :header, delete: :header, put: :body, patch: :body}
end
let(:options) { {mode: mode_hash} }
VERBS.each do |verb|
it "correctly handles a #{verb.to_s.upcase} via Hash" do
expected = mode_hash[verb] || :header
expect(subject.__send__(verb, "/token/#{expected}").body).to include(token)
end
end
context "with fallback to :header for missing key" do
let(:mode_hash) { {get: :query} }
it "defaults POST to header when not specified" do
expect(subject.post("/token/header").body).to include(token)
end
end
context "when invalid value" do
let(:mode_hash) { {get: "foobar"} }
it "raises an error for invalid mapping" do
expect { subject.get("/token/foobar") }.to raise_error("invalid :mode option of foobar")
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
before do
@original_sntw = OAuth2.config.silence_no_tokens_warning
OAuth2.config.silence_no_tokens_warning = false
end
after do
OAuth2.config.silence_no_tokens_warning = @original_sntw
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 }
before do
@original_sntw = OAuth2.config.silence_no_tokens_warning
OAuth2.config.silence_no_tokens_warning = false
end
after do
OAuth2.config.silence_no_tokens_warning = @original_sntw
end
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, {error: "OAuth2::AccessToken has no token", error_description: "Options are: #{{raise_errors: true}}"}.to_s)
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, {error: "OAuth2::AccessToken has no token", error_description: "Options are: #{{raise_errors: true}}"}.to_s)
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 0 (token is permanent)" do
expect(described_class.new(client, token, refresh_token: "abaca", expires_in: 0)).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(OAuth2::Error, {error: "A refresh_token is not available"}.to_s)
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 "#revoke" do
let(:token) { "monkey123" }
let(:refresh_token) { "refreshmonkey123" }
let(:access_token) { described_class.new(client, token, refresh_token: refresh_token) }
context "with no token_type_hint specified" do
it "revokes the access token by default" do
expect(access_token.revoke.status).to eq(200)
end
end
context "with access_token token_type_hint" do
it "revokes the access token" do
expect {
access_token.revoke(token_type_hint: "access_token")
}.not_to raise_error
end
end
context "with refresh_token token_type_hint" do
it "revokes the refresh token" do
expect {
access_token.revoke(token_type_hint: "refresh_token")
}.not_to raise_error
end
end
context "with invalid token_type_hint" do
it "raises an OAuth2::Error" do
expect {
access_token.revoke(token_type_hint: "invalid_type")
}.to raise_error(OAuth2::Error, /token_type_hint must be one of/)
end
end
context "when refresh_token is specified but not available" do
let(:access_token) { described_class.new(client, "abc", refresh_token: nil) }
it "raises an OAuth2::Error" do
expect {
access_token.revoke(token_type_hint: "refresh_token")
}.to raise_error(OAuth2::Error, /refresh_token is not available for revoking/)
end
end
context "when refresh_token is, but access_token is not, available" do
let(:access_token) { described_class.new(client, "abc", refresh_token: refresh_token) }
before do
allow(client).to receive(:revoke_token).
with(refresh_token, "refresh_token", {}).
and_return(OAuth2::Response.new(double(status: 200)))
# The code path being tested shouldn't be reachable... so this is hacky.
# Testing it for anal level compliance. Revoking a refresh token without an access token is valid.
# In other words, the implementation of AccessToken doesn't allow instantiation without an access token.
# But in a revocation scenario it should theoretically work.
# It is intended that AccessToken be subclassed, so this is worth testing, as subclasses may change behavior.
allow(access_token).to receive(:token).and_return(nil)
end
it "revokes refresh_token" do
expect {
access_token.revoke
}.not_to raise_error
end
end
context "when no tokens are available" do
let(:access_token) { described_class.new(client, "abc", refresh_token: nil) }
before do
# The code path being tested shouldn't be reachable... so this is hacky.
# Testing it for anal level compliance. Revoking a refresh token without an access token is valid.
# In other words, the implementation of AccessToken doesn't allow instantiation without an access token.
# But in a revocation scenario it should theoretically work.
# It is intended that AccessToken be subclassed, so this is worth testing, as subclasses may change behavior.
allow(access_token).to receive(:token).and_return(nil)
end
it "raises an OAuth2::Error" do
expect {
access_token.revoke
}.to raise_error(OAuth2::Error, /unknown token type is not available for revoking/)
end
end
context "with additional params" do
before do
allow(client).to receive(:revoke_token).
with(token, "access_token", {extra: "param"}).
and_return(OAuth2::Response.new(double(status: 200)))
end
it "passes them to the client" do
expect {
access_token.revoke({extra: "param"})
}.not_to raise_error
end
end
context "with a block" do
it "passes the block to the client" do
expect {
access_token.revoke do |_req|
puts "Hello from the other side"
end
}.not_to raise_error
end
it "has status 200" do
expect(
access_token.revoke do |_req|
puts "Hello again"
end.status,
).to eq(200)
end
it "executes the block" do
@apple = 0
access_token.revoke do |_req|
@apple += 1
end
expect(@apple).to eq(1)
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,
header_format: "Bearer %",
mode: :header,
param_name: "access_token",
foo: "bar",
}
access_token = described_class.from_hash(client, hash.clone)
expect(access_token.to_hash).to eq(hash)
end
context "with token_name" do
it "return a hash equal to the hash used to initialize access token" do
hash = {
access_token: "",
refresh_token: "foobar",
expires_at: Time.now.to_i + 200,
header_format: "Bearer %",
mode: :header,
param_name: "access_token",
token_name: "banana_face",
foo: "bar",
}
access_token = described_class.from_hash(client, hash.clone)
expect(access_token.to_hash).to eq(hash)
end
end
end
describe "#inspect" do
let(:inspect_result) { described_class.new(nil, "secret-token", {refresh_token: "secret-refresh-token"}).inspect }
it "filters out the @token value" do
expect(inspect_result).to include("@token=[FILTERED]")
end
it "filters out the @refresh_token value" do
expect(inspect_result).to include("@refresh_token=[FILTERED]")
end
end
end
|