Skip to content

Commit 3164268

Browse files
committed
Add tests for payload validation
1 parent 89bd01c commit 3164268

File tree

1 file changed

+117
-3
lines changed

1 file changed

+117
-3
lines changed

spec/client_spec.rb

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,20 +551,134 @@
551551
end
552552

553553
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)
554+
expect { @client.notify(["test"], { foo: "bar" }) }.to raise_error(Pusher::Error)
555+
end
556+
557+
it "should raise an error if more than one interest is provided" do
558+
payload = {
559+
gcm: {
560+
notification: {
561+
title: "Hello",
562+
icon: "icon",
563+
}
564+
}
565+
}
566+
567+
expect { @client.notify(["test1", "test2"], payload) }.to raise_error(Pusher::Error)
568+
end
569+
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)
555632
end
556633

557634
it "should send a request to the notifications endpoint" do
558635
notification_host_regexp = %r{hedwig-staging.herokuapp.com}
636+
payload = {
637+
interests: ["test"],
638+
gcm: {
639+
notification: {
640+
title: "Hello",
641+
icon: "icon",
642+
}
643+
}
644+
}
645+
646+
stub_request(
647+
:post,
648+
notification_host_regexp,
649+
).with(
650+
body: MultiJson.encode(payload)
651+
).to_return({
652+
:status => 200,
653+
:body => MultiJson.encode({ :foo => "bar" })
654+
})
655+
656+
@client.notify(["test"], payload)
657+
end
658+
659+
it "should delete restricted keys before sending a notification" do
660+
notification_host_regexp = %r{hedwig-staging.herokuapp.com}
661+
payload = {
662+
interests: ["test"],
663+
gcm: {
664+
notification: {
665+
title: "Hello",
666+
icon: "icon",
667+
}
668+
}
669+
}
670+
559671
stub_request(
560672
:post,
561673
notification_host_regexp,
674+
).with(
675+
body: MultiJson.encode(payload)
562676
).to_return({
563677
:status => 200,
564-
:body => MultiJson.encode({ :foo => 'bar' })
678+
:body => MultiJson.encode({ :foo => "bar" })
565679
})
566680

567-
@client.notify(["test"], { gcm: { foo: 'bar' } })
681+
@client.notify(["test"], payload.merge!(to: "blah", registration_ids: ["reg1", "reg2"]))
568682
end
569683
end
570684
end

0 commit comments

Comments
 (0)