| 
									
										
										
										
											2017-03-08 16:32:31 +08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2017-03-28 17:15:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 20:35:01 +08:00
										 |  |  | require 'spec_helper' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RSpec.describe GrapeSwagger::Rake::OapiTasks do | 
					
						
							|  |  |  |   let(:api) do | 
					
						
							|  |  |  |     item = Class.new(Grape::API) do | 
					
						
							|  |  |  |       version 'v1', using: :path | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-16 06:46:22 +08:00
										 |  |  |       namespace :item do | 
					
						
							| 
									
										
										
										
											2016-09-10 20:35:01 +08:00
										 |  |  |         get '/' | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-16 06:46:22 +08:00
										 |  |  |       namespace :otherItem do | 
					
						
							| 
									
										
										
										
											2016-09-10 20:35:01 +08:00
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2017-01-16 06:46:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe 'call it' do | 
					
						
							|  |  |  |       before do | 
					
						
							|  |  |  |         subject.send(:make_request) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       specify do | 
					
						
							|  |  |  |         expect(subject).to respond_to :oapi | 
					
						
							|  |  |  |         expect(subject.oapi).to be_a String | 
					
						
							|  |  |  |         expect(subject.oapi).not_to be_empty | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-09-10 20:35:01 +08:00
										 |  |  |   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 |