Bug 24786: (QA follow-up) Remove trailing space in classes
[koha.git] / t / db_dependent / Passwordrecovery.t
blobf4a72097041046fc3987bc1eb0bb8cfe7629d9fe
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use C4::Context;
21 use C4::Letters;
22 use Koha::Database;
23 use Koha::DateUtils;
24 use Koha::Patrons;
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
29 use Test::More tests => 22;
30 use Test::MockModule;
31 use Test::Warn;
32 use Carp;
34 my ( $email_object, $sendmail_params );
36 my $email_sender_module = Test::MockModule->new('Email::Stuffer');
37 $email_sender_module->mock(
38 'send_or_die',
39 sub {
40 ( $email_object, $sendmail_params ) = @_;
41 warn 'Fake sendmail';
45 use_ok('Koha::Patron::Password::Recovery');
47 t::lib::Mocks::mock_preference('KohaAdminEmailAddress', 'test@koha-community.org');
49 my $schema = Koha::Database->new()->schema();
50 $schema->storage->txn_begin();
53 # Start with fresh data
55 my $builder = t::lib::TestBuilder->new;
56 my $borrowernumber1 = '2000000000';
57 my $borrowernumber2 = '2000000001';
58 my $borrowernumber3 = '2000000002';
59 my $userid1 = "I83MFItzRpGPxD3vW0";
60 my $userid2 = "Gh5t43980hfSAOcvne";
61 my $userid3 = "adsfada80hfSAOcvne";
62 my $email1 = $userid1 . '@koha-community.org';
63 my $email2 = $userid2 . '@koha-community.org';
64 my $email3 = $userid3 . '@koha-community.org';
65 my $uuid1 = "ABCD1234";
66 my $uuid2 = "WXYZ0987";
67 my $uuid3 = "LMNO4561";
69 my $patron_category = $builder->build({ source => 'Category' });
70 my $branch = $builder->build({
71 source => 'Branch',
72 value => {
73 branchemail => undef,
74 branchreplyto => undef,
75 branchreturnpath => $email1,
77 });
79 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
81 $schema->resultset('Borrower')->create(
83 borrowernumber => $borrowernumber1,
84 surname => '',
85 address => '',
86 city => '',
87 userid => $userid1,
88 email => $email1,
89 categorycode => $patron_category->{categorycode},
90 branchcode => $branch->{branchcode},
93 $schema->resultset('Borrower')->create(
95 borrowernumber => $borrowernumber2,
96 surname => '',
97 address => '',
98 city => '',
99 userid => $userid2,
100 email => $email2,
101 categorycode => $patron_category->{categorycode},
102 branchcode => $branch->{branchcode},
105 $schema->resultset('Borrower')->create(
107 borrowernumber => $borrowernumber3,
108 surname => '',
109 address => '',
110 city => '',
111 userid => $userid3,
112 email => $email3,
113 categorycode => $patron_category->{categorycode},
114 branchcode => $branch->{branchcode},
118 $schema->resultset('BorrowerPasswordRecovery')->create(
120 borrowernumber => $borrowernumber1,
121 uuid => $uuid1,
122 valid_until => dt_from_string()->add( days => 2 )->datetime()
125 $schema->resultset('BorrowerPasswordRecovery')->create(
127 borrowernumber => $borrowernumber2,
128 uuid => $uuid2,
129 valid_until => dt_from_string()->subtract( days => 2 )->datetime()
132 $schema->resultset('BorrowerPasswordRecovery')->create(
134 borrowernumber => $borrowernumber3,
135 uuid => $uuid3,
136 valid_until => dt_from_string()->subtract( days => 3 )->datetime()
141 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
143 ############################################################
144 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
145 ############################################################
147 ok( Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
148 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
149 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
151 ######################################################
152 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
153 ######################################################
155 my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
156 my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
157 my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
159 is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
160 is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
161 ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
162 ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
164 ##############################################################
165 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
166 ##############################################################
168 is( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
170 $schema->resultset('BorrowerPasswordRecovery')->create(
172 borrowernumber => $borrowernumber2,
173 uuid => $uuid2,
174 valid_until => dt_from_string()->subtract( days => 2 )->datetime()
178 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
179 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
181 ###################################################################
182 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
183 ###################################################################
185 $schema->resultset('BorrowerPasswordRecovery')->create(
187 borrowernumber => $borrowernumber3,
188 uuid => $uuid3,
189 valid_until => dt_from_string()->subtract( days => 3 )->datetime()
193 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 1, "[DeleteExpiredPasswordRecovery] we can delete the unused entry" );
194 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 0, "[DeleteExpiredPasswordRecovery] Returns 0 on a clean table" );
196 ###############################################################
197 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
198 ###############################################################
200 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
201 my $success;
202 warning_is {
203 $success = Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0); }
204 "Fake sendmail",
205 '[SendPasswordRecoveryEmail] expecting fake sendmail';
206 ok( $success == 1, '[SendPasswordRecoveryEmail] Returns 1 on success');
208 my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
209 ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
211 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
212 my $tempuuid1 = $bpr->next->uuid;
214 warning_is {
215 Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1); }
216 "Fake sendmail",
217 '[SendPasswordRecoveryEmail] expecting fake sendmail';
219 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
220 my $tempuuid2 = $bpr->next->uuid;
222 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
224 ok( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
225 ok( scalar @$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
227 foreach my $letter (@$letters) {
228 ok( $letter->{status} eq 'sent',
229 'Test SendPasswordRecoverEmail sent due to TestBuilder Sender being a valid email address as expected.' );
232 $schema->storage->txn_rollback();