|
86 | 86 | Faraday::Adapter::Test::Stubs.new do |stub| |
87 | 87 | stub.post("https://auth.example.com/oauth2/token") do |env| |
88 | 88 | fail "Authorization is present.#{env.request_headers}" if env.request_headers.key?("Authorization") |
| 89 | + [200, {"Content-Type" => "application/json"}, response_payload.to_json] |
89 | 90 | end |
90 | 91 | end |
91 | 92 | end |
|
103 | 104 | id_and_secret = "#{client_id}:#{client_secret}" |
104 | 105 | basic_auth = "Basic #{Base64.urlsafe_encode64(id_and_secret)}" |
105 | 106 | fail "Basic Authorization is missing." unless env.request_headers["Authorization"] == basic_auth |
| 107 | + [200, {"Content-Type" => "application/json"}, response_payload.to_json] |
106 | 108 | end |
107 | 109 | end |
108 | 110 | end |
|
129 | 131 | end |
130 | 132 | let(:error) { "invalid_request" } |
131 | 133 |
|
132 | | - it { is_expected.to be_nil } |
| 134 | + it "raises a CognitoIdp::Error" do |
| 135 | + expect { token }.to raise_error(CognitoIdp::Error) do |e| |
| 136 | + expect(e.error).to eq("invalid_request") |
| 137 | + expect(e.http_status).to eq(400) |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context "when response is an error with error_description" do |
| 143 | + let(:stubs) do |
| 144 | + Faraday::Adapter::Test::Stubs.new do |stub| |
| 145 | + stub.post("https://auth.example.com/oauth2/token") do |env| |
| 146 | + [400, {"Content-Type" => "application/json"}, response_payload.to_json] |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | + let(:response_payload) do |
| 151 | + {error: "invalid_grant", error_description: "Authorization code has expired"} |
| 152 | + end |
| 153 | + |
| 154 | + it "raises a CognitoIdp::Error with error_description" do |
| 155 | + expect { token }.to raise_error(CognitoIdp::Error) do |e| |
| 156 | + expect(e.error).to eq("invalid_grant") |
| 157 | + expect(e.error_description).to eq("Authorization code has expired") |
| 158 | + expect(e.message).to eq("invalid_grant: Authorization code has expired") |
| 159 | + expect(e.http_status).to eq(400) |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + context "when response is a server error" do |
| 165 | + let(:stubs) do |
| 166 | + Faraday::Adapter::Test::Stubs.new do |stub| |
| 167 | + stub.post("https://auth.example.com/oauth2/token") do |env| |
| 168 | + [500, {"Content-Type" => "text/plain"}, "Internal Server Error"] |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | + |
| 173 | + it "raises a CognitoIdp::Error with http_error" do |
| 174 | + expect { token }.to raise_error(CognitoIdp::Error) do |e| |
| 175 | + expect(e.error).to eq("http_error") |
| 176 | + expect(e.error_description).to eq("the server responded with status 500") |
| 177 | + expect(e.http_status).to eq(500) |
| 178 | + end |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + context "when response is an error it does not yield" do |
| 183 | + let(:stubs) do |
| 184 | + Faraday::Adapter::Test::Stubs.new do |stub| |
| 185 | + stub.post("https://auth.example.com/oauth2/token") do |env| |
| 186 | + [400, {"Content-Type" => "application/json"}, {error: "invalid_request"}.to_json] |
| 187 | + end |
| 188 | + end |
| 189 | + end |
| 190 | + |
| 191 | + it "does not yield the block" do |
| 192 | + expect { |b| client.get_token(grant_type: grant_type, code: code, redirect_uri: redirect_uri, &b) }.to raise_error(CognitoIdp::Error) |
| 193 | + end |
133 | 194 | end |
134 | 195 | end |
135 | 196 |
|
|
192 | 253 | end |
193 | 254 | let(:error) { "invalid_request" } |
194 | 255 |
|
195 | | - it { is_expected.to be_nil } |
| 256 | + it "raises a CognitoIdp::Error" do |
| 257 | + expect { token }.to raise_error(CognitoIdp::Error) do |e| |
| 258 | + expect(e.error).to eq("invalid_request") |
| 259 | + expect(e.http_status).to eq(400) |
| 260 | + end |
| 261 | + end |
196 | 262 | end |
197 | 263 | end |
198 | 264 |
|
|
243 | 309 | Faraday::Adapter::Test::Stubs.new do |stub| |
244 | 310 | stub.post("https://auth.example.com/oauth2/token") do |env| |
245 | 311 | fail "Authorization is present.#{env.request_headers}" if env.request_headers.key?("Authorization") |
| 312 | + [200, {"Content-Type" => "application/json"}, response_payload.to_json] |
246 | 313 | end |
247 | 314 | end |
248 | 315 | end |
|
260 | 327 | id_and_secret = "#{client_id}:#{client_secret}" |
261 | 328 | basic_auth = "Basic #{Base64.urlsafe_encode64(id_and_secret)}" |
262 | 329 | fail "Basic Authorization is missing." unless env.request_headers["Authorization"] == basic_auth |
| 330 | + [200, {"Content-Type" => "application/json"}, response_payload.to_json] |
263 | 331 | end |
264 | 332 | end |
265 | 333 | end |
|
286 | 354 | end |
287 | 355 | let(:error) { "invalid_request" } |
288 | 356 |
|
289 | | - it { is_expected.to be_nil } |
| 357 | + it "raises a CognitoIdp::Error" do |
| 358 | + expect { token }.to raise_error(CognitoIdp::Error) do |e| |
| 359 | + expect(e.error).to eq("invalid_request") |
| 360 | + expect(e.http_status).to eq(400) |
| 361 | + end |
| 362 | + end |
290 | 363 | end |
291 | 364 | end |
292 | 365 |
|
|
349 | 422 | end |
350 | 423 | let(:error) { "invalid_request" } |
351 | 424 |
|
352 | | - it { is_expected.to be_nil } |
| 425 | + it "raises a CognitoIdp::Error" do |
| 426 | + expect { token }.to raise_error(CognitoIdp::Error) do |e| |
| 427 | + expect(e.error).to eq("invalid_request") |
| 428 | + expect(e.http_status).to eq(400) |
| 429 | + end |
| 430 | + end |
353 | 431 | end |
354 | 432 | end |
355 | 433 | end |
|
453 | 531 | end |
454 | 532 | let(:error) { "invalid_request" } |
455 | 533 |
|
456 | | - it { is_expected.to be_nil } |
| 534 | + it "raises a CognitoIdp::Error" do |
| 535 | + expect { user_info }.to raise_error(CognitoIdp::Error) do |e| |
| 536 | + expect(e.error).to eq("invalid_request") |
| 537 | + expect(e.http_status).to eq(400) |
| 538 | + end |
| 539 | + end |
| 540 | + end |
| 541 | + |
| 542 | + context "when response is an unauthorized error" do |
| 543 | + let(:token) { "ACCESS_TOKEN" } |
| 544 | + let(:access_token) { token } |
| 545 | + let(:stubs) do |
| 546 | + Faraday::Adapter::Test::Stubs.new do |stub| |
| 547 | + stub.post("https://auth.example.com/oauth2/userInfo") do |env| |
| 548 | + [401, {"Content-Type" => "application/json"}, {error: "invalid_token", error_description: "Access token is expired"}.to_json] |
| 549 | + end |
| 550 | + end |
| 551 | + end |
| 552 | + |
| 553 | + it "raises a CognitoIdp::Error with error_description" do |
| 554 | + expect { user_info }.to raise_error(CognitoIdp::Error) do |e| |
| 555 | + expect(e.error).to eq("invalid_token") |
| 556 | + expect(e.error_description).to eq("Access token is expired") |
| 557 | + expect(e.http_status).to eq(401) |
| 558 | + end |
| 559 | + end |
| 560 | + end |
| 561 | + |
| 562 | + context "when response is a server error" do |
| 563 | + let(:token) { "ACCESS_TOKEN" } |
| 564 | + let(:access_token) { token } |
| 565 | + let(:stubs) do |
| 566 | + Faraday::Adapter::Test::Stubs.new do |stub| |
| 567 | + stub.post("https://auth.example.com/oauth2/userInfo") do |env| |
| 568 | + [500, {"Content-Type" => "text/plain"}, "Internal Server Error"] |
| 569 | + end |
| 570 | + end |
| 571 | + end |
| 572 | + |
| 573 | + it "raises a CognitoIdp::Error with http_error" do |
| 574 | + expect { user_info }.to raise_error(CognitoIdp::Error) do |e| |
| 575 | + expect(e.error).to eq("http_error") |
| 576 | + expect(e.error_description).to eq("the server responded with status 500") |
| 577 | + expect(e.http_status).to eq(500) |
| 578 | + end |
| 579 | + end |
| 580 | + end |
| 581 | + |
| 582 | + context "when response is an error it does not yield" do |
| 583 | + let(:token) { "ACCESS_TOKEN" } |
| 584 | + let(:access_token) { token } |
| 585 | + let(:stubs) do |
| 586 | + Faraday::Adapter::Test::Stubs.new do |stub| |
| 587 | + stub.post("https://auth.example.com/oauth2/userInfo") do |env| |
| 588 | + [400, {"Content-Type" => "application/json"}, {error: "invalid_request"}.to_json] |
| 589 | + end |
| 590 | + end |
| 591 | + end |
| 592 | + |
| 593 | + it "does not yield the block" do |
| 594 | + expect { |b| client.get_user_info(access_token, &b) }.to raise_error(CognitoIdp::Error) |
| 595 | + end |
457 | 596 | end |
458 | 597 | end |
459 | 598 |
|
|
0 commit comments