Skip to content

Commit be0e363

Browse files
author
Sunny Juneja
committed
Check for oauth2 tokens.
It appears that Swagger-UI correctly sets an authorization header when added in the interface (api key field). However, if the endpoint has the option "authorizations" set with "oauth2", it will override the value. See ruby-grape#13.
1 parent 7caa399 commit be0e363

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

spec/dummy/app/api/api.rb

+5
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ class API < Grape::API
2626
request.params.as_json
2727
end
2828

29+
desc 'Return oauth2 token', authorizations: { oauth2: [] }
30+
get '/oauth2' do
31+
request.headers.slice('Authorization').as_json
32+
end
33+
2934
add_swagger_documentation
3035
end

spec/features/swagger_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@
8989
expect(page).to have_css 'span.string', text: 'dummy'
9090
end
9191
end
92+
context '#api_auth:oauth2' do
93+
before do
94+
GrapeSwaggerRails.options.api_auth = 'bearer'
95+
GrapeSwaggerRails.options.api_key_name = 'Authorization'
96+
GrapeSwaggerRails.options.api_key_type = 'header'
97+
visit '/swagger'
98+
end
99+
it 'adds a token when the route specifies oauth2 authorization' do
100+
page.execute_script("$('#input_apiKey').val('token')")
101+
page.execute_script("$('#input_apiKey').trigger('change')")
102+
find('#endpointListTogger_oauth2', visible: true).click
103+
first('a[href="#!/oauth2/GET_api_oauth2_format"]', visible: true).click
104+
click_button 'Try it out!'
105+
expect(page).to have_css 'span.attribute', text: 'Authorization'
106+
expect(page).to have_css 'span.string', text: 'Bearer token'
107+
end
108+
end
92109
context '#before_filter' do
93110
before do
94111
GrapeSwaggerRails.options.before_filter do |_request|

0 commit comments

Comments
 (0)