|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +class SampleAuth < Grape::Middleware::Base |
| 6 | + module AuthMethods |
| 7 | + attr_accessor :access_token |
| 8 | + |
| 9 | + def protected_endpoint=(protected) |
| 10 | + @protected_endpoint = protected |
| 11 | + end |
| 12 | + |
| 13 | + def protected_endpoint? |
| 14 | + @protected_endpoint || false |
| 15 | + end |
| 16 | + |
| 17 | + def resource_owner |
| 18 | + @resource_owner = true if access_token == '12345' |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + def context |
| 23 | + env['api.endpoint'] |
| 24 | + end |
| 25 | + |
| 26 | + def before |
| 27 | + context.extend(SampleAuth::AuthMethods) |
| 28 | + context.protected_endpoint = context.options[:route_options][:auth].present? |
| 29 | + |
| 30 | + return unless context.protected_endpoint? |
| 31 | + |
| 32 | + scopes = context.options[:route_options][:auth][:scopes] |
| 33 | + authorize!(*scopes) unless scopes.include? false |
| 34 | + context.access_token = env['HTTP_AUTHORIZATION'] |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +module Extension |
| 39 | + def sample_auth(*scopes) |
| 40 | + description = route_setting(:description) || route_setting(:description, {}) |
| 41 | + description[:auth] = { scopes: scopes } |
| 42 | + end |
| 43 | + |
| 44 | + Grape::API.extend self |
| 45 | +end |
| 46 | + |
| 47 | +describe 'a guarded api endpoint' do |
| 48 | + before :all do |
| 49 | + class GuardedMountedApi < Grape::API |
| 50 | + resource_owner_valid = proc { |token_owner = nil| token_owner.nil? } |
| 51 | + |
| 52 | + desc 'Show endpoint if authenticated' |
| 53 | + route_setting :swagger, hidden: resource_owner_valid |
| 54 | + get '/auth' do |
| 55 | + { foo: 'bar' } |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + class GuardedApi < Grape::API |
| 60 | + mount GuardedMountedApi |
| 61 | + add_swagger_documentation openapi_version: '3.0', |
| 62 | + endpoint_auth_wrapper: SampleAuth, |
| 63 | + swagger_endpoint_guard: 'sample_auth false', |
| 64 | + token_owner: 'resource_owner' |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def app |
| 69 | + GuardedApi |
| 70 | + end |
| 71 | + |
| 72 | + context 'when a correct token is passed with the request' do |
| 73 | + subject do |
| 74 | + get '/swagger_doc.json', {}, 'HTTP_AUTHORIZATION' => '12345' |
| 75 | + JSON.parse(last_response.body) |
| 76 | + end |
| 77 | + |
| 78 | + it 'retrieves swagger-documentation for the endpoint' do |
| 79 | + expect(subject).to eq( |
| 80 | + 'info' => { 'title' => 'API title', 'version' => '0.0.1' }, |
| 81 | + 'openapi' => '3.0.0', |
| 82 | + 'servers' => [{ 'url' => 'http://example.org' }], |
| 83 | + 'tags' => [{ 'name' => 'auth', 'description' => 'Operations about auths' }], |
| 84 | + 'paths' => { |
| 85 | + '/auth' => { |
| 86 | + 'get' => { |
| 87 | + 'description' => 'Show endpoint if authenticated', |
| 88 | + 'operationId' => 'getAuth', |
| 89 | + 'responses' => { |
| 90 | + '200' => { |
| 91 | + 'content' => { 'application/json' => {} }, |
| 92 | + 'description' => 'Show endpoint if authenticated' |
| 93 | + } |
| 94 | + }, |
| 95 | + 'tags' => ['auth'] |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + ) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + context 'when a bad token is passed with the request' do |
| 104 | + subject do |
| 105 | + get '/swagger_doc.json', {}, 'HTTP_AUTHORIZATION' => '123456' |
| 106 | + JSON.parse(last_response.body) |
| 107 | + end |
| 108 | + |
| 109 | + it 'does not retrieve swagger-documentation for the endpoint - only the info_object' do |
| 110 | + expect(subject).to eq( |
| 111 | + 'info' => { 'title' => 'API title', 'version' => '0.0.1' }, |
| 112 | + 'openapi' => '3.0.0', |
| 113 | + 'servers' => [{ 'url' => 'http://example.org' }] |
| 114 | + ) |
| 115 | + end |
| 116 | + end |
| 117 | +end |
0 commit comments