Skip to content

Commit b35b14f

Browse files
authored
Merge pull request #737 from urkle/fix-swagger-endpoint-guard
add the swagger_endpoint_guard to both documentation endpoints
2 parents c7c93b6 + 69fd941 commit b35b14f

File tree

4 files changed

+88
-37
lines changed

4 files changed

+88
-37
lines changed

.rubocop_todo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Gemspec/RequiredRubyVersion:
2222

2323
# Offense count: 30
2424
Metrics/AbcSize:
25-
Max: 57
25+
Max: 59
2626

2727
# Offense count: 10
2828
Metrics/CyclomaticComplexity:
@@ -31,7 +31,7 @@ Metrics/CyclomaticComplexity:
3131
# Offense count: 22
3232
# Configuration parameters: CountComments, ExcludedMethods.
3333
Metrics/MethodLength:
34-
Max: 44
34+
Max: 45
3535

3636
# Offense count: 7
3737
Metrics/PerceivedComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#737](https://github.com/ruby-grape/grape-swagger/pull/737): Add swagger endpoint guard to both doc endpoints - [@urkle](https://github.com/urkle).
1011

1112
### 0.32.1 (December 7, 2018)
1213

lib/grape-swagger/doc_methods.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ def setup(options)
4545

4646
desc api_doc.delete(:desc), api_doc
4747

48-
instance_eval(guard) unless guard.nil?
49-
5048
output_path_definitions = proc do |combi_routes, endpoint|
5149
output = endpoint.swagger_object(
5250
target_class,
@@ -64,6 +62,8 @@ def setup(options)
6462
output
6563
end
6664

65+
instance_eval(guard) unless guard.nil?
66+
6767
get mount_path do
6868
header['Access-Control-Allow-Origin'] = '*'
6969
header['Access-Control-Request-Method'] = '*'
@@ -79,6 +79,8 @@ def setup(options)
7979
optional :locale, type: Symbol, desc: 'Locale of API documentation'
8080
end
8181

82+
instance_eval(guard) unless guard.nil?
83+
8284
get "#{mount_path}/:name" do
8385
I18n.locale = params[:locale] || I18n.default_locale
8486

spec/swagger_v2/guarded_endpoint_spec.rb

Lines changed: 81 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,47 +68,95 @@ def app
6868
GuardedApi
6969
end
7070

71-
context 'when a correct token is passed with the request' do
72-
subject do
73-
get '/swagger_doc.json', {}, 'HTTP_AUTHORIZATION' => '12345'
74-
JSON.parse(last_response.body)
75-
end
71+
let(:endpoint) { '/swagger_doc.json' }
72+
let(:auth_token) { nil }
73+
74+
subject do
75+
get endpoint, {}, 'HTTP_AUTHORIZATION' => auth_token
76+
JSON.parse(last_response.body)
77+
end
7678

77-
it 'retrieves swagger-documentation for the endpoint' do
78-
expect(subject).to eq(
79-
'info' => { 'title' => 'API title', 'version' => '0.0.1' },
80-
'swagger' => '2.0',
81-
'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
82-
'host' => 'example.org',
83-
'tags' => [{ 'name' => 'auth', 'description' => 'Operations about auths' }],
84-
'paths' => {
85-
'/auth' => {
86-
'get' => {
87-
'description' => 'Show endpoint if authenticated',
88-
'produces' => ['application/json'],
89-
'tags' => ['auth'],
90-
'operationId' => 'getAuth',
91-
'responses' => { '200' => { 'description' => 'Show endpoint if authenticated' } }
79+
context 'accessing the main doc endpoint' do
80+
let(:endpoint) { '/swagger_doc.json' }
81+
82+
context 'when a correct token is passed with the request' do
83+
let(:auth_token) { '12345' }
84+
85+
it 'retrieves swagger-documentation for the endpoint' do
86+
expect(subject).to eq(
87+
'info' => { 'title' => 'API title', 'version' => '0.0.1' },
88+
'swagger' => '2.0',
89+
'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
90+
'host' => 'example.org',
91+
'tags' => [{ 'name' => 'auth', 'description' => 'Operations about auths' }],
92+
'paths' => {
93+
'/auth' => {
94+
'get' => {
95+
'description' => 'Show endpoint if authenticated',
96+
'produces' => ['application/json'],
97+
'tags' => ['auth'],
98+
'operationId' => 'getAuth',
99+
'responses' => { '200' => { 'description' => 'Show endpoint if authenticated' } }
100+
}
92101
}
93102
}
94-
}
95-
)
103+
)
104+
end
105+
end
106+
107+
context 'when a bad token is passed with the request' do
108+
let(:auth_token) { '123456' }
109+
110+
it 'does not retrieve swagger-documentation for the endpoint - only the info_object' do
111+
expect(subject).to eq(
112+
'info' => { 'title' => 'API title', 'version' => '0.0.1' },
113+
'swagger' => '2.0',
114+
'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
115+
'host' => 'example.org'
116+
)
117+
end
96118
end
97119
end
98120

99-
context 'when a bad token is passed with the request' do
100-
subject do
101-
get '/swagger_doc.json', {}, 'HTTP_AUTHORIZATION' => '123456'
102-
JSON.parse(last_response.body)
121+
context 'accessing the tag specific endpoint' do
122+
let(:endpoint) { '/swagger_doc/auth.json' }
123+
124+
context 'when a correct token is passed with the request' do
125+
let(:auth_token) { '12345' }
126+
127+
it 'retrieves swagger-documentation for the endpoint' do
128+
expect(subject).to eq(
129+
'info' => { 'title' => 'API title', 'version' => '0.0.1' },
130+
'swagger' => '2.0',
131+
'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
132+
'host' => 'example.org',
133+
'tags' => [{ 'name' => 'auth', 'description' => 'Operations about auths' }],
134+
'paths' => {
135+
'/auth' => {
136+
'get' => {
137+
'description' => 'Show endpoint if authenticated',
138+
'produces' => ['application/json'],
139+
'tags' => ['auth'],
140+
'operationId' => 'getAuth',
141+
'responses' => { '200' => { 'description' => 'Show endpoint if authenticated' } }
142+
}
143+
}
144+
}
145+
)
146+
end
103147
end
104148

105-
it 'does not retrieve swagger-documentation for the endpoint - only the info_object' do
106-
expect(subject).to eq(
107-
'info' => { 'title' => 'API title', 'version' => '0.0.1' },
108-
'swagger' => '2.0',
109-
'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
110-
'host' => 'example.org'
111-
)
149+
context 'when a bad token is passed with the request' do
150+
let(:auth_token) { '123456' }
151+
152+
it 'does not retrieve swagger-documentation for the endpoint - only the info_object' do
153+
expect(subject).to eq(
154+
'info' => { 'title' => 'API title', 'version' => '0.0.1' },
155+
'swagger' => '2.0',
156+
'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
157+
'host' => 'example.org'
158+
)
159+
end
112160
end
113161
end
114162
end

0 commit comments

Comments
 (0)