Skip to content

Commit

Permalink
Fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cacheflow committed Dec 18, 2024
1 parent 4bbd710 commit bb13556
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
28 changes: 15 additions & 13 deletions lib/fcm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,23 +293,25 @@ def jwt_token
token["access_token"]
end

def credentials_error_msg(json_key_path)
param_klass = if @json_key_path.nil?
'nil'
else
"a #{@json_key_path.class.name}"
end
def credentials_error_msg(param)
error_msg = 'credentials must be an IO-like ' \
'object or path You passed '

error_msg += param.class.name.to_s
raise InvalidCredentialError, error_msg
end

"credentials must be an IO-like object or a path. You passed #{param_klass}"
def filename_or_io_like?(path)
(path.is_a?(String) || path.respond_to?(:open)) && File.file?(path)
end

def json_key
@json_key ||= if @json_key_path.respond_to?(:read)
@json_key_path
elsif (@json_key_path.is_a?(String) || @json_key_path.respond_to?(:open)) && File.file?(@json_key_path)
File.open(@json_key_path)
else
raise InvalidCredentialError, credentials_error_msg(@json_key_path)
end
@json_key_path
elsif filename_or_io_like?(@json_key_path)
File.open(@json_key_path)
else
credentials_error_msg(@json_key_path)
end
end
end
56 changes: 35 additions & 21 deletions spec/fcm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,33 @@
}
end

let(:client_email) do
'83315528762cf7e0-7bbcc3aad87e0083391bc7f234d487' \
'c8@developer.gserviceaccount.com'
end

let(:client_x509_cert_url) do
'https://www.googleapis.com/robot/v1/metadata/x509/' \
'fd6b61037dd2bb8585527679" + "-7bbcc3aad87e0083391b' \
'c7f234d487c8%40developer.gserviceaccount.com'
end

let(:creds_error) do
FCM::InvalidCredentialError
end

let(:json_credentials) do
{
"type": 'service_account',
"project_id": 'example',
"private_key_id": 'c09c4593eee53707ca9f4208fbd6fe72b29fc7ab',
"private_key": OpenSSL::PKey::RSA.new(2048),
"client_email": '83315528762cf7e0-7bbcc3aad87e0083391bc7f234d487c8@developer.gserviceaccount.com',
"client_id": 'acedc3c0a63b3562376386f0-f3b94aafbecd0e7d60563bf7bb8bb47f.apps.googleusercontent.com',
"client_email": client_email,
"client_id": 'acedc3c0a63b3562376386f0.apps.googleusercontent.com',
"auth_uri": 'https://accounts.google.com/o/oauth2/auth',
"token_uri": 'https://oauth2.googleapis.com/token',
"auth_provider_x509_cert_url": 'https://www.googleapis.com/oauth2/v1/certs',
"client_x509_cert_url": 'https://www.googleapis.com/robot/v1/metadata/x509/fd6b61037dd2bb8585527679-7bbcc3aad87e0083391bc7f234d487c8%40developer.gserviceaccount.com',
"client_x509_cert_url": client_x509_cert_url,
"universe_domain": 'googleapis.com'
}.to_json
end
Expand All @@ -42,35 +57,34 @@
end

describe "credentials path" do
it "can be a path to a file" do
it 'can be a path to a file' do
fcm = FCM.new("README.md")
expect(fcm.__send__(:json_key).class).to eq(File)
end

it "can be an IO object" do
fcm = FCM.new(StringIO.new("hey"))
it 'can be an IO object' do
fcm = FCM.new(StringIO.new('hey'))
expect(fcm.__send__(:json_key).class).to eq(StringIO)
end

it "raises an error when passed a non-existent credentials file path" do
fcm = FCM.new('spec/fake_credentials.json', '', {})
expect { fcm.__send__(:json_key).class }.to raise_error(FCM::InvalidCredentialError)
it 'raises an error when passed a non IO-like object' do
[
FCM.new(nil, '', {}),
FCM.new({}, '', {}),
FCM.new(json_credentials, '', {})
].each do |fcm|
expect { fcm.__send__(:json_key) }.to raise_error(creds_error)
end
end

it "raises an error when passed a string of a file that does not exist" do
fcm = FCM.new("fake_credentials.json", '', {})
expect { fcm.__send__(:json_key).class }.to raise_error(FCM::InvalidCredentialError)
it 'raises an error when passed a non-existent credentials file path' do
fcm = FCM.new('spec/fake_credentials.json', '', {})
expect { fcm.__send__(:json_key) }.to raise_error(creds_error)
end

it 'raises an error when passed a non IO-like object' do
fcm_with_non_io_objects = [
fcm_with_nil_creds = FCM.new(nil, '', {}),
fcm_with_hash_creds = FCM.new({}, '', {}),
fcm_with_json = FCM.new(json_credentials, '', {})
]
fcm_with_non_io_objects.each do |fcm_with_non_io_object|
expect { fcm_with_non_io_object.__send__(:json_key).class }.to raise_error(FCM::InvalidCredentialError)
end
it 'raises an error when passed a string of a file that does not exist' do
fcm = FCM.new('fake_credentials.json', '', {})
expect { fcm.__send__(:json_key) }.to raise_error(creds_error)
end
end

Expand Down

0 comments on commit bb13556

Please sign in to comment.