# frozen_string_literal: true require 'spec_helper' describe BasicAuthSession do let(:session) { FactoryBot.build :basic_auth_session } let(:response) { double('response', code: 201, parsed_response: 'some_response') } describe '#initialize' do it 'sets the fizzy_url instance attribute' do session = Fizzy::Api.basic_auth_session fizzy_url: 'some_fizzy_url' expect(session.fizzy_url).to eq('some_fizzy_url') end it 'defaults the fizzy_url to the FIZZY_URL env variable' do orginal_env_fizzy_url = ENV['FIZZY_URL'] ENV['FIZZY_URL'] = 'some_env_fizzy_url' session = Fizzy::Api.basic_auth_session ENV['FIZZY_URL'] = orginal_env_fizzy_url expect(session.fizzy_url).to eq('some_env_fizzy_url') end it 'sets the username instance variable' do session = Fizzy::Api.basic_auth_session username: 'some_username' expect(session.username).to eq('some_username') end it 'defaults the username to the FIZZY_BASICAUTH_ID env variable' do orginal_env_fizzy_key = ENV['FIZZY_BASICAUTH_ID'] ENV['FIZZY_BASICAUTH_ID'] = 'some_env_fizzy_key' session = Fizzy::Api.basic_auth_session ENV['FIZZY_BASICAUTH_ID'] = orginal_env_fizzy_key expect(session.username).to eq('some_env_fizzy_key') end it 'sets the password instance variable' do session = Fizzy::Api.basic_auth_session password: 'some_password' expect(session.password).to eq('some_password') end it 'defaults the password to the FIZZY_BASICAUTH_SECRET env variable' do orginal_env_fizzy_secret = ENV['FIZZY_BASICAUTH_SECRET'] ENV['FIZZY_BASICAUTH_SECRET'] = 'some_env_fizzy_secret' session = Fizzy::Api.basic_auth_session ENV['FIZZY_BASICAUTH_SECRET'] = orginal_env_fizzy_secret expect(session.password).to eq('some_env_fizzy_secret') end end describe '#get' do it 'performs a get request' do expect(HTTParty).to receive(:get).with('http://fizzy.dev/api/v1/some_path', query: { some: 'param' }, basic_auth: { username: 'some_username', password: 'some_password' }) .and_return(response) session.get('/some_path', some: 'param') end it 'throws an error if the reponse is not within the 200 range' do allow(response).to receive(:code).and_return(500) allow(HTTParty).to receive(:get).and_return(response) expect { session.get('/some_path') }.to raise_error(RuntimeError, 'some_response') end it 'throws a NoSession error if the basic auth is incorrect' do allow(response).to receive(:code).and_return(401) 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(Fizzy::Api::NoSession) end it 'throws a Unauthorized error on 401 without www-authenticate header' do allow(response).to receive(:code).and_return(401) 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(Fizzy::Api::Unauthorized) end it 'returns the response' do allow(HTTParty).to receive(:get).and_return(response) expect(session.get('/some_path')).to eq(response) end end end