| 1 |
# Test notifications explorer API |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use Test::More; |
| 5 |
use strict; |
| 6 |
use IO::String; |
| 7 |
use JSON qw(from_json); |
| 8 |
|
| 9 |
eval { mkdir 't/notifications' }; |
| 10 |
`rm -rf t/notifications/*`; |
| 11 |
require 't/test-lib.pm'; |
| 12 |
|
| 13 |
# Try to create a notification |
| 14 |
my $notif = |
| 15 |
'{"date":"2099-02-30","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; |
| 16 |
my $res = |
| 17 |
&client->jsonPostResponse( 'notifications/actives', '', |
| 18 |
IO::String->new($notif), |
| 19 |
'application/json', length($notif) ); |
| 20 |
ok( $res->{error} =~ /^Notification not created: Bad date/, |
| 21 |
'Notification not inserted' ); |
| 22 |
count(1); |
| 23 |
|
| 24 |
$notif = |
| 25 |
'{"date":"2099-13-30","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; |
| 26 |
$res = |
| 27 |
&client->jsonPostResponse( 'notifications/actives', '', |
| 28 |
IO::String->new($notif), |
| 29 |
'application/json', length($notif) ); |
| 30 |
ok( $res->{error} =~ /^Notification not created: Bad date/, |
| 31 |
'Notification not inserted' ); |
| 32 |
count(1); |
| 33 |
|
| 34 |
$notif = |
| 35 |
'{"date":"2099-05_12","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}'; |
| 36 |
$res = |
| 37 |
&client->jsonPostResponse( 'notifications/actives', '', |
| 38 |
IO::String->new($notif), |
| 39 |
'application/json', length($notif) ); |
| 40 |
ok( $res->{error} =~ /^Malformed date$/, 'Notification not inserted' ); |
| 41 |
count(1); |
| 42 |
|
| 43 |
$notif = |
| 44 |
'{"date":"2099-12-31","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}", "condition": "1==1"}'; |
| 45 |
$res = |
| 46 |
&client->jsonPostResponse( 'notifications/actives', '', |
| 47 |
IO::String->new($notif), |
| 48 |
'application/json', length($notif) ); |
| 49 |
|
| 50 |
ok( $res->{result}, 'Result is true' ); |
| 51 |
count(1); |
| 52 |
|
| 53 |
# Test "actives" notification display |
| 54 |
displayTests('actives'); |
| 55 |
|
| 56 |
# Mark notification as done |
| 57 |
$notif = '{"done":1}'; |
| 58 |
$res = &client->jsonPutResponse( |
| 59 |
'notifications/actives/dwho_Test', |
| 60 |
'', IO::String->new($notif), |
| 61 |
'application/json', length($notif) |
| 62 |
); |
| 63 |
ok( $res->{result} == 1, 'Result = 1' ); |
| 64 |
|
| 65 |
# Test that notification is not active now |
| 66 |
$res = |
| 67 |
&client->jsonResponse( 'notifications/actives', 'groupBy=substr(uid,1)' ); |
| 68 |
ok( $res->{result} == 1, 'Result = 1' ); |
| 69 |
ok( $res->{count} == 0, 'Count = 0' ); |
| 70 |
count(3); |
| 71 |
|
| 72 |
# Test "done" notifications display |
| 73 |
displayTests('done'); |
| 74 |
|
| 75 |
# Delete notification |
| 76 |
$res = |
| 77 |
&client->_del( |
| 78 |
'notifications/done/dwho_Test_20991231_dwho_VGVzdA==_MT09MQ==.done'); |
| 79 |
$res = &client->jsonResponse( 'notifications/done', 'groupBy=substr(uid,1)' ); |
| 80 |
ok( $res->{result} == 1, 'Result = 1' ); |
| 81 |
ok( $res->{count} == 0, 'Count = 0' ); |
| 82 |
count(2); |
| 83 |
|
| 84 |
#print STDERR Dumper($res); |
| 85 |
|
| 86 |
`rm -f t/notifications/*`; |
| 87 |
|
| 88 |
done_testing( count() ); |
| 89 |
|
| 90 |
sub displayTests { |
| 91 |
my $type = shift; |
| 92 |
$res = |
| 93 |
&client->jsonResponse( "notifications/$type", 'groupBy=substr(uid,1)' ); |
| 94 |
ok( $res->{result} == 1, 'Result = 1' ); |
| 95 |
ok( $res->{count} == 1, 'Count = 1' ); |
| 96 |
ok( $res->{values}->[0]->{value} eq 'd', 'Value is "d"' ); |
| 97 |
count(3); |
| 98 |
|
| 99 |
$res = &client->jsonResponse( "notifications/$type", 'groupBy=uid' ); |
| 100 |
ok( $res->{result} == 1, 'Result = 1' ); |
| 101 |
ok( $res->{count} == 1, 'Count = 1' ); |
| 102 |
ok( $res->{values}->[0]->{value} eq 'dwho', 'Value is "dwho"' ); |
| 103 |
count(3); |
| 104 |
|
| 105 |
$res = &client->jsonResponse( "notifications/$type", 'uid=d*&groupBy=uid' ); |
| 106 |
ok( $res->{result} == 1, 'Result = 1' ); |
| 107 |
ok( $res->{count} == 1, 'Count = 1' ); |
| 108 |
ok( $res->{values}->[0]->{value} eq 'dwho', 'Value is "dwho"' ); |
| 109 |
count(3); |
| 110 |
|
| 111 |
$res = &client->jsonResponse( "notifications/$type", 'uid=d*' ); |
| 112 |
ok( $res->{result} == 1, 'Result = 1' ); |
| 113 |
ok( $res->{count} == 1, 'Count = 1' ); |
| 114 |
ok( $res->{values}->[0]->{uid} eq 'dwho', 'Value is "dwho"' ); |
| 115 |
count(3); |
| 116 |
|
| 117 |
$res = &client->jsonResponse( "notifications/$type", 'uid=dwho' ); |
| 118 |
ok( $res->{result} == 1, 'Result = 1' ); |
| 119 |
ok( $res->{count} == 1, 'Count = 1' ); |
| 120 |
ok( $res->{values}->[0]->{uid} eq 'dwho', 'Value is "dwho"' ); |
| 121 |
count(3); |
| 122 |
|
| 123 |
if ( $type eq 'actives' ) { |
| 124 |
$res = &client->jsonResponse( "notifications/$type/dwho_Test", '' ); |
| 125 |
ok( $res->{result} == 1, 'Result = 1' ); |
| 126 |
ok( $res->{count} == 1, 'Count = 1' ); |
| 127 |
ok( $res = eval { from_json( $res->{notifications}->[0] ) }, |
| 128 |
'Response is JSON' ) |
| 129 |
or print STDERR "Expect JSON, found:\n$res->{notifications}->[0]\n"; |
| 130 |
is_deeply( |
| 131 |
$res, |
| 132 |
{ |
| 133 |
'condition' => '1==1', |
| 134 |
'date' => '2099-12-31', |
| 135 |
'reference' => 'Test', |
| 136 |
'title' => 'Test', |
| 137 |
'uid' => 'dwho' |
| 138 |
}, |
| 139 |
"Notification is as expected" |
| 140 |
); |
| 141 |
|
| 142 |
count(4); |
| 143 |
} |
| 144 |
|
| 145 |
if ( $type eq 'done' ) { |
| 146 |
$res = &client->jsonResponse( "notifications/$type", 'uid=dwho' ); |
| 147 |
ok( |
| 148 |
$res->{values}->[0]->{notification} =~ |
| 149 |
/^\d{8}_dwho_VGVzdA==_MT09MQ==\.done$/, |
| 150 |
'Reference found' |
| 151 |
) or diag Dumper($res); |
| 152 |
my $internal_ref = $res->{values}->[0]->{notification}; |
| 153 |
my $ref = $res->{values}->[0]->{reference}; |
| 154 |
$res = &client->jsonResponse("notifications/$type/$internal_ref"); |
| 155 |
ok( $res->{done} eq $internal_ref, 'Internal reference found' ) |
| 156 |
or diag Dumper($res); |
| 157 |
ok( $res = eval { from_json( $res->{notifications}->[0] ) }, |
| 158 |
'Response is JSON' ) |
| 159 |
or print STDERR "Expect JSON, found:\n$res->{notifications}->[0]\n"; |
| 160 |
is_deeply( |
| 161 |
$res, |
| 162 |
{ |
| 163 |
'condition' => '1==1', |
| 164 |
'date' => '2099-12-31', |
| 165 |
'reference' => 'Test', |
| 166 |
'title' => 'Test', |
| 167 |
'uid' => 'dwho' |
| 168 |
}, |
| 169 |
"Notification is as expected" |
| 170 |
); |
| 171 |
count(4); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
# Remove notifications directory |
| 176 |
`rm -rf t/notifications`; |