1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
# frozen_string_literal: true
RSpec.describe FaradayMiddleware::OAuth2 do
def query_params(env)
Faraday::Utils.parse_query env[:url].query
end
def auth_header(env)
env[:request_headers]['Authorization']
end
def perform(params = {}, headers = {})
env = {
url: URI("http://example.com/?#{Faraday::Utils.build_query(params)}"),
request_headers: Faraday::Utils::Headers.new.update(headers)
}
app = make_app
app.call(faraday_env(env))
end
def make_app
described_class.new(->(env) { env }, *Array(options))
end
context 'no token configured' do
let(:options) { [nil, { token_type: :param }] }
it "doesn't add params" do
request = perform(q: 'hello')
expect(query_params(request)).to eq('q' => 'hello')
end
it "doesn't add headers" do
expect(auth_header(perform)).to be_nil
end
it 'creates header for explicit token' do
request = perform(q: 'hello', access_token: 'abc123')
expect(query_params(request)).to eq('q' => 'hello', 'access_token' => 'abc123')
expect(auth_header(request)).to eq(%(Token token="abc123"))
end
context 'bearer token type configured' do
let(:options) { [nil, { token_type: :bearer }] }
it "doesn't add headers" do
expect(auth_header(perform)).to be_nil
end
it 'adds header for explicit token' do
request = perform(q: 'hello', access_token: 'abc123')
expect(auth_header(request)).to eq(%(Bearer abc123))
end
end
end
context 'default token configured' do
let(:options) { ['XYZ', { token_type: :param }] }
it 'adds token param' do
expect(query_params(perform(q: 'hello'))).to eq('q' => 'hello', 'access_token' => 'XYZ')
end
it 'adds token header' do
expect(auth_header(perform)).to eq(%(Token token="XYZ"))
end
it 'overrides default with explicit token' do
request = perform(q: 'hello', access_token: 'abc123')
expect(query_params(request)).to eq('q' => 'hello', 'access_token' => 'abc123')
expect(auth_header(request)).to eq(%(Token token="abc123"))
end
it 'clears default with empty explicit token' do
request = perform(q: 'hello', access_token: nil)
expect(query_params(request).fetch('access_token')).to_not eq('XYZ')
expect(auth_header(request)).to be_nil
end
context 'bearer token type configured' do
let(:options) { ['XYZ', { token_type: :bearer }] }
it 'adds token header' do
expect(auth_header(perform)).to eq(%(Bearer XYZ))
end
it 'overrides default with explicit token' do
request = perform(q: 'hello', access_token: 'abc123')
expect(auth_header(request)).to eq(%(Bearer abc123))
end
it 'clears default with empty explicit token' do
request = perform(q: 'hello', access_token: nil)
expect(auth_header(request)).to be_nil
end
end
end
context 'existing Authorization header' do
let(:options) { ['XYZ', { token_type: :param }] }
subject { perform({ q: 'hello' }, 'Authorization' => 'custom') }
it 'adds token param' do
expect(query_params(subject)).to eq('q' => 'hello', 'access_token' => 'XYZ')
end
it "doesn't override existing header" do
expect(auth_header(subject)).to eq('custom')
end
context 'bearer token type configured' do
let(:options) { ['XYZ', { token_type: :bearer }] }
subject { perform({ q: 'hello' }, 'Authorization' => 'custom') }
it "doesn't override existing header" do
expect(auth_header(subject)).to eq('custom')
end
end
end
context 'custom param name configured' do
let(:options) { ['XYZ', { token_type: :param, param_name: :oauth }] }
it 'adds token param' do
expect(query_params(perform)).to eq('oauth' => 'XYZ')
end
it 'overrides default with explicit token' do
request = perform(oauth: 'abc123')
expect(query_params(request)).to eq('oauth' => 'abc123')
expect(auth_header(request)).to eq(%(Token token="abc123"))
end
end
context 'options without token configuration' do
let(:options) { [{ token_type: :param, param_name: :oauth }] }
it "doesn't add param" do
expect(query_params(perform)).to be_empty
end
it 'overrides default with explicit token' do
expect(query_params(perform(oauth: 'abc123'))).to eq('oauth' => 'abc123')
end
end
context 'invalid param name configured' do
let(:options) { ['XYZ', { token_type: :param, param_name: nil }] }
it 'raises error' do
expect { make_app }.to raise_error(ArgumentError, ":param_name can't be blank")
end
end
end
|