# frozen_string_literal: true require 'spec_helper' require 'json' require 'omniauth-cz-shop-platforms' require 'stringio' describe OmniAuth::Strategies::WebAreal do let(:request) { double('Request', params: {}, cookies: {}, env: {}) } let(:app) do lambda do [200, {}, ['Hello.']] end end subject do OmniAuth::Strategies::WebAreal.new(app, 'appid', 'secret', @options || {}).tap do |strategy| allow(strategy).to receive(:request) do request end end end before do OmniAuth.config.test_mode = true end after do OmniAuth.config.test_mode = false end describe '#client_options' do it 'has correct site' do expect(subject.client.site).to eq('https://marketplace.webareal.cz') end it 'has correct authorize_url' do expect(subject.client.options[:authorize_url]).to eq('/user-auth') end it 'has correct token_url' do expect(subject.client.options[:token_url]).to eq('/api/token') end describe 'overrides' do context 'as strings' do it 'should allow overriding the site' do @options = { client_options: { 'site' => 'https://example.com' } } expect(subject.client.site).to eq('https://example.com') end it 'should allow overriding the authorize_url' do @options = { client_options: { 'authorize_url' => 'https://example.com' } } expect(subject.client.options[:authorize_url]).to eq('https://example.com') end it 'should allow overriding the token_url' do @options = { client_options: { 'token_url' => 'https://example.com' } } expect(subject.client.options[:token_url]).to eq('https://example.com') end end context 'as symbols' do it 'should allow overriding the site' do @options = { client_options: { site: 'https://example.com' } } expect(subject.client.site).to eq('https://example.com') end it 'should allow overriding the authorize_url' do @options = { client_options: { authorize_url: 'https://example.com' } } expect(subject.client.options[:authorize_url]).to eq('https://example.com') end it 'should allow overriding the token_url' do @options = { client_options: { token_url: 'https://example.com' } } expect(subject.client.options[:token_url]).to eq('https://example.com') end end end end describe '#authorize_options' do describe 'redirect_uri' do it 'should default to nil' do @options = {} expect(subject.authorize_params['redirect_uri']).to eq(nil) end it 'should set the redirect_uri parameter if present' do @options = { redirect_uri: 'https://example.com' } expect(subject.authorize_params['redirect_uri']).to eq('https://example.com') end end describe 'state' do it 'should set the state parameter' do @options = { state: 'some_state' } expect(subject.authorize_params['state']).to eq('some_state') expect(subject.authorize_params[:state]).to eq('some_state') expect(subject.session['omniauth.state']).to eq('some_state') end it 'should set the omniauth.state dynamically' do allow(subject).to receive(:request) { double('Request', params: { 'state' => 'some_state' }, env: {}) } expect(subject.authorize_params['state']).to eq('some_state') expect(subject.authorize_params[:state]).to eq('some_state') expect(subject.session['omniauth.state']).to eq('some_state') end end describe 'overrides' do it 'should include top-level options that are marked as :authorize_options' do @options = { authorize_options: %i[scope foo request_visible_actions], scope: 'http://bar', foo: 'baz', hd: 'wow', request_visible_actions: 'something' } expect(subject.authorize_params['scope']).to eq('http://bar') expect(subject.authorize_params['foo']).to eq('baz') expect(subject.authorize_params['hd']).to eq(nil) expect(subject.authorize_params['request_visible_actions']).to eq('something') end describe 'request overrides' do %i[scope state].each do |k| context "authorize option #{k}" do let(:request) { double('Request', params: { k.to_s => 'http://example.com' }, cookies: {}, env: {}) } it "should set the #{k} authorize option dynamically in the request" do @options = { k: '' } expect(subject.authorize_params[k.to_s]).to eq('http://example.com') end end end describe 'custom authorize_options' do let(:request) { double('Request', params: { 'foo' => 'something' }, cookies: {}, env: {}) } it 'should support request overrides from custom authorize_options' do @options = { authorize_options: [:foo], foo: '' } expect(subject.authorize_params['foo']).to eq('something') end end end end end describe '#authorize_params' do it 'should include any authorize params passed in the :authorize_params option' do @options = { authorize_params: { request_visible_actions: 'something', foo: 'bar', baz: 'zip' }, hd: 'wow', bad: 'not_included' } expect(subject.authorize_params['request_visible_actions']).to eq('something') expect(subject.authorize_params['foo']).to eq('bar') expect(subject.authorize_params['baz']).to eq('zip') expect(subject.authorize_params['bad']).to eq(nil) end end describe '#token_params' do it 'should include any token params passed in the :token_params option' do @options = { token_params: { foo: 'bar', baz: 'zip' } } expect(subject.token_params['foo']).to eq('bar') expect(subject.token_params['baz']).to eq('zip') end end describe '#token_options' do it 'should include top-level options that are marked as :token_options' do @options = { token_options: %i[scope foo], scope: 'bar', foo: 'baz', bad: 'not_included' } expect(subject.token_params['scope']).to eq('bar') expect(subject.token_params['foo']).to eq('baz') expect(subject.token_params['bad']).to eq(nil) end end end