grape-swagger/spec/swagger_v2/hide_api_spec.rb

129 lines
3.8 KiB
Ruby
Raw Normal View History

2015-10-05 02:05:22 +08:00
require 'spec_helper'
describe 'a hide mounted api' do
before :all do
class HideMountedApi < Grape::API
desc 'Show this endpoint'
get '/simple' do
{ foo: 'bar' }
end
desc 'Hide this endpoint', hidden: true
get '/hide' do
{ foo: 'bar' }
end
desc 'Lazily show endpoint', hidden: -> { false }
get '/lazy' do
{ foo: 'bar' }
end
end
class HideApi < Grape::API
mount HideMountedApi
add_swagger_documentation
end
end
def app
HideApi
end
subject do
get '/swagger_doc.json'
JSON.parse(last_response.body)
end
it "retrieves swagger-documentation that doesn't include hidden endpoints" do
expect(subject).to eq({
"info"=>{"title"=>"API title", "version"=>"0.0.1"},
2015-10-05 02:05:22 +08:00
"swagger"=>"2.0",
"produces"=>["application/xml", "application/json", "application/octet-stream", "text/plain"],
"host"=>"example.org",
2016-03-09 23:37:03 +08:00
"tags" => [{"name"=>"simple", "description"=>"Operations about simples"}, {"name"=>"lazy", "description"=>"Operations about lazies"}],
2015-10-05 02:05:22 +08:00
"paths"=>{
2016-03-16 00:53:03 +08:00
"/simple"=>{
"get"=>{
2016-04-08 09:05:35 +08:00
"description"=>"Show this endpoint",
2016-03-16 00:53:03 +08:00
"produces"=>["application/json"],
"tags"=>["simple"],
"operationId"=>"getSimple",
"responses"=>{"200"=>{"description"=>"Show this endpoint"}}}},
"/lazy"=>{
"get"=>{
2016-04-08 09:05:35 +08:00
"description"=>"Lazily show endpoint",
2016-03-16 00:53:03 +08:00
"produces"=>["application/json"],
"tags"=>["lazy"],
"operationId"=>"getLazy",
"responses"=>{"200"=>{"description"=>"Lazily show endpoint"}}}}}
2015-10-05 02:05:22 +08:00
})
end
end
describe 'a hide mounted api with same namespace' do
before :all do
class HideNamespaceMountedApi < Grape::API
desc 'Show this endpoint'
get '/simple/show' do
{ foo: 'bar' }
end
desc 'Hide this endpoint', hidden: true
get '/simple/hide' do
{ foo: 'bar' }
end
desc 'Lazily hide endpoint', hidden: -> { true }
get '/simple/lazy' do
{ foo: 'bar' }
end
end
class HideNamespaceApi < Grape::API
mount HideNamespaceMountedApi
add_swagger_documentation
end
end
def app
HideNamespaceApi
end
it 'retrieves swagger-documentation on /swagger_doc' do
get '/swagger_doc.json'
expect(JSON.parse(last_response.body)).to eq({
"info"=>{"title"=>"API title", "version"=>"0.0.1"},
2015-10-05 02:05:22 +08:00
"swagger"=>"2.0",
"produces"=>["application/xml", "application/json", "application/octet-stream", "text/plain"],
"host"=>"example.org",
2016-03-09 23:37:03 +08:00
"tags" => [{"name"=>"simple", "description"=>"Operations about simples"}],
2015-10-05 02:05:22 +08:00
"paths"=>{
2016-03-16 00:53:03 +08:00
"/simple/show"=>{
"get"=>{
2016-04-08 09:05:35 +08:00
"description"=>"Show this endpoint",
2016-03-16 00:53:03 +08:00
"produces"=>["application/json"],
"operationId"=>"getSimpleShow",
"tags"=>["simple"], "responses"=>{"200"=>{"description"=>"Show this endpoint"}}}}}
2015-10-05 02:05:22 +08:00
})
end
it "retrieves the documentation for mounted-api that doesn't include hidden endpoints" do
get '/swagger_doc/simple.json'
expect(JSON.parse(last_response.body)).to eq({
"info"=>{"title"=>"API title", "version"=>"0.0.1"},
2015-10-05 02:05:22 +08:00
"swagger"=>"2.0",
"produces"=>["application/xml", "application/json", "application/octet-stream", "text/plain"],
"host"=>"example.org",
2016-03-09 23:37:03 +08:00
"tags" => [{"name"=>"simple", "description"=>"Operations about simples"}],
2015-10-05 02:05:22 +08:00
"paths"=>{
2016-03-16 00:53:03 +08:00
"/simple/show"=>{
"get"=>{
2016-04-08 09:05:35 +08:00
"description"=>"Show this endpoint",
2016-03-16 00:53:03 +08:00
"produces"=>["application/json"],
"tags"=>["simple"],
"operationId"=>"getSimpleShow",
"responses"=>{"200"=>{"description"=>"Show this endpoint"}}}}}
2015-10-05 02:05:22 +08:00
})
end
end