Bug 25758: Return on_reserve over too_soon when not calling from automatic_renewals...
[koha.git] / t / db_dependent / Passwordrecovery.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use C4::Context;
21 use Mail::Sendmail;
22 use C4::Letters;
23 use Koha::Database;
24 use Koha::DateUtils;
25 use Koha::Patrons;
26 use t::lib::TestBuilder;
27
28 use Test::More tests => 22;
29 use Test::MockModule;
30 use Test::Warn;
31 use Carp;
32
33 my %mail;
34 my $module = Test::MockModule->new('Mail::Sendmail');
35 $module->mock(
36     'sendmail',
37     sub {
38         carp 'Fake sendmail';
39         %mail = @_;
40     }
41 );
42
43 use_ok('Koha::Patron::Password::Recovery');
44
45 my $schema = Koha::Database->new()->schema();
46 $schema->storage->txn_begin();
47
48 my $dbh = C4::Context->dbh;
49 $dbh->{RaiseError} = 1;
50
51 #
52 # Start with fresh data
53 #
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";
67
68 my $patron_category = $builder->build({ source => 'Category' });
69 my $branch = $builder->build({
70     source => 'Branch',
71     value => {
72         branchreturnpath => $email1,
73     },
74 });
75
76 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
77
78 $schema->resultset('Borrower')->create(
79     {
80         borrowernumber  => $borrowernumber1,
81         surname         => '',
82         address         => '',
83         city            => '',
84         userid          => $userid1,
85         email           => $email1,
86         categorycode    => $patron_category->{categorycode},
87         branchcode      => $branch->{branchcode},
88     }
89 );
90 $schema->resultset('Borrower')->create(
91     {
92         borrowernumber  => $borrowernumber2,
93         surname         => '',
94         address         => '',
95         city            => '',
96         userid          => $userid2,
97         email           => $email2,
98         categorycode    => $patron_category->{categorycode},
99         branchcode      => $branch->{branchcode},
100     }
101 );
102 $schema->resultset('Borrower')->create(
103     {
104         borrowernumber => $borrowernumber3,
105         surname        => '',
106         address        => '',
107         city           => '',
108         userid         => $userid3,
109         email          => $email3,
110         categorycode   => $patron_category->{categorycode},
111         branchcode     => $branch->{branchcode},
112     }
113 );
114
115 $schema->resultset('BorrowerPasswordRecovery')->create(
116     {
117         borrowernumber => $borrowernumber1,
118         uuid           => $uuid1,
119         valid_until    => dt_from_string()->add( days => 2 )->datetime()
120     }
121 );
122 $schema->resultset('BorrowerPasswordRecovery')->create(
123     {
124         borrowernumber => $borrowernumber2,
125         uuid           => $uuid2,
126         valid_until    => dt_from_string()->subtract( days => 2 )->datetime()
127     }
128 );
129 $schema->resultset('BorrowerPasswordRecovery')->create(
130     {
131         borrowernumber => $borrowernumber3,
132         uuid           => $uuid3,
133         valid_until    => dt_from_string()->subtract( days => 3 )->datetime()
134     }
135 );
136
137
138 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
139
140 ############################################################
141 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
142 ############################################################
143
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" );
147
148 ######################################################
149 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
150 ######################################################
151
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");
155
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" );
160
161 ##############################################################
162 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
163 ##############################################################
164
165 is( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
166
167 $schema->resultset('BorrowerPasswordRecovery')->create(
168     {
169         borrowernumber => $borrowernumber2,
170         uuid           => $uuid2,
171         valid_until    => dt_from_string()->subtract( days => 2 )->datetime()
172     }
173 );
174
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" );
177
178 ###################################################################
179 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
180 ###################################################################
181
182 $schema->resultset('BorrowerPasswordRecovery')->create(
183     {
184         borrowernumber => $borrowernumber3,
185         uuid           => $uuid3,
186         valid_until    => dt_from_string()->subtract( days => 3 )->datetime()
187     }
188 );
189
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" );
192
193 ###############################################################
194 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
195 ###############################################################
196
197 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
198 my $success;
199 warning_is {
200     $success = Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0); }
201     "Fake sendmail",
202     '[SendPasswordRecoveryEmail] expecting fake sendmail';
203 ok( $success == 1, '[SendPasswordRecoveryEmail] Returns 1 on success');
204
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");
207
208 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
209 my $tempuuid1 = $bpr->next->uuid;
210
211 warning_is {
212     Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1); }
213     "Fake sendmail",
214     '[SendPasswordRecoveryEmail] expecting fake sendmail';
215
216 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
217 my $tempuuid2 = $bpr->next->uuid;
218
219 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
220
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");
223
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.' );
227 }
228
229 $schema->storage->txn_rollback();
230