|
| 1 | +require 'core/spec_helper' |
| 2 | + |
| 3 | +describe ZendeskAPI::Middleware::Response::TokenRefresher do |
| 4 | + let(:refresh_token_body) do |
| 5 | + { |
| 6 | + access_token: "nt", |
| 7 | + refresh_token: "nrt", |
| 8 | + token_type: "bearer", |
| 9 | + scope: "read write", |
| 10 | + expires_in: 300, |
| 11 | + refresh_token_expires_in: 604800 |
| 12 | + } |
| 13 | + end |
| 14 | + |
| 15 | + before do |
| 16 | + skip "TODO: If you decide to use TokenRefresher as a middleware, remove this skip." |
| 17 | + |
| 18 | + client.config.client_id = "client" |
| 19 | + client.config.client_secret = "secret" |
| 20 | + client.config.refresh_token = "abc" |
| 21 | + end |
| 22 | + |
| 23 | + describe "with access token" do |
| 24 | + before do |
| 25 | + client.config.access_token = "xyz" |
| 26 | + end |
| 27 | + |
| 28 | + describe "when unauthorized" do |
| 29 | + before do |
| 30 | + stub_request(:any, /whatever/).to_return( |
| 31 | + status: 401, |
| 32 | + body: "", |
| 33 | + headers: { content_type: "application/json" } |
| 34 | + ) |
| 35 | + stub_request(:post, %r{/oauth/tokens}).to_return( |
| 36 | + status: refresh_token_status, |
| 37 | + body: refresh_token_body.to_json, |
| 38 | + headers: { content_type: "application/json" } |
| 39 | + ) |
| 40 | + end |
| 41 | + |
| 42 | + describe "when refreshing token succeeds" do |
| 43 | + let(:refresh_token_status) { 200 } |
| 44 | + |
| 45 | + it "refreshes token" do |
| 46 | + expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
| 47 | + |
| 48 | + expect(client.config.access_token).to eq "nt" |
| 49 | + expect(client.config.refresh_token).to eq "nrt" |
| 50 | + end |
| 51 | + |
| 52 | + it "calls refresh token callback" do |
| 53 | + new_access_token = nil |
| 54 | + new_refresh_token = nil |
| 55 | + client.config.refresh_token_callback = lambda do |access_token, refresh_token| |
| 56 | + new_access_token = access_token |
| 57 | + new_refresh_token = refresh_token |
| 58 | + end |
| 59 | + expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
| 60 | + |
| 61 | + expect(new_access_token).to eq "nt" |
| 62 | + expect(new_refresh_token).to eq "nrt" |
| 63 | + end |
| 64 | + |
| 65 | + it "raises unauthorized exception" do |
| 66 | + expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe "when refreshing token fails" do |
| 71 | + let(:refresh_token_status) { 500 } |
| 72 | + |
| 73 | + it "does not update configuration" do |
| 74 | + expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::NetworkError) |
| 75 | + |
| 76 | + expect(client.config.access_token).to eq "xyz" |
| 77 | + expect(client.config.refresh_token).to eq "abc" |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + describe "when ok" do |
| 83 | + before do |
| 84 | + stub_request(:any, /whatever/).to_return( |
| 85 | + status: 200, |
| 86 | + body: "", |
| 87 | + headers: { content_type: "application/json" } |
| 88 | + ) |
| 89 | + end |
| 90 | + |
| 91 | + it "does not refresh token" do |
| 92 | + client.connection.get "/whatever" |
| 93 | + |
| 94 | + expect_any_instance_of(ZendeskAPI::TokenRefresher).to receive(:refresh_token).never |
| 95 | + expect(client.config.access_token).to eq "xyz" |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + describe "with other type of authorization" do |
| 101 | + before do |
| 102 | + client.config.username = "xyz" |
| 103 | + client.config.password = "xyz" |
| 104 | + |
| 105 | + stub_request(:any, /whatever/).to_return( |
| 106 | + status: 401, |
| 107 | + body: "", |
| 108 | + headers: { content_type: "application/json" } |
| 109 | + ) |
| 110 | + end |
| 111 | + |
| 112 | + it "refreshes token" do |
| 113 | + expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
| 114 | + |
| 115 | + expect_any_instance_of(ZendeskAPI::TokenRefresher).to receive(:refresh_token).never |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments