40-sessions.t 5.16 KB
Newer Older
1 2 3
#!/usr/bin/env perl -I pl/lib

use Test::More;
4
use JSON::MaybeXS;
5 6 7
use strict;
use Lemonldap::NG::Common::Session;

Xavier Guimard's avatar
Xavier Guimard committed
8 9 10
eval { mkdir 't/sessions' };
`rm -rf t/sessions/*`;
require 't/test-lib.pm';
11 12 13 14 15 16 17 18 19

sub newSession {
    my ( $uid, $ip ) = splice @_;
    my $tmp;
    ok(
        $tmp = Lemonldap::NG::Common::Session->new(
            {
                storageModule        => 'Apache::Session::File',
                storageModuleOptions => {
Xavier Guimard's avatar
Xavier Guimard committed
20
                    Directory => 't/sessions',
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
                    generateModule =>
'Lemonldap::NG::Common::Apache::Session::Generate::SHA256',
                },
            }
        ),
        'Sessions module'
    );
    count(1);
    $tmp->update(
        { ipAddr => $ip, _whatToTrace => $uid, uid => $uid, _utime => time } );
    return $tmp->{id};
}

# Single session access
my @ids;
$ids[0] = newSession( 'dwho',  '127.10.0.1' );
$ids[1] = newSession( 'dwho2', '127.2.0.2' );
38
my $res = &client->jsonResponse("/sessions/global/$ids[0]");
39 40 41 42 43
ok( ( $res->{uid}    and $res->{uid} eq 'dwho' ),          'Uid found' );
ok( ( $res->{ipAddr} and $res->{ipAddr} eq '127.10.0.1' ), 'IP found' );
count(2);

# "All" query
44
$res = &client->jsonResponse("/sessions/global/");
45 46 47 48 49 50 51 52 53
ok( $res->{result} == 1,      'Result code = 1' );
ok( $res->{count} == 2,       'Found 2 sessions' );
ok( @{ $res->{values} } == 2, 'List 2 sessions' );
ok( $res->{values}->[$_]->{session} =~ /^(?:$ids[0]|$ids[1])$/,
    'Good session id' )
  foreach ( 0 .. 1 );
count(5);

# GroupBy query
54
$res = &client->jsonResponse( '/sessions/global', 'groupBy=substr(uid,1)' );
55 56 57 58 59 60 61 62 63
ok( $res->{result} == 1, 'Result code = 1' );
ok( $res->{count} == 1,  'Found 1 entry' );
ok( $res->{values}->[0]->{value} && $res->{values}->[0]->{value} eq 'd',
    'Result match "uid=d"' )
  or print STDERR Dumper($res);
ok( $res->{values}->[0]->{count} == 2, 'Found 2 sessions starting with "d"' );
count(4);

$ids[2] = newSession( 'foo', '127.3.0.3' );
64
$res = &client->jsonResponse( '/sessions/global', 'groupBy=substr(uid,1)' );
65 66 67 68
ok( $res->{count} == 2, 'Found 2 entries' );
count(1);

# Filtered queries
69
$res = &client->jsonResponse( '/sessions/global', 'uid=d*' );
70 71 72 73 74
ok( $res->{count} == 2, 'Found 2 sessions' );
ok( $res->{values}->[$_]->{session} =~ /^(?:$ids[0]|$ids[1])$/,
    'Good session id' )
  foreach ( 0 .. 1 );
count(3);
75
$res = &client->jsonResponse( '/sessions/global', 'uid=f*' );
76 77 78 79 80 81
ok( $res->{count} == 1,                        'Found 1 sessions' );
ok( $res->{values}->[0]->{session} eq $ids[2], 'Good session id' );
count(2);

# DoubleIp
$ids[3] = newSession( 'foo', '127.3.0.4' );
82
$res = &client->jsonResponse( '/sessions/global', 'doubleIp' );
83 84 85 86 87 88 89 90 91
ok( $res->{count} == 1,                    'Found 1 user' );
ok( $res->{values}->[0]->{value} eq 'foo', 'User is foo' );
ok(
    $res->{values}->[0]->{sessions}->[$_]->{session} =~ /^(?:$ids[2]|$ids[3])$/,
    'Good session id'
) foreach ( 0 .. 1 );
count(4);

# New GroupBy query test with 4 sessions
92
$res = &client->jsonResponse( '/sessions/global', 'groupBy=uid' );
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
ok(
    (
              $res->{values}->[0]->{value} eq 'dwho'
          and $res->{values}->[0]->{count} == 1
    ),
    '1st user is dwho'
) or print STDERR Dumper($res);
ok(
    (
              $res->{values}->[1]->{value} eq 'dwho2'
          and $res->{values}->[1]->{count} == 1
    ),
    '2nd user is dwho2'
) or print STDERR Dumper($res);
ok(
    (
              $res->{values}->[2]->{value} eq 'foo'
          and $res->{values}->[2]->{count} == 2
    ),
    '3rd user is foo with 2 sessions'
) or print STDERR Dumper($res);
count(3);

# Ordered queries
117
$res = &client->jsonResponse( '/sessions/global', 'orderBy=uid' );
118 119 120 121 122 123 124
ok( $res->{values}->[0]->{uid} eq 'dwho',  '1st user is dwho' );
ok( $res->{values}->[1]->{uid} eq 'dwho2', '2nd user is dwho2' );
ok( $res->{values}->[2]->{uid} eq 'foo',   '3rd user is foo' );
ok( $res->{values}->[3]->{uid} eq 'foo',   '4rd user is foo' );
count(4);

# IPv4 networks
125
$res = &client->jsonResponse( '/sessions/global', 'groupBy=net4(ipAddr,1)' );
126 127
ok( $res->{count} == 1,                'One A subnet' );
ok( $res->{values}->[0]->{count} == 4, 'All sessions found' );
128
$res = &client->jsonResponse( '/sessions/global', 'groupBy=net4(ipAddr,2)' );
129 130 131 132
ok( $res->{count} == 3,                'Three B subnet' );
ok( $res->{values}->[2]->{count} == 2, 'All sessions found' );
count(4);

133
$res = &client->jsonResponse( '/sessions/global', 'orderBy=net4(ipAddr)' );
134 135 136 137 138 139 140 141 142 143 144 145
ok( $res->{count} == 4,                        '4 sessions ordered' );
ok( $res->{values}->[0]->{session} eq $ids[1], '1st is id[1]' );
ok( $res->{values}->[1]->{session} eq $ids[2], '2nd is id[2]' );
ok( $res->{values}->[2]->{session} eq $ids[3], '3rd is id[3]' );
ok( $res->{values}->[3]->{session} eq $ids[0], '4rd is id[0]' );
count(5);

#print STDERR Dumper($res);

# Delete sessions
foreach (@ids) {
    my $res;
146
    ok( $res = &client->del("/sessions/global/$_"), "Delete $_" );
147
    ok( $res->[0] == 200, 'Result code is 200' );
148
    ok( decode_json( $res->[2]->[0] )->{result} == 1,
149 150 151 152
        'Body is JSON and result==1' );
    count(3);
}

Xavier Guimard's avatar
Xavier Guimard committed
153
opendir D, 't/sessions' or die 'Unknown dir';
154 155 156 157 158
my @files = grep { not /^\./ } readdir D;
ok( @files == 0, "Session directory is empty" );
count(1);

done_testing( count() );
159 160 161

# Remove sessions directory
`rm -rf t/sessions`;