| 1 |
# Test 2F API |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use Test::More; |
| 5 |
use strict; |
| 6 |
use JSON; |
| 7 |
use IO::String; |
| 8 |
use Lemonldap::NG::Common::Session; |
| 9 |
|
| 10 |
require 't/test-lib.pm'; |
| 11 |
|
| 12 |
our $_json = JSON->new->allow_nonref; |
| 13 |
|
| 14 |
sub newSession { |
| 15 |
my ( $uid, $loginHistory ) = splice @_; |
| 16 |
my $tmp; |
| 17 |
ok( |
| 18 |
$tmp = Lemonldap::NG::Common::Session->new( { |
| 19 |
storageModule => 'Apache::Session::File', |
| 20 |
storageModuleOptions => { |
| 21 |
Directory => "$main::tmpdir/sessions", |
| 22 |
LockDirectory => "$main::tmpdir/sessions", |
| 23 |
backend => 'Apache::Session::File', |
| 24 |
generateModule => |
| 25 |
'Lemonldap::NG::Common::Apache::Session::Generate::SHA256', |
| 26 |
}, |
| 27 |
} |
| 28 |
), |
| 29 |
'Sessions module' |
| 30 |
); |
| 31 |
count(1); |
| 32 |
ok( |
| 33 |
$tmp->update( { |
| 34 |
"_loginHistory" => $loginHistory, |
| 35 |
_whatToTrace => $uid, |
| 36 |
uid => $uid, |
| 37 |
_session_uid => $uid, |
| 38 |
_utime => time, |
| 39 |
_session_kind => 'Persistent', |
| 40 |
} |
| 41 |
), |
| 42 |
"New session for $uid" |
| 43 |
); |
| 44 |
count(1); |
| 45 |
} |
| 46 |
|
| 47 |
sub check200 { |
| 48 |
my ($res) = splice @_; |
| 49 |
is( $res->[0], 200, "Response code is 200" ); |
| 50 |
count(1); |
| 51 |
return $res; |
| 52 |
} |
| 53 |
|
| 54 |
sub check400 { |
| 55 |
my ($res) = splice @_; |
| 56 |
is( $res->[0], 400, "Response code is 400" ); |
| 57 |
count(1); |
| 58 |
return $res; |
| 59 |
} |
| 60 |
|
| 61 |
sub check404 { |
| 62 |
my ($res) = splice @_; |
| 63 |
is( $res->[0], 404, "Response code is 404" ); |
| 64 |
count(1); |
| 65 |
return $res; |
| 66 |
} |
| 67 |
|
| 68 |
sub getJson { |
| 69 |
my ($res) = splice @_; |
| 70 |
my $json; |
| 71 |
|
| 72 |
ok( $json = from_json( $res->[2]->[0] ), "Got JSON" ); |
| 73 |
count(1); |
| 74 |
return $json; |
| 75 |
} |
| 76 |
|
| 77 |
sub get { |
| 78 |
my ( $path, $query ) = splice @_; |
| 79 |
my ($res); |
| 80 |
ok( $res = &client->_get( "/api/v1/history/$path", $query ), |
| 81 |
"Get request on $path" ); |
| 82 |
count(1); |
| 83 |
return $res; |
| 84 |
} |
| 85 |
|
| 86 |
my $sfaDevices = []; |
| 87 |
my $ret; |
| 88 |
newSession( |
| 89 |
'dwho', |
| 90 |
{ |
| 91 |
"failedLogin" => [ { |
| 92 |
"_utime" => 1677062205, |
| 93 |
"error" => "5", |
| 94 |
"ipAddr" => "10.128.239.1" |
| 95 |
}, |
| 96 |
{ |
| 97 |
"_utime" => 1676452178, |
| 98 |
"error" => "5", |
| 99 |
"ipAddr" => "10.128.239.1" |
| 100 |
}, |
| 101 |
{ |
| 102 |
"_utime" => 1676303145, |
| 103 |
"error" => "5", |
| 104 |
"ipAddr" => "10.128.239.1" |
| 105 |
}, |
| 106 |
{ |
| 107 |
"_utime" => 1676303099, |
| 108 |
"error" => "5", |
| 109 |
"ipAddr" => "10.128.239.1" |
| 110 |
} |
| 111 |
], |
| 112 |
"successLogin" => [ { |
| 113 |
"_utime" => 1677665858, |
| 114 |
"ipAddr" => "10.128.239.1" |
| 115 |
}, |
| 116 |
{ |
| 117 |
"_utime" => 1677665776, |
| 118 |
"error" => "-4", |
| 119 |
"ipAddr" => "10.128.239.1" |
| 120 |
}, |
| 121 |
{ |
| 122 |
"_utime" => 1677665074, |
| 123 |
"ipAddr" => "10.128.239.1" |
| 124 |
}, |
| 125 |
{ |
| 126 |
"_utime" => 1677665047, |
| 127 |
"error" => "-4", |
| 128 |
"ipAddr" => "10.128.239.1" |
| 129 |
}, |
| 130 |
{ |
| 131 |
"_utime" => 1677665000, |
| 132 |
"ipAddr" => "10.128.239.1" |
| 133 |
} |
| 134 |
] |
| 135 |
} |
| 136 |
); |
| 137 |
|
| 138 |
newSession( |
| 139 |
'rtyler', |
| 140 |
{ |
| 141 |
"failedLogin" => [ { |
| 142 |
"_utime" => 1677665858, |
| 143 |
"error" => "5", |
| 144 |
"ipAddr" => "10.128.239.1" |
| 145 |
}, |
| 146 |
{ |
| 147 |
"_utime" => 1676452178, |
| 148 |
"error" => "5", |
| 149 |
"ipAddr" => "10.128.239.1" |
| 150 |
}, |
| 151 |
{ |
| 152 |
"_utime" => 1676303145, |
| 153 |
"error" => "5", |
| 154 |
"ipAddr" => "10.128.239.1" |
| 155 |
}, |
| 156 |
{ |
| 157 |
"_utime" => 1676303099, |
| 158 |
"error" => "5", |
| 159 |
"ipAddr" => "10.128.239.1" |
| 160 |
} |
| 161 |
], |
| 162 |
} |
| 163 |
); |
| 164 |
|
| 165 |
newSession( 'msmith', undef ); |
| 166 |
|
| 167 |
subtest "Get all entries for user" => sub { |
| 168 |
$ret = getJson check200 get("dwho"); |
| 169 |
is( @$ret, 9, "Found 9 entries" ); |
| 170 |
}; |
| 171 |
|
| 172 |
subtest "Type any returns all entries" => sub { |
| 173 |
$ret = getJson check200 get( "dwho", "result=any" ); |
| 174 |
is( @$ret, 9, "Found 9 entries" ); |
| 175 |
}; |
| 176 |
|
| 177 |
subtest "Get all successes for user" => sub { |
| 178 |
$ret = getJson check200 get( "dwho", "result=success" ); |
| 179 |
is( @$ret, 5, "Found 5 entries" ); |
| 180 |
}; |
| 181 |
|
| 182 |
subtest "Get all failures for user" => sub { |
| 183 |
$ret = getJson check200 get( "dwho", "result=failed" ); |
| 184 |
is( @$ret, 4, "Found 4 entries" ); |
| 185 |
}; |
| 186 |
|
| 187 |
subtest "Get last success for user" => sub { |
| 188 |
$ret = getJson check200 get( "dwho/last", "result=success" ); |
| 189 |
is_deeply( |
| 190 |
$ret, |
| 191 |
{ |
| 192 |
'date' => 1677665858, |
| 193 |
'ipAddr' => '10.128.239.1', |
| 194 |
'result' => 'success' |
| 195 |
} |
| 196 |
); |
| 197 |
}; |
| 198 |
|
| 199 |
subtest "Get last failure for user" => sub { |
| 200 |
$ret = getJson check200 get( "dwho/last", "result=failed" ); |
| 201 |
is_deeply( |
| 202 |
$ret, |
| 203 |
{ |
| 204 |
'date' => 1677062205, |
| 205 |
'error' => '5', |
| 206 |
'ipAddr' => '10.128.239.1', |
| 207 |
'result' => 'failed' |
| 208 |
} |
| 209 |
); |
| 210 |
}; |
| 211 |
|
| 212 |
subtest "Get last event for user" => sub { |
| 213 |
$ret = getJson check200 get("dwho/last"); |
| 214 |
is_deeply( |
| 215 |
$ret, |
| 216 |
{ |
| 217 |
'date' => 1677665858, |
| 218 |
'ipAddr' => '10.128.239.1', |
| 219 |
'result' => 'success' |
| 220 |
} |
| 221 |
); |
| 222 |
}; |
| 223 |
|
| 224 |
subtest "Get last event for user" => sub { |
| 225 |
$ret = getJson check200 get("rtyler/last"); |
| 226 |
is_deeply( |
| 227 |
$ret, |
| 228 |
{ |
| 229 |
'date' => 1677665858, |
| 230 |
'error' => '5', |
| 231 |
'ipAddr' => '10.128.239.1', |
| 232 |
'result' => 'failed' |
| 233 |
} |
| 234 |
); |
| 235 |
}; |
| 236 |
|
| 237 |
# User with empty history return empty array |
| 238 |
is_deeply( getJson( check200 get("msmith") ), [] ); |
| 239 |
is_deeply( getJson( check200 get( "msmith", "result=success" ) ), [] ); |
| 240 |
is_deeply( getJson( check200 get( "rtyler", "result=success" ) ), [] ); |
| 241 |
|
| 242 |
# no user returns 404 |
| 243 |
is_deeply( getJson( check404 get "nobody" ), { error => "No such user" } ); |
| 244 |
is_deeply( getJson( check404 get "nobody", "result=success" ), |
| 245 |
{ error => "No such user" } ); |
| 246 |
|
| 247 |
# Last returns a 404 in those cases |
| 248 |
is_deeply( getJson( check404 get "msmith/last" ), |
| 249 |
{ error => "No such event" } ); |
| 250 |
is_deeply( getJson( check404 get "rtyler/last", "result=success" ), |
| 251 |
{ error => "No such event" } ); |
| 252 |
is_deeply( getJson( check404 get "nobody/last" ), |
| 253 |
{ error => "No such event" } ); |
| 254 |
|
| 255 |
# Unknown type |
| 256 |
$ret = getJson check400 get( "toto", "result=unknown" ); |
| 257 |
like( $ret->{error}, qr/Unknown result/ ); |
| 258 |
|
| 259 |
# Unknown subpath |
| 260 |
$ret = getJson check404 get("toto/unknown"); |
| 261 |
like( $ret->{error}, qr/Unknown path/ ); |
| 262 |
|
| 263 |
# Missing UID |
| 264 |
$ret = getJson check404 get(""); |
| 265 |
like( $ret->{error}, qr/Missing user identifier/ ); |
| 266 |
|
| 267 |
done_testing(); |