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>.
27 use t::lib::TestBuilder;
30 use Test::More tests => 22;
35 my ( $email_object, $sendmail_params );
37 my $email_sender_module = Test::MockModule->new('Email::Stuffer');
38 $email_sender_module->mock(
41 ( $email_object, $sendmail_params ) = @_;
46 use_ok('Koha::Patron::Password::Recovery');
48 t::lib::Mocks::mock_preference('KohaAdminEmailAddress', 'test@koha-community.org');
50 my $schema = Koha::Database->new()->schema();
51 $schema->storage->txn_begin();
54 # Start with fresh data
56 my $builder = t::lib::TestBuilder->new;
57 my $borrowernumber1 = '2000000000';
58 my $borrowernumber2 = '2000000001';
59 my $borrowernumber3 = '2000000002';
60 my $userid1 = "I83MFItzRpGPxD3vW0";
61 my $userid2 = "Gh5t43980hfSAOcvne";
62 my $userid3 = "adsfada80hfSAOcvne";
63 my $email1 = $userid1 . '@koha-community.org';
64 my $email2 = $userid2 . '@koha-community.org';
65 my $email3 = $userid3 . '@koha-community.org';
66 my $uuid1 = "ABCD1234";
67 my $uuid2 = "WXYZ0987";
68 my $uuid3 = "LMNO4561";
70 my $patron_category = $builder->build({ source => 'Category' });
71 my $branch = $builder->build({
75 branchreplyto => undef,
76 branchreturnpath => $email1,
80 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
82 $schema->resultset('Borrower')->create(
84 borrowernumber => $borrowernumber1,
90 categorycode => $patron_category->{categorycode},
91 branchcode => $branch->{branchcode},
94 $schema->resultset('Borrower')->create(
96 borrowernumber => $borrowernumber2,
102 categorycode => $patron_category->{categorycode},
103 branchcode => $branch->{branchcode},
106 $schema->resultset('Borrower')->create(
108 borrowernumber => $borrowernumber3,
114 categorycode => $patron_category->{categorycode},
115 branchcode => $branch->{branchcode},
119 $schema->resultset('BorrowerPasswordRecovery')->create(
121 borrowernumber => $borrowernumber1,
123 valid_until => dt_from_string()->add( days => 2 )->datetime()
126 $schema->resultset('BorrowerPasswordRecovery')->create(
128 borrowernumber => $borrowernumber2,
130 valid_until => dt_from_string()->subtract( days => 2 )->datetime()
133 $schema->resultset('BorrowerPasswordRecovery')->create(
135 borrowernumber => $borrowernumber3,
137 valid_until => dt_from_string()->subtract( days => 3 )->datetime()
142 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
144 ############################################################
145 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
146 ############################################################
148 ok( Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
149 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
150 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
152 ######################################################
153 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
154 ######################################################
156 my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
157 my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
158 my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
160 is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
161 is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
162 ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
163 ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
165 ##############################################################
166 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
167 ##############################################################
169 is( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
171 $schema->resultset('BorrowerPasswordRecovery')->create(
173 borrowernumber => $borrowernumber2,
175 valid_until => dt_from_string()->subtract( days => 2 )->datetime()
179 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
180 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
182 ###################################################################
183 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
184 ###################################################################
186 $schema->resultset('BorrowerPasswordRecovery')->create(
188 borrowernumber => $borrowernumber3,
190 valid_until => dt_from_string()->subtract( days => 3 )->datetime()
194 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 1, "[DeleteExpiredPasswordRecovery] we can delete the unused entry" );
195 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 0, "[DeleteExpiredPasswordRecovery] Returns 0 on a clean table" );
197 ###############################################################
198 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
199 ###############################################################
201 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
204 $success = Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0); }
206 '[SendPasswordRecoveryEmail] expecting fake sendmail';
207 ok( $success == 1, '[SendPasswordRecoveryEmail] Returns 1 on success');
209 my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
210 ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
212 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
213 my $tempuuid1 = $bpr->next->uuid;
216 Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1); }
218 '[SendPasswordRecoveryEmail] expecting fake sendmail';
220 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
221 my $tempuuid2 = $bpr->next->uuid;
223 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
225 ok( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
226 ok( scalar @$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
228 foreach my $letter (@$letters) {
229 ok( $letter->{status} eq 'sent',
230 'Test SendPasswordRecoverEmail sent due to TestBuilder Sender being a valid email address as expected.' );
233 $schema->storage->txn_rollback();