rspec-rails-api 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +48 -78
- data/lib/rspec/rails/api/field_config.rb +1 -1
- data/lib/rspec/rails/api/open_api_renderer.rb +9 -3
- data/lib/rspec/rails/api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4771b145e32ae7cbfd7f03d6252618d3a4cd8b557930a67a562e45046d4e302b
|
4
|
+
data.tar.gz: 6bc0e55b1c2e5d51ba5bdfc52ac532efbf5ad539e138557a99895ef637e0311a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cafa1ebfc333e41474f85dfb9389f976df1ffa8000de62c8a412c6436088db9182a7f15ec162dff46848265e6cf86564e5a79c93e0814afcc454a242ad77c8b6
|
7
|
+
data.tar.gz: a24717ab5c36a863eaa6939c4ee3f5df3b5fbf058f45b17923d50dd0656680becca60f325d2d9c11b13db84dfaf243a4f24dd77450c33b28911086b986f9cb9e
|
data/CHANGELOG.md
CHANGED
@@ -18,6 +18,14 @@ Maintenance, in case of rework, dependencies change
|
|
18
18
|
|
19
19
|
## [Unreleased]
|
20
20
|
|
21
|
+
## [0.6.1] - 2024-01-17
|
22
|
+
|
23
|
+
### Fixed
|
24
|
+
|
25
|
+
- Renderer: handle arrays of object with inline attributes (not a reference)
|
26
|
+
- Renderer: Remove the `name` attribute from `basic` authentication declaration
|
27
|
+
- Renderer: Add missing `items` key on arrays of any type
|
28
|
+
|
21
29
|
## [0.6.0] - 2023-07-17
|
22
30
|
|
23
31
|
### Added
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -26,89 +26,56 @@ bundle
|
|
26
26
|
|
27
27
|
Configuration should be made manually for now:
|
28
28
|
|
29
|
-
**spec/
|
29
|
+
**spec/support/rspec_rails_api.rb**:
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
require 'rails_helper'
|
33
32
|
require 'rspec_rails_api'
|
34
33
|
|
34
|
+
# Associate spec/acceptance/* to acceptance tests
|
35
|
+
RSpec::Rails::DIRECTORY_MAPPINGS[:acceptance] = %w[spec acceptance]
|
36
|
+
|
35
37
|
RSpec.configure do |config|
|
36
38
|
config.include RSpec::Rails::Api::DSL::Example
|
37
|
-
|
38
|
-
|
39
|
-
renderer = RSpec::Rails::Api::OpenApiRenderer.new
|
40
|
-
# Options here should be customized
|
41
|
-
renderer.api_title = 'YourProject API'
|
42
|
-
renderer.api_version = '1'
|
43
|
-
renderer.api_description = 'Manage data on YourProject'
|
44
|
-
# Options below are optional
|
45
|
-
renderer.api_servers = [{ url: 'https://example.com' }]
|
46
|
-
renderer.api_tos = 'http://example.com/tos.html'
|
47
|
-
renderer.api_contact = { name: 'Admin', email: 'admin@example.com', url: 'http://example.com/contact' }
|
48
|
-
renderer.api_license = { name: 'Apache', url: 'https://opensource.org/licenses/Apache-2.0' }
|
39
|
+
config.include RSpec::Rails::RequestExampleGroup, type: :acceptance
|
49
40
|
|
50
|
-
|
51
|
-
renderer
|
52
|
-
|
41
|
+
# Define the renderer if you want to generate the OpenApi documentation
|
42
|
+
renderer = RSpec::Rails::Api::OpenApiRenderer.new
|
43
|
+
# Options here should be customized
|
44
|
+
renderer.api_title = 'YourProject API'
|
45
|
+
renderer.api_version = '1'
|
46
|
+
renderer.api_description = 'Manage data on YourProject'
|
47
|
+
# Options below are optional
|
48
|
+
renderer.api_servers = [{ url: 'https://example.com' }]
|
49
|
+
renderer.api_tos = 'http://example.com/tos.html'
|
50
|
+
renderer.api_contact = { name: 'Admin', email: 'admin@example.com', url: 'http://example.com/contact' }
|
51
|
+
renderer.api_license = { name: 'Apache', url: 'https://opensource.org/licenses/Apache-2.0' }
|
52
|
+
# ... Check the "Configuration" section for all the options
|
53
|
+
|
54
|
+
config.after(:context, type: :acceptance) do |context|
|
55
|
+
renderer.merge_context context.class.metadata[:rra].to_h
|
56
|
+
end
|
53
57
|
|
54
|
-
|
55
|
-
|
56
|
-
|
58
|
+
config.after(:suite) do
|
59
|
+
# Default path is 'tmp/rspec_rails_api_output.json/yaml'
|
60
|
+
renderer.write_files Rails.root.join('public', 'swagger_doc'), only: [:json]
|
61
|
+
end
|
57
62
|
end
|
58
63
|
```
|
59
64
|
|
60
|
-
**spec/rails_helper.rb
|
65
|
+
**spec/rails_helper.rb**:
|
61
66
|
|
62
67
|
```ruby
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
RSpec.configure do |config|
|
68
|
-
# ...
|
69
|
-
config.include RSpec::Rails::RequestExampleGroup, type: :acceptance
|
70
|
-
end
|
68
|
+
#...
|
69
|
+
require 'support/rspec_rails_api'
|
70
|
+
#...
|
71
71
|
```
|
72
72
|
|
73
73
|
## Configuration
|
74
74
|
|
75
75
|
**TODO: This section is incomplete and the gem has no generator yet.**
|
76
76
|
|
77
|
-
Here is an example configuration:
|
78
|
-
|
79
|
-
```rb
|
80
|
-
# spec/rails_helper.rb
|
81
|
-
require 'spec/support/rspec_rails_api'
|
82
|
-
```
|
83
|
-
|
84
|
-
```rb
|
85
|
-
# spec/support/acceptance_entities.rb
|
86
|
-
|
87
|
-
# This file contains common object definitions
|
88
|
-
{
|
89
|
-
error: {
|
90
|
-
error: { type: :string, description: "Error message" }
|
91
|
-
},
|
92
|
-
form_error: {
|
93
|
-
title: { type: :array, required: false, description: "Title errors", of: :string }
|
94
|
-
},
|
95
|
-
}.each do |name, attributes|
|
96
|
-
RSpec::Rails::Api::Metadata.add_entity name, attributes
|
97
|
-
end
|
98
|
-
```
|
99
77
|
|
100
78
|
```rb
|
101
|
-
# spec/support/rspec_rails_api.rb
|
102
|
-
require 'rspec_rails_api'
|
103
|
-
require 'support/acceptance_entities'
|
104
|
-
|
105
|
-
# Include DSL in RSpec
|
106
|
-
RSpec.configure do |config|
|
107
|
-
config.include RSpec::Rails::Api::DSL::Example
|
108
|
-
end
|
109
|
-
|
110
|
-
# Initialize and configure the renderer
|
111
|
-
renderer = RSpec::Rails::Api::OpenApiRenderer.new
|
112
79
|
# Server URL for quick reference
|
113
80
|
server_url = 'https://example.com'
|
114
81
|
|
@@ -229,10 +196,6 @@ end
|
|
229
196
|
This solution comes from [this article](https://makandracards.com/makandra/37161-rspec-devise-how-to-sign-in-users-in-request-specs)
|
230
197
|
by Arne Hartherz (MIT license).
|
231
198
|
|
232
|
-
## Usage
|
233
|
-
|
234
|
-
Write some spec files and run RSpec as you would usually do.
|
235
|
-
|
236
199
|
## Writing specs
|
237
200
|
|
238
201
|
There is a [commented example](dummy/spec/acceptance/posts_spec.rb) available in
|
@@ -334,21 +297,23 @@ RSpec::Rails::Api::Metadata.add_entity :user,
|
|
334
297
|
|
335
298
|
Organization of the global entities declaration is up to you.
|
336
299
|
|
300
|
+
For small projects, we usually put them all in one file:
|
301
|
+
|
337
302
|
```rb
|
338
|
-
# spec/
|
339
|
-
require 'rails_helper'
|
303
|
+
# spec/support/acceptance_entities.rb
|
340
304
|
|
341
|
-
|
342
|
-
resource 'Users', 'Manage users'
|
305
|
+
require 'rspec/rails/api/metadata'
|
343
306
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
307
|
+
# This file contains common object definitions
|
308
|
+
{
|
309
|
+
error: {
|
310
|
+
error: { type: :string, description: "Error message" }
|
311
|
+
},
|
312
|
+
form_error: {
|
313
|
+
title: { type: :array, required: false, description: "Title errors", of: :string }
|
314
|
+
},
|
315
|
+
}.each do |name, attributes|
|
316
|
+
RSpec::Rails::Api::Metadata.add_entity name, attributes
|
352
317
|
end
|
353
318
|
```
|
354
319
|
|
@@ -746,6 +711,11 @@ MRs to improve this are welcome.
|
|
746
711
|
|
747
712
|
There is no support for file fields yet.
|
748
713
|
|
714
|
+
### Security
|
715
|
+
|
716
|
+
`Security` mechanisms are only declared for the OpenApi documentation as a reference, and is
|
717
|
+
not checked nor enforced in the tests.
|
718
|
+
|
749
719
|
## Development
|
750
720
|
|
751
721
|
After checking out the repo, run `bin/setup` to install dependencies.
|
@@ -43,7 +43,7 @@ module RSpec
|
|
43
43
|
def add_security_scheme(reference, name, definition)
|
44
44
|
raise "Security scheme #{reference} is already defined" if @api_security.key? reference
|
45
45
|
|
46
|
-
definition[:name] = name
|
46
|
+
definition[:name] = name unless reference == :basic
|
47
47
|
@api_security[reference] = definition
|
48
48
|
end
|
49
49
|
|
@@ -241,6 +241,8 @@ module RSpec
|
|
241
241
|
property = { '$ref' => "#/components/schemas/#{field.attributes}" }
|
242
242
|
elsif field.type == :array && field.attributes.is_a?(Symbol)
|
243
243
|
property = { type: :array, items: { '$ref' => "#/components/schemas/#{field.attributes}" } }
|
244
|
+
elsif field.type == :array
|
245
|
+
property = { type: :array, items: process_entity(field.attributes) }
|
244
246
|
end
|
245
247
|
|
246
248
|
required.push name unless field.required == false
|
@@ -420,7 +422,7 @@ module RSpec
|
|
420
422
|
response
|
421
423
|
end
|
422
424
|
|
423
|
-
def response_schema(expectations) # rubocop:disable Metrics/MethodLength
|
425
|
+
def response_schema(expectations) # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
424
426
|
if expectations[:many]
|
425
427
|
items = if PRIMITIVES.include?(expectations[:many])
|
426
428
|
{ type: expectations[:many] }
|
@@ -429,7 +431,11 @@ module RSpec
|
|
429
431
|
end
|
430
432
|
{ type: 'array', items: items }
|
431
433
|
elsif expectations[:one]
|
432
|
-
|
434
|
+
# Unspecified arrays
|
435
|
+
if expectations[:one] == :array
|
436
|
+
{ type: :array, items: {} }
|
437
|
+
# Array of specified type
|
438
|
+
elsif PRIMITIVES.include?(expectations[:one])
|
433
439
|
{ type: expectations[:one] }
|
434
440
|
else
|
435
441
|
{ '$ref' => "#/components/schemas/#{expectations[:one]}" }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-rails-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel Tancoigne
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|