File: Apache-Session-MongoDB.t

package info (click to toggle)
libapache-session-mongodb-perl 0.23-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: perl: 141; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 4,313 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Before 'make install' is performed this script should be runnable with
# 'make test'. After 'make install' it should work as 'perl Apache-Session-MongoDB.t'

#########################

# change 'tests => 1' to 'tests => last_test_to_print';

use strict;
use warnings;
use utf8;

use Test::More tests => 24;
BEGIN { use_ok('Apache::Session::MongoDB') }

#########################

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.

SKIP: {

    unless ( defined $ENV{MONGODB_SERVER} ) {
        skip 'MONGODB_SERVER is not set', 23;
    }
    my %h;
    my $args = { host => $ENV{MONGODB_SERVER} };

    ok( tie( %h, 'Apache::Session::MongoDB', undef, $args ), 'New object' );

    my $id;
    ok( $id = $h{_session_id}, '_session_id is defined' );
    $h{some}         = 'data';
    $h{utf8}         = 'éàèœ';
    $h{'dotted.key'} = 'test';
    $h{'dollar$key'} = 'test';

    untie %h;

    my %h2;
    ok(
        tie(
            %h2, 'Apache::Session::MongoDB',
            $id, { host => $ENV{MONGODB_SERVER} }
        ),
        'Access to previous session'
    );

    ok( $h2{some} eq 'data',         'Find data' );
    ok( $h2{utf8} eq 'éàèœ',     'UTF string' );
    ok( $h2{'dotted.key'} eq 'test', 'Dotted key' );
    ok( $h2{'dollar$key'} eq 'test', 'Dollar key' );
    Apache::Session::MongoDB->get_key_from_all_sessions($args);

    #binmode(STDERR, ":utf8");
    #print STDERR $h2{utf8}."\n";

    # Create a few sessions to test deleteIfLowerThan
    my @delSessions;
    push @delSessions,
      newsession( $args, type => "persistent", ttl => 100 ),
      newsession( $args, type => "persistent", ttl => 10 ),
      newsession( $args, type => "temporary",  ttl => 100 ),
      newsession( $args, type => "temporary",  ttl => 10 ),
      newsession( $args, type => "temporary",  ttl => 100, actttl => 10 ),
      newsession( $args, type => "temporary",  ttl => 10 );

    is(
        keys
          %{ Apache::Session::MongoDB->searchOn( $args, "type", "persistent" )
          },
        2,
        "Check correct number of permanent sessions"
    );
    is(
        keys
          %{ Apache::Session::MongoDB->searchOn( $args, "type", "temporary" ) },
        4,
        "check correct number of temp sessions"
    );

    my ( $status, $count ) = Apache::Session::MongoDB->deleteIfLowerThan(
        $args,
        {
            not => { 'type' => 'persistent' },
            or  => {
                ttl    => 50,
                actttl => 50,
            }
        }
    );
    is( $status, 1, "reported success" );
    is( $count,  3, "3 sessions deleted" );

    # Make sure success is correctly returned as a scalar when no job is done
    $status = Apache::Session::MongoDB->deleteIfLowerThan(
        $args,
        {
            not => { 'type' => 'persistent' },
            or  => {
                ttl    => 50,
                actttl => 50,
            }
        }
    );
    is( $status, 1, "Status is OK" );

    is(
        keys
          %{ Apache::Session::MongoDB->searchOn( $args, "type", "persistent" )
          },
        2,
        "Check correct number of permanent sessions"
    );
    is(
        keys
          %{ Apache::Session::MongoDB->searchOn( $args, "type", "temporary" ) },
        1,
        "check correct number of temp sessions"
    );

    # Delete sessions
    for (@delSessions) {
        my %h;
        eval {
            tie( %h, 'Apache::Session::MongoDB', $_, $args );
            tied(%h)->delete;
        }
    }

    ok( ( tied(%h2)->delete or 1 ), 'Delete session' );

    unless ( defined $ENV{MONGODB_USER} and defined $ENV{MONGODB_DB_NAME} ) {

        skip 'MONGODB_USER and MONGODB_DB_NAME are not set', 2;
    }
    for my $w (qw(db_name username password)) {
        $args->{$w} = $ENV{ "MONGODB_" . uc($w) };
    }
    ok( tie( %h, 'Apache::Session::MongoDB', undef, $args ),
        'Authentified object' );
    ok( ( tied(%h)->delete or 1 ), 'Delete session' );
}

sub newsession {
    my ( $args, %data ) = @_;
    my %h;
    ok( tie( %h, 'Apache::Session::MongoDB', undef, $args ), 'New object' );
    for ( keys %data ) {
        $h{$_} = $data{$_};
    }
    my $id = $h{_session_id};
    untie(%h);
    return $id;
}