Basic rubocops

This commit is contained in:
Greg Myers
2020-03-24 16:02:13 +00:00
parent 5060697761
commit 6b4f49aacb
25 changed files with 171 additions and 165 deletions

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'generator_spec'
require 'generators/rspec/swagger_generator'
require 'tmpdir'
@@ -9,12 +11,11 @@ module Rspec
before(:all) do
prepare_destination
fixtures_dir = File.expand_path('../fixtures', __FILE__)
fixtures_dir = File.expand_path('fixtures', __dir__)
FileUtils.cp_r("#{fixtures_dir}/spec", destination_root)
end
after(:all) do
end
it 'installs the swagger_helper for rspec' do
@@ -31,11 +32,11 @@ module Rspec
def fake_routes
{
"/posts/{post_id}/comments/{id}" => {
:params => ["post_id", "id"],
:actions => {
"get" => { :summary=>"show comment" },
"patch" => { :summary=>"update_comments comment" }
'/posts/{post_id}/comments/{id}' => {
params: ['post_id', 'id'],
actions: {
'get' => { summary: 'show comment' },
'patch' => { summary: 'update_comments comment' }
}
}
}

View File

@@ -1,16 +1,17 @@
# frozen_string_literal: true
require 'generator_spec'
require 'generators/rswag/specs/install/install_generator'
module Rswag
module Specs
RSpec.describe InstallGenerator do
include GeneratorSpec::TestCase
destination File.expand_path('../tmp', __FILE__)
destination File.expand_path('tmp', __dir__)
before(:all) do
prepare_destination
fixtures_dir = File.expand_path('../fixtures', __FILE__)
fixtures_dir = File.expand_path('fixtures', __dir__)
FileUtils.cp_r("#{fixtures_dir}/spec", destination_root)
run_generator

View File

@@ -4,7 +4,6 @@ require 'rswag/specs/configuration'
module Rswag
module Specs
RSpec.describe Configuration do
subject { described_class.new(rspec_config) }

View File

@@ -4,7 +4,6 @@ require 'rswag/specs/example_group_helpers'
module Rswag
module Specs
RSpec.describe ExampleGroupHelpers do
subject { double('example_group') }
@@ -50,12 +49,12 @@ module Rswag
it "adds to the 'operation' metadata" do
expect(api_metadata[:operation]).to match(
tags: [ 'Blogs', 'Admin' ],
tags: ['Blogs', 'Admin'],
description: 'Some description',
operationId: 'createBlog',
consumes: [ 'application/json', 'application/xml' ],
produces: [ 'application/json', 'application/xml' ],
schemes: [ 'http', 'https' ],
consumes: ['application/json', 'application/xml'],
produces: ['application/json', 'application/xml'],
schemes: ['http', 'https'],
deprecated: true
)
end
@@ -76,12 +75,12 @@ module Rswag
it "adds to the 'operation' metadata" do
expect(api_metadata[:operation]).to match(
tags: [ 'Blogs', 'Admin' ],
tags: ['Blogs', 'Admin'],
description: 'Some description',
operationId: 'createBlog',
consumes: [ 'application/json', 'application/xml' ],
produces: [ 'application/json', 'application/xml' ],
schemes: [ 'http', 'https' ],
consumes: ['application/json', 'application/xml'],
produces: ['application/json', 'application/xml'],
schemes: ['http', 'https'],
deprecated: true,
security: { api_key: [] }
)
@@ -123,14 +122,13 @@ module Rswag
# end
describe '#parameter(attributes)' do
context "when called at the 'path' level" do
before { subject.parameter(name: :blog, in: :body, schema: { type: 'object' }) }
let(:api_metadata) { { path_item: {} } } # i.e. operation not defined yet
it "adds to the 'path_item parameters' metadata" do
expect(api_metadata[:path_item][:parameters]).to match(
[ name: :blog, in: :body, schema: { type: 'object' } ]
[name: :blog, in: :body, schema: { type: 'object' }]
)
end
end
@@ -141,7 +139,7 @@ module Rswag
it "adds to the 'operation parameters' metadata" do
expect(api_metadata[:operation][:parameters]).to match(
[ name: :blog, in: :body, schema: { type: 'object' } ]
[name: :blog, in: :body, schema: { type: 'object' }]
)
end
end
@@ -152,7 +150,7 @@ module Rswag
it "automatically sets the 'required' flag" do
expect(api_metadata[:operation][:parameters]).to match(
[ name: :id, in: :path, required: true ]
[name: :id, in: :path, required: true]
)
end
end
@@ -162,7 +160,7 @@ module Rswag
let(:api_metadata) { { operation: {} } }
it "does not require the 'in' parameter key" do
expect(api_metadata[:operation][:parameters]).to match([ name: :id ])
expect(api_metadata[:operation][:parameters]).to match([name: :id])
end
end
end

View File

@@ -4,7 +4,6 @@ require 'rswag/specs/example_helpers'
module Rswag
module Specs
RSpec.describe ExampleHelpers do
subject { double('example') }
@@ -34,7 +33,7 @@ module Rswag
operation: {
verb: :put,
summary: 'Updates a blog',
consumes: [ 'application/json' ],
consumes: ['application/json'],
parameters: [
{ name: :blog_id, in: :path, type: 'integer' },
{ name: 'id', in: :path, type: 'integer' },

View File

@@ -4,7 +4,6 @@ require 'rswag/specs/request_factory'
module Rswag
module Specs
RSpec.describe RequestFactory do
subject { RequestFactory.new(config) }
@@ -55,7 +54,7 @@ module Rswag
allow(example).to receive(:q2).and_return('bar')
end
it "builds the query string from example values" do
it 'builds the query string from example values' do
expect(request[:path]).to eq('/blogs?q1=foo&q2=bar')
end
end
@@ -65,40 +64,40 @@ module Rswag
metadata[:operation][:parameters] = [
{ name: 'things', in: :query, type: :array, collectionFormat: collection_format }
]
allow(example).to receive(:things).and_return([ 'foo', 'bar' ])
allow(example).to receive(:things).and_return(['foo', 'bar'])
end
context 'collectionFormat = csv' do
let(:collection_format) { :csv }
it "formats as comma separated values" do
it 'formats as comma separated values' do
expect(request[:path]).to eq('/blogs?things=foo,bar')
end
end
context 'collectionFormat = ssv' do
let(:collection_format) { :ssv }
it "formats as space separated values" do
it 'formats as space separated values' do
expect(request[:path]).to eq('/blogs?things=foo bar')
end
end
context 'collectionFormat = tsv' do
let(:collection_format) { :tsv }
it "formats as tab separated values" do
it 'formats as tab separated values' do
expect(request[:path]).to eq('/blogs?things=foo\tbar')
end
end
context 'collectionFormat = pipes' do
let(:collection_format) { :pipes }
it "formats as pipe separated values" do
it 'formats as pipe separated values' do
expect(request[:path]).to eq('/blogs?things=foo|bar')
end
end
context 'collectionFormat = multi' do
let(:collection_format) { :multi }
it "formats as multiple parameter instances" do
it 'formats as multiple parameter instances' do
expect(request[:path]).to eq('/blogs?things=foo&things=bar')
end
end
@@ -106,7 +105,7 @@ module Rswag
context "'header' parameters" do
before do
metadata[:operation][:parameters] = [ { name: 'Api-Key', in: :header, type: :string } ]
metadata[:operation][:parameters] = [{ name: 'Api-Key', in: :header, type: :string }]
allow(example).to receive(:'Api-Key').and_return('foobar')
end
@@ -129,9 +128,9 @@ module Rswag
end
end
context "consumes content" do
context 'consumes content' do
before do
metadata[:operation][:consumes] = [ 'application/json', 'application/xml' ]
metadata[:operation][:consumes] = ['application/json', 'application/xml']
end
context "no 'Content-Type' provided" do
@@ -152,18 +151,18 @@ module Rswag
context 'JSON payload' do
before do
metadata[:operation][:parameters] = [ { name: 'comment', in: :body, schema: { type: 'object' } } ]
metadata[:operation][:parameters] = [{ name: 'comment', in: :body, schema: { type: 'object' } }]
allow(example).to receive(:comment).and_return(text: 'Some comment')
end
it "serializes first 'body' parameter to JSON string" do
expect(request[:payload]).to eq("{\"text\":\"Some comment\"}")
expect(request[:payload]).to eq('{"text":"Some comment"}')
end
end
context 'form payload' do
before do
metadata[:operation][:consumes] = [ 'multipart/form-data' ]
metadata[:operation][:consumes] = ['multipart/form-data']
metadata[:operation][:parameters] = [
{ name: 'f1', in: :formData, type: :string },
{ name: 'f2', in: :formData, type: :string }
@@ -183,7 +182,7 @@ module Rswag
context 'produces content' do
before do
metadata[:operation][:produces] = [ 'application/json', 'application/xml' ]
metadata[:operation][:produces] = ['application/json', 'application/xml']
end
context "no 'Accept' value provided" do
@@ -194,7 +193,7 @@ module Rswag
context "explicit 'Accept' value provided" do
before do
allow(example).to receive(:'Accept').and_return('application/xml')
allow(example).to receive(:Accept).and_return('application/xml')
end
it "sets 'HTTP_ACCEPT' header to example value" do
@@ -207,7 +206,7 @@ module Rswag
context 'swagger 2.0' do
before do
swagger_doc[:securityDefinitions] = { basic: { type: :basic } }
metadata[:operation][:security] = [ basic: [] ]
metadata[:operation][:security] = [basic: []]
allow(example).to receive(:Authorization).and_return('Basic foobar')
end
@@ -220,7 +219,7 @@ module Rswag
let(:swagger_doc) { { openapi: '3.0.1' } }
before do
swagger_doc[:components] = { securitySchemes: { basic: { type: :basic } } }
metadata[:operation][:security] = [ basic: [] ]
metadata[:operation][:security] = [basic: []]
allow(example).to receive(:Authorization).and_return('Basic foobar')
end
@@ -234,7 +233,7 @@ module Rswag
before do
allow(ActiveSupport::Deprecation).to receive(:warn)
swagger_doc[:securityDefinitions] = { basic: { type: :basic } }
metadata[:operation][:security] = [ basic: [] ]
metadata[:operation][:security] = [basic: []]
allow(example).to receive(:Authorization).and_return('Basic foobar')
end
@@ -254,7 +253,7 @@ module Rswag
# swagger_doc[:components] = { securitySchemes: {
# apiKey: { type: :apiKey, name: 'api_key', in: key_location }
# } }
metadata[:operation][:security] = [ apiKey: [] ]
metadata[:operation][:security] = [apiKey: []]
allow(example).to receive(:api_key).and_return('foobar')
end
@@ -294,12 +293,12 @@ module Rswag
context 'oauth2' do
before do
swagger_doc[:securityDefinitions] = { oauth2: { type: :oauth2, scopes: [ 'read:blogs' ] } }
swagger_doc[:securityDefinitions] = { oauth2: { type: :oauth2, scopes: ['read:blogs'] } }
## OA3
# swagger_doc[:components] = { securitySchemes: {
# oauth2: { type: :oauth2, scopes: ['read:blogs'] }
# } }
metadata[:operation][:security] = [ oauth2: [ 'read:blogs' ] ]
metadata[:operation][:security] = [oauth2: ['read:blogs']]
allow(example).to receive(:Authorization).and_return('Bearer foobar')
end
@@ -319,26 +318,26 @@ module Rswag
# basic: { type: :basic },
# api_key: { type: :apiKey, name: 'api_key', in: :query }
# } }
metadata[:operation][:security] = [ { basic: [], api_key: [] } ]
metadata[:operation][:security] = [{ basic: [], api_key: [] }]
allow(example).to receive(:Authorization).and_return('Basic foobar')
allow(example).to receive(:api_key).and_return('foobar')
end
it "sets both params to example values" do
it 'sets both params to example values' do
expect(request[:headers]).to eq('HTTP_AUTHORIZATION' => 'Basic foobar')
expect(request[:path]).to eq('/blogs?api_key=foobar')
end
end
context "path-level parameters" do
context 'path-level parameters' do
before do
metadata[:operation][:parameters] = [ { name: 'q1', in: :query, type: :string } ]
metadata[:path_item][:parameters] = [ { name: 'q2', in: :query, type: :string } ]
metadata[:operation][:parameters] = [{ name: 'q1', in: :query, type: :string }]
metadata[:path_item][:parameters] = [{ name: 'q2', in: :query, type: :string }]
allow(example).to receive(:q1).and_return('foo')
allow(example).to receive(:q2).and_return('bar')
end
it "populates operation and path level parameters " do
it 'populates operation and path level parameters' do
expect(request[:path]).to eq('/blogs?q1=foo&q2=bar')
end
end
@@ -347,7 +346,7 @@ module Rswag
context 'swagger 2.0' do
before do
swagger_doc[:parameters] = { q1: { name: 'q1', in: :query, type: :string } }
metadata[:operation][:parameters] = [ { '$ref' => '#/parameters/q1' } ]
metadata[:operation][:parameters] = [{ '$ref' => '#/parameters/q1' }]
allow(example).to receive(:q1).and_return('foo')
end
@@ -360,7 +359,7 @@ module Rswag
let(:swagger_doc) { { openapi: '3.0.1' } }
before do
swagger_doc[:components] = { parameters: { q1: { name: 'q1', in: :query, type: :string } } }
metadata[:operation][:parameters] = [ { '$ref' => '#/components/parameters/q1' } ]
metadata[:operation][:parameters] = [{ '$ref' => '#/components/parameters/q1' }]
allow(example).to receive(:q1).and_return('foo')
end
@@ -374,7 +373,7 @@ module Rswag
before do
allow(ActiveSupport::Deprecation).to receive(:warn)
swagger_doc[:parameters] = { q1: { name: 'q1', in: :query, type: :string } }
metadata[:operation][:parameters] = [ { '$ref' => '#/parameters/q1' } ]
metadata[:operation][:parameters] = [{ '$ref' => '#/parameters/q1' }]
allow(example).to receive(:q1).and_return('foo')
end
@@ -396,20 +395,20 @@ module Rswag
end
end
context "global consumes" do
before { swagger_doc[:consumes] = [ 'application/xml' ] }
context 'global consumes' do
before { swagger_doc[:consumes] = ['application/xml'] }
it "defaults 'CONTENT_TYPE' to global value(s)" do
expect(request[:headers]).to eq('CONTENT_TYPE' => 'application/xml')
end
end
context "global security requirements" do
context 'global security requirements' do
before do
swagger_doc[:securityDefinitions] = { apiKey: { type: :apiKey, name: 'api_key', in: :query } }
## OA3
# swagger_doc[:components] = { securitySchemes: { apiKey: { type: :apiKey, name: 'api_key', in: :query } } }
swagger_doc[:security] = [ apiKey: [] ]
swagger_doc[:security] = [apiKey: []]
allow(example).to receive(:api_key).and_return('foobar')
end

View File

@@ -4,7 +4,6 @@ require 'rswag/specs/response_validator'
module Rswag
module Specs
RSpec.describe ResponseValidator do
subject { ResponseValidator.new(config) }
@@ -23,7 +22,7 @@ module Rswag
schema: {
type: :object,
properties: { text: { type: :string } },
required: [ 'text' ]
required: ['text']
}
## OA3
# content: {
@@ -49,21 +48,21 @@ module Rswag
)
end
context "response matches metadata" do
context 'response matches metadata' do
it { expect { call }.to_not raise_error }
end
context "response code differs from metadata" do
context 'response code differs from metadata' do
before { response.code = '400' }
it { expect { call }.to raise_error(/Expected response code/) }
end
context "response headers differ from metadata" do
context 'response headers differ from metadata' do
before { response.headers = {} }
it { expect { call }.to raise_error(/Expected response header/) }
end
context "response body differs from metadata" do
context 'response body differs from metadata' do
before { response.body = '{"foo":"Some comment"}' }
it { expect { call }.to raise_error(/Expected response body/) }
end
@@ -75,7 +74,7 @@ module Rswag
'blog' => {
type: :object,
properties: { foo: { type: :string } },
required: [ 'foo' ]
required: ['foo']
}
}
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }
@@ -96,7 +95,7 @@ module Rswag
'blog' => {
type: :object,
properties: { foo: { type: :string } },
required: [ 'foo' ]
required: ['foo']
}
}
}
@@ -116,7 +115,7 @@ module Rswag
'blog' => {
type: :object,
properties: { foo: { type: :string } },
required: [ 'foo' ]
required: ['foo']
}
}
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }

