Skip to content

Commit

Permalink
adds specs for rake tasks (ruby-grape#502)
Browse files Browse the repository at this point in the history
- make it <0.16.0 compatible
- adds changelog entry
- [scouting] removes non existend examples contexts
  • Loading branch information
LeFnord authored Sep 10, 2016
1 parent b4a6f6c commit 81877a6
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#502](https://github.com/ruby-grape/grape-swagger/pull/502): Adds specs for rake tasks - [@LeFnord](https://github.com/LeFnord).
* [#501](https://github.com/ruby-grape/grape-swagger/pull/501): Adds getting of a specified resource for Rake Tasks - [@LeFnord](https://github.com/LeFnord).
* [#500](https://github.com/ruby-grape/grape-swagger/pull/500): Adds Rake tasks to get and validate OAPI/Swagger documentation - [@LeFnord](https://github.com/LeFnord).
* [#493](https://github.com/ruby-grape/grape-swagger/pull/493): Swagger UI endpoint authorization. - [@texpert](https://github.com/texpert).
Expand Down
5 changes: 3 additions & 2 deletions lib/grape-swagger/rake/oapi_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class OapiTasks < ::Rake::TaskLib

def initialize(api_class)
super()

@api_class = api_class
define_tasks
end
Expand Down Expand Up @@ -71,14 +72,14 @@ def make_request

def url_for
oapi_route = api_class.routes[-2]
path = oapi_route.pattern.origin
path = oapi_route.path.sub(/\(\.\w+\)$/, '').sub(/\(\.:\w+\)$/, '')
path.sub!(':version', oapi_route.version.to_s)

[path, ENV['resource']].join('/').chomp('/')
end

def save_to_file?
ENV['store'] && !error?
ENV['store'].present? && !error?
end

def error?
Expand Down
133 changes: 133 additions & 0 deletions spec/lib/oapi_tasks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
require 'spec_helper'

RSpec.describe GrapeSwagger::Rake::OapiTasks do
let(:api) do
item = Class.new(Grape::API) do
version 'v1', using: :path

resource :item do
get '/'
end

resource :otherItem do
get '/'
end
end

Class.new(Grape::API) do
prefix :api
mount item
add_swagger_documentation add_version: true
end
end

subject { described_class.new(api) }

describe '#make_request' do
describe 'complete documentation' do
before do
subject.send(:make_request)
end

describe 'not storing' do
it 'has no error' do
expect(subject.send(:error?)).to be false
end

it 'does not allow to save' do
expect(subject.send(:save_to_file?)).to be false
end

it 'requests doc url' do
expect(subject.send(:url_for)).to eql '/api/swagger_doc'
end
end

describe 'store it' do
before { ENV['store'] = 'true' }
after { ENV.delete('store') }

it 'allows to save' do
expect(subject.send(:save_to_file?)).to be true
end
end
end

describe 'documentation for resource' do
before do
ENV['resource'] = resource
subject.send(:make_request)
end

let(:response) { JSON.parse(subject.send(:make_request)) }

after { ENV.delete('resource') }

describe 'valid name' do
let(:resource) { 'otherItem' }

it 'has no error' do
expect(subject.send(:error?)).to be false
end

it 'requests doc url' do
expect(subject.send(:url_for)).to eql "/api/swagger_doc/#{resource}"
end

it 'has only one resource path' do
expect(response['paths'].length).to eql 1
expect(response['paths'].keys.first).to end_with resource
end
end

describe 'wrong name' do
let(:resource) { 'foo' }

it 'has error' do
expect(subject.send(:error?)).to be true
end
end

describe 'empty name' do
let(:resource) { nil }

it 'has no error' do
expect(subject.send(:error?)).to be false
end

it 'returns complete doc' do
expect(response['paths'].length).to eql 2
end
end
end
end

describe '#file' do
describe 'no store given' do
it 'returns swagger_doc.json' do
expect(subject.send(:file)).to end_with 'swagger_doc.json'
end
end

describe 'store given' do
after { ENV.delete('store') }

describe 'boolean true' do
before { ENV['store'] = 'true' }

it 'returns swagger_doc.json' do
expect(subject.send(:file)).to end_with 'swagger_doc.json'
end
end

describe 'name given' do
let(:name) { 'oapi_doc.json' }
before { ENV['store'] = name }

it 'returns swagger_doc.json' do
expect(subject.send(:file)).to end_with name
end
end
end
end
end
2 changes: 0 additions & 2 deletions spec/swagger_v2/description_not_initialized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

describe 'details' do
describe 'details, pass markdown with redcarpet even with nil description and detail', unless: RUBY_PLATFORM.eql?('java') do
include_context 'the api entities'

before :all do
module TheApi
class GfmRcDetailApi < Grape::API
Expand Down
2 changes: 0 additions & 2 deletions spec/swagger_v2/host.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'spec_helper'

describe 'host in the swagger_doc' do
include_context 'the api entities'

before :all do
module TheApi
class EmptyApi < Grape::API
Expand Down

0 comments on commit 81877a6

Please sign in to comment.