-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathswagger_spec.rb
353 lines (301 loc) · 11.9 KB
/
swagger_spec.rb
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
# frozen_string_literal: true
require 'spec_helper'
describe 'Swagger' do
it "uses grape-swagger=#{GrapeSwagger::VERSION} grape-swagger-rails=#{GrapeSwaggerRails::VERSION}" do
expect(GrapeSwagger::VERSION).not_to be_blank
expect(GrapeSwaggerRails::VERSION).not_to be_blank
end
context 'swaggerUi' do
before do
visit '/swagger'
end
it 'loads foos resource' do
expect(page).to have_css 'li#resource_foos'
end
it 'loads Swagger UI' do
expect(page.evaluate_script('window.swaggerUi != null')).to be true
end
end
describe '#options' do
before do
@options = GrapeSwaggerRails.options.dup
end
after do
GrapeSwaggerRails.options = @options
end
it 'evaluates config options correctly' do
visit '/swagger'
page_options = page.evaluate_script("$('html').data('swagger-options')").symbolize_keys
expect(page_options).to eq(@options.marshal_dump)
end
describe '#headers' do
before do
GrapeSwaggerRails.options.headers['X-Test-Header'] = 'Test Value'
GrapeSwaggerRails.options.headers['X-Another-Header'] = 'Another Value'
visit '/swagger'
end
it 'adds headers' do
headers = page.evaluate_script('swaggerUi.api.clientAuthorizations')['authz']
expect(headers.select { |key| key.to_s.match(/^header/) }).not_to be_blank
expect(headers.fetch('header_0', {}).fetch('name', {})).to eq GrapeSwaggerRails.options.headers.keys.first
find_by_id('endpointListTogger_headers', visible: true).click
first('span[class="http_method"] a', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.hljs-attr', text: /X-Test-Header/i
expect(page).to have_css 'span.hljs-string', text: 'Test Value'
end
it 'supports multiple headers' do
find_by_id('endpointListTogger_headers', visible: true).click
first('span[class="http_method"] a', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.hljs-attr', text: /X-Test-Header/i
expect(page).to have_css 'span.hljs-string', text: 'Test Value'
expect(page).to have_css 'span.hljs-attr', text: /X-Another-Header/i
expect(page).to have_css 'span.hljs-string', text: 'Another Value'
end
end
describe '#api_key_default_value' do
before do
GrapeSwaggerRails.options.api_auth = 'bearer'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
GrapeSwaggerRails.options.api_key_default_value = 'token'
visit '/swagger'
end
it 'adds an Authorization header' do
headers = page.evaluate_script('swaggerUi.api.clientAuthorizations')['authz']
last_header = headers.fetch("header_#{headers.length - 1}", {})
expect(last_header.slice('name', 'value'))
.to eq('name' => 'Authorization', 'value' => 'Bearer token')
end
end
describe '#api_key_placeholder' do
before do
GrapeSwaggerRails.options.api_key_placeholder = 'authorization_code'
visit '/swagger'
end
it 'adds a custom placeholder' do
expect(find_by_id('input_apiKey')['placeholder']).to eq 'authorization_code'
end
end
describe '#api_auth:basic' do
before do
GrapeSwaggerRails.options.api_auth = 'basic'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
visit '/swagger'
end
it 'adds an Authorization header' do
page.execute_script("$('#input_apiKey').val('username:password')")
page.execute_script("$('#input_apiKey').trigger('change')")
find_by_id('endpointListTogger_headers', visible: true).click
first('span[class="http_method"] a', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.hljs-attr', text: /Authorization/i
expect(page).to have_css 'span.hljs-string', text: "Basic #{Base64.encode64('username:password').strip}"
end
end
describe '#api_auth:bearer' do
before do
GrapeSwaggerRails.options.api_auth = 'bearer'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
visit '/swagger'
end
it 'adds an Authorization header' do
page.execute_script("$('#input_apiKey').val('token')")
page.execute_script("$('#input_apiKey').trigger('change')")
find_by_id('endpointListTogger_headers', visible: true).click
first('span[class="http_method"] a', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.hljs-attr', text: /Authorization/i
expect(page).to have_css 'span.hljs-string', text: 'Bearer token'
end
end
describe '#api_auth:token and #api_key_type:header' do
before do
GrapeSwaggerRails.options.api_auth = 'token'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
visit '/swagger'
end
it 'adds an Authorization header' do
page.execute_script("$('#input_apiKey').val('token')")
page.execute_script("$('#input_apiKey').trigger('change')")
find_by_id('endpointListTogger_headers', visible: true).click
first('span[class="http_method"] a', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.hljs-attr', text: /Authorization/i
expect(page).to have_css 'span.hljs-string', text: 'Token token'
end
end
describe '#api_auth:token' do
before do
GrapeSwaggerRails.options.api_key_name = 'api_token'
GrapeSwaggerRails.options.api_key_type = 'query'
visit '/swagger'
end
it 'adds an api_token query parameter' do
page.execute_script("$('#input_apiKey').val('dummy')")
page.execute_script("$('#input_apiKey').trigger('change')")
find_by_id('endpointListTogger_params', visible: true).click
first('span[class="http_method"] a', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.hljs-attr', text: 'api_token'
expect(page).to have_css 'span.hljs-string', text: 'dummy'
end
end
describe '#before_filter' do
before do
allow(GrapeSwaggerRails.deprecator).to receive(:warn)
end
it 'throws deprecation warning' do
GrapeSwaggerRails.options.before_filter { true }
expect(GrapeSwaggerRails.deprecator).to have_received(:warn).with(
'This option is deprecated and going to be removed in 1.0.0. ' \
'Please use `before_action` instead'
)
end
end
describe '#before_action' do
before do
GrapeSwaggerRails.options.before_action do |_request|
flash[:error] = 'Unauthorized Access'
redirect_to '/'
false
end
visit '/swagger'
end
it 'denies access' do
expect(page).to have_current_path '/', ignore_query: true
expect(page).to have_content 'Unauthorized Access'
end
end
describe '#app_name' do
context 'set' do
before do
GrapeSwaggerRails.options.app_name = 'Test App'
visit '/swagger'
end
it 'sets page title' do
expect(page.title).to eq 'Test App'
end
end
context 'not set' do
before do
visit '/swagger'
end
it 'defaults page title' do
expect(page.title).to eq 'Swagger'
end
end
end
describe '#doc_expansion' do
context 'set list' do
before do
GrapeSwaggerRails.options.doc_expansion = 'list'
visit '/swagger'
end
it 'sets SwaggerUI docExpansion with list' do
expect(page.evaluate_script('window.swaggerUi.options.docExpansion == "list"')).to be true
end
end
context 'set full' do
before do
GrapeSwaggerRails.options.doc_expansion = 'full'
visit '/swagger'
end
it 'sets SwaggerUI docExpansion with full' do
expect(page.evaluate_script('window.swaggerUi.options.docExpansion == "full"')).to be true
end
end
context 'not set' do
before do
visit '/swagger'
end
it 'defaults SwaggerUI docExpansion' do
expect(page.evaluate_script('window.swaggerUi.options.docExpansion == "none"')).to be true
end
end
end
describe '#supported_submit_methods' do
context 'set all operations' do
before do
GrapeSwaggerRails.options.supported_submit_methods = %w[get post put delete patch]
visit '/swagger'
end
it 'sets SwaggerUI supportedSubmitMethods with all operations' do
expect(page.evaluate_script('window.swaggerUi.options.supportedSubmitMethods.length')).to eq 5
find_by_id('endpointListTogger_params', visible: true).click
first('span[class="http_method"] a', visible: true).click
expect(page).to have_button('Try it out!', disabled: false)
end
end
context 'set some operations' do
before do
GrapeSwaggerRails.options.supported_submit_methods = ['post']
visit '/swagger'
end
it 'sets SwaggerUI supportedSubmitMethods with some operations' do
expect(page.evaluate_script('window.swaggerUi.options.supportedSubmitMethods.length')).to eq 1
find_by_id('endpointListTogger_params', visible: true).click
first('span[class="http_method"] a', visible: true).click
expect(page).to have_no_button('Try it out!')
end
end
context 'set nil' do
before do
GrapeSwaggerRails.options.supported_submit_methods = nil
visit '/swagger'
end
it 'clears SwaggerUI supportedSubmitMethods' do
expect(page.evaluate_script('window.swaggerUi.options.supportedSubmitMethods.length')).to eq 0
find_by_id('endpointListTogger_params', visible: true).click
first('span[class="http_method"] a', visible: true).click
expect(page).to have_no_button('Try it out!')
end
end
context 'not set' do
before do
visit '/swagger'
end
it 'defaults SwaggerUI supportedSubmitMethods' do
expect(page.evaluate_script('window.swaggerUi.options.supportedSubmitMethods.length')).to eq 5
find_by_id('endpointListTogger_params', visible: true).click
first('span[class="http_method"] a', visible: true).click
expect(page).to have_button('Try it out!', disabled: false)
end
end
end
describe '#validator_url' do
context 'set null' do
before do
GrapeSwaggerRails.options.validator_url = nil
visit '/swagger'
end
it 'sets SwaggerUI validatorUrl to null' do
expect(page.evaluate_script('window.swaggerUi.options.validatorUrl === null && ' \
'typeof window.swaggerUi.options.validatorUrl === "object"')).to be true
end
end
context 'set a url' do
before do
GrapeSwaggerRails.options.validator_url = 'http://www.example.com/'
visit '/swagger'
end
it 'sets SwaggerUI validatorUrl to expected url' do
expect(page.evaluate_script('window.swaggerUi.options.validatorUrl === "http://www.example.com/"')).to be true
end
end
context 'not set' do
before do
visit '/swagger'
end
it 'defaults SwaggerUI validatorUrl' do
expect(page.evaluate_script('window.swaggerUi.options.validatorUrl === undefined && ' \
'typeof window.swaggerUi.options.validatorUrl === "undefined"')).to be true
end
end
end
end
end