Skip to content

Commit 42791e7

Browse files
committed
Fix: before block is incorrectly documented + tests.
1 parent d1b35c0 commit 42791e7

File tree

7 files changed

+89
-29
lines changed

7 files changed

+89
-29
lines changed

README.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,16 @@ You can use the ```api_key``` input box to fill in your API token.
7575
### Swagger UI Authorization
7676

7777
You may want to authenticate users before displaying the Swagger UI, particularly when the API is protected by Basic Authentication.
78-
Use the `authenticate_with` option to inspect the request to the Swagger UI:
78+
Use the `before` option to inspect the request before Swagger UI:
7979

8080
```ruby
81-
GrapeSwaggerRails.options.authenticate_with do |request|
82-
# 1. Inspect the `request` or access the Swagger UI controller via `self`
83-
# 2. Check `current_user` or `can? :access, :api`, etc....
84-
# 3. return a boolean value
81+
GrapeSwaggerRails.options.before_filter do |request|
82+
# 1. Inspect the `request` or access the Swagger UI controller via `self`.
83+
# 2. Check `current_user` or `can? :access, :api`, etc.
84+
# 3. Redirect or error in case of failure.
8585
end
8686
```
8787

88-
The block above is stored in the `authentication_proc` option:
89-
90-
```ruby
91-
GrapeSwaggerRails.options.authentication_proc: Proc.new{|request| # return a boolean value}
92-
```
93-
9488
## Contributing
9589

9690
1. Fork it

app/controllers/grape_swagger_rails/application_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module GrapeSwaggerRails
22
class ApplicationController < ActionController::Base
33
before_filter do
4-
if GrapeSwaggerRails.options.authentication_proc
5-
instance_exec(request, &GrapeSwaggerRails.options.authentication_proc)
4+
if GrapeSwaggerRails.options.before_filter
5+
instance_exec(request, &GrapeSwaggerRails.options.before_filter)
66
end
77
end
8-
8+
99
def index
1010
end
1111
end

lib/grape-swagger-rails.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
module GrapeSwaggerRails
44
class Options < OpenStruct
5-
def authenticate_with(&block)
6-
self.authentication_proc = block
5+
def before_filter(&block)
6+
if block_given?
7+
self.before_filter_proc = block
8+
else
9+
self.before_filter_proc
10+
end
711
end
812
end
913

@@ -21,7 +25,7 @@ def authenticate_with(&block)
2125
api_key_name: 'api_key', # 'Authorization'
2226
api_key_type: 'query', # 'header'
2327

24-
authentication_proc: nil # Proc used as a controller before filter that returns a boolean
28+
before_filter_proc: nil # Proc used as a controller before filter
2529
)
2630

2731
end

spec/dummy/app/api/api.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ class API < Grape::API
2222
request.headers.as_json
2323
end
2424

25+
desc 'Get params.'
26+
get '/params' do
27+
request.params.as_json
28+
end
29+
2530
add_swagger_documentation
2631
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<% flash.each do |name, msg| %>
2+
<% if msg.is_a?(String) %>
3+
<%= content_tag :div, msg, :class => "flash_#{name}" %>
4+
<% end %>
5+
<% end %>

spec/dummy/app/views/layouts/application.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
<html>
33
<head>
44
<title>Nambla</title>
5-
<%= stylesheet_link_tag 'application', media: 'all' %>
5+
<%= stylesheet_link_tag 'application', media: 'all' %>
66
<%= javascript_include_tag 'application' %>
77
<%= csrf_meta_tags %>
88
</head>
99
<body>
1010

11+
<%= render 'layouts/messages' %>
12+
1113
<%= yield %>
1214

1315
</body>

spec/features/swagger_spec.rb

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,70 @@
1212
expect(page.evaluate_script('window.swaggerUi != null')).to be true
1313
end
1414
end
15-
context "#options.headers" do
15+
context "#options" do
1616
before do
17-
GrapeSwaggerRails.options.headers['X-Test-Header'] = 'Test Value'
18-
visit '/swagger'
17+
@options = GrapeSwaggerRails.options.dup
18+
end
19+
context "#headers" do
20+
before do
21+
GrapeSwaggerRails.options.headers['X-Test-Header'] = 'Test Value'
22+
visit '/swagger'
23+
end
24+
it 'adds headers' do
25+
find('#endpointListTogger_headers', visible: true).click
26+
find('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
27+
click_button 'Try it out!'
28+
expect(page).to have_css "span.attribute", text: 'X-Test-Header'
29+
expect(page).to have_css "span.string", text: 'Test Value'
30+
end
31+
end
32+
context "#api_auth:basic" do
33+
before do
34+
GrapeSwaggerRails.options.api_auth = 'basic'
35+
GrapeSwaggerRails.options.api_key_name = 'Authorization'
36+
GrapeSwaggerRails.options.api_key_type = 'header'
37+
visit '/swagger'
38+
end
39+
it 'adds an Authorization header' do
40+
fill_in 'apiKey', with: 'username:password'
41+
find('#endpointListTogger_headers', visible: true).click
42+
find('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
43+
click_button 'Try it out!'
44+
expect(page).to have_css "span.attribute", text: 'Authorization'
45+
expect(page).to have_css "span.string", text: "Basic #{Base64.encode64('username:password').strip}"
46+
end
47+
end
48+
context "#api_auth:token" do
49+
before do
50+
GrapeSwaggerRails.options.api_key_name = 'api_token'
51+
GrapeSwaggerRails.options.api_key_type = 'query'
52+
visit '/swagger'
53+
end
54+
it 'adds an api_token query parameter' do
55+
fill_in 'apiKey', with: 'dummy'
56+
find('#endpointListTogger_params', visible: true).click
57+
find('a[href="#!/params/GET_api_params_format"]', visible: true).click
58+
click_button 'Try it out!'
59+
expect(page).to have_css "span.attribute", text: 'api_token'
60+
expect(page).to have_css "span.string", text: "dummy"
61+
end
62+
end
63+
context "#before_filter" do
64+
before do
65+
GrapeSwaggerRails.options.before_filter do |request|
66+
flash[:error] = "Unauthorized Access"
67+
redirect_to '/'
68+
false
69+
end
70+
visit '/swagger'
71+
end
72+
it 'denies access' do
73+
expect(current_path).to eq "/"
74+
expect(page).to have_content "Unauthorized Access"
75+
end
1976
end
2077
after do
21-
GrapeSwaggerRails.options.headers = {}
22-
end
23-
it 'adds headers' do
24-
find('#endpointListTogger_headers', visible: true).click
25-
find('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
26-
find('.sandbox_header input[name="commit"]', visible: true).click
27-
expect(page).to have_css "span.attribute", text: 'X-Test-Header'
28-
expect(page).to have_css "span.string", text: 'Test Value'
78+
GrapeSwaggerRails.options = @options
2979
end
3080
end
3181
end

0 commit comments

Comments
 (0)