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();
49 # Start with fresh data
51 my $builder = t
::lib
::TestBuilder
->new;
52 my $borrowernumber1 = '2000000000';
53 my $borrowernumber2 = '2000000001';
54 my $borrowernumber3 = '2000000002';
55 my $userid1 = "I83MFItzRpGPxD3vW0";
56 my $userid2 = "Gh5t43980hfSAOcvne";
57 my $userid3 = "adsfada80hfSAOcvne";
58 my $email1 = $userid1 . '@koha-community.org';
59 my $email2 = $userid2 . '@koha-community.org';
60 my $email3 = $userid3 . '@koha-community.org';
61 my $uuid1 = "ABCD1234";
62 my $uuid2 = "WXYZ0987";
63 my $uuid3 = "LMNO4561";
65 my $patron_category = $builder->build({ source
=> 'Category' });
66 my $branch = $builder->build({
69 branchreturnpath
=> $email1,
73 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
75 $schema->resultset('Borrower')->create(
77 borrowernumber
=> $borrowernumber1,
83 categorycode
=> $patron_category->{categorycode
},
84 branchcode
=> $branch->{branchcode
},
87 $schema->resultset('Borrower')->create(
89 borrowernumber
=> $borrowernumber2,
95 categorycode
=> $patron_category->{categorycode
},
96 branchcode
=> $branch->{branchcode
},
99 $schema->resultset('Borrower')->create(
101 borrowernumber
=> $borrowernumber3,
107 categorycode
=> $patron_category->{categorycode
},
108 branchcode
=> $branch->{branchcode
},
112 $schema->resultset('BorrowerPasswordRecovery')->create(
114 borrowernumber
=> $borrowernumber1,
116 valid_until
=> dt_from_string
()->add( days
=> 2 )->datetime()
119 $schema->resultset('BorrowerPasswordRecovery')->create(
121 borrowernumber
=> $borrowernumber2,
123 valid_until
=> dt_from_string
()->subtract( days
=> 2 )->datetime()
126 $schema->resultset('BorrowerPasswordRecovery')->create(
128 borrowernumber
=> $borrowernumber3,
130 valid_until
=> dt_from_string
()->subtract( days
=> 3 )->datetime()
135 can_ok
( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
137 ############################################################
138 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
139 ############################################################
141 ok
( Koha
::Patron
::Password
::Recovery
::ValidateBorrowernumber
($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
142 ok
( ! Koha
::Patron
::Password
::Recovery
::ValidateBorrowernumber
($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
143 ok
( ! Koha
::Patron
::Password
::Recovery
::ValidateBorrowernumber
(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
145 ######################################################
146 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
147 ######################################################
149 my ($bnum1, $uname1) = Koha
::Patron
::Password
::Recovery
::GetValidLinkInfo
($uuid1);
150 my ($bnum2, $uname2) = Koha
::Patron
::Password
::Recovery
::GetValidLinkInfo
($uuid2);
151 my ($bnum3, $uname3) = Koha
::Patron
::Password
::Recovery
::GetValidLinkInfo
("THISISANINVALIDUUID");
153 is
( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
154 is
( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
155 ok
( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
156 ok
( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
158 ##############################################################
159 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
160 ##############################################################
162 is
( Koha
::Patron
::Password
::Recovery
::CompletePasswordRecovery
($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
164 $schema->resultset('BorrowerPasswordRecovery')->create(
166 borrowernumber
=> $borrowernumber2,
168 valid_until
=> dt_from_string
()->subtract( days
=> 2 )->datetime()
172 ok
( Koha
::Patron
::Password
::Recovery
::CompletePasswordRecovery
($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
173 ok
( Koha
::Patron
::Password
::Recovery
::CompletePasswordRecovery
($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
175 ###################################################################
176 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
177 ###################################################################
179 $schema->resultset('BorrowerPasswordRecovery')->create(
181 borrowernumber
=> $borrowernumber3,
183 valid_until
=> dt_from_string
()->subtract( days
=> 3 )->datetime()
187 ok
( Koha
::Patron
::Password
::Recovery
::DeleteExpiredPasswordRecovery
($borrowernumber3) == 1, "[DeleteExpiredPasswordRecovery] we can delete the unused entry" );
188 ok
( Koha
::Patron
::Password
::Recovery
::DeleteExpiredPasswordRecovery
($borrowernumber3) == 0, "[DeleteExpiredPasswordRecovery] Returns 0 on a clean table" );
190 ###############################################################
191 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
192 ###############################################################
194 my $borrower = Koha
::Patrons
->search( { userid
=> $userid1 } )->next;
197 $success = Koha
::Patron
::Password
::Recovery
::SendPasswordRecoveryEmail
($borrower, $email1, 0); }
199 '[SendPasswordRecoveryEmail] expecting fake sendmail';
200 ok
( $success == 1, '[SendPasswordRecoveryEmail] Returns 1 on success');
202 my $letters = C4
::Letters
::GetQueuedMessages
( { borrowernumber
=> $borrowernumber1, limit
=> 99 } );
203 ok
( scalar @
$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
205 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber
=> $borrowernumber1 } );
206 my $tempuuid1 = $bpr->next->uuid;
209 Koha
::Patron
::Password
::Recovery
::SendPasswordRecoveryEmail
($borrower, $email1, 1); }
211 '[SendPasswordRecoveryEmail] expecting fake sendmail';
213 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber
=> $borrowernumber1 } );
214 my $tempuuid2 = $bpr->next->uuid;
216 $letters = C4
::Letters
::GetQueuedMessages
( { borrowernumber
=> $borrowernumber1, limit
=> 99 } );
218 ok
( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
219 ok
( scalar @
$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
221 foreach my $letter (@
$letters) {
222 ok
( $letter->{status
} eq 'sent',
223 'Test SendPasswordRecoverEmail sent due to TestBuilder Sender being a valid email address as expected.' );
226 $schema->storage->txn_rollback();