Bug 26635: Add tests
[koha.git] / t / CookieManager.t
1 #!/usr/bin/perl
2 #
3 # Copyright 2022 Rijksmuseum, Koha Development Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI;
22 use Data::Dumper qw(Dumper);
23 use Test::More tests => 3;
24
25 use t::lib::Mocks;
26
27 use C4::Context;
28 use Koha::CookieManager;
29
30 subtest 'new' => sub {
31     plan tests => 3;
32
33     t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, 'just_one' );
34     my $cmgr = Koha::CookieManager->new;
35     is( scalar keys %{$cmgr->{_remove_unless}}, 1, 'one entry' );
36     is( exists $cmgr->{_secure}, 1, 'secure key found' );
37
38     t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, [ 'two', 'entries' ] );
39     $cmgr = Koha::CookieManager->new;
40     is( scalar keys %{$cmgr->{_remove_unless}}, 2, 'two entries' );
41 };
42
43 subtest 'clear_unless' => sub {
44     plan tests => 17;
45
46     t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, [ 'aap', 'noot' ] );
47
48     my $q = CGI->new;
49     my $cmgr = Koha::CookieManager->new;
50
51     my $cookie1 = $q->cookie( -name => 'aap', -value => 'aap', -expires => '+1d' );
52     my $cookie2 = $q->cookie( -name => 'noot', -value => 'noot' );
53     my $cookie3 = $q->cookie( -name => 'wim', -value => q{wim}, -HttpOnly => 1 );
54     my $cookie4 = $q->cookie( -name => 'aap', -value => q{aap2}, -HttpOnly => 1 );
55     my $list = [ $cookie1, $cookie2, $cookie3, $cookie4, 'mies', 'zus' ]; # 4 cookies, 2 names
56
57     # No results expected
58     is( @{$cmgr->clear_unless}, 0, 'Empty list' );
59     is( @{$cmgr->clear_unless( { hash => 1 }, [ 'array' ], $q )}, 0, 'Empty list for invalid arguments' );
60
61     # Pass list, expect 5 cookies (3 cleared, last aap kept)
62     my @rv = @{$cmgr->clear_unless( @$list )};
63     is( @rv, 5, '5 expected' );
64     is( $rv[0]->name, 'noot', '1st cookie' );
65     is( $rv[1]->name, 'wim', '2nd cookie' );
66     is( $rv[2]->name, 'aap', '3rd cookie' );
67     is( $rv[3]->name, 'mies', '4th cookie' );
68     is( $rv[4]->name, 'zus', '5th cookie' );
69     is( $rv[0]->value, q{noot}, 'noot not empty' );
70     is( $rv[1]->value, q{}, 'wim empty' );
71     is( $rv[2]->value, q{aap2}, 'aap not empty' );
72     is( $rv[3]->value, q{}, 'mies empty' );
73     is( $rv[4]->value, q{}, 'zus empty' );
74     is( $rv[1]->httponly, 0, 'cleared wim is not httponly' );
75     is( $rv[2]->httponly, 1, 'aap httponly' );
76
77     # Test with numeric suffix (via regex)
78     t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, [ 'catalogue_editor_\d+' ] );
79     $cmgr = Koha::CookieManager->new;
80     $cookie1 = $q->cookie( -name => 'catalogue_editor_abc', -value => '1', -expires => '+1y' );
81     $cookie2 = $q->cookie( -name => 'catalogue_editor_345', -value => '1', -expires => '+1y' );
82     $cookie3 = $q->cookie( -name => 'catalogue_editor_', -value => '1', -expires => '+1y' );
83     $cookie4 = $q->cookie( -name => 'catalogue_editor_123x', -value => '1', -expires => '+1y' );
84
85     $list = [ $cookie1, $cookie2, $cookie3, $cookie4 ];
86     @rv = @{$cmgr->clear_unless( @$list )};
87     is_deeply( [ map { $_->value ? $_->name : () } @rv ],
88         [ 'catalogue_editor_345' ],
89         'Cookie2 should be found only' );
90
91     # Test with another regex (yes, highly realistic examples :)
92     t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, [ 'next_\w+_number\d{2}_(now|never)' ] );
93     $cmgr = Koha::CookieManager->new;
94     my $cookie5;
95     $cookie1 = $q->cookie( -name => 'next_mynewword_number99_never', -value => '1', -expires => '+1y' ); #fine
96     $cookie2 = $q->cookie( -name => 'prefixed_next_mynewword_number99_never', -value => '1', -expires => '+1y' ); # wrong prefix
97     $cookie3 = $q->cookie( -name => 'next_mynew-word_number99_never', -value => '1', -expires => '+1y' ); # wrong: hyphen in word
98     $cookie4 = $q->cookie( -name => 'mynewword_number999_never', -value => '1', -expires => '+1y' ); # wrong: three digits
99     $cookie5 = $q->cookie( -name => 'next_mynewword_number99_always', -value => '1', -expires => '+1y' ); # wrong: always
100     @rv = @{$cmgr->clear_unless( $cookie1, $cookie2, $cookie3, $cookie4, $cookie5 )};
101     is_deeply( [ map { $_->value ? $_->name : () } @rv ], [ 'next_mynewword_number99_never' ], 'Only cookie1 matched' );
102
103 };
104
105 subtest 'replace_in_list' => sub {
106     plan tests => 13;
107
108     my $q = CGI->new;
109     my $cmgr = Koha::CookieManager->new;
110
111     my $cookie1 = $q->cookie( -name => 'c1', -value => q{c1} );
112     my $cookie2 = $q->cookie( -name => 'c2', -value => q{c2} );
113     my $cookie3 = $q->cookie( -name => 'c3', -value => q{c3} );
114     my $cookie4 = $q->cookie( -name => 'c2', -value => q{c4} ); # name c2 !
115
116     # Unusual arguments (show that $cmgr handles the cookie mocks in Auth.t)
117     my $res = $cmgr->replace_in_list( [ 1, 2, 3 ], 4 );
118     is( @$res, 0, 'No cookies' );
119     $res = $cmgr->replace_in_list( [ 1, 2, 3 ], $cookie1 );
120     is( @$res, 1, 'One cookie added' );
121     is( $res->[0]->name, 'c1', '1st cookie' );
122     $res = $cmgr->replace_in_list( [ $cookie2, 2, 3 ], 4 ); # filter 2,3 and ignore 4
123     is( @$res, 1, 'One cookie found' );
124     is( $res->[0]->name, 'c2', 'c2 found' );
125
126     # Pass c1 c2, add c3
127     $res = $cmgr->replace_in_list( [ $cookie1, $cookie2 ], $cookie3 );
128     is( @$res, 3, 'Returns three' );
129     is( $res->[2]->name, 'c3', '3rd cookie' );
130     is( $res->[2]->value, 'c3', 'value c3' );
131
132     # Pass c1 c2 c3 and replace c2
133     $res = $cmgr->replace_in_list( [ $cookie1, $cookie2, $cookie3 ], $cookie4 );
134     is( @$res, 3, 'Returns three' );
135     is( $res->[0]->name, 'c1', '1st cookie' );
136     is( $res->[1]->name, 'c3', '2nd cookie' );
137     is( $res->[2]->name, 'c2', '3rd cookie' );
138     is( $res->[2]->value, 'c4', 'value replaced' );
139 };