Skip to content

Commit

Permalink
Fix NoMethodError when using method with Pathname as FCM credentials (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yemchenko-dan committed Jan 15, 2024
1 parent 86893cd commit ffa31e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
11 changes: 5 additions & 6 deletions lib/noticed/delivery_methods/fcm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ def cleanup_invalid_token(device_token)
def credentials
@credentials ||= begin
option = options[:credentials]
credentials_hash = case option
credentials_to_parse = option.is_a?(Symbol) ? notification.send(option) : option
credentials_hash = case credentials_to_parse
when Hash
option
credentials_to_parse
when Pathname
load_json(option)
load_json(credentials_to_parse)
when String
load_json(Rails.root.join(option))
when Symbol
notification.send(option)
load_json(Rails.root.join(credentials_to_parse))
else
Rails.application.credentials.fcm
end
Expand Down
20 changes: 19 additions & 1 deletion test/delivery_methods/fcm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ def fcm_credentials
{project_id: "api-12345"}
end

def fcm_credentials_as_pathname
Rails.root.join("config/credentials/fcm.json")
end

def fcm_credentials_as_string
"config/credentials/fcm.json"
end

def format_notification(device_token)
{
token: device_token,
Expand Down Expand Up @@ -44,11 +52,21 @@ class FcmTest < ActiveSupport::TestCase
assert_equal credentials_hash, Noticed::DeliveryMethods::Fcm.new.assign_args(notification_class: "FcmExample", options: {credentials: "config/credentials/fcm.json"}).credentials
end

test "when credentials option is a symbol, it returns the return value of the method" do
test "when credentials option is a symbol and return value of a method is a hash, it returns the hash" do
credentials_hash = {project_id: "api-12345"}
assert_equal credentials_hash, Noticed::DeliveryMethods::Fcm.new.assign_args(notification_class: "FcmExample", options: {credentials: :fcm_credentials}).credentials
end

test "when credentials option is a symbol and return value of a method is a Pathname object, it returns the file contents" do
credentials_hash = {project_id: "api-12345"}
assert_equal credentials_hash, Noticed::DeliveryMethods::Fcm.new.assign_args(notification_class: "FcmExample", options: {credentials: :fcm_credentials_as_pathname}).credentials
end

test "when credentials option is a symbol and return value of a method is a string, it returns the file contents" do
credentials_hash = {project_id: "api-12345"}
assert_equal credentials_hash, Noticed::DeliveryMethods::Fcm.new.assign_args(notification_class: "FcmExample", options: {credentials: :fcm_credentials_as_string}).credentials
end

test "project_id returns the project id value from the credentials" do
assert_equal "api-12345", Noticed::DeliveryMethods::Fcm.new.assign_args(notification_class: "FcmExample", options: {credentials: :fcm_credentials}).project_id
end
Expand Down

0 comments on commit ffa31e7

Please sign in to comment.