# Configure Rails Envinronment ENV['RAILS_ENV'] = 'test' # # Test Coverage # require 'simplecov' # SimpleCov.start require 'shoulda' require 'shoulda/let' class Test let(:request) { double('Request', params: {}, cookies: {}, env: {}) } let(:app) do lambda do [200, {}, ['Hello.']] end end setup do OmniAuth::Strategies::Slooob.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 context '#client_options' do should 'have correct site' do assert_equal 'https://api.slooob.com', subject.client.site end should 'have correct authorize_url' do assert_equal '/oauth/authorize', subject.client.options[:authorize_url] end should 'have correct token_url' do assert_equal '/oauth/token', subject.client.options[:token_url] end context 'overrides' do context 'as strings' do should 'allow overriding the site' do @options = { client_options: { site: 'https://example.com' } } assert_equal 'https://example.com', subject.client.site end should 'allow overriding the authorize_url' do @options = { client_options: { authorize_url: 'https://example.com' } } assert_equal 'https://example.com', subject.client.options[:authorize_url] end should 'allow overriding the token_url' do @options = { client_options: { token_url: 'https://example.com' } } assert_equal 'https://example.com', subject.client.options[:token_url] end end context 'as symbols' do should 'allow overriding the site' do @options = { client_options: { site: 'https://example.com' } } assert_equal 'https://example.com', subject.client.site end should 'allow overriding the authorize_url' do @options = { client_options: { authorize_url: 'https://example.com' } } assert_equal 'https://example.com', subject.client.options[:authorize_url] end should 'allow overriding the token_url' do @options = { client_options: { token_url: 'https://example.com' } } assert_equal 'https://example.com', subject.client.options[:token_url] end end end end context '#authorize_options' do %i[email image_size redirect_uri incremental_authorization scope state].each do |k| should "support #{k}" do @options = { k => 'https://example.com' } assert_equal 'https://example.com', subject.authorize_params[k.to_s] end end context 'redirect_uri' do should 'default to nil' do @options = {} assert_equal nil, subject.authorize_params['redirect_uri'] end should 'set the redirect_uri parameter if present' do @options = { redirect_uri: 'https://example.com' } assert_equal 'https://example.com', subject.authorize_params['redirect_uri'] end end context 'email' do should 'default to nil' do assert_equal nil, subject.authorize_params['email'] end should 'set the email parameter if present' do @options = { email: 'john@example.com' } assert_equal 'john@example.com', subject.authorize_params['email'] end end context 'image_size' do should 'default to nil' do assert_equal nil, subject.authorize_params['image_size'] end should 'set the image_size parameter if present' do @options = { image_size: 'raw' } assert_equal 'raw', subject.authorize_params['image_size'] end end context 'incremental_authorization' do should 'default to nil' do assert_equal nil, subject.authorize_params['incremental_authorization'] end should 'set the incremental_authorization parameter if present' do @options = { incremental_authorization: true } assert_equal true, subject.authorize_params['incremental_authorization'] end end context 'scope' do should 'join scopes' do @options = { scope: 'public,email' } assert_equal 'public email', subject.authorize_params['scope'] end should 'deal with whitespace when joining scopes' do @options = { scope: 'public, email' } assert_equal 'public email', subject.authorize_params['scope'] end should 'set default scope to `email public`' do assert_equal 'public email', subject.authorize_params['scope'] end should 'support space delimited scopes' do @options = { scope: 'public email' } assert_equal 'public email', subject.authorize_params['scope'] end end context 'state' do should 'set the state parameter' do @options = { state: 'some_state' } assert_equal 'some_state', subject.authorize_params['state'] assert_equal 'some_state', subject.authorize_params[:state] assert_equal 'some_state', subject.session['omniauth.state'] end end context 'overrides' do should 'include top-level options that are marked as :authorize_options' do @options = { authorize_options: %i[scope], scope: 'messaging' } assert_equal 'messaging', subject.authorize_params['scope'] end context 'request overrides' do %i[incremental_authorization scope state].each do |k| context "authorize option #{k}" do let(:request) { double('Request', params: { k.to_s => 'http://example.com' }, cookies: {}, env: {}) } should "set the #{k} authorize option dynamically in the request" do @options = { k: '' } assert_equal 'http://example.com', subject.authorize_params[k.to_s] end end end context 'custom authorize_options' do let(:request) { double('Request', params: { 'foo' => 'bar' }, cookies: {}, env: {}) } should 'support request overrides from custom authorize_options' do @options = { authorize_options: [:foo], foo: '' } assert_equal 'bar', subject.authorize_params['foo'] end end end end end context '#authorize_params' do should 'include any authorize params passed in the :authorize_params option' do @options = { authorize_params: { scope: 'something', foo: 'bar', baz: 'zip' }, incremental_authorization: true, bad: 'not_included' } assert_equal 'something', subject.authorize_params['scope'] assert_equal 'bar', subject.authorize_params['foo'] assert_equal 'zip', subject.authorize_params['baz'] assert_equal true, subject.authorize_params['incremental_authorization'] assert_equal nil, subject.authorize_params['bad'] end end context '#token_params' do should 'include any token params passed in the :token_params option' do @options = { token_params: { foo: 'bar', baz: 'zip' } } assert_equal 'bar', subject.token_params['foo'] assert_equal 'zip', subject.token_params['baz'] end end context '#token_options' do 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' } assert_equal 'bar', subject.token_params['scope'] assert_equal 'baz', subject.token_params['foo'] assert_equal nil, subject.token_params['bad'] end end context '#callback_path' do should 'have the correct default callback path' do assert_equal '/auth/slooob/callback', subject.callback_path end should 'set the callback_path parameter if present' do @options = { callback_path: '/auth/foo/callback' } assert_equal '/auth/foo/callback', subject.callback_path end end end