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 Sep 14, 2023
1 parent fd9dea7 commit 293c10b
Show file tree
Hide file tree
Showing 9 changed files with 906 additions and 3 deletions.
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
19 changes: 19 additions & 0 deletions spec/api/v2/schemas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

Dir['./spec/api/v2/schemas/*.rb'].sort.each { |file| require file }

module Api
module V2
module Schemas
include Metadata
include SecurityGuides

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

module Api
module V2
module Schemas
module Metadata
UUID = { type: :string, format: :uuid }.freeze

METADATA = {
type: :object,
properties: {
total: {
type: :number,
examples: [1, 42, 770],
description: 'Total number of items'

},
limit: {
type: :number,
maximum: 100,
minimum: 1,
default: 10,
examples: [10, 100],
description: 'Maximum number of items to return'
},
offset: {
type: :number,
minimum: 0,
default: 0,
examples: [15, 90],
description: 'Offset of the first item returned'
},
sort_by: {
type: :string,
examples: %w[version:asc],
description: 'Attribute and direction the returned items are sorted by'
},
filter: {
type: :string,
default: '',
examples: ["title='Standard System Security Profile for Fedora'"],
description: 'scoped_search query language string used to filter items by their attributes'
}
}
}.freeze

LINKS = {
type: :object,
properties: {
first: {
type: :string,
format: :uri,
description: 'Link to first page'
},
last: {
type: :string,
format: :uri,
description: 'Link to last page'
},
previous: {
type: :string,
format: :uri,
description: 'Link to previous page'
},
next: {
type: :string,
format: :uri,
description: 'Link to next page'
}
}
}.freeze

TAGS = {
type: :array,
items: {
type: :string,
examples: ['insights/environment=production']
}
}.freeze
end
end
end
end
48 changes: 48 additions & 0 deletions spec/api/v2/schemas/security_guides.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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 os_major_version],
properties: {
ref_id: {
type: :string,
examples: ['xccdf_org.ssgproject.content_benchmark_RHEL-7'],
description: 'Identificator for Security Guide'
},
title: {
type: :string,
examples: ['Guide to the Secure Configuration of Red Hat ' \
'Enterprise Linux 7'],
description: 'Brief description of the Security Guide content'
},
version: {
type: :string,
examples: ['0.1.46'],
description: 'Version of Security Guide'
},
description: {
type: :string,
examples: ['This guide presents a catalog of security-relevant ' \
'configuration settings for Red Hat Enterprise Linux 7.'],
description: 'Longer description of the Security Guide content'
},
os_major_version: {
type: :number,
minimum: SupportedSsg.by_os_major.map(&:first).min.to_i,
examples: [7],
description: 'Major version of OS that the Security Guide covers'
}
}
}.freeze
end
end
end
end
13 changes: 13 additions & 0 deletions spec/api/v2/schemas/types.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 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
159 changes: 159 additions & 0 deletions spec/integration/security_guides_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# frozen_string_literal: true

require 'swagger_helper'

describe 'Security Guides', swagger_doc: 'v2/openapi.json' do
let(:identity_header) do
OpenStruct.new(
cert_based?: false,
valid?: true,
org_id: '1234',
identity: { org_id: '1234' },
raw: nil
)
end

before do
allow(Insights::Api::Common::IdentityHeader).to receive(:new).and_return(identity_header)
stub_rbac_permissions(Rbac::COMPLIANCE_ADMIN, Rbac::INVENTORY_HOSTS_READ)
end

path '/security_guides' do
get 'List all Security Guides' do
before do
SupportedSsg.all.map(&:os_major_version).uniq.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'
operationId 'ListSecurityGuides'
content_types
pagination_params_v2
sort_params_v2(V2::SecurityGuide)
search_params_v2(V2::SecurityGuide)

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

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

run_test!
end

response '200', 'Lists all Security Guides requested' do
let(:sort_by) { ['os_major_version'] }
schema type: :object,
properties: {
meta: ref_schema('metadata'),
links: ref_schema('links'),
data: {
type: :array,
items: {
properties: {
type: { type: :string },
id: ref_schema('id'),
attributes: ref_schema('security_guides')
}
}
}
}

after { |e| autogenerate_examples(e, 'List of Security Guides sorted by "os_major_verision:asc"') }

run_test!
end

response '200', 'Lists all Security Guides requested' do
let(:filter) { '(os_major_version=8)' }
schema type: :object,
properties: {
meta: ref_schema('metadata'),
links: ref_schema('links'),
data: {
type: :array,
items: {
properties: {
type: { type: :string },
id: ref_schema('id'),
attributes: ref_schema('security_guides')
}
}
}
}

after { |e| autogenerate_examples(e, 'List of Security Guides filtered by "(os_major_version=8)"') }

run_test!
end

response '422', 'Returns error if wrong parameters are used' do
let(:sort_by) { ['description'] }
schema type: :object,
properties: {
meta: ref_schema('metadata'),
links: ref_schema('links'),
data: {
type: :array,
items: {
properties: {
type: { type: :string },
id: ref_schema('id'),
attributes: ref_schema('security_guides')
}
}
}
}

after { |e| autogenerate_examples(e, 'Description of an error when sorting by incorrect parameter') }

run_test!
end

response '422', 'Returns error if wrong parameters are used' do
let(:limit) { 103 }
schema type: :object,
properties: {
meta: ref_schema('metadata'),
links: ref_schema('links'),
data: {
type: :array,
items: {
properties: {
type: { type: :string },
id: ref_schema('id'),
attributes: ref_schema('security_guides')
}
}
}
}

after { |e| autogenerate_examples(e, 'Description of an error when requesting higher limit than supported') }

run_test!
end
end
end
end
Loading

0 comments on commit 293c10b

Please sign in to comment.