From 03b4b989e6fa84463a6ce952b19366fcf11c6e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lle=C3=AFr=20Borr=C3=A0s=20Metje?= Date: Mon, 22 Apr 2024 10:05:41 +0200 Subject: [PATCH] Rubocop autofix --- lib/sharepoint/client.rb | 53 ++++---- lib/sharepoint/client/token.rb | 21 ++-- lib/sharepoint/errors.rb | 6 +- lib/sharepoint/spec_helpers.rb | 20 +-- spec/lib/sharepoint/client_spec.rb | 187 +++++++++++++---------------- 5 files changed, 136 insertions(+), 151 deletions(-) diff --git a/lib/sharepoint/client.rb b/lib/sharepoint/client.rb index 5e64eea..9f371fb 100644 --- a/lib/sharepoint/client.rb +++ b/lib/sharepoint/client.rb @@ -14,7 +14,7 @@ class Client attr_accessor :token - def authenticating(&block) + def authenticating get_token yield end @@ -24,7 +24,7 @@ def get_token end def bearer_auth - "Bearer #{token.to_s}" + "Bearer #{token}" end # @return [OpenStruct] The current configuration. @@ -376,7 +376,7 @@ def upload(filename, content, path, site_path = nil) url = uri_escape "#{url}GetFolderByServerRelativeUrl('#{path}')/Files/Add(url='#{sanitized_filename}',overwrite=true)" easy = ethon_easy_json_requester easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose', - 'X-RequestDigest' => xrequest_digest(site_path) }) + 'X-RequestDigest' => xrequest_digest(site_path) }) easy.http_request(url, :post, { body: content }) easy.perform check_and_raise_failure(easy) @@ -404,11 +404,11 @@ def update_metadata(filename, metadata, path, site_path = nil) prepared_metadata = prepare_metadata(metadata, __metadata['type']) easy = ethon_easy_json_requester - easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose', - 'content-type' => 'application/json;odata=verbose', - 'X-RequestDigest' => xrequest_digest(site_path), - 'X-Http-Method' => 'PATCH', - 'If-Match' => "*" }) + easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose', + 'content-type' => 'application/json;odata=verbose', + 'X-RequestDigest' => xrequest_digest(site_path), + 'X-Http-Method' => 'PATCH', + 'If-Match' => '*' }) easy.http_request(update_metadata_url, :post, { body: prepared_metadata }) @@ -486,10 +486,10 @@ def process_url(url, fields) parsed_response_body = JSON.parse(easy.response_body) page_content = if fields - parsed_response_body['d']['results'].map{|v|v.fetch_values(*fields)} - else - parsed_response_body['d']['results'] - end + parsed_response_body['d']['results'].map { |v| v.fetch_values(*fields) } + else + parsed_response_body['d']['results'] + end if next_url = parsed_response_body['d']['__next'] page_content + process_url(next_url, fields) @@ -513,7 +513,7 @@ def with_bearer_authentication_header(h) end def bearer_auth_header - {"Authorization" => bearer_auth } + { 'Authorization' => bearer_auth } end def base_url @@ -542,7 +542,7 @@ def computed_web_api_url(site) def ethon_easy_json_requester easy = ethon_easy_requester - easy.headers = with_bearer_authentication_header({ 'accept'=> 'application/json;odata=verbose'}) + easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose' }) easy end @@ -629,11 +629,11 @@ def extract_paths(url) end def validate_token_config - valid_config_options( %i(client_id client_secret tenant_id cert_name auth_scope) ) + valid_config_options(%i[client_id client_secret tenant_id cert_name auth_scope]) end def validate_ntlm_config - valid_config_options( %i(username password) ) + valid_config_options(%i[username password]) end def valid_config_options(options = []) @@ -641,21 +641,22 @@ def valid_config_options(options = []) c = config.send(opt) next if c.present? && string_not_blank?(c) + opt end.compact end def validate_config! - raise Errors::InvalidAuthenticationError.new unless valid_authentication?(config.authentication) + raise Errors::InvalidAuthenticationError.new unless valid_authentication?(config.authentication) - if config.authentication == "token" + if config.authentication == 'token' invalid_token_opts = validate_token_config raise Errors::InvalidTokenConfigError.new(invalid_token_opts) unless invalid_token_opts.empty? raise Errors::UriConfigurationError.new unless valid_uri?(config.auth_scope) end - if config.authentication == "ntlm" + if config.authentication == 'ntlm' invalid_ntlm_opts = validate_ntlm_config raise Errors::InvalidNTLMConfigError.new(invalid_ntlm_opts) unless invalid_ntlm_opts.empty? @@ -672,14 +673,14 @@ def string_not_blank?(object) def valid_uri?(which) if which and which.is_a? String uri = URI.parse(which) - uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS) + uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) else false end end def valid_authentication?(which) - %w(ntlm token).include?(which) + %w[ntlm token].include?(which) end # Waiting for RFC 3986 to be implemented, we need to escape square brackets @@ -834,11 +835,11 @@ def update_object_metadata(metadata, new_metadata, site_path = '') prepared_metadata = prepare_metadata(new_metadata, metadata['type']) easy = ethon_easy_json_requester - easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose', - 'content-type' => 'application/json;odata=verbose', - 'X-RequestDigest' => xrequest_digest(site_path), - 'X-Http-Method' => 'PATCH', - 'If-Match' => "*" }) + easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose', + 'content-type' => 'application/json;odata=verbose', + 'X-RequestDigest' => xrequest_digest(site_path), + 'X-Http-Method' => 'PATCH', + 'If-Match' => '*' }) easy.http_request(update_metadata_url, :post, diff --git a/lib/sharepoint/client/token.rb b/lib/sharepoint/client/token.rb index ae8cf26..c230cf2 100644 --- a/lib/sharepoint/client/token.rb +++ b/lib/sharepoint/client/token.rb @@ -2,11 +2,9 @@ module Sharepoint class Client class Token class InvalidTokenError < StandardError - end - - attr_accessor :expires_in - attr_accessor :access_token - attr_accessor :fetched_at + end + + attr_accessor :expires_in, :access_token, :fetched_at attr_reader :config def initialize(config) @@ -15,6 +13,7 @@ def initialize(config) def get_or_fetch return access_token unless access_token.nil? || expired? + fetch end @@ -25,10 +24,10 @@ def to_s def fetch response = request_new_token - details = response["Token"] + details = response['Token'] self.fetched_at = Time.now.utc.to_i - self.expires_in = details["expires_in"] - self.access_token = details["access_token"] + self.expires_in = details['expires_in'] + self.access_token = details['access_token'] end private @@ -39,8 +38,8 @@ def expired? (fetched_at + expires_in) < Time.now.utc.to_i end - def - def request_new_token + def + def(_request_new_token) auth_request = { client_id: config.client_id, client_secret: config.client_secret, @@ -49,7 +48,7 @@ def request_new_token auth_scope: config.auth_scope }.to_json - headers = {'Content-Type' => 'application/json'} + headers = { 'Content-Type' => 'application/json' } ethon = Ethon::Easy.new(followlocation: true) ethon.http_request(config.token_url, :post, body: auth_request, headers: headers) diff --git a/lib/sharepoint/errors.rb b/lib/sharepoint/errors.rb index a56bba2..65ca127 100644 --- a/lib/sharepoint/errors.rb +++ b/lib/sharepoint/errors.rb @@ -14,7 +14,7 @@ def initialize class InvalidAuthenticationError < StandardError def initialize - super "Invalid authentication mechanism" + super('Invalid authentication mechanism') end end @@ -24,7 +24,7 @@ def initialize(invalid_entries) "Invalid #{e} in Token configuration" end - super error_messages.join(',') + super(error_messages.join(',')) end end @@ -34,7 +34,7 @@ def initialize(invalid_entries) "Invalid #{e} in NTLM configuration" end - super error_messages.join(',') + super(error_messages.join(',')) end end end diff --git a/lib/sharepoint/spec_helpers.rb b/lib/sharepoint/spec_helpers.rb index 92b26a4..7d05a0d 100644 --- a/lib/sharepoint/spec_helpers.rb +++ b/lib/sharepoint/spec_helpers.rb @@ -13,15 +13,15 @@ def value_to_string(value) def sp_config { - uri: ENV['SP_URL'], - authentication: ENV['SP_AUTHENTICATION'], - client_id: ENV['SP_CLIENT_ID'], - client_secret: ENV['SP_CLIENT_SECRET'], - tenant_id: ENV['SP_TENANT_ID'], - cert_name: ENV['SP_CERT_NAME'], - auth_scope: ENV['SP_AUTH_SCOPE'], - username: ENV['SP_USERNAME'], - password: ENV['SP_PASSWORD'] + uri: ENV.fetch('SP_URL', nil), + authentication: ENV.fetch('SP_AUTHENTICATION', nil), + client_id: ENV.fetch('SP_CLIENT_ID', nil), + client_secret: ENV.fetch('SP_CLIENT_SECRET', nil), + tenant_id: ENV.fetch('SP_TENANT_ID', nil), + cert_name: ENV.fetch('SP_CERT_NAME', nil), + auth_scope: ENV.fetch('SP_AUTH_SCOPE', nil), + username: ENV.fetch('SP_USERNAME', nil), + password: ENV.fetch('SP_PASSWORD', nil) } end @@ -34,7 +34,7 @@ def mock_requests def mock_token_responses allow_any_instance_of(Sharepoint::Client::Token) .to receive(:request_new_token) - .and_return({"Token" => { "expires_in" => 3600, "access_token" => "access_token" }}) + .and_return({ 'Token' => { 'expires_in' => 3600, 'access_token' => 'access_token' } }) end def mock_responses(fixture_file) diff --git a/spec/lib/sharepoint/client_spec.rb b/spec/lib/sharepoint/client_spec.rb index a276b1e..8f4a112 100644 --- a/spec/lib/sharepoint/client_spec.rb +++ b/spec/lib/sharepoint/client_spec.rb @@ -18,7 +18,7 @@ it 'sets config object' do client_config = client.config expect(client_config).to be_a OpenStruct - [:client_id, :client_secret, :tenant_id, :cert_name, :auth_scope, :url].each do |key| + %i[client_id client_secret tenant_id cert_name auth_scope url].each do |key| value = client_config.send(key) expect(value).to eq config[key] end @@ -28,26 +28,25 @@ expect(client.send(:base_url)).to eql(ENV.fetch('SP_URL', nil)) end - it "sets base_api_url in the client" do - expect(subject.send :base_api_url).to eql("#{ENV['SP_URL']}/_api/") + it 'sets base_api_url in the client' do + expect(subject.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['SP_URL']}/_api/web/") + 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/") end end - context "correct authentication" do - [{ value: "ntlm", name: 'ntlm' }, - { value: "token", name: 'token' } ].each do |ocurrence| - - it "should not raise authentication configuration error for #{ ocurrence[:name]} authentication" do + context 'correct 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 correct_config = config correct_config[:authentication] = ocurrence[:value] - expect { - described_class.new(correct_config) - }.not_to raise_error(Sharepoint::Errors::InvalidAuthenticationError) + expect do + described_class.new(correct_config) + end.not_to raise_error(Sharepoint::Errors::InvalidAuthenticationError) end end end @@ -74,172 +73,158 @@ end context 'failure' do - - context "bad authentication" do - [{ value: nil, name: 'nil' }, - { value: '', name: 'blank' }, - { value: 344, name: 344 } ].each do |ocurrence| - - it "should raise authentication configuration error for #{ ocurrence[:name]} authentication" do + context 'bad authentication' do + [{ value: nil, name: 'nil' }, + { value: '', name: 'blank' }, + { value: 344, name: 344 }].each do |ocurrence| + it "raises authentication configuration error for #{ocurrence[:name]} authentication" do wrong_config = config wrong_config[:authentication] = ocurrence[:value] - expect { - described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::InvalidAuthenticationError) + expect do + described_class.new(wrong_config) + end.to raise_error(Sharepoint::Errors::InvalidAuthenticationError) end end end - - context "token" do - before { ENV['SP_AUTHENTICATION'] = 'token' } - context "bad client_id" do - [{ value: nil, name: 'nil' }, - { value: '', name: 'blank' }, - { value: 344, name: 344 } ].each do |ocurrence| + context 'token' do + before { ENV['SP_AUTHENTICATION'] = 'token' } - it "should raise client_id configuration error for #{ ocurrence[:name]} client_id" do + context 'bad client_id' do + [{ value: nil, name: 'nil' }, + { value: '', name: 'blank' }, + { value: 344, name: 344 }].each do |ocurrence| + it "raises client_id configuration error for #{ocurrence[:name]} client_id" do wrong_config = config wrong_config[:client_id] = ocurrence[:value] - expect { + expect do described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) + end.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) end end end - context "bad client_secret" do - [{ value: nil, name: 'nil' }, - { value: '', name: 'blank' }, - { value: 344, name: 344 } ].each do |ocurrence| - - it "should raise client_secret configuration error for #{ocurrence[:name]} client_secret" do + context 'bad client_secret' do + [{ value: nil, name: 'nil' }, + { value: '', name: 'blank' }, + { value: 344, name: 344 }].each do |ocurrence| + it "raises client_secret configuration error for #{ocurrence[:name]} client_secret" do wrong_config = config wrong_config[:client_secret] = ocurrence[:value] - expect { + expect do described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) + end.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) end end end - context "bad tenant_id" do - [{ value: nil, name: 'nil' }, - { value: '', name: 'blank' }, - { value: 344, name: 344 } ].each do |ocurrence| - - it "should raise tenant_id configuration error for #{ocurrence[:name]} tenant_id" do + context 'bad tenant_id' do + [{ value: nil, name: 'nil' }, + { value: '', name: 'blank' }, + { value: 344, name: 344 }].each do |ocurrence| + it "raises tenant_id configuration error for #{ocurrence[:name]} tenant_id" do wrong_config = config wrong_config[:tenant_id] = ocurrence[:value] - expect { + expect do described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) + end.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) end end end - context "bad cert_name" do - [{ value: nil, name: 'nil' }, - { value: '', name: 'blank' }, - { value: 344, name: 344 } ].each do |ocurrence| - - it "should raise cert_name configuration error for #{ocurrence[:name]} cert_name" do + context 'bad cert_name' do + [{ value: nil, name: 'nil' }, + { value: '', name: 'blank' }, + { value: 344, name: 344 }].each do |ocurrence| + it "raises cert_name configuration error for #{ocurrence[:name]} cert_name" do wrong_config = config wrong_config[:cert_name] = ocurrence[:value] - expect { + expect do described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) + end.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) end end end - context "bad auth_scope" do - [{ value: nil, name: 'nil' }, - { value: '', name: 'blank' }, - { value: 344, name: 344 }].each do |ocurrence| - - it "should raise auth_scope configuration error for #{ocurrence[:name]} auth_scope" do + context 'bad auth_scope' do + [{ value: nil, name: 'nil' }, + { value: '', name: 'blank' }, + { value: 344, name: 344 }].each do |ocurrence| + it "raises auth_scope configuration error for #{ocurrence[:name]} auth_scope" do wrong_config = config wrong_config[:auth_scope] = ocurrence[:value] - expect { + expect do described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) + end.to raise_error(Sharepoint::Errors::InvalidTokenConfigError) end end - end - context "bad auth_scope" do - [{ value: 'ftp://www.test.com', name: "invalid auth_scope" }].each do |ocurrence| - - it "should raise auth_scope configuration error for #{ocurrence[:name]} auth_scope" do + context 'bad auth_scope' 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 wrong_config[:auth_scope] = ocurrence[:value] - expect { + expect do described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::UriConfigurationError) + end.to raise_error(Sharepoint::Errors::UriConfigurationError) end end - end - context "bad uri" do - [{ value: nil, name: 'nil' }, - { value: '', name: 'blank' }, - { value: 344, name: 344 }, - { value: 'ftp://www.test.com', name: "invalid uri" }].each do |ocurrence| - - it "should raise uri configuration error for #{ocurrence[:name]} uri" do + context 'bad uri' do + [{ value: nil, name: 'nil' }, + { value: '', name: 'blank' }, + { value: 344, name: 344 }, + { value: 'ftp://www.test.com', name: 'invalid uri' }].each do |ocurrence| + it "raises uri configuration error for #{ocurrence[:name]} uri" do wrong_config = config wrong_config[:uri] = ocurrence[:value] - expect { + expect do described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::UriConfigurationError) + end.to raise_error(Sharepoint::Errors::UriConfigurationError) end end - end end - context "ntlm" do + context 'ntlm' do before { ENV['SP_AUTHENTICATION'] = 'ntlm' } - context "bad username" do - [{ value: nil, name: 'nil' }, - { value: '', name: 'blank' }, - { value: 344, name: 344 } ].each do |ocurrence| - - it "should raise username configuration error for #{ ocurrence[:name]} username" do + context 'bad username' do + [{ value: nil, name: 'nil' }, + { value: '', name: 'blank' }, + { value: 344, name: 344 }].each do |ocurrence| + it "raises username configuration error for #{ocurrence[:name]} username" do wrong_config = config wrong_config[:username] = ocurrence[:value] - expect { - described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::InvalidNTLMConfigError) + expect do + described_class.new(wrong_config) + end.to raise_error(Sharepoint::Errors::InvalidNTLMConfigError) end end end - context "bad password" do - [{ value: nil, name: 'nil' }, - { value: '', name: 'blank' }, - { value: 344, name: 344 } ].each do |ocurrence| - - it "should raise password configuration error for #{ocurrence[:name]} password" do + context 'bad password' do + [{ value: nil, name: 'nil' }, + { value: '', name: 'blank' }, + { value: 344, name: 344 }].each do |ocurrence| + it "raises password configuration error for #{ocurrence[:name]} password" do wrong_config = config wrong_config[:password] = ocurrence[:value] - expect { + expect do described_class.new(wrong_config) - }.to raise_error(Sharepoint::Errors::InvalidNTLMConfigError) + end.to raise_error(Sharepoint::Errors::InvalidNTLMConfigError) end end end