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>.
25 use t::lib::TestBuilder;
27 use Test::More tests => 22;
33 my $module = Test::MockModule->new('Mail::Sendmail');
42 use_ok('Koha::Patron::Password::Recovery');
44 my $schema = Koha::Database->new()->schema();
45 $schema->storage->txn_begin();
48 # Start with fresh data
50 my $builder = t::lib::TestBuilder->new;
51 my $borrowernumber1 = '2000000000';
52 my $borrowernumber2 = '2000000001';
53 my $borrowernumber3 = '2000000002';
54 my $userid1 = "I83MFItzRpGPxD3vW0";
55 my $userid2 = "Gh5t43980hfSAOcvne";
56 my $userid3 = "adsfada80hfSAOcvne";
57 my $email1 = $userid1 . '@koha-community.org';
58 my $email2 = $userid2 . '@koha-community.org';
59 my $email3 = $userid3 . '@koha-community.org';
60 my $uuid1 = "ABCD1234";
61 my $uuid2 = "WXYZ0987";
62 my $uuid3 = "LMNO4561";
64 my $patron_category = $builder->build({ source => 'Category' });
65 my $branch = $builder->build({
68 branchreturnpath => $email1,
72 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
74 $schema->resultset('Borrower')->create(
76 borrowernumber => $borrowernumber1,
82 categorycode => $patron_category->{categorycode},
83 branchcode => $branch->{branchcode},
86 $schema->resultset('Borrower')->create(
88 borrowernumber => $borrowernumber2,
94 categorycode => $patron_category->{categorycode},
95 branchcode => $branch->{branchcode},
98 $schema->resultset('Borrower')->create(
100 borrowernumber => $borrowernumber3,
106 categorycode => $patron_category->{categorycode},
107 branchcode => $branch->{branchcode},
111 $schema->resultset('BorrowerPasswordRecovery')->create(
113 borrowernumber => $borrowernumber1,
115 valid_until => DateTime->now( time_zone => C4::Context->tz() )->add( days => 2 )->datetime()
118 $schema->resultset('BorrowerPasswordRecovery')->create(
120 borrowernumber => $borrowernumber2,
122 valid_until => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
125 $schema->resultset('BorrowerPasswordRecovery')->create(
127 borrowernumber => $borrowernumber3,
129 valid_until => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 3 )->datetime()
134 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
136 ############################################################
137 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
138 ############################################################
140 ok( Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
141 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
142 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
144 ######################################################
145 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
146 ######################################################
148 my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
149 my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
150 my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
152 is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
153 is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
154 ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
155 ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
157 ##############################################################
158 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
159 ##############################################################
161 is( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
163 $schema->resultset('BorrowerPasswordRecovery')->create(
165 borrowernumber => $borrowernumber2,
167 valid_until => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
171 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
172 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
174 ###################################################################
175 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
176 ###################################################################
178 $schema->resultset('BorrowerPasswordRecovery')->create(
180 borrowernumber => $borrowernumber3,
182 valid_until => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 3 )->datetime()
186 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 1, "[DeleteExpiredPasswordRecovery] we can delete the unused entry" );
187 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 0, "[DeleteExpiredPasswordRecovery] Returns 0 on a clean table" );
189 ###############################################################
190 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
191 ###############################################################
193 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
196 $success = Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0); }
198 '[SendPasswordRecoveryEmail] expecting fake sendmail';
199 ok( $success == 1, '[SendPasswordRecoveryEmail] Returns 1 on success');
201 my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
202 ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
204 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
205 my $tempuuid1 = $bpr->next->uuid;
208 Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1); }
210 '[SendPasswordRecoveryEmail] expecting fake sendmail';
212 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
213 my $tempuuid2 = $bpr->next->uuid;
215 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
217 ok( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
218 ok( scalar @$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
220 foreach my $letter (@$letters) {
221 ok( $letter->{status} eq 'sent',
222 'Test SendPasswordRecoverEmail sent due to TestBuilder Sender being a valid email address as expected.' );
225 $schema->storage->txn_rollback();