autoapi_testing 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/autoapi/AutoAPI-0.0.1.jar +0 -0
- data/lib/autoapi/convert.rb +22 -0
- data/lib/autoapi/run_autoapi.rb +30 -0
- data/lib/autoapi/test_case.rb +170 -0
- data/lib/autoapi_testing/version.rb +5 -0
- data/lib/autoapi_testing.rb +280 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 69a3af2449f91e96268858936dbb1d102036489eaea8fc79f4ff62197bd4d4d0
|
4
|
+
data.tar.gz: bdf4c0eb52fc405e7a399f56ce39cc479260fb2beb0d3a2c22e6f92bd54ce485
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5c1bdd2391e72511cf4ebc6cee3763746fcae0b9f4aa5c871c230950ea93d8e6c3a6ca4b6765607d70efd427d63186d2fbf75fb963fceb2aa11fa94baea09a0d
|
7
|
+
data.tar.gz: 128c8c2e46950b8d29afeb1e1caf5b4f4074d1cfb217362ad5da515fcddf2f17171683ee23c07968a6363eb2ad17660495f88afc0238796b736eff0730c997db
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at danny.porrello@procore.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Danny Porrello
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# AutoapiTesting
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/autoapi_testing`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'autoapi_testing'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install autoapi_testing
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/autoapi_testing. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/autoapi_testing/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the AutoapiTesting project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/autoapi_testing/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "autoapi_testing"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
Binary file
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# domain: AutoAPI
|
3
|
+
|
4
|
+
# TODO: follow guide and implement remaining changes
|
5
|
+
# https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0
|
6
|
+
|
7
|
+
# convert schema from OAS 3.0 to 3.1
|
8
|
+
def convert(schema)
|
9
|
+
if schema.class == Array
|
10
|
+
schema.map { |child| convert child }
|
11
|
+
elsif schema.class == Hash
|
12
|
+
if schema['nullable']
|
13
|
+
# not sure what to do when/if this happens
|
14
|
+
raise 'no type' unless schema['type']
|
15
|
+
|
16
|
+
schema['type'] = [schema['type'], nil]
|
17
|
+
end
|
18
|
+
schema.transform_values { |child| convert child }
|
19
|
+
else
|
20
|
+
schema
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# domain: AutoAPI
|
3
|
+
|
4
|
+
# check if file of generated tests exists and return that
|
5
|
+
# otherwise execute autoapi to create the file first
|
6
|
+
def run_autoapi(oas_filename)
|
7
|
+
use_cache = ENV['CI'] || ENV.fetch('AUTOAPI_USE_CACHE', 'false').casecmp('true').zero?
|
8
|
+
# json_filename = Rails.root.join("doc/api_docs/autoapi/generated_tests/#{oas_filename.split('.')[0]}.json")
|
9
|
+
json_filename = Rails.root.join("doc/api_docs/autoapi/generated_tests/#{oas_filename.split('.')[0]}.json")
|
10
|
+
if use_cache && File.exist?(json_filename)
|
11
|
+
puts 'Reusing existing test file'
|
12
|
+
JSON.parse(File.read(json_filename))
|
13
|
+
else
|
14
|
+
puts 'Generating test file with AutoAPI'
|
15
|
+
jar_path = File.join(File.dirname(__FILE__), 'AutoAPI-0.0.1.jar')
|
16
|
+
oas_path = Rails.root.join('doc/api_docs', oas_filename)
|
17
|
+
json = `java -jar #{jar_path} --generatetests #{oas_path}`
|
18
|
+
begin
|
19
|
+
autoapi_test_fixture = JSON.parse(json)
|
20
|
+
rescue JSON::ParserError
|
21
|
+
raise "Error Generating test cases with AutoAPI. \n" + json
|
22
|
+
end
|
23
|
+
json_file_dir = File.dirname(json_filename)
|
24
|
+
unless File.directory?(json_file_dir)
|
25
|
+
FileUtils.mkdir_p(json_file_dir)
|
26
|
+
end
|
27
|
+
File.write(json_filename, json)
|
28
|
+
autoapi_test_fixture
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# domain: AutoAPI
|
3
|
+
|
4
|
+
=begin
|
5
|
+
module Types
|
6
|
+
include Dry.Types()
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestCase < Dry::Struct
|
10
|
+
attribute :request do
|
11
|
+
attribute :operation, Types::String
|
12
|
+
attribute :path, Types::String
|
13
|
+
attribute :media_type, Types::String
|
14
|
+
attribute :parameters, Types::Array
|
15
|
+
attribute :body_schema, Types::Hash.optional
|
16
|
+
end
|
17
|
+
attribute :response do
|
18
|
+
attribute :status, Types::Integer
|
19
|
+
attribute :body_schema, Types::Hash.optional
|
20
|
+
attribute :media_type, Types::String.optional
|
21
|
+
attribute :header_schema_map, Types::Hash.optional
|
22
|
+
end
|
23
|
+
|
24
|
+
def method
|
25
|
+
request.operation
|
26
|
+
end
|
27
|
+
|
28
|
+
def path
|
29
|
+
request.path
|
30
|
+
end
|
31
|
+
|
32
|
+
def media_type
|
33
|
+
request.media_type
|
34
|
+
end
|
35
|
+
|
36
|
+
def status
|
37
|
+
response.status
|
38
|
+
end
|
39
|
+
|
40
|
+
def query_params
|
41
|
+
@query_params ||=
|
42
|
+
request.parameters.filter { |p| p['in'] == 'query' }
|
43
|
+
end
|
44
|
+
|
45
|
+
def query_fields
|
46
|
+
@query_fields ||= query_params.map { |p| p['name'] }.sort
|
47
|
+
end
|
48
|
+
|
49
|
+
def path_params
|
50
|
+
@path_params ||=
|
51
|
+
request.parameters.filter { |p| p['in'] == 'path' }
|
52
|
+
end
|
53
|
+
|
54
|
+
def path_fields
|
55
|
+
@path_fields ||= path_params.map { |p| p['name'] }.sort
|
56
|
+
end
|
57
|
+
|
58
|
+
def header_params
|
59
|
+
@header_params ||=
|
60
|
+
request.parameters.filter { |p| p['in'] == 'header' }
|
61
|
+
end
|
62
|
+
|
63
|
+
def header_fields
|
64
|
+
@header_fields ||= header_params.map { |p| p['name'] }.sort
|
65
|
+
end
|
66
|
+
|
67
|
+
def id
|
68
|
+
"#{method} (#{status}) #{media_type} #{path}"
|
69
|
+
end
|
70
|
+
|
71
|
+
def full_id
|
72
|
+
"#{id} query: #{query_fields} header: #{header_fields}"
|
73
|
+
end
|
74
|
+
|
75
|
+
def config(path, operation=nil, &block)
|
76
|
+
if request.path.eql?(path) && (!operation.nil? && request.operation.eql?(operation))
|
77
|
+
instance_exec this, &block
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.from_json(json)
|
82
|
+
new(
|
83
|
+
request: {
|
84
|
+
body_schema: json['request']['bodySchema'],
|
85
|
+
media_type: json['request']['contentMediaType'],
|
86
|
+
operation: json['request']['operation'],
|
87
|
+
parameters: json['request']['parameterList'],
|
88
|
+
path: json['path']
|
89
|
+
},
|
90
|
+
response: {
|
91
|
+
body_schema: json['expected']['bodySchema'],
|
92
|
+
media_type: json['expected']['contentMediaType'],
|
93
|
+
status: json['expected']['code'],
|
94
|
+
header_schema_map: json['expected']['headerSchemaMap']
|
95
|
+
}
|
96
|
+
)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class TestCaseResult < Dry::Struct
|
101
|
+
# https://dry-rb.org/gems/dry-struct/1.0/recipes/#resolving-default-values-on-code-nil-code
|
102
|
+
transform_types do |type|
|
103
|
+
if type.default?
|
104
|
+
type.constructor do |value|
|
105
|
+
value.nil? ? Dry::Types::Undefined : value
|
106
|
+
end
|
107
|
+
else
|
108
|
+
type
|
109
|
+
end
|
110
|
+
end
|
111
|
+
NIL_RESPONSE = ActionDispatch::TestResponse.new(0, {}, '').freeze
|
112
|
+
attribute :test, TestCase
|
113
|
+
attribute :actual, Types.Instance(ActionDispatch::TestResponse).default(NIL_RESPONSE)
|
114
|
+
attribute :filename, Types::String
|
115
|
+
attribute :info, Types::Hash.optional
|
116
|
+
attribute :validation_errors, Types::Array
|
117
|
+
attribute :autoapi_test_id, Types::String
|
118
|
+
|
119
|
+
def as_json
|
120
|
+
{
|
121
|
+
path: test.request.path,
|
122
|
+
filename: filename,
|
123
|
+
operation: test.request.operation,
|
124
|
+
autoapi_test_id: autoapi_test_id,
|
125
|
+
info: info,
|
126
|
+
transaction:
|
127
|
+
{
|
128
|
+
request: test_request,
|
129
|
+
expected: test_expected_response,
|
130
|
+
actual: actual_response,
|
131
|
+
result: test_result
|
132
|
+
}
|
133
|
+
}.to_json
|
134
|
+
end
|
135
|
+
|
136
|
+
def headers
|
137
|
+
headers = ''
|
138
|
+
actual.request.headers.each do |key, value|
|
139
|
+
headers += "#{key}='#{value}'; " if key.start_with? 'HTTP'
|
140
|
+
end
|
141
|
+
headers
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_request
|
145
|
+
{
|
146
|
+
parameters: test.request.parameters,
|
147
|
+
headers: headers,
|
148
|
+
request_body: actual.request.raw_post.encode('utf-8', invalid: :replace, undef: :replace),
|
149
|
+
content_media_type: actual.request.media_type
|
150
|
+
}
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_expected_response
|
154
|
+
{ status_code: test.response.status, body_schema: test.response.body_schema, content_media_type: test.response.media_type }
|
155
|
+
end
|
156
|
+
|
157
|
+
def actual_response
|
158
|
+
{ status_code: actual.status, body: actual.body, content_media_type: actual.media_type, headers: actual.headers }
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_result
|
162
|
+
issues = []
|
163
|
+
validation_errors.each do |error|
|
164
|
+
issues.concat([{ message: error }])
|
165
|
+
end
|
166
|
+
|
167
|
+
{ status: issues.empty? ? 'PASS' : 'FAIL', issues: issues }
|
168
|
+
end
|
169
|
+
end
|
170
|
+
=end
|
@@ -0,0 +1,280 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "autoapi_testing/version"
|
4
|
+
|
5
|
+
require 'autoapi/run_autoapi'
|
6
|
+
require 'autoapi/test_case'
|
7
|
+
require 'autoapi/convert'
|
8
|
+
|
9
|
+
module AutoapiTesting
|
10
|
+
class Error < StandardError; end
|
11
|
+
# Your code goes here...
|
12
|
+
class Sumo
|
13
|
+
def self.logger
|
14
|
+
@logger ||=
|
15
|
+
begin
|
16
|
+
Dir.glob('./test-results/autoapi/sumo.log*').each { |f| File.delete(f) }
|
17
|
+
FileUtils.mkdir_p './test-results/autoapi'
|
18
|
+
Logger.new('./test-results/autoapi/sumo.log', 100, 1.megabyte).tap do |logger|
|
19
|
+
logger.level = Logger::INFO
|
20
|
+
logger.formatter = proc do |_severity, _datetime, _progname, msg|
|
21
|
+
"#{msg}\n"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
# Track validation errors found so they can be sent to sumo
|
28
|
+
# after the test ends
|
29
|
+
class ValidationErrors
|
30
|
+
attr_reader :errors
|
31
|
+
def initialize
|
32
|
+
@errors = []
|
33
|
+
end
|
34
|
+
|
35
|
+
def process(error)
|
36
|
+
if error != true
|
37
|
+
error.each { |e| ::Rails.logger.error("Validation error Thrown by AutoAPI rails client: \n" + e.to_s) }
|
38
|
+
@errors.concat(error)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
class AutoAPI
|
43
|
+
|
44
|
+
def printy
|
45
|
+
puts 'TESTING'
|
46
|
+
end
|
47
|
+
|
48
|
+
def run_autoapi_2(oas_filename)
|
49
|
+
run_autoapi(oas_filename)
|
50
|
+
end
|
51
|
+
|
52
|
+
# bundle name and schema so `match_param` and `match_response_header`
|
53
|
+
# can provide the field name for context in the error message
|
54
|
+
NameAndSchema = Struct.new(:name, :schema)
|
55
|
+
|
56
|
+
# Send message to logger and to the rspec reporter
|
57
|
+
# which will display it to text-based formatters
|
58
|
+
# like progress and documentation that listen for
|
59
|
+
# the `message` event
|
60
|
+
def log_message(msg)
|
61
|
+
Rails.logger.info msg
|
62
|
+
RSpec.configuration.reporter.message msg
|
63
|
+
end
|
64
|
+
|
65
|
+
def output_request(path, params, headers)
|
66
|
+
log_message 'Request:'
|
67
|
+
log_message " Path: #{path}"
|
68
|
+
log_message " Parameters: #{params}"
|
69
|
+
log_message " Headers: #{headers}"
|
70
|
+
end
|
71
|
+
|
72
|
+
def output_response(status, body, headers)
|
73
|
+
log_message 'Response:'
|
74
|
+
log_message " Status Code: #{status}"
|
75
|
+
log_message " Headers: #{headers}"
|
76
|
+
log_message " Body: #{body}\n\n"
|
77
|
+
end
|
78
|
+
|
79
|
+
# Suppress type error for attachments.
|
80
|
+
#
|
81
|
+
# Attachments are added as Rack::Test::UploadedFile objects
|
82
|
+
# which raises a schema validation error as the interpretted
|
83
|
+
# type is `any` rather than `string` which is how attachments
|
84
|
+
# are marked in OAS 3.0.3
|
85
|
+
# https://procoretech.atlassian.net/browse/ACV-1019
|
86
|
+
def suppress_attachment_type_error(validation_errors)
|
87
|
+
validation_errors.select! do |error|
|
88
|
+
if error.match?(/attachment.*of type any did not match the following type: string/)
|
89
|
+
puts "Suppressing validation error: #{error}"
|
90
|
+
false
|
91
|
+
else
|
92
|
+
true
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# run OpenAPI specification tests generated by autoapi
|
98
|
+
def generate_autoapi(oas_filename, &block)
|
99
|
+
autoapi_test_id = ENV.fetch('CIRCLE_BUILD_NUM', RSpec.configuration.start_time.to_i).to_s
|
100
|
+
RSpec.describe oas_filename, :autoapi, type: :request, order: :defined do
|
101
|
+
matcher :match_body do |expected|
|
102
|
+
validation_errors = []
|
103
|
+
match do |actual|
|
104
|
+
validation_errors.concat(JSON::Validator.fully_validate(expected, actual, parse_data: false))
|
105
|
+
suppress_attachment_type_error(validation_errors)
|
106
|
+
validation_errors.empty?
|
107
|
+
end
|
108
|
+
failure_message { validation_errors.join("\n") }
|
109
|
+
end
|
110
|
+
|
111
|
+
matcher :match_param do |expected|
|
112
|
+
match do |actual|
|
113
|
+
JSON::Validator.fully_validate(expected.schema, actual, parse_data: false).empty?
|
114
|
+
end
|
115
|
+
failure_message do |actual|
|
116
|
+
"\"#{expected.name}\": expected \"#{actual}\" to match schema #{expected.schema}"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
matcher :match_response_header do |expected|
|
121
|
+
match do |actual|
|
122
|
+
JSON::Validator.fully_validate(expected.schema, actual).empty?
|
123
|
+
end
|
124
|
+
failure_message do |actual|
|
125
|
+
"\"#{expected.name}\": expected \"#{actual}\" to match schema #{expected.schema}"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
test_fixture = run_autoapi(oas_filename)
|
130
|
+
# rubocop:disable Metrics/BlockLength
|
131
|
+
test_fixture['test_cases'].each do |test_json|
|
132
|
+
validation_errors = ValidationErrors.new
|
133
|
+
test = TestCase.from_json(test_json)
|
134
|
+
describe test.full_id do
|
135
|
+
after do
|
136
|
+
unless response.nil?
|
137
|
+
Sumo.logger.info TestCaseResult.new(test: test, actual: @response, filename: test_fixture['filename'], info: test_fixture['info'], validation_errors: validation_errors.errors, autoapi_test_id: autoapi_test_id).as_json
|
138
|
+
end
|
139
|
+
end
|
140
|
+
let(:headers) { {} }
|
141
|
+
let(:path_params) { {} }
|
142
|
+
let(:query_params) { {} }
|
143
|
+
let(:body_params) { {} }
|
144
|
+
# run test setup defined by test writer
|
145
|
+
# if a failure is encountered, save and re-raise within the test
|
146
|
+
# so the test run can continue
|
147
|
+
setup_error = nil
|
148
|
+
begin
|
149
|
+
instance_exec test, &block
|
150
|
+
rescue StandardError => e
|
151
|
+
setup_error = e
|
152
|
+
end
|
153
|
+
it 'matches spec' do
|
154
|
+
raise setup_error unless setup_error.nil?
|
155
|
+
|
156
|
+
path = test.path.gsub('{', '%{') % path_params
|
157
|
+
# create query string parameters based on the test,
|
158
|
+
# for the value use the query_params block and fallback to the static example
|
159
|
+
computed_qs_params = Hash[
|
160
|
+
test.query_params.map do |p|
|
161
|
+
[p['name'], query_params.fetch(p['name'], p['example'])]
|
162
|
+
end
|
163
|
+
]
|
164
|
+
# merge body and query parameters since rails does not differentiate between them
|
165
|
+
params =
|
166
|
+
case test.request.media_type
|
167
|
+
when ''
|
168
|
+
computed_qs_params
|
169
|
+
when 'application/json'
|
170
|
+
computed_qs_params.merge(body_params).to_json
|
171
|
+
when 'multipart/form-data'
|
172
|
+
computed_qs_params.merge(body_params)
|
173
|
+
else
|
174
|
+
raise 'Unknown media type'
|
175
|
+
end
|
176
|
+
|
177
|
+
# Add Accept and Content-Type headers if they have not been set
|
178
|
+
# The header's class performs case standardization and handles
|
179
|
+
# whether the user used HTTP_ACCEPT or Accept
|
180
|
+
http_headers = ActionDispatch::Http::Headers.from_hash(headers)
|
181
|
+
unless http_headers.include?('Accept') || test.response.media_type.blank?
|
182
|
+
headers['HTTP_ACCEPT'] = test.response.media_type
|
183
|
+
end
|
184
|
+
unless http_headers.include?('Content-Type') || test.request.media_type.blank?
|
185
|
+
headers['CONTENT_TYPE'] = test.request.media_type
|
186
|
+
end
|
187
|
+
|
188
|
+
output_request path, params, headers
|
189
|
+
process test.method.to_sym, path, params: params, headers: headers
|
190
|
+
output_response response.status, response.body, response.headers
|
191
|
+
|
192
|
+
aggregate_failures 'validate request and response' do
|
193
|
+
# request query string parameters
|
194
|
+
test.query_params.each do |query_param|
|
195
|
+
expected_query_param = NameAndSchema.new(query_param['name'], query_param['schema'])
|
196
|
+
query_param_value = computed_qs_params[expected_query_param.name]
|
197
|
+
validation_errors.process(
|
198
|
+
expect(query_param_value).to(match_param(expected_query_param))
|
199
|
+
)
|
200
|
+
end
|
201
|
+
# request headers
|
202
|
+
unless test.request.media_type.blank?
|
203
|
+
validation_errors.process(
|
204
|
+
expect(response.request.headers['Content-Type']).to(start_with test.request.media_type)
|
205
|
+
)
|
206
|
+
end
|
207
|
+
unless test.response.media_type.blank?
|
208
|
+
validation_errors.process(
|
209
|
+
expect(response.request.headers['Accept']).to(eq test.response.media_type)
|
210
|
+
)
|
211
|
+
end
|
212
|
+
# request body
|
213
|
+
unless test.request.body_schema.nil?
|
214
|
+
validation_errors.process(
|
215
|
+
expect(body_params).to(match_body(test.request.body_schema))
|
216
|
+
)
|
217
|
+
end
|
218
|
+
# response status code
|
219
|
+
status_code_mismatch = "Expected response code to be #{test.status} but actual was #{response.status}. \nResponse body: #{response.body}"
|
220
|
+
validation_errors.process(
|
221
|
+
expect(response).to(have_http_status(test.status), status_code_mismatch)
|
222
|
+
)
|
223
|
+
# response body
|
224
|
+
response_body = response.body.blank? ? response.body : json_response
|
225
|
+
expected_response_body_schema = convert test.response.body_schema
|
226
|
+
if expected_response_body_schema
|
227
|
+
if test.response.media_type == 'application/json'
|
228
|
+
validation_errors.process(
|
229
|
+
expect(response_body).to(match_body(expected_response_body_schema))
|
230
|
+
)
|
231
|
+
if expected_response_body_schema['type'] == 'array'
|
232
|
+
validation_errors.process(
|
233
|
+
expect(response_body).not_to(be_empty,
|
234
|
+
'Response was an empty list, setup the test so some data is returned')
|
235
|
+
)
|
236
|
+
end
|
237
|
+
else
|
238
|
+
log_message 'Non-json response bodies are not validated'
|
239
|
+
end
|
240
|
+
# response body content-type header
|
241
|
+
# using start_with instead of eq since the charset
|
242
|
+
# may be added like "application/json; utf-8"
|
243
|
+
validation_errors.process(
|
244
|
+
expect(response.headers['Content-Type']).to(start_with(test.response.media_type))
|
245
|
+
)
|
246
|
+
else
|
247
|
+
validation_errors.process(
|
248
|
+
expect(response_body).to(be_blank)
|
249
|
+
)
|
250
|
+
end
|
251
|
+
# response headers
|
252
|
+
expected_response_header_schemas = convert test.response.header_schema_map
|
253
|
+
expected_response_header_schemas&.each do |header_key, header_schema|
|
254
|
+
# Treat all response headers as optional until AutoAPI starts sending
|
255
|
+
# whether the header is required or optional
|
256
|
+
if response.has_header? header_key
|
257
|
+
response_header_value = response.headers[header_key]
|
258
|
+
expected_response_header = NameAndSchema.new(header_key, header_schema)
|
259
|
+
validation_errors.process(
|
260
|
+
expect(response_header_value).to(match_response_header(expected_response_header))
|
261
|
+
)
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
# rubocop:enable Metrics/BlockLength
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
end
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
end
|
280
|
+
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: autoapi_testing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Danny Porrello
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-types
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Testing Description
|
28
|
+
email:
|
29
|
+
- danny.porrello@procore.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- CHANGELOG.md
|
35
|
+
- CODE_OF_CONDUCT.md
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- bin/console
|
41
|
+
- bin/setup
|
42
|
+
- lib/autoapi/AutoAPI-0.0.1.jar
|
43
|
+
- lib/autoapi/convert.rb
|
44
|
+
- lib/autoapi/run_autoapi.rb
|
45
|
+
- lib/autoapi/test_case.rb
|
46
|
+
- lib/autoapi_testing.rb
|
47
|
+
- lib/autoapi_testing/version.rb
|
48
|
+
homepage: https://example.com
|
49
|
+
licenses: []
|
50
|
+
metadata:
|
51
|
+
allowed_push_host: https://rubygems.org
|
52
|
+
homepage_uri: https://example.com
|
53
|
+
source_code_uri: https://example.com
|
54
|
+
changelog_uri: https://example.com
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 2.6.0
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.0.3
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Testing Summary
|
74
|
+
test_files: []
|