power_api 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/Gemfile.lock +71 -85
- data/README.md +15 -281
- data/lib/generators/power_api/controller/controller_generator.rb +0 -28
- data/lib/generators/power_api/exposed_api_config/exposed_api_config_generator.rb +0 -11
- data/lib/generators/power_api/version/version_generator.rb +0 -23
- data/lib/power_api/engine.rb +2 -5
- data/lib/power_api/generator_helper/active_record_resource.rb +0 -14
- data/lib/power_api/generator_helper/rspec_controller_helper.rb +20 -4
- data/lib/power_api/generator_helpers.rb +0 -1
- data/lib/power_api/version.rb +1 -1
- data/lib/power_api.rb +0 -2
- data/power_api.gemspec +0 -3
- data/spec/dummy/spec/lib/power_api/generator_helper/rspec_controller_helper_spec.rb +18 -6
- data/spec/dummy/spec/support/shared_examples/active_record_resource_atrributes.rb +20 -20
- metadata +2 -47
- data/lib/power_api/generator_helper/swagger_helper.rb +0 -468
- data/spec/dummy/spec/lib/power_api/generator_helper/swagger_helper_spec.rb +0 -470
@@ -1,470 +0,0 @@
|
|
1
|
-
describe PowerApi::GeneratorHelper::SwaggerHelper, type: :generator do
|
2
|
-
describe "#swagger_helper_path" do
|
3
|
-
let(:expected_path) { "spec/swagger_helper.rb" }
|
4
|
-
|
5
|
-
def perform
|
6
|
-
generators_helper.swagger_helper_path
|
7
|
-
end
|
8
|
-
|
9
|
-
it { expect(perform).to eq(expected_path) }
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "#spec_swagger_path" do
|
13
|
-
let(:expected_path) { "spec/swagger/.gitkeep" }
|
14
|
-
|
15
|
-
def perform
|
16
|
-
generators_helper.spec_swagger_path
|
17
|
-
end
|
18
|
-
|
19
|
-
it { expect(perform).to eq(expected_path) }
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "#spec_integration_path" do
|
23
|
-
let(:expected_path) { "spec/integration/.gitkeep" }
|
24
|
-
|
25
|
-
def perform
|
26
|
-
generators_helper.spec_integration_path
|
27
|
-
end
|
28
|
-
|
29
|
-
it { expect(perform).to eq(expected_path) }
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "#rswag_ui_initializer_path" do
|
33
|
-
let(:expected_path) { "config/initializers/rswag-ui.rb" }
|
34
|
-
|
35
|
-
def perform
|
36
|
-
generators_helper.rswag_ui_initializer_path
|
37
|
-
end
|
38
|
-
|
39
|
-
it { expect(perform).to eq(expected_path) }
|
40
|
-
end
|
41
|
-
|
42
|
-
describe "#swagger_resource_schema_path" do
|
43
|
-
let(:expected_path) { "spec/swagger/v1/schemas/blog_schema.rb" }
|
44
|
-
|
45
|
-
def perform
|
46
|
-
generators_helper.swagger_resource_schema_path
|
47
|
-
end
|
48
|
-
|
49
|
-
it { expect(perform).to eq(expected_path) }
|
50
|
-
end
|
51
|
-
|
52
|
-
describe "#swagger_version_definition_path" do
|
53
|
-
let(:expected_path) { "spec/swagger/v1/definition.rb" }
|
54
|
-
|
55
|
-
def perform
|
56
|
-
generators_helper.swagger_version_definition_path
|
57
|
-
end
|
58
|
-
|
59
|
-
it { expect(perform).to eq(expected_path) }
|
60
|
-
end
|
61
|
-
|
62
|
-
describe "#swagger_resource_spec_path" do
|
63
|
-
let(:expected_path) { "spec/integration/api/v1/blogs_spec.rb" }
|
64
|
-
|
65
|
-
def perform
|
66
|
-
generators_helper.swagger_resource_spec_path
|
67
|
-
end
|
68
|
-
|
69
|
-
it { expect(perform).to eq(expected_path) }
|
70
|
-
end
|
71
|
-
|
72
|
-
describe "#rswag_ui_configure_line" do
|
73
|
-
let(:expected_path) do
|
74
|
-
"Rswag::Ui.configure do |c|\n"
|
75
|
-
end
|
76
|
-
|
77
|
-
def perform
|
78
|
-
generators_helper.rswag_ui_configure_line
|
79
|
-
end
|
80
|
-
|
81
|
-
it { expect(perform).to eq(expected_path) }
|
82
|
-
end
|
83
|
-
|
84
|
-
describe "#swagger_helper_api_definition_line" do
|
85
|
-
let(:expected_line) do
|
86
|
-
"config.swagger_docs = {\n"
|
87
|
-
end
|
88
|
-
|
89
|
-
def perform
|
90
|
-
generators_helper.swagger_helper_api_definition_line
|
91
|
-
end
|
92
|
-
|
93
|
-
it { expect(perform).to eq(expected_line) }
|
94
|
-
end
|
95
|
-
|
96
|
-
describe "#swagger_definition_line_to_inject_schema" do
|
97
|
-
let(:expected_line) { /definitions: {/ }
|
98
|
-
|
99
|
-
def perform
|
100
|
-
generators_helper.swagger_definition_line_to_inject_schema
|
101
|
-
end
|
102
|
-
|
103
|
-
it { expect(perform).to eq(expected_line) }
|
104
|
-
end
|
105
|
-
|
106
|
-
describe "#swagger_helper_api_definition" do
|
107
|
-
let(:expected_tpl) do
|
108
|
-
<<-VERSION
|
109
|
-
'v1/swagger.json' => API_V1
|
110
|
-
VERSION
|
111
|
-
end
|
112
|
-
|
113
|
-
def perform
|
114
|
-
generators_helper.swagger_helper_api_definition
|
115
|
-
end
|
116
|
-
|
117
|
-
it { expect(perform).to eq(expected_tpl) }
|
118
|
-
|
119
|
-
context "with another version" do
|
120
|
-
let(:version_number) { "2" }
|
121
|
-
|
122
|
-
let(:expected_tpl) do
|
123
|
-
<<-VERSION
|
124
|
-
'v2/swagger.json' => API_V2,
|
125
|
-
VERSION
|
126
|
-
end
|
127
|
-
|
128
|
-
it { expect(perform).to eq(expected_tpl) }
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
describe "#rswag_ui_initializer_tpl" do
|
133
|
-
let(:expected_tpl) do
|
134
|
-
<<~INITIALIZER
|
135
|
-
Rswag::Ui.configure do |c|
|
136
|
-
end
|
137
|
-
INITIALIZER
|
138
|
-
end
|
139
|
-
|
140
|
-
def perform
|
141
|
-
generators_helper.rswag_ui_initializer_tpl
|
142
|
-
end
|
143
|
-
|
144
|
-
it { expect(perform).to eq(expected_tpl) }
|
145
|
-
end
|
146
|
-
|
147
|
-
describe "#swagger_definition_tpl" do
|
148
|
-
let(:expected_tpl) do
|
149
|
-
<<~DEFINITION
|
150
|
-
API_V1 = {
|
151
|
-
swagger: '2.0',
|
152
|
-
info: {
|
153
|
-
title: 'API V1',
|
154
|
-
version: 'v1'
|
155
|
-
},
|
156
|
-
basePath: '/api/v1',
|
157
|
-
definitions: {
|
158
|
-
}
|
159
|
-
}
|
160
|
-
DEFINITION
|
161
|
-
end
|
162
|
-
|
163
|
-
def perform
|
164
|
-
generators_helper.swagger_definition_tpl
|
165
|
-
end
|
166
|
-
|
167
|
-
it { expect(perform).to eq(expected_tpl) }
|
168
|
-
end
|
169
|
-
|
170
|
-
describe "swagger_helper_tpl" do
|
171
|
-
let(:template) do
|
172
|
-
<<~SWAGGER
|
173
|
-
require 'rails_helper'
|
174
|
-
|
175
|
-
Dir[::Rails.root.join("spec/swagger/**/schemas/*.rb")].each { |f| require f }
|
176
|
-
Dir[::Rails.root.join("spec/swagger/**/definition.rb")].each { |f| require f }
|
177
|
-
|
178
|
-
RSpec.configure do |config|
|
179
|
-
# Specify a root folder where Swagger JSON files are generated
|
180
|
-
# NOTE: If you're using the rswag-api to serve API descriptions, you'll need
|
181
|
-
# to ensure that it's confiugred to serve Swagger from the same folder
|
182
|
-
config.swagger_root = Rails.root.to_s + '/swagger'
|
183
|
-
|
184
|
-
# Define one or more Swagger documents and provide global metadata for each one
|
185
|
-
# When you run the 'rswag:specs:to_swagger' rake task, the complete Swagger will
|
186
|
-
# be generated at the provided relative path under swagger_root
|
187
|
-
# By default, the operations defined in spec files are added to the first
|
188
|
-
# document below. You can override this behavior by adding a swagger_doc tag to the
|
189
|
-
# the root example_group in your specs, e.g. describe '...', swagger_doc: 'v2/swagger.json'
|
190
|
-
config.swagger_docs = {
|
191
|
-
}
|
192
|
-
end
|
193
|
-
SWAGGER
|
194
|
-
end
|
195
|
-
|
196
|
-
def perform
|
197
|
-
generators_helper.swagger_helper_tpl
|
198
|
-
end
|
199
|
-
|
200
|
-
it { expect(perform).to eq(template) }
|
201
|
-
end
|
202
|
-
|
203
|
-
describe "#swagger_schema_tpl" do
|
204
|
-
let(:template) do
|
205
|
-
<<~SCHEMA
|
206
|
-
BLOG_SCHEMA = {
|
207
|
-
type: :object,
|
208
|
-
properties: {
|
209
|
-
id: { type: :integer, example: 666 },
|
210
|
-
title: { type: :string, example: 'Some title' },
|
211
|
-
body: { type: :string, example: 'Some body' },
|
212
|
-
created_at: { type: :string, example: '1984-06-04 09:00', 'x-nullable': true },
|
213
|
-
updated_at: { type: :string, example: '1984-06-04 09:00', 'x-nullable': true },
|
214
|
-
portfolio_id: { type: :integer, example: 666, 'x-nullable': true }
|
215
|
-
},
|
216
|
-
required: [
|
217
|
-
:id,
|
218
|
-
:title,
|
219
|
-
:body
|
220
|
-
]
|
221
|
-
}
|
222
|
-
|
223
|
-
BLOGS_COLLECTION_SCHEMA = {
|
224
|
-
type: "object",
|
225
|
-
properties: {
|
226
|
-
blogs: {
|
227
|
-
type: "array",
|
228
|
-
items: { "$ref" => "#/definitions/blog" }
|
229
|
-
}
|
230
|
-
},
|
231
|
-
required: [
|
232
|
-
:blogs
|
233
|
-
]
|
234
|
-
}
|
235
|
-
|
236
|
-
BLOG_RESOURCE_SCHEMA = {
|
237
|
-
type: "object",
|
238
|
-
properties: {
|
239
|
-
blog: { "$ref" => "#/definitions/blog" }
|
240
|
-
},
|
241
|
-
required: [
|
242
|
-
:blog
|
243
|
-
]
|
244
|
-
}
|
245
|
-
SCHEMA
|
246
|
-
end
|
247
|
-
|
248
|
-
def perform
|
249
|
-
generators_helper.swagger_schema_tpl
|
250
|
-
end
|
251
|
-
|
252
|
-
it { expect(perform).to eq(template) }
|
253
|
-
end
|
254
|
-
|
255
|
-
describe "#swagger_resource_spec_tpl" do
|
256
|
-
let(:template) do
|
257
|
-
<<~SPEC
|
258
|
-
require 'swagger_helper'
|
259
|
-
|
260
|
-
describe 'API V1 Blogs', swagger_doc: 'v1/swagger.json' do
|
261
|
-
path '/blogs' do
|
262
|
-
get 'Retrieves Blogs' do
|
263
|
-
description 'Retrieves all the blogs'
|
264
|
-
produces 'application/json'
|
265
|
-
|
266
|
-
let(:collection_count) { 5 }
|
267
|
-
let(:expected_collection_count) { collection_count }
|
268
|
-
|
269
|
-
before { create_list(:blog, collection_count) }
|
270
|
-
response '200', 'Blogs retrieved' do
|
271
|
-
schema('$ref' => '#/definitions/blogs_collection')
|
272
|
-
|
273
|
-
run_test! do |response|
|
274
|
-
expect(JSON.parse(response.body)['blogs'].count).to eq(expected_collection_count)
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
end
|
279
|
-
|
280
|
-
post 'Creates Blog' do
|
281
|
-
description 'Creates Blog'
|
282
|
-
consumes 'application/json'
|
283
|
-
produces 'application/json'
|
284
|
-
parameter(name: :blog, in: :body)
|
285
|
-
|
286
|
-
response '201', 'blog created' do
|
287
|
-
let(:blog) do
|
288
|
-
{
|
289
|
-
title: 'Some title',
|
290
|
-
body: 'Some body'}
|
291
|
-
end
|
292
|
-
|
293
|
-
run_test!
|
294
|
-
end
|
295
|
-
|
296
|
-
response '400', 'invalid attributes' do
|
297
|
-
let(:blog) do
|
298
|
-
{
|
299
|
-
title: nil}
|
300
|
-
end
|
301
|
-
|
302
|
-
run_test!
|
303
|
-
end
|
304
|
-
|
305
|
-
end
|
306
|
-
|
307
|
-
end
|
308
|
-
|
309
|
-
path '/blogs/{id}' do
|
310
|
-
parameter name: :id, in: :path, type: :integer
|
311
|
-
|
312
|
-
let(:existent_blog) { create(:blog) }
|
313
|
-
let(:id) { existent_blog.id }
|
314
|
-
|
315
|
-
get 'Retrieves Blog' do
|
316
|
-
produces 'application/json'
|
317
|
-
|
318
|
-
response '200', 'blog retrieved' do
|
319
|
-
schema('$ref' => '#/definitions/blog_resource')
|
320
|
-
|
321
|
-
run_test!
|
322
|
-
end
|
323
|
-
|
324
|
-
response '404', 'invalid blog id' do
|
325
|
-
let(:id) { 'invalid' }
|
326
|
-
run_test!
|
327
|
-
end
|
328
|
-
|
329
|
-
end
|
330
|
-
|
331
|
-
put 'Updates Blog' do
|
332
|
-
description 'Updates Blog'
|
333
|
-
consumes 'application/json'
|
334
|
-
produces 'application/json'
|
335
|
-
parameter(name: :blog, in: :body)
|
336
|
-
|
337
|
-
response '200', 'blog updated' do
|
338
|
-
let(:blog) do
|
339
|
-
{
|
340
|
-
title: 'Some title',
|
341
|
-
body: 'Some body'}
|
342
|
-
end
|
343
|
-
|
344
|
-
run_test!
|
345
|
-
end
|
346
|
-
|
347
|
-
response '400', 'invalid attributes' do
|
348
|
-
let(:blog) do
|
349
|
-
{
|
350
|
-
title: nil}
|
351
|
-
end
|
352
|
-
|
353
|
-
run_test!
|
354
|
-
end
|
355
|
-
|
356
|
-
end
|
357
|
-
|
358
|
-
delete 'Deletes Blog' do
|
359
|
-
produces 'application/json'
|
360
|
-
description 'Deletes specific blog'
|
361
|
-
|
362
|
-
response '204', 'blog deleted' do
|
363
|
-
run_test!
|
364
|
-
end
|
365
|
-
|
366
|
-
response '404', 'blog not found' do
|
367
|
-
let(:id) { 'invalid' }
|
368
|
-
|
369
|
-
run_test!
|
370
|
-
end
|
371
|
-
|
372
|
-
end
|
373
|
-
|
374
|
-
end
|
375
|
-
|
376
|
-
end
|
377
|
-
SPEC
|
378
|
-
end
|
379
|
-
|
380
|
-
def perform
|
381
|
-
generators_helper.swagger_resource_spec_tpl
|
382
|
-
end
|
383
|
-
|
384
|
-
it { expect(perform).to eq(template) }
|
385
|
-
|
386
|
-
context "with authenticated_resource option" do
|
387
|
-
let(:authenticated_resource) { "user" }
|
388
|
-
|
389
|
-
it { expect(perform).to include("let(:user) { create(:user) }") }
|
390
|
-
it { expect(perform).to include("let(:user_email) { user.email }") }
|
391
|
-
it { expect(perform).to include("let(:user_token) { user.authentication_token }") }
|
392
|
-
it { expect(perform).to include("parameter name: :user_email, in: :query, type: :string") }
|
393
|
-
it { expect(perform).to include("parameter name: :user_token, in: :query, type: :string") }
|
394
|
-
it { expect(perform).to include("response '401', 'user unauthorized' do") }
|
395
|
-
end
|
396
|
-
|
397
|
-
context "with owned_by_authenticated_resource option" do
|
398
|
-
let(:authenticated_resource) { "user" }
|
399
|
-
let(:owned_by_authenticated_resource) { true }
|
400
|
-
|
401
|
-
it { expect(perform).to include("create_list(:blog, collection_count, user: user)") }
|
402
|
-
it { expect(perform).to include("(:existent_blog) { create(:blog, user: user) }") }
|
403
|
-
end
|
404
|
-
|
405
|
-
context "with parent_resource option" do
|
406
|
-
let(:parent_resource_name) { "portfolio" }
|
407
|
-
|
408
|
-
it { expect(perform).to include("/portfolios/{portfolio_id}/blogs") }
|
409
|
-
it { expect(perform).to include("parameter name: :portfolio_id, in: :path, type: :integer") }
|
410
|
-
it { expect(perform).to include("reate_list(:blog, collection_count, portfolio: portfolio)") }
|
411
|
-
it { expect(perform).to include("let(:portfolio) { create(:portfolio) }") }
|
412
|
-
it { expect(perform).to include("(:existent_blog) { create(:blog, portfolio: portfolio) }") }
|
413
|
-
end
|
414
|
-
|
415
|
-
context 'with only some resource actions (show and update)' do
|
416
|
-
let(:controller_actions) do
|
417
|
-
[
|
418
|
-
"show",
|
419
|
-
"update"
|
420
|
-
]
|
421
|
-
end
|
422
|
-
|
423
|
-
it { expect(perform).to include("path '/blogs/{id}' do\n") }
|
424
|
-
it { expect(perform).to include("get 'Retrieves Blog' do\n") }
|
425
|
-
it { expect(perform).to include("put 'Updates Blog' do\n") }
|
426
|
-
it { expect(perform).not_to include("delete 'Deletes Blog' do\n") }
|
427
|
-
it { expect(perform).not_to include("path '/blogs' do\n") }
|
428
|
-
it { expect(perform).not_to include("get 'Retrieves Blogs' do\n") }
|
429
|
-
it { expect(perform).not_to include("post 'Creates Blog' do\n") }
|
430
|
-
end
|
431
|
-
|
432
|
-
context 'with only some collection actions (index)' do
|
433
|
-
let(:controller_actions) { ["index"] }
|
434
|
-
|
435
|
-
it { expect(perform).not_to include("path '/blogs/{id}' do\n") }
|
436
|
-
it { expect(perform).not_to include("get 'Retrieves Blog' do\n") }
|
437
|
-
it { expect(perform).not_to include("put 'Updates Blog' do\n") }
|
438
|
-
it { expect(perform).not_to include("delete 'Deletes Blog' do\n") }
|
439
|
-
it { expect(perform).to include("path '/blogs' do\n") }
|
440
|
-
it { expect(perform).to include("get 'Retrieves Blogs' do\n") }
|
441
|
-
it { expect(perform).not_to include("post 'Creates Blog' do\n") }
|
442
|
-
end
|
443
|
-
end
|
444
|
-
|
445
|
-
describe "#rswag_ui_swagger_endpoint" do
|
446
|
-
let(:expected_entry) do
|
447
|
-
" c.swagger_endpoint '/api-docs/v1/swagger.json', 'API V1 Docs'\n"
|
448
|
-
end
|
449
|
-
|
450
|
-
def perform
|
451
|
-
generators_helper.rswag_ui_swagger_endpoint
|
452
|
-
end
|
453
|
-
|
454
|
-
it { expect(perform).to eq(expected_entry) }
|
455
|
-
end
|
456
|
-
|
457
|
-
describe "#swagger_definition_entry" do
|
458
|
-
let(:expected_entry) do
|
459
|
-
"\n blog: BLOG_SCHEMA,\
|
460
|
-
\n blogs_collection: BLOGS_COLLECTION_SCHEMA,\
|
461
|
-
\n blog_resource: BLOG_RESOURCE_SCHEMA,"
|
462
|
-
end
|
463
|
-
|
464
|
-
def perform
|
465
|
-
generators_helper.swagger_definition_entry
|
466
|
-
end
|
467
|
-
|
468
|
-
it { expect(perform).to eq(expected_entry) }
|
469
|
-
end
|
470
|
-
end
|