|
13 | 13 | end |
14 | 14 |
|
15 | 15 | before do |
| 16 | + skip "TODO: If you decide to use TokenRefresher as a middleware, remove this skip." |
| 17 | + |
16 | 18 | client.config.client_id = "client" |
17 | 19 | client.config.client_secret = "secret" |
18 | 20 | client.config.refresh_token = "abc" |
|
30 | 32 | body: "", |
31 | 33 | headers: { content_type: "application/json" } |
32 | 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 | + ) |
33 | 40 | end |
34 | 41 |
|
35 | 42 | describe "when refreshing token succeeds" do |
36 | 43 | let(:refresh_token_status) { 200 } |
37 | 44 |
|
38 | | - before do |
39 | | - stub_request(:post, %r{/oauth/tokens}).to_return( |
40 | | - status: refresh_token_status, |
41 | | - body: refresh_token_body.to_json, |
42 | | - headers: { content_type: "application/json" } |
43 | | - ) |
44 | | - end |
45 | | - |
46 | 45 | it "refreshes token" do |
47 | 46 | expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
48 | 47 |
|
49 | | - assert_requested :post, %r{oauth/tokens} |
50 | 48 | expect(client.config.access_token).to eq "nt" |
51 | 49 | expect(client.config.refresh_token).to eq "nrt" |
52 | 50 | end |
|
64 | 62 | expect(new_refresh_token).to eq "nrt" |
65 | 63 | end |
66 | 64 |
|
67 | | - it "does not include expiration params when not configured" do |
68 | | - expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
69 | | - |
70 | | - assert_requested(:post, %r{oauth/tokens}) do |req| |
71 | | - expect(req.body).to_not match(/expires_in/) |
72 | | - expect(req.body).to_not match(/refresh_token_expires_in/) |
73 | | - end |
74 | | - end |
75 | | - |
76 | | - it "includes expiration params when configured" do |
77 | | - client.config.access_token_expiration = 300 |
78 | | - client.config.refresh_token_expiration = 604800 |
79 | | - expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
80 | | - |
81 | | - assert_requested(:post, %r{oauth/tokens}) do |req| |
82 | | - expect(req.body).to match(/expires_in/) |
83 | | - expect(req.body).to match(/refresh_token_expires_in/) |
84 | | - end |
85 | | - end |
86 | | - |
87 | 65 | it "raises unauthorized exception" do |
88 | 66 | expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
89 | 67 | end |
90 | 68 | end |
91 | 69 |
|
92 | | - describe "with client id not configuration" do |
93 | | - before do |
94 | | - client.config.client_id = nil |
95 | | - end |
96 | | - |
97 | | - it "does not try to refresh token" do |
98 | | - expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
99 | | - |
100 | | - refute_requested :post, %r{/oauth/tokens} |
101 | | - expect(client.config.access_token).to eq "xyz" |
102 | | - end |
103 | | - end |
104 | | - |
105 | | - describe "with client secret not configuration" do |
106 | | - before do |
107 | | - client.config.client_secret = nil |
108 | | - end |
109 | | - |
110 | | - it "does not try to refresh token" do |
111 | | - expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
112 | | - |
113 | | - refute_requested :post, %r{/oauth/tokens} |
114 | | - expect(client.config.access_token).to eq "xyz" |
115 | | - end |
116 | | - end |
117 | | - |
118 | | - describe "with client secret not configuration" do |
119 | | - before do |
120 | | - client.config.refresh_token = nil |
121 | | - end |
122 | | - |
123 | | - it "does not try to refresh token" do |
124 | | - expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
125 | | - |
126 | | - refute_requested :post, %r{oauth/tokens} |
127 | | - expect(client.config.access_token).to eq "xyz" |
128 | | - end |
129 | | - end |
130 | | - |
131 | 70 | describe "when refreshing token fails" do |
132 | 71 | let(:refresh_token_status) { 500 } |
133 | 72 |
|
134 | | - before do |
135 | | - stub_request(:post, %r{/oauth/tokens}).to_return( |
136 | | - status: refresh_token_status, |
137 | | - body: refresh_token_body.to_json, |
138 | | - headers: { content_type: "application/json" } |
139 | | - ) |
140 | | - end |
141 | | - |
142 | | - it "does not change token configuration" do |
| 73 | + it "does not update configuration" do |
143 | 74 | expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::NetworkError) |
144 | 75 |
|
145 | | - assert_requested :post, %r{oauth/tokens} |
146 | 76 | expect(client.config.access_token).to eq "xyz" |
147 | 77 | expect(client.config.refresh_token).to eq "abc" |
148 | 78 | end |
|
158 | 88 | ) |
159 | 89 | end |
160 | 90 |
|
161 | | - it "succeeds" do |
162 | | - result = client.connection.get "/whatever" |
163 | | - expect(result.status).to eq 200 |
164 | | - end |
165 | | - |
166 | 91 | it "does not refresh token" do |
167 | 92 | client.connection.get "/whatever" |
168 | 93 |
|
169 | | - refute_requested :post, %r{oauth/tokens} |
| 94 | + expect_any_instance_of(ZendeskAPI::TokenRefresher).to receive(:refresh_token).never |
170 | 95 | expect(client.config.access_token).to eq "xyz" |
171 | 96 | end |
172 | 97 | end |
|
182 | 107 | body: "", |
183 | 108 | headers: { content_type: "application/json" } |
184 | 109 | ) |
185 | | - |
186 | | - stub_request(:post, %r{/oauth/tokens}).to_return( |
187 | | - status: 200, |
188 | | - body: refresh_token_body.to_json, |
189 | | - headers: { content_type: "application/json" } |
190 | | - ) |
191 | 110 | end |
192 | 111 |
|
193 | 112 | it "refreshes token" do |
194 | 113 | expect { client.connection.get "/whatever" }.to raise_error(ZendeskAPI::Error::Unauthorized) |
195 | 114 |
|
196 | | - refute_requested :post, %r{oauth/tokens} |
| 115 | + expect_any_instance_of(ZendeskAPI::TokenRefresher).to receive(:refresh_token).never |
197 | 116 | end |
198 | 117 | end |
199 | 118 | end |
0 commit comments