require 'spec_helper' require 'rack' require 'rack/test' describe AuthenticationIdentifierTransposer::RackMiddleware do include Rack::Test::Methods before :each do @test_app = lambda do |env| request = Rack::Request.new env session = request.session test_app_response_data = { 'message' => "tested", 'session_user' => request.session['user'], 'remote_user' => request.env['REMOTE_USER'] } [200, {"Content-Type" => "application/json"}, test_app_response_data ] end @iut = AuthenticationIdentifierTransposer::RackMiddleware.new(@test_app) end context "when initialized" do it 'remembers the app provided' do expect(@iut.instance_variable_get("@app")).to eq(@test_app) end end context "when called with a request environment" do context 'with X-GATEWAY-AUTHENTICATED-IDENTIFIER header' do it "pass requests to the application" do opts = { 'X-GATEWAY-AUTHENTICATED-IDENTIFIER' => 'test_uuid' } code, env, body = @iut.call Rack::MockRequest.env_for('http://service', opts) expect([code, env, body['message']]).to eq([200, {"Content-Type"=>"application/json"}, "tested"]) end it "set the user key in the request session" do opts = { 'X-GATEWAY-AUTHENTICATED-IDENTIFIER' => 'test_uuid' } code, env, body = @iut.call Rack::MockRequest.env_for('http://service', opts) expect(body['session_user']).to eq 'test_uuid' end it "set the remote user in the request environment" do opts = { 'X-GATEWAY-AUTHENTICATED-IDENTIFIER' => 'test_uuid' } code, env, body = @iut.call Rack::MockRequest.env_for('http://service', opts) expect(body['remote_user']).to eq 'test_uuid' end end context 'without X-GATEWAY-AUTHENTICATED-IDENTIFIER header' do it "pass requests to the application" do opts = { } code, env, body = @iut.call Rack::MockRequest.env_for('http://service', opts) expect([code, env, body['message'], body['session_user'], body['remote_user']]).to eq([200, {"Content-Type"=>"application/json"}, "tested", nil, nil]) end it "does not modify the user key in the request session" do opts = { } code, env, body = @iut.call Rack::MockRequest.env_for('http://service', opts) expect(body['session_user']).to eq nil end it "does not modify the remote user in the request environment" do opts = { } code, env, body = @iut.call Rack::MockRequest.env_for('http://service', opts) expect(body['remote_user']).to eq nil end end end end