2019-11-13 19:06:24 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-09 11:38:58 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::ExternalAuthorization::Response do
|
2020-07-27 17:09:16 -04:00
|
|
|
let(:http_response) { double }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2020-07-27 17:09:16 -04:00
|
|
|
subject(:response) { described_class.new(http_response) }
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
describe '#valid?' do
|
|
|
|
it 'is valid for 200, 401, and 403 responses' do
|
2020-07-27 17:09:16 -04:00
|
|
|
[200, 401, 403].each do |code|
|
|
|
|
allow(http_response).to receive(:code).and_return(code)
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
expect(response).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is invalid for other statuses" do
|
2020-07-27 17:09:16 -04:00
|
|
|
expect(http_response).to receive(:code).and_return(500)
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
expect(response).not_to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#reason' do
|
|
|
|
it 'returns a reason if it was included in the response body' do
|
2020-07-27 17:09:16 -04:00
|
|
|
expect(http_response).to receive(:body).and_return({ reason: 'Not authorized' }.to_json)
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
expect(response.reason).to eq('Not authorized')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when there was no body' do
|
2020-07-27 17:09:16 -04:00
|
|
|
expect(http_response).to receive(:body).and_return('')
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
expect(response.reason).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#successful?' do
|
|
|
|
it 'is `true` if the status is 200' do
|
2020-07-27 17:09:16 -04:00
|
|
|
allow(http_response).to receive(:code).and_return(200)
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
expect(response).to be_successful
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is `false` if the status is 401 or 403' do
|
2020-07-27 17:09:16 -04:00
|
|
|
[401, 403].each do |code|
|
|
|
|
allow(http_response).to receive(:code).and_return(code)
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
expect(response).not_to be_successful
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|