Debian Codegraph @

lemonldap-ng-manager/t/50-notifications-DBI.t

raw
1 # Test notifications explorer API
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use IO::String;
7 use JSON qw(from_json);
8 use Test::More;
9 require 't/test-lib.pm';
10
11 my $count = 0;
12 my $file = 't/notifications.db';
13 my $maintests = 11;
14 my ( $res, $client );
15 eval { unlink $file };
16
17 sub count {
18 my $c = shift;
19 $count += $c if ($c);
20 return $count;
21 }
22
23 SKIP: {
24 eval { require DBI; require DBD::SQLite; };
25 if ($@) {
26 skip 'DBD::SQLite not found', $maintests;
27 }
28
29 my $dbh = DBI->connect("dbi:SQLite:dbname=$file");
30 $dbh->do(
31 'CREATE TABLE notifications (uid text,ref text,date datetime,xml text,cond text,done datetime)'
32 );
33
34 use_ok('Lemonldap::NG::Manager::Cli::Lib');
35 ok(
36 $client = LLNG::Manager::Test->new(
37 ini => {
38 notificationStorage => 'DBI',
39 notificationStorageOptions =>
40 { dbiChain => "dbi:SQLite:dbname=t/notifications.db" },
41 }
42 ),
43 'Client object'
44 );
45
46 # Try to create a notification
47 my $notif =
48 '{"date":"2099-02-30","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}';
49 $res =
50 $client->jsonPostResponse( 'notifications/actives', '',
51 IO::String->new($notif),
52 'application/json', length($notif) );
53 ok( $res->{error} =~ /^Notification not created: Bad date/,
54 'Notification not inserted' );
55
56 $notif =
57 '{"date":"2099-13-30","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}';
58 $res =
59 $client->jsonPostResponse( 'notifications/actives', '',
60 IO::String->new($notif),
61 'application/json', length($notif) );
62 ok( $res->{error} =~ /^Notification not created: Bad date/,
63 'Notification not inserted' );
64
65 $notif =
66 '{"date":"2099-05_12","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}"}';
67 $res =
68 $client->jsonPostResponse( 'notifications/actives', '',
69 IO::String->new($notif),
70 'application/json', length($notif) );
71 ok( $res->{error} =~ /^Malformed date$/, 'Notification not inserted' );
72
73 $notif =
74 '{"date":"2099-12-31","uid":"dwho","reference":"Test","xml":"{\"title\":\"Test\"}", "condition": "1==1"}';
75 $res =
76 $client->jsonPostResponse( 'notifications/actives', '',
77 IO::String->new($notif),
78 'application/json', length($notif) );
79 $notif =
80 '{"date":"2099-12-31","uid":"dwho","reference":"Test2","xml":"{\"title\":\"Test\"}", "condition": "1==1"}';
81 $res =
82 $client->jsonPostResponse( 'notifications/actives', '',
83 IO::String->new($notif),
84 'application/json', length($notif) );
85
86 ok( $res->{result}, 'Result is true' );
87
88 # Test "actives" notification display
89 displayTests('actives');
90
91 # Mark notification as done
92 $notif = '{"done":1}';
93 $res = $client->jsonPutResponse(
94 'notifications/actives/dwho_Test',
95 '', IO::String->new($notif),
96 'application/json', length($notif)
97 );
98 ok( $res->{result} == 1, 'Result = 1' );
99
100 # Test that notification is not active now
101 $res =
102 $client->jsonResponse( 'notifications/actives', 'groupBy=substr(uid,1)' );
103 ok( $res->{result} == 1, 'Result = 1' );
104 ok( $res->{count} == 1, 'Count = 1' );
105
106 # Test "done" notifications display
107 displayTests('done');
108
109 # Delete notification
110 $res = $client->_del('notifications/done/dwho_Test_20991231');
111 $res =
112 $client->jsonResponse( 'notifications/done', 'groupBy=substr(uid,1)' );
113 ok( $res->{result} == 1, 'Result = 1' );
114 ok( $res->{count} == 0, 'Count = 0' ) or diag Dumper($res);
115 }
116
117 eval { unlink $file };
118 count($maintests);
119 done_testing( count() );
120
121 sub displayTests {
122 my $type = shift;
123 my $count = $type eq 'actives' ? 2 : 1;
124
125 $res =
126 $client->jsonResponse( "notifications/$type", 'groupBy=substr(uid,1)' );
127 ok( $res->{result} == 1, 'Result = 1' );
128 ok( $res->{count} == $count, "Count = $count" );
129 ok( $res->{values}->[0]->{value} eq 'd', 'Value is "d"' );
130 count(3);
131
132 $res = $client->jsonResponse( "notifications/$type", 'groupBy=uid' );
133 ok( $res->{result} == 1, 'Result = 1' );
134 ok( $res->{count} == $count, "Count = $count" );
135 ok( $res->{values}->[0]->{value} eq 'dwho', 'Value is "dwho"' );
136 count(3);
137
138 $res = $client->jsonResponse( "notifications/$type", 'uid=d*&groupBy=uid' );
139 ok( $res->{result} == 1, 'Result = 1' );
140 ok( $res->{count} == $count, "Count = $count" );
141 ok( $res->{values}->[0]->{value} eq 'dwho', 'Value is "dwho"' );
142 count(3);
143
144 $res = $client->jsonResponse( "notifications/$type", 'uid=d*' );
145 ok( $res->{result} == 1, 'Result = 1' );
146 ok( $res->{count} == $count, "Count = $count" );
147 ok( $res->{values}->[0]->{uid} eq 'dwho', 'Value is "dwho"' );
148 count(3);
149
150 $res = $client->jsonResponse( "notifications/$type", 'uid=dwho' );
151 ok( $res->{result} == 1, 'Result = 1' );
152 ok( $res->{count} == $count, "Count = $count" );
153 ok( $res->{values}->[0]->{uid} eq 'dwho', 'Value is "dwho"' );
154 count(3);
155
156 if ( $type eq 'actives' ) {
157 $res = $client->jsonResponse( "notifications/$type/dwho_Test", '' );
158 ok( $res->{result} == 1, 'Result = 1' );
159 ok( $res->{count} == 1, 'Count = 1' );
160 ok( $res = eval { from_json( $res->{notifications}->[0] ) },
161 'Response is JSON' )
162 or print STDERR "Expect JSON, found:\n$res->{notifications}->[0]\n";
163 is_deeply(
164 $res,
165 {
166 'condition' => '1==1',
167 'date' => '2099-12-31',
168 'reference' => 'Test',
169 'title' => 'Test',
170 'uid' => 'dwho'
171 },
172 "Notification is as expected"
173 );
174 count(4);
175 }
176
177 if ( $type eq 'done' ) {
178 $res = $client->jsonResponse( "notifications/$type", 'uid=dwho' );
179 ok(
180 $res->{values}->[0]->{notification} =~
181 /^\d{4}-\d{2}-\d{2}#dwho#Test$/,
182 'Reference found'
183 ) or diag Dumper($res);
184 my $internal_ref = $res->{values}->[0]->{notification};
185 my $ref = $res->{values}->[0]->{reference};
186 $internal_ref =~ s/#/_/g; # Fix 2353
187 $res = $client->jsonResponse("notifications/$type/$internal_ref")
188 or diag Dumper($res);
189 ok( $res = eval { from_json( $res->{notifications}->[0] ) },
190 'Response is JSON' )
191 or print STDERR "Expect JSON, found:\n$res->{notifications}->[0]\n";
192 is_deeply(
193 $res,
194 {
195 'condition' => '1==1',
196 'date' => '2099-12-31',
197 'reference' => 'Test',
198 'title' => 'Test',
199 'uid' => 'dwho'
200 },
201 "Notification is as expected"
202 );
203 count(3);
204 }
205 }