From f483cc2818c840db2b9f3d8c4e33a1b9510ef6e3 Mon Sep 17 00:00:00 2001 From: Rumperuu Date: Thu, 25 Mar 2021 13:54:14 +0000 Subject: [PATCH] Add topic and device token tests --- t/api/device_tokens.t | 109 +++++++++++++++++++++++++++++++++++ t/api/topics.t | 130 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100644 t/api/device_tokens.t create mode 100644 t/api/topics.t diff --git a/t/api/device_tokens.t b/t/api/device_tokens.t new file mode 100644 index 0000000..1e80ccc --- /dev/null +++ b/t/api/device_tokens.t @@ -0,0 +1,109 @@ +use Mojo::Base -strict; + +use FindBin qw/ $Bin /; + +use Test::More; +use Mojo::JSON; +use Test::Pear::LocalLoop; +use DateTime; + +my $framework = Test::Pear::LocalLoop->new( + etc_dir => "$Bin/../etc", +); +$framework->install_fixtures('users'); + +my $t = $framework->framework; +my $schema = $t->app->schema; + +my $session_key = $framework->login({ + email => 'test1@example.com', + password => 'abc123', +}); + +my $token = 'd-ukAXXVWudTtDg1q2kHY6:APA91bEfTE3VGB0EjDuVA0QX5XMTrQU4szYWv64LpV9_VUD4zfL7SKEKLd0gnm0yPPPcWaol-PcADVkfXQCmQKLMhVnqTSVs1MzGv0j_6bpWb0Rrqnv63umFbv99jZV8rEgIfSjsbMki'; + +## Device Tokens + +#No JSON sent +$t->post_ok('/api/device-token/check') + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/JSON is missing/i); + +#Empty JSON +$t->post_ok('/api/device-token/check' => json => {}) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false); + +#No session key +$t->post_ok('/api/device-token/check' => json => { + token => $token + }) + ->status_is(401) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/Invalid Session/); + +#Non-existent token +$t->post_ok('/api/device-token/check' => json => { + session_key => $session_key, + token => $token + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/exists', Mojo::JSON->false); + +#TODO: add a check for a real token + +#No JSON sent +$t->post_ok('/api/device-token/add') + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/JSON is missing/i); + +#Empty JSON +$t->post_ok('/api/device-token/add' => json => {}) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false); + +#No session key +$t->post_ok('/api/device-token/add' => json => { + token => $token, + email => 'test@test.com' + }) + ->status_is(401) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/Invalid Session/); + +#Non-existent token +$t->post_ok('/api/device-token/add' => json => { + session_key => $session_key, + token => $token, + email => 'test1@example.com' + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true); + +#TODO: add a check for a real token + +#No JSON sent +$t->post_ok('/api/device-tokens') + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/JSON is missing/i); + +#Empty JSON +$t->post_ok('/api/device-tokens' => json => {}) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false); + +#Non-existent token +$t->post_ok('/api/device-tokens' => json => { + session_key => $session_key, + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true); + +#TODO: add a check for a real token + +$framework->logout( $session_key ); + +done_testing; diff --git a/t/api/topics.t b/t/api/topics.t new file mode 100644 index 0000000..044b9c6 --- /dev/null +++ b/t/api/topics.t @@ -0,0 +1,130 @@ +use Mojo::Base -strict; + +use FindBin qw/ $Bin /; + +use Test::More; +use Mojo::JSON; +use Test::Pear::LocalLoop; +use DateTime; + +my $framework = Test::Pear::LocalLoop->new( + etc_dir => "$Bin/../etc", +); +$framework->install_fixtures('users'); + +my $t = $framework->framework; +my $schema = $t->app->schema; + +my $session_key = $framework->login({ + email => 'org@example.com', + password => 'abc123', +}); + +## Topics + +#No JSON sent +$t->post_ok('/api/topic/add') + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/JSON is missing/i); + +#Empty JSON +$t->post_ok('/api/topic/add' => json => {}) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false); + +#No session key +$t->post_ok('/api/topic/add' => json => { + topic => 'test', + }) + ->status_is(401) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/Invalid Session/); + +#Create new topic +$t->post_ok('/api/topic/add' => json => { + session_key => $session_key, + topic => 'test', + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true); + +#No JSON sent +$t->post_ok('/api/topics') + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/JSON is missing/i); + +#Empty JSON +$t->post_ok('/api/topics' => json => {}) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false); + +#Get all topics +$t->post_ok('/api/topics' => json => { + session_key => $session_key + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true); + +#No JSON sent +$t->post_ok('/api/topics/subscriptions') + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/JSON is missing/i); + +#Empty JSON +$t->post_ok('/api/topics/subscriptions' => json => {}) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false); + +#Get all subscriptions +$t->post_ok('/api/topics/subscriptions' => json => { + session_key => $session_key, + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true); + +#No JSON sent +$t->post_ok('/api/topics/update') + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/JSON is missing/i); + +#Empty JSON +$t->post_ok('/api/topics/update' => json => {}) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false); + +#No session key +$t->post_ok('/api/topics/update' => json => { + topicSubscriptions => [ + { + id => 1, + name => 'test', + isSubscribed => Mojo::JSON->true, + } + ], + }) + ->status_is(401) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/Invalid Session/); + +#Create new topic +$t->post_ok('/api/topics/update' => json => { + session_key => $session_key, + topicSubscriptions => [ + { + id => 1, + name => 'test', + isSubscribed => Mojo::JSON->true, + } + ], + }) + ->status_is(200)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true); + + +$framework->logout( $session_key ); + +done_testing;