Bug 22254: Correction to datehandling in test
[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::Patrons;
25 use t::lib::TestBuilder;
26
27 use Test::More tests => 22;
28 use Test::MockModule;
29 use Test::Warn;
30 use Carp;
31
32 my %mail;
33 my $module = Test::MockModule->new('Mail::Sendmail');
34 $module->mock(
35     'sendmail',
36     sub {
37         carp 'Fake sendmail';
38         %mail = @_;
39     }
40 );
41
42 use_ok('Koha::Patron::Password::Recovery');
43
44 my $schema = Koha::Database->new()->schema();
45 $schema->storage->txn_begin();
46
47 my $dbh = C4::Context->dbh;
48 $dbh->{RaiseError} = 1;
49
50 #
51 # Start with fresh data
52 #
53 my $builder = t::lib::TestBuilder->new;
54 my $borrowernumber1 = '2000000000';
55 my $borrowernumber2 = '2000000001';
56 my $borrowernumber3 = '2000000002';
57 my $userid1 = "I83MFItzRpGPxD3vW0";
58 my $userid2 = "Gh5t43980hfSAOcvne";
59 my $userid3 = "adsfada80hfSAOcvne";
60 my $email1  = $userid1 . '@koha-community.org';
61 my $email2  = $userid2 . '@koha-community.org';
62 my $email3  = $userid3 . '@koha-community.org';
63 my $uuid1   = "ABCD1234";
64 my $uuid2   = "WXYZ0987";
65 my $uuid3   = "LMNO4561";
66
67 my $patron_category = $builder->build({ source => 'Category' });
68 my $branch = $builder->build({
69     source => 'Branch',
70     value => {
71         branchreturnpath => $email1,
72     },
73 });
74
75 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
76
77 $schema->resultset('Borrower')->create(
78     {
79         borrowernumber  => $borrowernumber1,
80         surname         => '',
81         address         => '',
82         city            => '',
83         userid          => $userid1,
84         email           => $email1,
85         categorycode    => $patron_category->{categorycode},
86         branchcode      => $branch->{branchcode},
87     }
88 );
89 $schema->resultset('Borrower')->create(
90     {
91         borrowernumber  => $borrowernumber2,
92         surname         => '',
93         address         => '',
94         city            => '',
95         userid          => $userid2,
96         email           => $email2,
97         categorycode    => $patron_category->{categorycode},
98         branchcode      => $branch->{branchcode},
99     }
100 );
101 $schema->resultset('Borrower')->create(
102     {
103         borrowernumber => $borrowernumber3,
104         surname        => '',
105         address        => '',
106         city           => '',
107         userid         => $userid3,
108         email          => $email3,
109         categorycode   => $patron_category->{categorycode},
110         branchcode     => $branch->{branchcode},
111     }
112 );
113
114 $schema->resultset('BorrowerPasswordRecovery')->create(
115     {
116         borrowernumber => $borrowernumber1,
117         uuid           => $uuid1,
118         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->add( days => 2 )->datetime()
119     }
120 );
121 $schema->resultset('BorrowerPasswordRecovery')->create(
122     {
123         borrowernumber => $borrowernumber2,
124         uuid           => $uuid2,
125         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
126     }
127 );
128 $schema->resultset('BorrowerPasswordRecovery')->create(
129     {
130         borrowernumber => $borrowernumber3,
131         uuid           => $uuid3,
132         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 3 )->datetime()
133     }
134 );
135
136
137 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
138
139 ############################################################
140 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
141 ############################################################
142
143 ok(   Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
144 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
145 ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
146
147 ######################################################
148 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
149 ######################################################
150
151 my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
152 my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
153 my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
154
155 is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
156 is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
157 ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
158 ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
159
160 ##############################################################
161 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
162 ##############################################################
163
164 is( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
165
166 $schema->resultset('BorrowerPasswordRecovery')->create(
167     {
168         borrowernumber => $borrowernumber2,
169         uuid           => $uuid2,
170         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
171     }
172 );
173
174 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
175 ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
176
177 ###################################################################
178 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
179 ###################################################################
180
181 $schema->resultset('BorrowerPasswordRecovery')->create(
182     {
183         borrowernumber => $borrowernumber3,
184         uuid           => $uuid3,
185         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 3 )->datetime()
186     }
187 );
188
189 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 1, "[DeleteExpiredPasswordRecovery] we can delete the unused entry" );
190 ok( Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery($borrowernumber3) == 0, "[DeleteExpiredPasswordRecovery] Returns 0 on a clean table" );
191
192 ###############################################################
193 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
194 ###############################################################
195
196 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
197 my $success;
198 warning_is {
199     $success = Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0); }
200     "Fake sendmail",
201     '[SendPasswordRecoveryEmail] expecting fake sendmail';
202 ok( $success == 1, '[SendPasswordRecoveryEmail] Returns 1 on success');
203
204 my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
205 ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
206
207 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
208 my $tempuuid1 = $bpr->next->uuid;
209
210 warning_is {
211     Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1); }
212     "Fake sendmail",
213     '[SendPasswordRecoveryEmail] expecting fake sendmail';
214
215 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
216 my $tempuuid2 = $bpr->next->uuid;
217
218 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
219
220 ok( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
221 ok( scalar @$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
222
223 foreach my $letter (@$letters) {
224     ok( $letter->{status} eq 'sent',
225         'Test SendPasswordRecoverEmail sent due to TestBuilder Sender being a valid email address as expected.' );
226 }
227
228 $schema->storage->txn_rollback();
229