3 # Copyright 2022 Rijksmuseum, Koha Development Team
5 # This file is part of Koha.
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.
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.
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>.
22 use Data::Dumper qw(Dumper);
23 use Test::More tests => 3;
28 use Koha::CookieManager;
30 subtest 'new' => sub {
33 t::lib::Mocks::mock_config( Koha::CookieManager::ALLOW_LIST_VAR, 'just_one' );
34 my $cmgr = Koha::CookieManager->new;
35 is( scalar keys %{$cmgr->{_remove_allowed}}, 1, 'one entry' );
36 is( exists $cmgr->{_secure}, 1, 'secure key found' );
38 t::lib::Mocks::mock_config( Koha::CookieManager::ALLOW_LIST_VAR, [ 'two', 'entries' ] );
39 $cmgr = Koha::CookieManager->new;
40 is( scalar keys %{$cmgr->{_remove_allowed}}, 2, 'two entries' );
43 subtest 'clear_if_allowed' => sub {
46 t::lib::Mocks::mock_config( Koha::CookieManager::ALLOW_LIST_VAR, [ 'aap', 'noot', 'mies' ] );
49 my $cmgr = Koha::CookieManager->new;
51 my $cookie1 = $q->cookie(
58 my $cookie2 = $q->cookie(
65 my $cookie3 = $q->cookie( -name => 'wim', -value => q{wim}, -HttpOnly => 1 );
66 my $cookie4 = $q->cookie( -name => 'aap', -value => q{aap2} );
67 my $list = [ $cookie1, $cookie2, $cookie3, $cookie4, 'mies', 'zus' ]; # 4 cookies, 2 names
70 is( @{$cmgr->clear_if_allowed}, 0, 'Empty list' );
71 is( @{$cmgr->clear_if_allowed( 'scalar', [], $q )}, 0, 'Empty list for invalid arguments' );
73 # Pass list, expect 4 cookies (3 cleared)
74 my @rv = @{$cmgr->clear_if_allowed( @$list )};
75 is( @rv, 4, 'Four expected' );
76 is( $rv[0]->name, 'aap', 'First cookie' );
77 is( $rv[1]->name, 'noot', '2nd cookie' );
78 is( $rv[2]->name, 'wim', '3rd cookie' );
79 is( $rv[3]->name, 'mies', '4th cookie' );
80 is( $rv[0]->value, q{}, 'aap should be empty' );
81 is( $rv[1]->value, q{}, 'noot should be empty' );
82 is( $rv[2]->value, 'wim', 'wim not empty' );
83 is( $rv[3]->value, q{}, 'mies empty' );
84 is( $rv[0]->httponly, 0, 'cleared aap isnt httponly' );
85 is( $rv[2]->httponly, 1, 'wim still httponly' );
88 subtest 'replace_in_list' => sub {
92 my $cmgr = Koha::CookieManager->new;
94 my $cookie1 = $q->cookie( -name => 'c1', -value => q{c1} );
95 my $cookie2 = $q->cookie( -name => 'c2', -value => q{c2} );
96 my $cookie3 = $q->cookie( -name => 'c3', -value => q{c3} );
97 my $cookie4 = $q->cookie( -name => 'c2', -value => q{c4} ); # name c2 !
99 # Unusual arguments (show that $cmgr handles the cookie mocks in Auth.t)
100 my $res = $cmgr->replace_in_list( [ 1, 2, 3 ], 4 );
101 is( @$res, 0, 'No cookies' );
102 $res = $cmgr->replace_in_list( [ 1, 2, 3 ], $cookie1 );
103 is( @$res, 1, 'One cookie added' );
104 is( $res->[0]->name, 'c1', '1st cookie' );
105 $res = $cmgr->replace_in_list( [ $cookie2, 2, 3 ], 4 ); # filter 2,3 and ignore 4
106 is( @$res, 1, 'One cookie found' );
107 is( $res->[0]->name, 'c2', 'c2 found' );
110 $res = $cmgr->replace_in_list( [ $cookie1, $cookie2 ], $cookie3 );
111 is( @$res, 3, 'Returns three' );
112 is( $res->[2]->name, 'c3', '3rd cookie' );
113 is( $res->[2]->value, 'c3', 'value c3' );
115 # Pass c1 c2 c3 and replace c2
116 $res = $cmgr->replace_in_list( [ $cookie1, $cookie2, $cookie3 ], $cookie4 );
117 is( @$res, 3, 'Returns three' );
118 is( $res->[0]->name, 'c1', '1st cookie' );
119 is( $res->[1]->name, 'c3', '2nd cookie' );
120 is( $res->[2]->name, 'c2', '3rd cookie' );
121 is( $res->[2]->value, 'c4', 'value replaced' );