Add topic and device token tests

This commit is contained in:
Rumperuu 2021-03-25 13:54:14 +00:00
parent 5292540d1d
commit f483cc2818
2 changed files with 239 additions and 0 deletions

109
t/api/device_tokens.t Normal file
View file

@ -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;

130
t/api/topics.t Normal file
View file

@ -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;