diff --git a/lib/sharepoint/client.rb b/lib/sharepoint/client.rb index 9f371fb..c33e37c 100644 --- a/lib/sharepoint/client.rb +++ b/lib/sharepoint/client.rb @@ -9,18 +9,16 @@ require 'sharepoint/client/token' module Sharepoint - class Client + class Client # rubocop:disable Metrics/ClassLength FILENAME_INVALID_CHARS = '~"#%&*:<>?/\{|}' - attr_accessor :token - def authenticating - get_token + generate_new_token yield end - def get_token - token.get_or_fetch + def generate_new_token + token.retrieve end def bearer_auth @@ -649,21 +647,24 @@ def valid_config_options(options = []) def validate_config! raise Errors::InvalidAuthenticationError.new unless valid_authentication?(config.authentication) - if config.authentication == 'token' - invalid_token_opts = validate_token_config + validate_token_config! if config.authentication == 'token' + validate_ntlm_config! if config.authentication == 'ntlm' - raise Errors::InvalidTokenConfigError.new(invalid_token_opts) unless invalid_token_opts.empty? - raise Errors::UriConfigurationError.new unless valid_uri?(config.auth_scope) - end + raise Errors::UriConfigurationError.new unless valid_uri?(config.uri) + raise Errors::EthonOptionsConfigurationError.new unless ethon_easy_options.is_a?(Hash) + end - if config.authentication == 'ntlm' - invalid_ntlm_opts = validate_ntlm_config + def validate_token_config! + invalid_token_opts = validate_token_config - raise Errors::InvalidNTLMConfigError.new(invalid_ntlm_opts) unless invalid_ntlm_opts.empty? - end + raise Errors::InvalidTokenConfigError.new(invalid_token_opts) unless invalid_token_opts.empty? + raise Errors::UriConfigurationError.new unless valid_uri?(config.auth_scope) + end - raise Errors::UriConfigurationError.new unless valid_uri?(config.uri) - raise Errors::EthonOptionsConfigurationError.new unless ethon_easy_options.is_a?(Hash) + def validate_ntlm_config! + invalid_ntlm_opts = validate_ntlm_config + + raise Errors::InvalidNTLMConfigError.new(invalid_ntlm_opts) unless invalid_ntlm_opts.empty? end def string_not_blank?(object) diff --git a/lib/sharepoint/client/token.rb b/lib/sharepoint/client/token.rb index c230cf2..05c226b 100644 --- a/lib/sharepoint/client/token.rb +++ b/lib/sharepoint/client/token.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + module Sharepoint class Client - class Token + class Token # rubocop:disable Style/Documentation class InvalidTokenError < StandardError end @@ -11,7 +13,7 @@ def initialize(config) @config = config end - def get_or_fetch + def retrieve return access_token unless access_token.nil? || expired? fetch diff --git a/spec/lib/sharepoint/client_spec.rb b/spec/lib/sharepoint/client_spec.rb index 8f4a112..36bce0d 100644 --- a/spec/lib/sharepoint/client_spec.rb +++ b/spec/lib/sharepoint/client_spec.rb @@ -29,15 +29,15 @@ end it 'sets base_api_url in the client' do - expect(subject.send(:base_api_url)).to eql("#{ENV.fetch('SP_URL', nil)}/_api/") + expect(client.send(:base_api_url)).to eql("#{ENV.fetch('SP_URL', nil)}/_api/") end it 'sets base_api_web_url in the client' do - expect(subject.send(:base_api_web_url)).to eql("#{ENV.fetch('SP_URL', nil)}/_api/web/") + expect(client.send(:base_api_web_url)).to eql("#{ENV.fetch('SP_URL', nil)}/_api/web/") end end - context 'correct authentication' do + context 'with authentication' do [{ value: 'ntlm', name: 'ntlm' }, { value: 'token', name: 'token' }].each do |ocurrence| it "does not raise authentication configuration error for #{ocurrence[:name]} authentication" do @@ -51,8 +51,8 @@ end end - context 'ethon easy options' do - context 'success' do + context 'with ethon easy options' do + context 'with success' do let(:config_ethon) { config.merge({ ethon_easy_options: ssl_verifypeer }) } let(:ssl_verifypeer) { { ssl_verifypeer: false } } @@ -72,8 +72,8 @@ end end - context 'failure' do - context 'bad authentication' do + context 'with failure' do + context 'with bad authentication' do [{ value: nil, name: 'nil' }, { value: '', name: 'blank' }, { value: 344, name: 344 }].each do |ocurrence| @@ -88,10 +88,10 @@ end end - context 'token' do + context 'with token' do before { ENV['SP_AUTHENTICATION'] = 'token' } - context 'bad client_id' do + context 'with bad client_id' do [{ value: nil, name: 'nil' }, { value: '', name: 'blank' }, { value: 344, name: 344 }].each do |ocurrence| @@ -106,7 +106,7 @@ end end - context 'bad client_secret' do + context 'with bad client_secret' do [{ value: nil, name: 'nil' }, { value: '', name: 'blank' }, { value: 344, name: 344 }].each do |ocurrence| @@ -121,7 +121,7 @@ end end - context 'bad tenant_id' do + context 'with bad tenant_id' do [{ value: nil, name: 'nil' }, { value: '', name: 'blank' }, { value: 344, name: 344 }].each do |ocurrence| @@ -136,7 +136,7 @@ end end - context 'bad cert_name' do + context 'with bad cert_name' do [{ value: nil, name: 'nil' }, { value: '', name: 'blank' }, { value: 344, name: 344 }].each do |ocurrence| @@ -151,7 +151,7 @@ end end - context 'bad auth_scope' do + context 'with bad auth_scope' do [{ value: nil, name: 'nil' }, { value: '', name: 'blank' }, { value: 344, name: 344 }].each do |ocurrence| @@ -166,7 +166,7 @@ end end - context 'bad auth_scope' do + context 'with bad auth_scope uri format' do [{ value: 'ftp://www.test.com', name: 'invalid auth_scope' }].each do |ocurrence| it "raises auth_scope configuration error for #{ocurrence[:name]} auth_scope" do wrong_config = config @@ -179,7 +179,7 @@ end end - context 'bad uri' do + context 'with bad uri' do [{ value: nil, name: 'nil' }, { value: '', name: 'blank' }, { value: 344, name: 344 }, @@ -196,10 +196,10 @@ end end - context 'ntlm' do + context 'when ntlm' do before { ENV['SP_AUTHENTICATION'] = 'ntlm' } - context 'bad username' do + context 'with bad username' do [{ value: nil, name: 'nil' }, { value: '', name: 'blank' }, { value: 344, name: 344 }].each do |ocurrence| @@ -214,7 +214,7 @@ end end - context 'bad password' do + context 'with bad password' do [{ value: nil, name: 'nil' }, { value: '', name: 'blank' }, { value: 344, name: 344 }].each do |ocurrence|