Skip to content

Commit

Permalink
feat(REST): RHICOMPL-3904 generate APIv2 spec
Browse files Browse the repository at this point in the history
  • Loading branch information
romanblanco committed Aug 4, 2023
1 parent 963f328 commit fd57718
Show file tree
Hide file tree
Showing 10 changed files with 626 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ GEM
rspec-support (3.10.3)
rswag-api (2.8.0)
railties (>= 3.1, < 7.1)
rswag-specs (2.8.0)
rswag-specs (2.10.1)
activesupport (>= 3.1, < 7.1)
json-schema (>= 2.2, < 4.0)
railties (>= 3.1, < 7.1)
Expand Down
56 changes: 56 additions & 0 deletions spec/api/v2/openapi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require './spec/api/v2/schemas'

module Api
module V2
class Openapi
include Api::V2::Schemas

def self.doc
new.doc
end

def doc
{
openapi: '3.1.0',
info: info,
servers: servers,
paths: {},
components: {
schemas: SCHEMAS
}
}
end

def servers
[
# TODO: when v1 gets deprecated
# {
# url: 'https://{defaultHost}/api/compliance',
# variables: { defaultHost: { default: 'console.redhat.com' } }
# },
{
url: 'https://{defaultHost}/api/compliance/v2',
variables: { defaultHost: { default: 'console.redhat.com' } }
}
]
end

def info
{
title: 'Cloud Services for RHEL Compliance API V2',
version: 'v2',
description: description
}
end

def description
'This is the API for Cloud Services for RHEL Compliance. '\
'You can find out more about Red Hat Cloud Services for RHEL at '\
'[https://console.redhat.com/]'\
'(https://console.redhat.com/)'
end
end
end
end
20 changes: 20 additions & 0 deletions spec/api/v2/schemas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require './spec/api/v2/schemas/security_guides'
require './spec/api/v2/schemas/metadata'

module Api
module V2
module Schemas
include Metadata
include SecurityGuides

SCHEMAS = {
uuid: UUID,
links: LINKS,
metadata: METADATA,
security_guides: SECURITY_GUIDE
}.freeze
end
end
end
74 changes: 74 additions & 0 deletions spec/api/v2/schemas/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

module Api
module V2
module Schemas
module Metadata
UUID = { type: :string, format: :uuid }.freeze
METADATA = {
type: 'object',
properties: {
total: {
type: 'integer',
examples: [1]
},
limit: {
type: 'integer',
maximum: 100,
minimum: 1,
default: 10,
examples: [10, 100]
},
offset: {
type: 'integer',
minimum: 1,
default: 1,
examples: [1, 3]
},
sort_by: {
type: 'string',
examples: %w[asc desc],
default: ''
},
relationships: {
type: 'boolean',
examples: [false, true]
},
filter: {
type: 'string',
examples: ["name='Standard System Security Profile for Fedora'"]
}
},
required: %w[limit offset]
}.freeze

LINKS = {
type: 'object',
properties: {
first: {
type: 'string'
},
last: {
type: 'string'
},
previous: {
type: 'string'
},
next: {
type: 'string'
}
},
required: %w[first last]
}.freeze

TAGS = {
type: 'array',
items: {
type: 'string',
example: 'insights/environment=production'
}
}.freeze
end
end
end
end
40 changes: 40 additions & 0 deletions spec/api/v2/schemas/security_guides.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require './spec/api/v2/schemas/util'

module Api
module V2
module Schemas
module SecurityGuides
extend Api::V2::Schemas::Util

SECURITY_GUIDE = {
type: 'object',
required: %w[ref_id title version],
properties: {
ref_id: {
type: 'string',
examples: ['xccdf_org.ssgproject.content_benchmark_RHEL-7']
},
title: {
type: 'string',
examples: ['Guide to the Secure Configuration of Red Hat '\
'Enterprise Linux 7']
},
version: {
type: 'string',
examples: ['0.1.46']
},
description: {
type: 'string'
},
os_major_version: {
type: 'number',
examples: [7]
}
}
}.freeze
end
end
end
end
15 changes: 15 additions & 0 deletions spec/api/v2/schemas/types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require './spec/api/v1/schemas/util'

module Api
module V2
module Schemas
module Types
extend Util

UUID = { type: :string, format: :uuid }.freeze
end
end
end
end
13 changes: 13 additions & 0 deletions spec/api/v2/schemas/util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Api
module V2
module Schemas
module Util
def ref_schema(label)
{ '$ref' => "#/components/schemas/#{label}" }
end
end
end
end
end
77 changes: 77 additions & 0 deletions spec/integration/security_guides_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require 'swagger_helper'

describe 'Security Guides', swagger_doc: 'v2/openapi.json' do
before do
stub_rbac_permissions(Rbac::COMPLIANCE_ADMIN, Rbac::INVENTORY_VIEWER)
end

path '/security_guides' do
get 'List all Security Guides' do
before do
FactoryBot.create(
:v2_security_guide,
title: 'Guide to the Secure Configuration of Red Hat Enterprise '\
'Linux 6',
description: 'This guide presents a catalog of security-relevant '\
'configuration settings for Red Hat Enterprise Linux 6.',
os_major_version: 6,
version: '0.1.47'
)
FactoryBot.create(
:v2_security_guide,
title: 'Guide to the Secure Configuration of Red Hat Enterprise '\
'Linux 6',
description: 'This guide presents a catalog of security-relevant '\
'configuration settings for Red Hat Enterprise Linux 6.',
os_major_version: 6,
version: '0.1.53'
)
[7, 8, 9].each do |os_version|
FactoryBot.create(
:v2_security_guide,
title: 'Guide to the Secure Configuration of Red Hat Enterprise '\
"Linux #{os_version}",
description: 'This guide presents a catalog of security-relevant '\
"configuration settings for Red Hat Enterprise Linux #{os_version}.",
os_major_version: os_version
)
end
end

tags 'security_guide'
description 'Lists all Security guides requested'
operationId 'ListSecurityGuides'

auth_header
pagination_params
search_params
sort_params(Xccdf::Benchmark)
content_types

response '200', 'Lists all Security Guides requested' do
let(:'X-RH-IDENTITY') { encoded_header }
schema type: :object,
properties: {
meta: ref_schema('metadata'),
links: ref_schema('links'),
data: {
type: :array,
items: {
properties: {
type: { type: :string },
id: ref_schema('uuid'),
attributes: ref_schema('security_guides')
}
}
}
}

after { |e| autogenerate_examples(e, 'List of Security Guides') }

run_test!
end
end
end
end
8 changes: 6 additions & 2 deletions spec/swagger_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
require 'webmock/rspec'
require './spec/api/v1/openapi'
require './spec/api/v1/schemas/util'
require './spec/api/v2/openapi'

include Api::V1::Schemas::Util # rubocop:disable Style/MixinUsage
include Api::V2::Schemas::Util # rubocop:disable Style/MixinUsage

RSpec.configure do |config|
config.swagger_root = Rails.root.to_s + '/swagger'

# FIXME: https://github.com/rswag/rswag/issues/666
# config.swagger_strict_schema_validation = true
config.before(:each) do
stub_request(:get, /#{Settings.endpoints.rbac_url}/)
.to_return(status: 200,
Expand All @@ -18,7 +21,8 @@
end

config.swagger_docs = {
'v1/openapi.json' => Api::V1::Openapi.doc
'v1/openapi.json' => Api::V1::Openapi.doc,
'v2/openapi.json' => Api::V2::Openapi.doc
}
end

Expand Down
Loading

0 comments on commit fd57718

Please sign in to comment.