# frozen_string_literal: true require 'spec_helper' require 'timecop' class MockClass attr_reader :token include Minty::Mixins::Initializer include Minty::Mixins::HTTPProxy include Minty::Mixins::Headers include Minty::Mixins::TokenManagement end describe Minty::Mixins::Initializer do let(:params) { { namespace: 'samples.minty.page' } } let(:instance) { DummyClassForProxy.send(:include, described_class).new(params) } let(:time_now) { Time.zone.now } context 'api v2' do it 'sets retry_count when passed' do params[:token] = '123' params[:retry_count] = 10 expect(instance.instance_variable_get('@retry_count')).to eq(10) end end context 'token initialization' do before do params[:api_version] = 2 Timecop.freeze(time_now) end after do Timecop.return end it 'sets token when access_token is passed' do params[:access_token] = '123' expect(instance.instance_variable_get('@token')).to eq('123') end it 'sets token when token is passed' do params[:token] = '123' expect(instance.instance_variable_get('@token')).to eq('123') end it 'fetches a token if none was given' do params[:client_id] = client_id = 'test_client_id' params[:client_secret] = client_secret = 'test_client_secret' params[:api_identifier] = api_identifier = 'test' payload = { grant_type: 'client_credentials', client_id: client_id, client_secret: client_secret, audience: api_identifier } expect(RestClient::Request).to receive(:execute).with(hash_including( method: :post, url: 'https://samples.minty.page/oauth/token', payload: payload.to_json )) .and_return(StubResponse.new({ 'access_token' => 'test', 'expires_in' => 86_400 }, true, 200)) expect(instance.instance_variable_get('@token')).to eq('test') expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86_400) end it "doesn't get a new token if one was supplied using 'token'" do params[:token] = 'access-token' expect(RestClient::Request).not_to receive(:execute).with(hash_including( method: :post, url: 'https://samples.minty.page/oauth/token' )) expect(instance.instance_variable_get('@token')).to eq('access-token') expect(instance.instance_variable_get('@token_expires_at')).to eq(Time.now.to_i + 3600) end it "doesn't get a new token if one was supplied using 'access_token'" do params[:access_token] = 'access-token' expect(RestClient::Request).not_to receive(:execute).with(hash_including( method: :post, url: 'https://samples.minty.page/oauth/token' )) expect(instance.instance_variable_get('@token')).to eq('access-token') expect(instance.instance_variable_get('@token_expires_at')).to eq(Time.now.to_i + 3600) end it 'can supply token_expires_at option' do params[:token] = 'access-token' params[:token_expires_at] = time_now.to_i + 300 expect(RestClient::Request).not_to receive(:execute).with(hash_including( method: :post, url: 'https://samples.minty.page/oauth/token' )) expect(instance.instance_variable_get('@token')).to eq('access-token') expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 300) end it 'throws if no token or credentials were given' do params[:client_id] = 'test-client-id' expect { instance }.to raise_error(Minty::InvalidCredentials) end end end