Sha256: 3f28051c00890bcf670c63cd3f4015545baeaed256881430f12f2c1da3008f7e

Contents?: true

Size: 1.72 KB

Versions: 9

Compression:

Stored size: 1.72 KB

Contents

RSpec.describe Rails::Auth::ErrorPage::Middleware do
  let(:request)    { Rack::MockRequest.env_for("https://www.example.com") }
  let(:error_page) { "<h1> Unauthorized!!! </h1>" }

  subject(:middleware) { described_class.new(app, page_body: error_page) }

  context "unspecified content type" do
    describe "access granted" do
      let(:code) { 200 }
      let(:app)  { ->(env) { [code, env, "Hello, world!"] } }

      it "renders the expected response" do
        response = middleware.call(request)
        expect(response.first).to eq code
      end
    end

    describe "access denied" do
      let(:app) { ->(_env) { raise(Rails::Auth::NotAuthorizedError, "not authorized!") } }

      it "renders the error page" do
        code, _env, body = middleware.call(request)
        expect(code).to eq 403
        expect(body).to eq [error_page]
      end
    end
  end

  context "JSON content type" do
    let(:app)     { ->(_env) { raise(Rails::Auth::NotAuthorizedError, "not authorized!") } }
    let(:message) { { message: "Access denied" }.to_json }

    context "via request path" do
      let(:request) { Rack::MockRequest.env_for("https://www.example.com/foobar.json?x=1&y=2") }

      it "renders a JSON response" do
        code, env, body = middleware.call(request)
        expect(code).to eq 403
        expect(env["Content-Type"]).to eq "application/json"
        expect(body).to eq [message]
      end
    end

    context "via Accept header" do
      it "renders a JSON response" do
        request["HTTP_ACCEPT"] = "application/json"

        code, env, body = middleware.call(request)
        expect(code).to eq 403
        expect(env["Content-Type"]).to eq "application/json"
        expect(body).to eq [message]
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rails-auth-2.1.4 spec/rails/auth/error_page/middleware_spec.rb
rails-auth-2.1.3 spec/rails/auth/error_page/middleware_spec.rb
rails-auth-2.1.2 spec/rails/auth/error_page/middleware_spec.rb
rails-auth-2.1.1 spec/rails/auth/error_page/middleware_spec.rb
rails-auth-2.1.0 spec/rails/auth/error_page/middleware_spec.rb
rails-auth-2.0.3 spec/rails/auth/error_page/middleware_spec.rb
rails-auth-2.0.2 spec/rails/auth/error_page/middleware_spec.rb
rails-auth-2.0.1 spec/rails/auth/error_page/middleware_spec.rb
rails-auth-1.3.0 spec/rails/auth/error_page/middleware_spec.rb