3 # This file is part of Koha.
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.
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.
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>.
26 use t::lib::TestBuilder;
28 use Test::More tests => 22;
34 my $module = Test::MockModule->new('Mail::Sendmail');
43 use_ok('Koha::Patron::Password::Recovery');
45 my $schema = Koha::Database->new()->schema();
46 $schema->storage->txn_begin();
48 my $dbh = C4::Context->dbh;
49 $dbh->{RaiseError} = 1;
52 # Start with fresh data
54 my $builder = t::lib::TestBuilder->new;
55 my $borrowernumber1 = '2000000000';
56 my $borrowernumber2 = '2000000001';
57 my $borrowernumber3 = '2000000002';
58 my $userid1 = "I83MFItzRpGPxD3vW0";
59 my $userid2 = "Gh5t43980hfSAOcvne";
60 my $userid3 = "adsfada80hfSAOcvne";
61 my $email1 = $userid1 . '@koha-community.org';
62 my $email2 = $userid2 . '@koha-community.org';
63 my $email3 = $userid3 . '@koha-community.org';
64 my $uuid1 = "ABCD1234";
65 my $uuid2 = "WXYZ0987";
66 my $uuid3 = "LMNO4561";
68 my $patron_category = $builder->build({ source => 'Category' });
69 my $branch = $builder->build({
72 branchreturnpath => $email1,
76 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
78 $schema->resultset('Borrower')->create(
80 borrowernumber => $borrowernumber1,
86 categorycode => $patron_category->{categorycode},
87 branchcode => $branch->{branchcode},
90 $schema->resultset('Borrower')->create(
92 borrowernumber => $borrowernumber2,
98 categorycode => $patron_category->{categorycode},
99 branchcode => $branch->{branchcode},
102 $schema->resultset('Borrower')->create(
104 borrowernumber => $borrowernumber3,
110 categorycode => $patron_category->{categorycode},
111 branchcode => $branch->{branchcode},
115 $schema->resultset('BorrowerPasswordRecovery')->create(
117 borrowernumber => $borrowernumber1,
119 valid_until => dt_from_string()->add( days => 2 )->datetime()
122 $schema->resultset('BorrowerPasswordRecovery')->create(
124 borrowernumber => $borrowernumber2,
126 valid_until => dt_from_string()->subtract( days => 2 )->datetime()
129 $schema->resultset('BorrowerPasswordRecovery')->create(
131 borrowernumber => $borrowernumber3,
133 valid_until => dt_from_string()->subtract( days => 3 )->datetime()
138 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
140 ############################################################
141 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
142 ############################################################
144 ok( Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
145 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
146 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
148 ######################################################
149 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
150 ######################################################
152 my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
153 my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
154 my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
156 is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
157 is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
158 ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
159 ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
161 ##############################################################
162 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
163 ##############################################################
165 is( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
167 $schema->resultset('BorrowerPasswordRecovery')->create(
169 borrowernumber => $borrowernumber2,
171 valid_until => dt_from_string()->subtract( days => 2 )->datetime()
175 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
176 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
178 ###################################################################
179 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
180 ###################################################################
182 $schema->resultset('BorrowerPasswordRecovery')->create(
184 borrowernumber => $borrowernumber3,
186 valid_until => dt_from_string()->subtract( days => 3 )->datetime()
190 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 1, "[DeleteExpiredPasswordRecovery] we can delete the unused entry" );
191 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 0, "[DeleteExpiredPasswordRecovery] Returns 0 on a clean table" );
193 ###############################################################
194 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
195 ###############################################################
197 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
200 $success = Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0); }
202 '[SendPasswordRecoveryEmail] expecting fake sendmail';
203 ok( $success == 1, '[SendPasswordRecoveryEmail] Returns 1 on success');
205 my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
206 ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
208 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
209 my $tempuuid1 = $bpr->next->uuid;
212 Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1); }
214 '[SendPasswordRecoveryEmail] expecting fake sendmail';
216 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
217 my $tempuuid2 = $bpr->next->uuid;
219 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
221 ok( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
222 ok( scalar @$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
224 foreach my $letter (@$letters) {
225 ok( $letter->{status} eq 'sent',
226 'Test SendPasswordRecoverEmail sent due to TestBuilder Sender being a valid email address as expected.' );
229 $schema->storage->txn_rollback();