2021-12-09 07:30:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Auth::OmniAuthStrategies::DiscourseGoogleOauth2 do
|
2021-12-09 07:30:27 -05:00
|
|
|
let(:response_hash) do
|
|
|
|
{
|
|
|
|
email: 'user@domain.com',
|
|
|
|
email_verified: true
|
|
|
|
}
|
|
|
|
end
|
|
|
|
let(:groups) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: "12345",
|
|
|
|
name: "group1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "67890",
|
|
|
|
name: "group2"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
let(:uid) { "12345" }
|
|
|
|
let(:domain) { "domain.com" }
|
|
|
|
|
|
|
|
def build_response(body, code = 200)
|
|
|
|
[code, { 'Content-Type' => 'application/json' }, body.to_json]
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_client(groups_response)
|
|
|
|
OAuth2::Client.new('abc', 'def') do |builder|
|
|
|
|
builder.request :url_encoded
|
|
|
|
builder.adapter :test do |stub|
|
|
|
|
stub.get('/oauth2/v3/userinfo') { build_response(response_hash) }
|
|
|
|
stub.get(described_class::GROUPS_PATH) { groups_response }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:successful_groups_client) do
|
|
|
|
build_client(
|
|
|
|
build_response(
|
|
|
|
groups: groups
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:unsuccessful_groups_client) do
|
|
|
|
build_client(
|
|
|
|
build_response(
|
|
|
|
error: {
|
|
|
|
code: 403,
|
|
|
|
message: "Not Authorized to access this resource/api"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:successful_groups_token) do
|
|
|
|
OAuth2::AccessToken.from_hash(successful_groups_client, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:unsuccessful_groups_token) do
|
|
|
|
OAuth2::AccessToken.from_hash(unsuccessful_groups_client, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
lambda do |_env|
|
|
|
|
[200, {}, ["Hello."]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_strategy(access_token)
|
|
|
|
strategy = described_class.new(app, 'appid', 'secret', @options)
|
|
|
|
strategy.stubs(:uid).returns(uid)
|
|
|
|
strategy.stubs(:access_token).returns(access_token)
|
|
|
|
strategy
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
@options = {}
|
|
|
|
OmniAuth.config.test_mode = true
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
OmniAuth.config.test_mode = false
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when request_groups is true' do
|
2021-12-09 07:30:27 -05:00
|
|
|
before do
|
|
|
|
@options[:request_groups] = true
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when groups request successful' do
|
2021-12-09 07:30:27 -05:00
|
|
|
before do
|
|
|
|
@strategy = build_strategy(successful_groups_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should include users groups' do
|
|
|
|
expect(@strategy.extra[:raw_groups].map(&:symbolize_keys)).to eq(groups)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when groups request unsuccessful' do
|
2021-12-09 07:30:27 -05:00
|
|
|
before do
|
|
|
|
@strategy = build_strategy(unsuccessful_groups_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'users groups should be empty' do
|
|
|
|
expect(@strategy.extra[:raw_groups].empty?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when request_groups is not true' do
|
2021-12-09 07:30:27 -05:00
|
|
|
before do
|
|
|
|
@options[:request_groups] = false
|
|
|
|
@strategy = build_strategy(successful_groups_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not include users groups' do
|
|
|
|
expect(@strategy.extra).not_to have_key(:raw_groups)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|