Skip to content

Commit

Permalink
Rubocop manual fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lleirborras committed Apr 22, 2024
1 parent 03b4b98 commit 191f586
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
35 changes: 18 additions & 17 deletions lib/sharepoint/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/sharepoint/client/token.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module Sharepoint
class Client
class Token
class Token # rubocop:disable Style/Documentation
class InvalidTokenError < StandardError
end

Expand All @@ -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
Expand Down
36 changes: 18 additions & 18 deletions spec/lib/sharepoint/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 } }

Expand All @@ -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|
Expand All @@ -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|
Expand All @@ -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|
Expand All @@ -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|
Expand All @@ -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|
Expand All @@ -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|
Expand All @@ -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
Expand All @@ -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 },
Expand All @@ -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|
Expand All @@ -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|
Expand Down

0 comments on commit 191f586

Please sign in to comment.