Sha256: 5d479d0665aa7ce2c487afc61f6a211f65727e5c5c77af0d9ad1ab20306a7662

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'spec_helper'
require 'hashie'

describe TentD::API::Authorizable do
  class TestMiddleware2
    include TentD::API::Authorizable

    def initialize(app)
      @app = app
    end

    def call(env)
      authorize_env!(env, :read_posts)
      @app.call(env)
    end
  end

  class OtherTestMiddleware < TentD::API::Middleware
    def action(env)
      authorize_env!(env, :read_posts)
      env
    end
  end

  def app
    TentD::API.new
  end

  let(:env) { Hashie::Mash.new }
  let(:middleware) { TestMiddleware2.new(app) }

  describe '#authorize_env!(env, scope)' do
    it 'should raise Unauthorized unless env.authorized_scopes includes scope' do
      expect( lambda { middleware.call(env) } ).to raise_error(described_class::Unauthorized)
    end

    it 'should do nothing if env.authorized_scopes includes scope' do
      env.authorized_scopes = [:read_posts]
      expect( lambda { middleware.call(env) } ).to_not raise_error
    end

    context 'when TentD::API::Middleware' do
      it 'should respond 403 unless env.authorized_scopes includes scope' do
        response = OtherTestMiddleware.new(app).call(env)
        expect(response).to be_an(Array)
        expect(response.first).to be(403)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tentd-0.0.1 spec/unit/api/authorizable_spec.rb