Skip to content

Commit 6ad5581

Browse files
blakenumbata
authored andcommitted
Fix guarded endpoint spec, after a rebase
1 parent 736aa14 commit 6ad5581

File tree

4 files changed

+131
-114
lines changed

4 files changed

+131
-114
lines changed

lib/grape-swagger.rb

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
require 'grape-swagger/instance'
66

7+
require 'grape-swagger/errors'
78
require 'grape-swagger/version'
89
require 'grape-swagger/model_parsers'
910

@@ -16,41 +17,6 @@ def model_parsers
1617
autoload :Rake, 'grape-swagger/rake/oapi_tasks'
1718
end
1819

19-
def add_swagger_documentation(options = {})
20-
options = { target_class: self }.merge(options)
21-
22-
version_for(options)
23-
24-
documentation_class = if options[:openapi_version] == '3.0'
25-
require 'grape-swagger/openapi_3/openapi3'
26-
OpenApi.new.add_swagger_documentation(options)
27-
else
28-
require 'grape-swagger/swagger_2/swagger2'
29-
Swagger.new.add_swagger_documentation(options)
30-
end
31-
32-
@target_class = options[:target_class]
33-
34-
mount(documentation_class)
35-
36-
@target_class.combined_routes = {}
37-
combine_routes(@target_class, documentation_class)
38-
39-
@target_class.combined_namespaces = {}
40-
combine_namespaces(@target_class)
41-
42-
@target_class.combined_namespace_routes = {}
43-
@target_class.combined_namespace_identifiers = {}
44-
combine_namespace_routes(@target_class.combined_namespaces)
45-
46-
exclusive_route_keys = @target_class.combined_routes.keys - @target_class.combined_namespaces.keys
47-
exclusive_route_keys.each do |key|
48-
@target_class.combined_namespace_routes[key] = @target_class.combined_routes[key]
49-
end
50-
51-
documentation_class
52-
end
53-
5420
module SwaggerRouting
5521
private
5622

@@ -148,11 +114,10 @@ module SwaggerDocumentationAdder
148114
include SwaggerRouting
149115

150116
def add_swagger_documentation(options = {})
151-
documentation_class = create_documentation_class
152-
153-
version_for(options)
154117
options = { target_class: self }.merge(options)
118+
version_for(options)
155119
@target_class = options[:target_class]
120+
documentation_class = create_documentation_class(options[:openapi_version])
156121
auth_wrapper = options[:endpoint_auth_wrapper] || Class.new
157122

158123
use auth_wrapper if auth_wrapper.method_defined?(:before) && !middleware.flatten.include?(auth_wrapper)
@@ -200,9 +165,17 @@ def combine_namespaces(app)
200165
combined_namespaces
201166
end
202167

203-
def create_documentation_class
204-
Class.new(GrapeInstance) do
205-
extend GrapeSwagger::DocMethods
168+
def create_documentation_class(openapi_version)
169+
Class.new(Grape::API) do
170+
if openapi_version == '3.0'
171+
require 'grape-swagger/openapi_3/endpoint'
172+
require 'grape-swagger/openapi_3/doc_methods'
173+
extend GrapeOpenAPI::DocMethods
174+
else
175+
require 'grape-swagger/swagger_2/endpoint'
176+
require 'grape-swagger/swagger_2/doc_methods'
177+
extend GrapeSwagger::DocMethods
178+
end
206179
end
207180
end
208181
end

lib/grape-swagger/openapi_3/openapi3.rb

Lines changed: 0 additions & 36 deletions
This file was deleted.

lib/grape-swagger/swagger_2/swagger2.rb

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
class SampleAuth < Grape::Middleware::Base
6+
module AuthMethods
7+
attr_accessor :access_token
8+
9+
def protected_endpoint=(protected)
10+
@protected_endpoint = protected
11+
end
12+
13+
def protected_endpoint?
14+
@protected_endpoint || false
15+
end
16+
17+
def resource_owner
18+
@resource_owner = true if access_token == '12345'
19+
end
20+
end
21+
22+
def context
23+
env['api.endpoint']
24+
end
25+
26+
def before
27+
context.extend(SampleAuth::AuthMethods)
28+
context.protected_endpoint = context.options[:route_options][:auth].present?
29+
30+
return unless context.protected_endpoint?
31+
32+
scopes = context.options[:route_options][:auth][:scopes]
33+
authorize!(*scopes) unless scopes.include? false
34+
context.access_token = env['HTTP_AUTHORIZATION']
35+
end
36+
end
37+
38+
module Extension
39+
def sample_auth(*scopes)
40+
description = route_setting(:description) || route_setting(:description, {})
41+
description[:auth] = { scopes: scopes }
42+
end
43+
44+
Grape::API.extend self
45+
end
46+
47+
describe 'a guarded api endpoint' do
48+
before :all do
49+
class GuardedMountedApi < Grape::API
50+
resource_owner_valid = proc { |token_owner = nil| token_owner.nil? }
51+
52+
desc 'Show endpoint if authenticated'
53+
route_setting :swagger, hidden: resource_owner_valid
54+
get '/auth' do
55+
{ foo: 'bar' }
56+
end
57+
end
58+
59+
class GuardedApi < Grape::API
60+
mount GuardedMountedApi
61+
add_swagger_documentation openapi_version: '3.0',
62+
endpoint_auth_wrapper: SampleAuth,
63+
swagger_endpoint_guard: 'sample_auth false',
64+
token_owner: 'resource_owner'
65+
end
66+
end
67+
68+
def app
69+
GuardedApi
70+
end
71+
72+
context 'when a correct token is passed with the request' do
73+
subject do
74+
get '/swagger_doc.json', {}, 'HTTP_AUTHORIZATION' => '12345'
75+
JSON.parse(last_response.body)
76+
end
77+
78+
it 'retrieves swagger-documentation for the endpoint' do
79+
expect(subject).to eq(
80+
'info' => { 'title' => 'API title', 'version' => '0.0.1' },
81+
'openapi' => '3.0.0',
82+
'servers' => [{ 'url' => 'http://example.org' }],
83+
'tags' => [{ 'name' => 'auth', 'description' => 'Operations about auths' }],
84+
'paths' => {
85+
'/auth' => {
86+
'get' => {
87+
'description' => 'Show endpoint if authenticated',
88+
'operationId' => 'getAuth',
89+
'responses' => {
90+
'200' => {
91+
'content' => { 'application/json' => {} },
92+
'description' => 'Show endpoint if authenticated'
93+
}
94+
},
95+
'tags' => ['auth']
96+
}
97+
}
98+
}
99+
)
100+
end
101+
end
102+
103+
context 'when a bad token is passed with the request' do
104+
subject do
105+
get '/swagger_doc.json', {}, 'HTTP_AUTHORIZATION' => '123456'
106+
JSON.parse(last_response.body)
107+
end
108+
109+
it 'does not retrieve swagger-documentation for the endpoint - only the info_object' do
110+
expect(subject).to eq(
111+
'info' => { 'title' => 'API title', 'version' => '0.0.1' },
112+
'openapi' => '3.0.0',
113+
'servers' => [{ 'url' => 'http://example.org' }]
114+
)
115+
end
116+
end
117+
end

0 commit comments

Comments
 (0)