Bug 14844: Corrupted storable string. When adding/editing an Item, cookie LastCreated...
[koha.git] / t / Token.t
1 #!/usr/bin/perl
2
3 # tests for Koha::Token
4
5 # Copyright 2016 Rijksmuseum
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 use Modern::Perl;
23 use Test::More tests => 6;
24 use Koha::Token;
25
26 my $tokenizer = Koha::Token->new;
27 is( length( $tokenizer->generate ), 1, "Generate without parameters" );
28 my $token = $tokenizer->generate({ length => 20 });
29 is( length($token), 20, "Token $token has 20 chars" );
30
31 my $id = $tokenizer->generate({ length => 8 });
32 my $secr = $tokenizer->generate({ length => 32 });
33 my $csrftoken = $tokenizer->generate_csrf({ id => $id, secret => $secr });
34 isnt( length($csrftoken), 0, "Token $csrftoken should not be empty" );
35
36 is( $tokenizer->check, undef, "Check without any parameters" );
37 my $result = $tokenizer->check_csrf({
38     id => $id, secret => $secr, token => $csrftoken,
39 });
40 is( $result, 1, "CSRF token verified" );
41
42 $result = $tokenizer->check({
43     type => 'CSRF', id => $id, secret => $secr, token => $token,
44 });
45 isnt( $result, 1, "This token is no CSRF token" );