Skip to content

Commit 623a127

Browse files
committed
Add test case for API key auth
1 parent e52694c commit 623a127

File tree

8 files changed

+84
-50
lines changed

8 files changed

+84
-50
lines changed

modules/swagger-codegen/src/main/resources/ruby/api.mustache

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ module {{moduleName}}
5050
{{/bodyParam}}{{#bodyParam}}post_body = Swagger::Request.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}})
5151
{{/bodyParam}}
5252

53-
{{#returnType}}response = Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
54-
{{#returnContainer}}response.map {|response| {{/returnContainer}}obj = {{returnBaseType}}.new() and obj.build_from_hash(response){{#returnContainer}} }{{/returnContainer}}{{/returnType}}{{^returnType}}Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
53+
auth_names = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
54+
{{#returnType}}response = Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
55+
{{#returnContainer}}response.map {|response| {{/returnContainer}}obj = {{returnBaseType}}.new() and obj.build_from_hash(response){{#returnContainer}} }{{/returnContainer}}{{/returnType}}{{^returnType}}Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
5556
nil{{/returnType}}
5657
end
5758
{{/operation}}

modules/swagger-codegen/src/main/resources/ruby/swagger/request.mustache

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ module {{moduleName}}
55
require 'addressable/uri'
66
require 'typhoeus'
77

8-
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params
8+
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names
99

1010
# All requests must have an HTTP method and a path
1111
# Optionals parameters are :params, :headers, :body, :format, :host
1212
def initialize(http_method, path, attributes={})
1313
attributes[:format] ||= Swagger.configuration.format
1414
attributes[:params] ||= {}
1515

16-
update_params_for_auth(attributes)
17-
1816
# Set default headers
1917
default_headers = {
2018
'Content-Type' => "application/#{attributes[:format].downcase}",
@@ -34,20 +32,19 @@ module {{moduleName}}
3432
attributes.each do |name, value|
3533
send("#{name.to_s.underscore.to_sym}=", value)
3634
end
35+
36+
update_params_for_auth!
3737
end
3838

39-
def update_params_for_auth(attributes)
40-
(attributes[:auth_names] || []).each do |auth_name|
39+
def update_params_for_auth!
40+
(@auth_names || []).each do |auth_name|
4141
case auth_name
42-
{{#authMethods}}
43-
when '{{name}}'
44-
{{#isApiKey}}{{#isKeyInHeader}}attributes[:headers] ||= {}
45-
attributes[:headers]['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInHeader}}{{#isKeyInQuery}}attributes[:params] ||= {}
46-
attributes[:params]['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInQuery}}{{/isApiKey}}
47-
{{#isBasic}}attributes[:headers] ||= {}
42+
{{#authMethods}}when '{{name}}'
43+
{{#isApiKey}}{{#isKeyInHeader}}@headers ||= {}
44+
@headers['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInHeader}}{{#isKeyInQuery}}@params ||= {}
45+
@params['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}@headers ||= {}
4846
http_auth_header = 'Basic ' + ["#{Swagger.configuration.username}:#{Swagger.configuration.password}"].pack('m').delete("\r\n")
49-
attributes[:headers]['Authorization'] = http_auth_header{{/isBasic}}
50-
{{#isOAuth}}# TODO: support oauth{{/isOAuth}}
47+
@headers['Authorization'] = http_auth_header{{/isBasic}}{{#isOAuth}}# TODO: support oauth{{/isOAuth}}
5148
{{/authMethods}}
5249
end
5350
end

samples/client/petstore/ruby/lib/swagger_client/api/pet_api.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def self.update_pet(opts = {})
3737
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
3838

3939

40-
Swagger::Request.new(:PUT, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
40+
auth_names = ['petstore_auth']
41+
Swagger::Request.new(:PUT, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
4142
nil
4243
end
4344

@@ -73,7 +74,8 @@ def self.add_pet(opts = {})
7374
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
7475

7576

76-
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
77+
auth_names = ['petstore_auth']
78+
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
7779
nil
7880
end
7981

@@ -110,7 +112,8 @@ def self.find_pets_by_status(opts = {})
110112
post_body = nil
111113

112114

113-
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
115+
auth_names = ['petstore_auth']
116+
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
114117
response.map {|response| obj = Pet.new() and obj.build_from_hash(response) }
115118
end
116119

@@ -147,7 +150,8 @@ def self.find_pets_by_tags(opts = {})
147150
post_body = nil
148151

149152

150-
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
153+
auth_names = ['petstore_auth']
154+
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
151155
response.map {|response| obj = Pet.new() and obj.build_from_hash(response) }
152156
end
153157

@@ -186,7 +190,8 @@ def self.get_pet_by_id(pet_id, opts = {})
186190
post_body = nil
187191

188192

189-
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
193+
auth_names = ['api_key', 'petstore_auth']
194+
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
190195
obj = Pet.new() and obj.build_from_hash(response)
191196
end
192197

@@ -229,7 +234,8 @@ def self.update_pet_with_form(pet_id, opts = {})
229234
post_body = nil
230235

231236

232-
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
237+
auth_names = ['petstore_auth']
238+
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
233239
nil
234240
end
235241

@@ -270,7 +276,8 @@ def self.delete_pet(pet_id, opts = {})
270276
post_body = nil
271277

272278

273-
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
279+
auth_names = ['petstore_auth']
280+
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
274281
nil
275282
end
276283

@@ -313,7 +320,8 @@ def self.upload_file(pet_id, opts = {})
313320
post_body = nil
314321

315322

316-
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
323+
auth_names = ['petstore_auth']
324+
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
317325
nil
318326
end
319327
end

samples/client/petstore/ruby/lib/swagger_client/api/store_api.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def self.get_inventory(opts = {})
3636
post_body = nil
3737

3838

39-
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
39+
auth_names = ['api_key']
40+
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
4041
response.map {|response| obj = map.new() and obj.build_from_hash(response) }
4142
end
4243

@@ -72,7 +73,8 @@ def self.place_order(opts = {})
7273
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
7374

7475

75-
response = Swagger::Request.new(:POST, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
76+
auth_names = []
77+
response = Swagger::Request.new(:POST, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
7678
obj = Order.new() and obj.build_from_hash(response)
7779
end
7880

@@ -111,7 +113,8 @@ def self.get_order_by_id(order_id, opts = {})
111113
post_body = nil
112114

113115

114-
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
116+
auth_names = []
117+
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
115118
obj = Order.new() and obj.build_from_hash(response)
116119
end
117120

@@ -150,7 +153,8 @@ def self.delete_order(order_id, opts = {})
150153
post_body = nil
151154

152155

153-
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
156+
auth_names = []
157+
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
154158
nil
155159
end
156160
end

samples/client/petstore/ruby/lib/swagger_client/api/user_api.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def self.create_user(opts = {})
3737
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
3838

3939

40-
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
40+
auth_names = []
41+
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
4142
nil
4243
end
4344

@@ -73,7 +74,8 @@ def self.create_users_with_array_input(opts = {})
7374
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
7475

7576

76-
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
77+
auth_names = []
78+
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
7779
nil
7880
end
7981

@@ -109,7 +111,8 @@ def self.create_users_with_list_input(opts = {})
109111
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
110112

111113

112-
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
114+
auth_names = []
115+
Swagger::Request.new(:POST, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
113116
nil
114117
end
115118

@@ -148,7 +151,8 @@ def self.login_user(opts = {})
148151
post_body = nil
149152

150153

151-
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
154+
auth_names = []
155+
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
152156
obj = string.new() and obj.build_from_hash(response)
153157
end
154158

@@ -183,7 +187,8 @@ def self.logout_user(opts = {})
183187
post_body = nil
184188

185189

186-
Swagger::Request.new(:GET, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
190+
auth_names = []
191+
Swagger::Request.new(:GET, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
187192
nil
188193
end
189194

@@ -222,7 +227,8 @@ def self.get_user_by_name(username, opts = {})
222227
post_body = nil
223228

224229

225-
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body}).make.body
230+
auth_names = []
231+
response = Swagger::Request.new(:GET, path, {:params => query_params, :headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make.body
226232
obj = User.new() and obj.build_from_hash(response)
227233
end
228234

@@ -262,7 +268,8 @@ def self.update_user(username, opts = {})
262268
post_body = Swagger::Request.object_to_http_body(opts[:'body'])
263269

264270

265-
Swagger::Request.new(:PUT, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
271+
auth_names = []
272+
Swagger::Request.new(:PUT, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
266273
nil
267274
end
268275

@@ -301,7 +308,8 @@ def self.delete_user(username, opts = {})
301308
post_body = nil
302309

303310

304-
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make
311+
auth_names = []
312+
Swagger::Request.new(:DELETE, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body, :auth_names => auth_names}).make
305313
nil
306314
end
307315
end

samples/client/petstore/ruby/lib/swagger_client/swagger/request.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ class Request
55
require 'addressable/uri'
66
require 'typhoeus'
77

8-
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params
8+
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names
99

1010
# All requests must have an HTTP method and a path
1111
# Optionals parameters are :params, :headers, :body, :format, :host
1212
def initialize(http_method, path, attributes={})
1313
attributes[:format] ||= Swagger.configuration.format
1414
attributes[:params] ||= {}
1515

16-
update_params_for_auth(attributes)
17-
1816
# Set default headers
1917
default_headers = {
2018
'Content-Type' => "application/#{attributes[:format].downcase}",
@@ -34,21 +32,17 @@ def initialize(http_method, path, attributes={})
3432
attributes.each do |name, value|
3533
send("#{name.to_s.underscore.to_sym}=", value)
3634
end
35+
36+
update_params_for_auth!
3737
end
3838

39-
def update_params_for_auth(attributes)
40-
(attributes[:auth_names] || []).each do |auth_name|
39+
def update_params_for_auth!
40+
(@auth_names || []).each do |auth_name|
4141
case auth_name
42-
4342
when 'api_key'
44-
attributes[:headers] ||= {}
45-
attributes[:headers]['api_key'] = get_api_key_with_prefix('api_key')
46-
47-
48-
43+
@headers ||= {}
44+
@headers['api_key'] = get_api_key_with_prefix('api_key')
4945
when 'petstore_auth'
50-
51-
5246
# TODO: support oauth
5347

5448
end

samples/client/petstore/ruby/spec/request_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
before(:each) do
66
SwaggerClient::Swagger.configure do |config|
77
inject_format = true
8-
config.api_key = 'special-key'
8+
config.api_key['api_key'] = 'special-key'
99
config.host = 'petstore.swagger.io'
1010
config.base_path = '/v2'
1111
end
@@ -159,4 +159,26 @@
159159

160160
end
161161

162+
describe "#update_params_for_auth!" do
163+
it "sets header api-key parameter with prefix" do
164+
SwaggerClient::Swagger.configure do |config|
165+
inject_format = true
166+
config.api_key_prefix['api_key'] = 'PREFIX'
167+
end
168+
@request.auth_names = ['api_key', 'unknown']
169+
@request.update_params_for_auth!
170+
@request.headers['api_key'].should == 'PREFIX special-key'
171+
end
172+
173+
it "sets header api-key parameter without prefix" do
174+
SwaggerClient::Swagger.configure do |config|
175+
inject_format = true
176+
config.api_key_prefix['api_key'] = nil
177+
end
178+
@request.auth_names = ['api_key', 'unknown']
179+
@request.update_params_for_auth!
180+
@request.headers['api_key'].should == 'special-key'
181+
end
182+
end
183+
162184
end

samples/client/petstore/ruby/spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def help
3838

3939
def configure_swagger
4040
SwaggerClient::Swagger.configure do |config|
41-
config.api_key = 'special-key'
41+
config.api_key['api_key'] = 'special-key'
4242
config.host = 'petstore.swagger.io'
4343
config.base_path = '/v2'
4444
end

0 commit comments

Comments
 (0)