Skip to content

Commit

Permalink
Add rswag and spec for sessions controller
Browse files Browse the repository at this point in the history
  • Loading branch information
leosoto committed Jan 11, 2020
1 parent 49c19b3 commit 0857681
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ gem 'puma', '~> 3.12'

gem 'bootsnap', '>= 1.4.2', require: false
gem 'pg'
gem 'rswag-api'
gem 'rswag-ui'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails', '~> 3.8'
gem 'rswag-specs'
end

group :development do
Expand Down
17 changes: 17 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.1, >= 2.1.8)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
bootsnap (1.4.5)
msgpack (~> 1.0)
builder (3.2.3)
Expand All @@ -77,6 +79,8 @@ GEM
multi_xml (>= 0.5.2)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
json-schema (2.8.1)
addressable (>= 2.4)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -105,6 +109,7 @@ GEM
pg (1.1.4)
puma (3.12.2)
rack (2.0.8)
public_suffix (4.0.1)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.0.0)
Expand Down Expand Up @@ -159,6 +164,15 @@ GEM
rspec-mocks (~> 3.9.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.0)
rswag-api (2.2.0)
railties (>= 3.1, < 6.1)
rswag-specs (2.2.0)
activesupport (>= 3.1, < 6.1)
json-schema (~> 2.2)
railties (>= 3.1, < 6.1)
rswag-ui (2.2.0)
actionpack (>= 3.1, < 6.1)
railties (>= 3.1, < 6.1)
ruby_dep (1.5.0)
spring (2.1.0)
spring-watcher-listen (2.0.1)
Expand Down Expand Up @@ -198,6 +212,9 @@ DEPENDENCIES
rails (~> 6.0.0)
rest-client
rspec-rails (~> 3.8)
rswag-api
rswag-specs
rswag-ui
spring
spring-watcher-listen (~> 2.0.0)
sqlite3 (~> 1.4)
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ See it live:

## Development

It's a simple Rails 6 API codebase. No tests so far :(
It's a simple Rails 6 API codebase. Contributions are very welcomed :)

Contributions are very welcomed :)
### Tests

For integration tests you need a valid Get on Board username and password. Set
them as environment variables `GOB_USERNAME` and `GOB_PASSWORD`, for example
via:

```bash
$ export GOB_USERNAME='[email protected]'
$ export GOB_PASSWORD='yoursupersecretpassword'
```

Then you can run:

```bash
$ rake spec
```

And tests should pass
14 changes: 14 additions & 0 deletions config/initializers/rswag-api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Rswag::Api.configure do |c|

# Specify a root folder where Swagger JSON files are located
# This is used by the Swagger middleware to serve requests for API descriptions
# NOTE: If you're using rswag-specs to generate Swagger, you'll need to ensure
# that it's configured to generate files in the same folder
c.swagger_root = Rails.root.to_s + '/swagger'

# Inject a lamda function to alter the returned Swagger prior to serialization
# The function will have access to the rack env for the current request
# For example, you could leverage this to dynamically assign the "host" property
#
#c.swagger_filter = lambda { |swagger, env| swagger['host'] = env['HTTP_HOST'] }
end
10 changes: 10 additions & 0 deletions config/initializers/rswag-ui.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Rswag::Ui.configure do |c|

# List the Swagger endpoints that you want to be documented through the swagger-ui
# The first parameter is the path (absolute or relative to the UI host) to the corresponding
# endpoint and the second is a title that will be displayed in the document selector
# NOTE: If you're using rspec-api to expose Swagger files (under swagger_root) as JSON or YAML endpoints,
# then the list below should correspond to the relative paths for those endpoints

c.swagger_endpoint '/api-docs/v1/swagger.yaml', 'API V1 Docs'
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
resources :sessions, only: [:create]
resources :jobs, only: [] do
collection do
Expand Down
46 changes: 46 additions & 0 deletions spec/requests/sessions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'swagger_helper'

RSpec.describe 'sessions', type: :request do

path '/sessions' do

post('Authenticate with email and password') do
consumes 'application/json'
produces 'application/json'

parameter name: :credentials, in: :body, schema: {
type: :object,
properties: {
email: { type: :string, format: :email },
password: { type: :string },
},
required: [:email, :password]
}

response(200, 'authentication successful') do
schema type: :object, properties: {
token: { type: :string, description: "Token to use for Autentication"},
required: [:token]
}
let(:credentials) do
{ email: ENV['GOB_USERNAME'], password: ENV['GOB_PASSWORD'] }
end
run_test!
end

response(422, 'authentication failed') do
schema type: :object, properties: {
errors: { type: :object, properties: {
email: { type: :array, items: { type: :string } },
password: { type: :array, items: { type: :string } }
}},
required: [:token]
}
let(:credentials) do
{ email: '[email protected]', password: 'wrong'}
end
run_test!
end
end
end
end
31 changes: 31 additions & 0 deletions spec/swagger_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails_helper'

RSpec.configure do |config|
# Specify a root folder where Swagger JSON files are generated
# NOTE: If you're using the rswag-api to serve API descriptions, you'll need
# to ensure that it's configured to serve Swagger from the same folder
config.swagger_root = Rails.root.join('swagger').to_s

# Define one or more Swagger documents and provide global metadata for each one
# When you run the 'rswag:specs:swaggerize' rake task, the complete Swagger will
# be generated at the provided relative path under swagger_root
# By default, the operations defined in spec files are added to the first
# document below. You can override this behavior by adding a swagger_doc tag to the
# the root example_group in your specs, e.g. describe '...', swagger_doc: 'v2/swagger.json'
config.swagger_docs = {
'v1/swagger.yaml' => {
swagger: '2.0',
info: {
title: 'GoB Company API',
version: 'v1'
},
paths: {}
}
}

# Specify the format of the output Swagger file when running 'rswag:specs:swaggerize'.
# The swagger_docs configuration option has the filename including format in
# the key, this may want to be changed to avoid putting yaml in json files.
# Defaults to json. Accepts ':json' and ':yaml'.
config.swagger_format = :yaml
end
56 changes: 56 additions & 0 deletions swagger/v1/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
swagger: '2.0'
info:
title: GoB Company API
version: v1
paths:
"/sessions":
post:
summary: Authenticate with email and password
consumes:
- application/json
produces:
- application/json
parameters:
- name: credentials
in: body
schema:
type: object
properties:
email:
type: string
format: email
password:
type: string
required:
- email
- password
responses:
'200':
description: authentication successful
schema:
type: object
properties:
token:
type: string
description: Token to use for Autentication
required:
- token
'422':
description: authentication failed
schema:
type: object
properties:
errors:
type: object
properties:
email:
type: array
items:
type: string
password:
type: array
items:
type: string
required:
- token

0 comments on commit 0857681

Please sign in to comment.