View File

@@ -5,7 +5,6 @@ require 'ostruct'
module Rswag
module Specs
RSpec.describe SwaggerFormatter do
subject { described_class.new(output, config) }
@@ -15,7 +14,7 @@ module Rswag
end
let(:config) { double('config') }
let(:output) { double('output').as_null_object }
let(:swagger_root) { File.expand_path('../tmp/swagger', __FILE__) }
let(:swagger_root) { File.expand_path('tmp/swagger', __dir__) }
describe '#example_group_finished(notification)' do
before do
@@ -69,19 +68,21 @@ module Rswag
end
context 'with metadata upgrades for 3.0' do
let(:swagger_doc) { {
openapi: '3.0.1',
basePath: '/foo',
schemes: ['http', 'https'],
host: 'api.example.com',
components: {
securitySchemes: {
my_oauth: {
flow: :anything
let(:swagger_doc) do
{
openapi: '3.0.1',
basePath: '/foo',
schemes: ['http', 'https'],
host: 'api.example.com',
components: {
securitySchemes: {
my_oauth: {
flow: :anything
}
}
}
}
} }
end
let(:document) { nil }
it 'converts query and path params, type: to schema: { type: }' do
@@ -128,7 +129,7 @@ module Rswag
describe '#stop' do
before do
FileUtils.rm_r(swagger_root) if File.exists?(swagger_root)
FileUtils.rm_r(swagger_root) if File.exist?(swagger_root)
allow(config).to receive(:swagger_docs).and_return(
'v1/swagger.json' => { info: { version: 'v1' } },
'v2/swagger.json' => { info: { version: 'v2' } }
@@ -160,7 +161,7 @@ module Rswag
end
after do
FileUtils.rm_r(swagger_root) if File.exists?(swagger_root)
FileUtils.rm_r(swagger_root) if File.exist?(swagger_root)
end
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module Rails
module VERSION
MAJOR = 3

View File

@@ -1,2 +1,4 @@
# frozen_string_literal: true
# NOTE: For the specs in this gem, all configuration is completely mocked out
# The file just needs to be present because it gets required by the swagger_formatter