# frozen_string_literal: true require 'spec_helper' describe Minty::Client do shared_examples 'invalid credentials' do |credentials, error| it "raise an error with credentials #{credentials}" do if error.nil? expect { MintyClient.new(credentials) }.to raise_error else expect { MintyClient.new(credentials) }.to raise_error(error) end end end it_should_behave_like 'invalid credentials', { namespace: 'samples.minty.page' }, Minty::InvalidCredentials it_should_behave_like 'invalid credentials', { namespace: 'samples.minty.page', client_id: 'client_id' }, Minty::InvalidCredentials it_should_behave_like 'invalid credentials', { namespace: 'samples.minty.page', client_secret: 'secret' }, Minty::InvalidCredentials it_should_behave_like 'invalid credentials', { namespace: 'samples.minty.page', api_version: 2 }, Minty::InvalidCredentials it_should_behave_like 'invalid credentials', {}, Minty::InvalidApiNamespace it_should_behave_like 'invalid credentials', { api_version: 2 }, Minty::InvalidApiNamespace it_should_behave_like 'invalid credentials', { api_version: 1 }, Minty::InvalidApiNamespace it_should_behave_like 'invalid credentials', { client_id: 'client_id', client_secret: 'secret' }, Minty::InvalidApiNamespace it_should_behave_like 'invalid credentials', { api_version: 2, token: 'token' }, Minty::InvalidApiNamespace let(:v2_credentials) { { domain: 'test.minty.page' } } shared_examples 'valid credentials' do it { expect { MintyClient.new(credentials) }.to_not raise_error } end it_should_behave_like 'valid credentials' do let(:credentials) { v2_credentials.merge(token: 'TEST_API_TOKEN') } end it_should_behave_like 'valid credentials' do let(:credentials) { v2_credentials.merge(access_token: 'TEST_API_TOKEN') } end context 'client headers' do let(:client) { Minty::Client.new(v2_credentials.merge(access_token: 'abc123', domain: 'myhost.minty.page')) } let(:headers) { client.headers } let(:telemetry) { JSON.parse(Base64.urlsafe_decode64(headers['Minty-Client'])) } it 'has the correct headers present' do expect(headers.keys.sort).to eql(%w[Minty-Client Authorization Content-Type]) end it 'uses the correct access token' do expect(headers['Authorization']).to eql 'Bearer abc123' end it 'is always json' do expect(headers['Content-Type']).to eql 'application/json' end it 'should include the correct name in telemetry data' do expect(telemetry['name']).to eq('ruby') end it 'should include the correct version in telemetry data' do expect(telemetry['version']).to eq(Minty::VERSION) end it 'should include the correct env in telemetry data' do expect(telemetry['env']['ruby']).to eq(RUBY_VERSION) end end end