Skip to content

Commit 2c1d792

Browse files
committed
Remove validation code
1 parent 40a2f64 commit 2c1d792

File tree

2 files changed

+1
-195
lines changed

2 files changed

+1
-195
lines changed

lib/pusher/native_notification/client.rb

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ class Client
55

66
API_PREFIX = "server_api"
77
API_VERSION = "v1"
8-
GCM_TTL = 241920
9-
RESTRICTED_GCM_PAYLOAD_KEYS = [:to, :registration_ids]
10-
WEBHOOK_LEVELS = ["DEBUG", "INFO"]
118

129
def initialize(app_id, host, scheme, pusher_client)
1310
@app_id = app_id
@@ -45,69 +42,15 @@ def notify(interests, data = {})
4542
# @return [String]
4643
def payload(interests, data)
4744
interests = Array(interests).map(&:to_s)
48-
4945
raise Pusher::Error, "Too many interests provided" if interests.length > 1
50-
51-
data = deep_symbolize_keys!(data)
52-
validate_payload(data)
53-
54-
data.merge!(interests: interests)
55-
46+
data = deep_symbolize_keys!(data).merge(interests: interests)
5647
MultiJson.encode(data)
5748
end
5849

5950
def url(path = nil)
6051
URI.parse("#{@scheme}://#{@host}/#{API_PREFIX}/#{API_VERSION}/apps/#{@app_id}#{path}")
6152
end
6253

63-
# Validate payload
64-
# `time_to_live` -> value b/w 0 and 241920
65-
# If the `notification` key is provided, ensure
66-
# that there is an accompanying `title` and `icon`
67-
# field
68-
def validate_payload(payload)
69-
unless (payload.has_key?(:apns) || payload.has_key?(:gcm))
70-
raise Pusher::Error, "GCM or APNS data must be provided"
71-
end
72-
73-
if (gcm_payload = payload[:gcm])
74-
# Restricted keys
75-
RESTRICTED_GCM_PAYLOAD_KEYS.each { |k| gcm_payload.delete(k) }
76-
if (ttl = gcm_payload[:time_to_live])
77-
78-
if ttl.to_i < 0 || ttl.to_i > GCM_TTL
79-
raise Pusher::Error, "Time to live must be between 0 and 241920 (4 weeks)"
80-
end
81-
end
82-
83-
# If the notification key is provided
84-
# validate the `icon` and `title`keys
85-
if (notification = gcm_payload[:notification])
86-
notification_title, notification_icon = notification.values_at(:title, :icon)
87-
88-
if (!notification_title || notification_title.empty?)
89-
raise Pusher::Error, "Notification title is a required field"
90-
end
91-
92-
if (!notification_icon || notification_icon.empty?)
93-
raise Pusher::Error, "Notification icon is a required field"
94-
end
95-
end
96-
end
97-
98-
if (webhook_url = payload[:webhook_url])
99-
raise Pusher::Error, "Webhook url is invalid" unless webhook_url =~ /\A#{URI::regexp(['http', 'https'])}\z/
100-
end
101-
102-
if (webhook_level = payload[:webhook_level])
103-
raise Pusher::Error, "Webhook level cannot be used without a webhook url" if !payload.has_key?(:webhook_url)
104-
105-
unless WEBHOOK_LEVELS.include?(webhook_level.upcase)
106-
raise Pusher::Error, "Webhook level must either be INFO or DEBUG"
107-
end
108-
end
109-
end
110-
11154
# Symbolize all keys in the hash recursively
11255
def deep_symbolize_keys!(hash)
11356
hash.keys.each do |k|

spec/client_spec.rb

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,6 @@
550550
expect(@client.notification_host).to eq(@client.notification_client.host)
551551
end
552552

553-
it "should raise an error if the gcm or apns key isn't provided in the payload" do
554-
expect { @client.notify(["test"], { foo: "bar" }) }.to raise_error(Pusher::Error)
555-
end
556-
557553
it "should raise an error if more than one interest is provided" do
558554
payload = {
559555
gcm: {
@@ -567,70 +563,6 @@
567563
expect { @client.notify(["test1", "test2"], payload) }.to raise_error(Pusher::Error)
568564
end
569565

570-
it "should raise an error if the notification hash is missing the title field" do
571-
payload = {
572-
gcm: {
573-
notification: {
574-
icon: "someicon"
575-
}
576-
}
577-
}
578-
579-
expect{ @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
580-
end
581-
582-
it "should raise an error if the notification title is empty" do
583-
payload = {
584-
gcm: {
585-
notification: {
586-
title: "",
587-
icon: "myicon"
588-
}
589-
}
590-
}
591-
592-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
593-
end
594-
595-
it "should raise an error if the notification hash is missing the icon field" do
596-
payload = {
597-
gcm: {
598-
notification: {
599-
title: "sometitle"
600-
}
601-
}
602-
}
603-
604-
expect{ @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
605-
end
606-
607-
it "should raise an error if the notification icon is empty" do
608-
payload = {
609-
gcm: {
610-
notification: {
611-
title: "title",
612-
icon: ""
613-
}
614-
}
615-
}
616-
617-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
618-
end
619-
620-
it "should raise an error if the ttl field is provided and has an illegal value" do
621-
payload = {
622-
gcm: {
623-
time_to_live: 98091283,
624-
notification: {
625-
title: "title",
626-
icon: "icon",
627-
}
628-
}
629-
}
630-
631-
expect{ @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
632-
end
633-
634566
it "should send a request to the notifications endpoint" do
635567
notification_host_regexp = %r{nativepush-cluster1.pusher.com}
636568
payload = {
@@ -656,75 +588,6 @@
656588
res = @client.notify(["test"], payload)
657589
expect(res).to eq({foo: "bar"})
658590
end
659-
660-
it "should delete restricted gcm keys before sending a notification" do
661-
notification_host_regexp = %r{nativepush-cluster1.pusher.com}
662-
payload = {
663-
interests: ["test"],
664-
gcm: {
665-
notification: {
666-
title: "Hello",
667-
icon: "icon",
668-
}
669-
}
670-
}
671-
672-
stub_request(
673-
:post,
674-
notification_host_regexp,
675-
).with(
676-
body: MultiJson.encode(payload)
677-
).to_return({
678-
:status => 200,
679-
:body => MultiJson.encode({ :foo => "bar" })
680-
})
681-
682-
payload[:gcm].merge!(to: "blah", registration_ids: ["reg1", "reg2"])
683-
@client.notify(["test"], payload)
684-
end
685-
686-
it "should raise an error for an invalid webhook url field" do
687-
payload = {
688-
gcm: {
689-
notification: {
690-
title: "Hello",
691-
icon: "icon"
692-
}
693-
},
694-
webhook_url: "totallyinvalid"
695-
}
696-
697-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
698-
end
699-
700-
it "should raise an error if the webhook level is not supported" do
701-
payload = {
702-
gcm: {
703-
notification: {
704-
title: "Hello",
705-
icon: "icon"
706-
}
707-
},
708-
webhook_url: "http://test.com/wh",
709-
webhook_level: "meh"
710-
}
711-
712-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
713-
end
714-
715-
it "should raise an error if the webhook level is used without the webhook url" do
716-
payload = {
717-
gcm: {
718-
notification: {
719-
title: "Hello",
720-
icon: "icon"
721-
}
722-
},
723-
webhook_level: "meh"
724-
}
725-
726-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
727-
end
728591
end
729592
end
730593

0 commit comments

Comments
 (0)