require 'spec_helper' require 'webmock/rspec' describe BasicAuthSession do describe '#initialize' do it 'sets the username instance variable' do session = Roqua::CoreApi.basic_auth_session username: 'some_username' expect(session.username).to eq('some_username') end it 'defaults the username to the CORE_BASICAUTH_ID env variable' do original_env_core_key = ENV.fetch('CORE_BASICAUTH_ID') ENV['CORE_BASICAUTH_ID'] = 'some_env_core_key' session = Roqua::CoreApi.basic_auth_session ENV['CORE_BASICAUTH_ID'] = original_env_core_key expect(session.username).to eq('some_env_core_key') end it 'sets the password instance variable' do session = Roqua::CoreApi.basic_auth_session password: 'some_password' expect(session.password).to eq('some_password') end it 'defaults the password to the CORE_BASICAUTH_SECRET env variable' do original_env_core_secret = ENV.fetch('CORE_BASICAUTH_SECRET') ENV['CORE_BASICAUTH_SECRET'] = 'some_env_core_secret' session = Roqua::CoreApi.basic_auth_session ENV['CORE_BASICAUTH_SECRET'] = original_env_core_secret expect(session.password).to eq('some_env_core_secret') end it 'allows to override the core_site variable' do session = Roqua::CoreApi.basic_auth_session core_site: 'some_core_site' expect(session.core_site).to eq('some_core_site') end it 'defaults the AuthSession core_site CORE_SITE env variable default value' do orginal_env_core_site = ENV.fetch('CORE_SITE') ENV['CORE_SITE'] = 'some_env_core_site' session = Roqua::CoreApi.basic_auth_session ENV['CORE_SITE'] = orginal_env_core_site expect(session.core_site).to eq('some_env_core_site') end end describe '#basic_auth' do it 'returns the username and password variables' do session = Roqua::CoreApi.basic_auth_session username: 'some_username', password: 'some_password' expect(session.send :basic_auth).to eq(username: 'some_username', password: 'some_password') end end describe '#access_denied' do let(:session) { Roqua::CoreApi.basic_auth_session username: 'some_username' } let(:response) { double('response', code: 401, parsed_response: 'some_response') } # No NoSession error in this case, since it's not something the user can fix. # Plus client_portal redirects to oauth login if they get NoSession. it 'throws a StandardError if the basic auth is incorrect' do allow(HTTParty).to receive(:get).and_return(response) allow(response).to receive(:headers).and_return('WWW-Authenticate' => 'Basic realm="Application"') expect { session.get '/some_path' }.to raise_error(StandardError) end it 'throws a Unauthorized error on 401 without www-authenticate header' do allow(HTTParty).to receive(:get).and_return(response) allow(response).to receive(:headers).and_return('foo' => 'bar') expect { session.get '/some_path' }.to raise_error(Roqua::CoreApi::Unauthorized) end end end