spec/api/configuration_spec.rb in deepl-rb-2.5.3 vs spec/api/configuration_spec.rb in deepl-rb-3.0.0

- old
+ new

@@ -1,48 +1,122 @@ +# Copyright 2018 Daniel Herzog +# Use of this source code is governed by an MIT +# license that can be found in the LICENSE.md file. # frozen_string_literal: true +require 'logger' require 'spec_helper' describe DeepL::Configuration do + subject(:config) { described_class.new(attributes) } + let(:attributes) { {} } - subject { DeepL::Configuration.new(attributes) } + around do |tests| + tmp_env = ENV.to_hash + ENV.clear + tests.call + ENV.replace(tmp_env) + end + describe '#initialize' do - context 'When using default configuration attributes' do - it 'should use default attributes' do - expect(subject.auth_key).to eq(ENV.fetch('DEEPL_AUTH_KEY', nil)) - expect(subject.host).to eq('https://api.deepl.com') - expect(subject.version).to eq('v2') + context 'when using default configuration attributes' do + it 'uses default attributes' do + expect(config.auth_key).to eq(ENV.fetch('DEEPL_AUTH_KEY', nil)) + expect(config.host).to eq('https://api.deepl.com') + expect(config.max_doc_status_queries).to be_nil + expect(config.version).to eq('v2') end + + it 'sends platform information' do + expect(config.user_agent).to include('deepl-ruby/') + expect(config.user_agent).to include('(') + expect(config.user_agent).to include(')') + expect(config.user_agent).to include(' ruby/') + end end - context 'When using custom configuration attributes' do - let(:attributes) { { auth_key: 'SAMPLE', host: 'https://api-free.deepl.com', version: 'v1' } } + context 'when using custom configuration attributes' do + let(:logger) { Logger.new($stdout) } + let(:attributes) { { auth_key: 'SAMPLE', host: 'https://api-free.deepl.com', logger: logger, max_doc_status_queries: 42, max_network_retries: 43, version: 'v1' } } - it 'should use custom attributes' do - expect(subject.auth_key).to eq(attributes[:auth_key]) - expect(subject.host).to eq(attributes[:host]) - expect(subject.version).to eq(attributes[:version]) + it 'uses custom attributes' do + expect(config.auth_key).to eq(attributes[:auth_key]) + expect(config.host).to eq(attributes[:host]) + expect(config.max_doc_status_queries).to eq(attributes[:max_doc_status_queries]) + expect(config.max_network_retries).to eq(attributes[:max_network_retries]) + expect(config.version).to eq(attributes[:version]) end end + + context 'when using a free key' do + let(:attributes) { { auth_key: '123e4567-e89b-12d3-a456-426614174000:fx' } } + + it 'uses the free API URL' do + expect(config.host).to eq('https://api-free.deepl.com') + end + end + + context 'when using a pro key' do + let(:attributes) { { auth_key: '123e4567-e89b-12d3-a456-426614174000' } } + + it 'uses the pro API URL' do + expect(config.host).to eq('https://api.deepl.com') + end + end + + context 'when opting out of sending platform info' do + subject(:config) { described_class.new(attributes, nil, nil, false) } + + it 'does not send platform information' do + expect(config.user_agent).to include('deepl-ruby/') + expect(config.user_agent).not_to include('(') + expect(config.user_agent).not_to include(')') + expect(config.user_agent).not_to include(' ruby/') + end + end + + context 'when registering an app and sending platform info' do + subject(:config) { described_class.new(attributes, 'MyTestApp', '0.1.3', true) } + + it 'sends platform and app info' do + expect(config.user_agent).to include('deepl-ruby/') + expect(config.user_agent).to include('(') + expect(config.user_agent).to include(')') + expect(config.user_agent).to include(' ruby/') + expect(config.user_agent).to include('MyTestApp/0.1.3') + end + end + + context 'when registering an app and not sending platform info' do + subject(:config) { described_class.new(attributes, 'MyTestApp', '0.1.3', false) } + + it 'sends app, but not platform info' do + expect(config.user_agent).to include('deepl-ruby/') + expect(config.user_agent).not_to include('(') + expect(config.user_agent).not_to include(')') + expect(config.user_agent).not_to include(' ruby/') + expect(config.user_agent).to include('MyTestApp/0.1.3') + end + end end describe '#validate!' do let(:auth_message) { 'auth_key not provided' } - context 'When providing a valid auth key' do + context 'when providing a valid auth key' do let(:attributes) { { auth_key: '' } } - it 'should raise an error' do - expect { subject.validate! }.to raise_error(DeepL::Exceptions::Error, auth_message) + it 'raises an error' do + expect { config.validate! }.to raise_error(DeepL::Exceptions::Error, auth_message) end end - context 'When providing an invalid auth key' do + context 'when providing an invalid auth key' do let(:attributes) { { auth_key: 'not-empty' } } - it 'should not raise an error' do - expect { subject.validate! }.not_to raise_error + it 'does not raise an error' do + expect { config.validate! }.not_to raise_error end end end end