Class: AutoapiTesting::AutoAPI
- Inherits:
-
Object
- Object
- AutoapiTesting::AutoAPI
- Defined in:
- lib/autoapi_testing.rb
Defined Under Namespace
Classes: NameAndSchema
Instance Method Summary collapse
-
#generate_autoapi(oas_filename, &block) ⇒ Object
run OpenAPI specification tests generated by autoapi.
-
#log_message(msg) ⇒ Object
Send message to logger and to the rspec reporter which will display it to text-based formatters like progress and documentation that listen for the ‘message` event.
- #output_request(path, params, headers) ⇒ Object
- #output_response(status, body, headers) ⇒ Object
- #printy ⇒ Object
- #run_autoapi_2(oas_filename) ⇒ Object
-
#suppress_attachment_type_error(validation_errors) ⇒ Object
Suppress type error for attachments.
Instance Method Details
#generate_autoapi(oas_filename, &block) ⇒ Object
run OpenAPI specification tests generated by autoapi
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/autoapi_testing.rb', line 98 def generate_autoapi(oas_filename, &block) autoapi_test_id = ENV.fetch('CIRCLE_BUILD_NUM', RSpec.configuration.start_time.to_i).to_s RSpec.describe oas_filename, :autoapi, type: :request, order: :defined do matcher :match_body do |expected| validation_errors = [] match do |actual| validation_errors.concat(JSON::Validator.fully_validate(expected, actual, parse_data: false)) (validation_errors) validation_errors.empty? end { validation_errors.join("\n") } end matcher :match_param do |expected| match do |actual| JSON::Validator.fully_validate(expected.schema, actual, parse_data: false).empty? end do |actual| "\"#{expected.name}\": expected \"#{actual}\" to match schema #{expected.schema}" end end matcher :match_response_header do |expected| match do |actual| JSON::Validator.fully_validate(expected.schema, actual).empty? end do |actual| "\"#{expected.name}\": expected \"#{actual}\" to match schema #{expected.schema}" end end test_fixture = run_autoapi(oas_filename) # rubocop:disable Metrics/BlockLength test_fixture['test_cases'].each do |test_json| validation_errors = ValidationErrors.new test = TestCase.from_json(test_json) describe test.full_id do after do unless response.nil? 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 end end let(:headers) { {} } let(:path_params) { {} } let(:query_params) { {} } let(:body_params) { {} } # run test setup defined by test writer # if a failure is encountered, save and re-raise within the test # so the test run can continue setup_error = nil begin instance_exec test, &block rescue StandardError => e setup_error = e end it 'matches spec' do raise setup_error unless setup_error.nil? path = test.path.gsub('{', '%{') % path_params # create query string parameters based on the test, # for the value use the query_params block and fallback to the static example computed_qs_params = Hash[ test.query_params.map do |p| [p['name'], query_params.fetch(p['name'], p['example'])] end ] # merge body and query parameters since rails does not differentiate between them params = case test.request.media_type when '' computed_qs_params when 'application/json' computed_qs_params.merge(body_params).to_json when 'multipart/form-data' computed_qs_params.merge(body_params) else raise 'Unknown media type' end # Add Accept and Content-Type headers if they have not been set # The header's class performs case standardization and handles # whether the user used HTTP_ACCEPT or Accept http_headers = ActionDispatch::Http::Headers.from_hash(headers) unless http_headers.include?('Accept') || test.response.media_type.blank? headers['HTTP_ACCEPT'] = test.response.media_type end unless http_headers.include?('Content-Type') || test.request.media_type.blank? headers['CONTENT_TYPE'] = test.request.media_type end output_request path, params, headers process test.method.to_sym, path, params: params, headers: headers output_response response.status, response.body, response.headers aggregate_failures 'validate request and response' do # request query string parameters test.query_params.each do |query_param| expected_query_param = NameAndSchema.new(query_param['name'], query_param['schema']) query_param_value = computed_qs_params[expected_query_param.name] validation_errors.process( expect(query_param_value).to(match_param(expected_query_param)) ) end # request headers unless test.request.media_type.blank? validation_errors.process( expect(response.request.headers['Content-Type']).to(start_with test.request.media_type) ) end unless test.response.media_type.blank? validation_errors.process( expect(response.request.headers['Accept']).to(eq test.response.media_type) ) end # request body unless test.request.body_schema.nil? validation_errors.process( expect(body_params).to(match_body(test.request.body_schema)) ) end # response status code status_code_mismatch = "Expected response code to be #{test.status} but actual was #{response.status}. \nResponse body: #{response.body}" validation_errors.process( expect(response).to(have_http_status(test.status), status_code_mismatch) ) # response body response_body = response.body.blank? ? response.body : json_response expected_response_body_schema = convert test.response.body_schema if expected_response_body_schema if test.response.media_type == 'application/json' validation_errors.process( expect(response_body).to(match_body(expected_response_body_schema)) ) if expected_response_body_schema['type'] == 'array' validation_errors.process( expect(response_body).not_to(be_empty, 'Response was an empty list, setup the test so some data is returned') ) end else 'Non-json response bodies are not validated' end # response body content-type header # using start_with instead of eq since the charset # may be added like "application/json; utf-8" validation_errors.process( expect(response.headers['Content-Type']).to(start_with(test.response.media_type)) ) else validation_errors.process( expect(response_body).to(be_blank) ) end # response headers expected_response_header_schemas = convert test.response.header_schema_map expected_response_header_schemas&.each do |header_key, header_schema| # Treat all response headers as optional until AutoAPI starts sending # whether the header is required or optional if response.has_header? header_key response_header_value = response.headers[header_key] expected_response_header = NameAndSchema.new(header_key, header_schema) validation_errors.process( expect(response_header_value).to(match_response_header(expected_response_header)) ) end end end end end end # rubocop:enable Metrics/BlockLength end end |
#log_message(msg) ⇒ Object
Send message to logger and to the rspec reporter which will display it to text-based formatters like progress and documentation that listen for the ‘message` event
60 61 62 63 |
# File 'lib/autoapi_testing.rb', line 60 def (msg) Rails.logger.info msg RSpec.configuration.reporter. msg end |
#output_request(path, params, headers) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/autoapi_testing.rb', line 65 def output_request(path, params, headers) 'Request:' " Path: #{path}" " Parameters: #{params}" " Headers: #{headers}" end |
#output_response(status, body, headers) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/autoapi_testing.rb', line 72 def output_response(status, body, headers) 'Response:' " Status Code: #{status}" " Headers: #{headers}" " Body: #{body}\n\n" end |
#printy ⇒ Object
44 45 46 |
# File 'lib/autoapi_testing.rb', line 44 def printy puts 'TESTING' end |
#run_autoapi_2(oas_filename) ⇒ Object
48 49 50 |
# File 'lib/autoapi_testing.rb', line 48 def run_autoapi_2(oas_filename) run_autoapi(oas_filename) end |
#suppress_attachment_type_error(validation_errors) ⇒ Object
Suppress type error for attachments.
Attachments are added as Rack::Test::UploadedFile objects which raises a schema validation error as the interpretted type is ‘any` rather than `string` which is how attachments are marked in OAS 3.0.3 procoretech.atlassian.net/browse/ACV-1019
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/autoapi_testing.rb', line 86 def (validation_errors) validation_errors.select! do |error| if error.match?(/attachment.*of type any did not match the following type: string/) puts "Suppressing validation error: #{error}" false else true end end end |