Bug 18025 - Expired password recovery links cause sql crash
[koha.git] / t / db_dependent / Passwordrecovery.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use C4::Context;
21 use C4::Letters;
22 use Koha::Database;
23 use Koha::Patrons;
24
25 use Test::More tests => 18;
26
27 use_ok('Koha::Patron::Password::Recovery');
28
29 my $schema = Koha::Database->new()->schema();
30 $schema->storage->txn_begin();
31
32 my $dbh = C4::Context->dbh;
33 $dbh->{RaiseError} = 1;
34
35 #
36 # Start with fresh data
37 #
38
39 my $borrowernumber1 = '2000000000';
40 my $borrowernumber2 = '2000000001';
41 my $borrowernumber3 = '2000000002';
42 my $userid1 = "I83MFItzRpGPxD3vW0";
43 my $userid2 = "Gh5t43980hfSAOcvne";
44 my $userid3 = "adsfada80hfSAOcvne";
45 my $email1  = $userid1 . '@koha-community.org';
46 my $email2  = $userid2 . '@koha-community.org';
47 my $email3  = $userid3 . '@koha-community.org';
48 my $uuid1   = "ABCD1234";
49 my $uuid2   = "WXYZ0987";
50 my $uuid3   = "LMNO4561";
51
52 my $categorycode = 'S'; #  staff
53 my $branch       = $schema->resultset('Branch')->first(); #  legit branch from your db
54
55 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
56
57 $schema->resultset('Borrower')->create(
58     {
59         borrowernumber  => $borrowernumber1,
60         surname         => '',
61         address         => '',
62         city            => '',
63         userid          => $userid1,
64         email           => $email1,
65         categorycode    => $categorycode,
66         branchcode      => $branch,
67     }
68 );
69 $schema->resultset('Borrower')->create(
70     {
71         borrowernumber  => $borrowernumber2,
72         surname         => '',
73         address         => '',
74         city            => '',
75         userid          => $userid2,
76         email           => $email2,
77         categorycode    => $categorycode,
78         branchcode      => $branch,
79     }
80 );
81 $schema->resultset('Borrower')->create(
82     {
83         borrowernumber => $borrowernumber3,
84         surname        => '',
85         address        => '',
86         city           => '',
87         userid         => $userid3,
88         email          => $email3,
89         categorycode   => $categorycode,
90         branchcode     => $branch,
91     }
92 );
93
94 $schema->resultset('BorrowerPasswordRecovery')->create(
95     {
96         borrowernumber => $borrowernumber1,
97         uuid           => $uuid1,
98         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->add( days => 2 )->datetime()
99     }
100 );
101 $schema->resultset('BorrowerPasswordRecovery')->create(
102     {
103         borrowernumber => $borrowernumber2,
104         uuid           => $uuid2,
105         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
106     }
107 );
108 $schema->resultset('BorrowerPasswordRecovery')->create(
109     {
110         borrowernumber => $borrowernumber3,
111         uuid           => $uuid3,
112         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 3 )->datetime()
113     }
114 );
115
116
117 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
118
119 ############################################################
120 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
121 ############################################################
122
123 ok(   Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
124 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
125 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
126
127 ######################################################
128 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
129 ######################################################
130
131 my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
132 my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
133 my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
134
135 is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
136 is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
137 ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
138 ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
139
140 ##############################################################
141 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
142 ##############################################################
143
144 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1) == 2, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
145
146 $schema->resultset('BorrowerPasswordRecovery')->create(
147     {
148         borrowernumber => $borrowernumber2,
149         uuid           => $uuid2,
150         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
151     }
152 );
153
154 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
155 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
156
157 ###################################################################
158 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
159 ###################################################################
160
161 $schema->resultset('BorrowerPasswordRecovery')->create(
162     {
163         borrowernumber => $borrowernumber3,
164         uuid           => $uuid3,
165         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 3 )->datetime()
166     }
167 );
168
169 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 1, "[DeleteExpiredPasswordRecovery] we can delete the unused entry" );
170 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 0, "[DeleteExpiredPasswordRecovery] Returns 0 on a clean table" );
171
172 ###############################################################
173 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
174 ###############################################################
175
176 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
177 ok( Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0) == 1, "[SendPasswordRecoveryEmail] Returns 1 on success" );
178 my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
179 ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
180
181 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
182 my $tempuuid1 = $bpr->next->uuid;
183
184 Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1);
185
186 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
187 my $tempuuid2 = $bpr->next->uuid;
188
189 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
190
191 ok( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
192 ok( scalar @$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
193
194 $schema->storage->txn_rollback();
195
196 1;