-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rswag and spec for sessions controller
- Loading branch information
Showing
9 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
- 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 |