0eabc80a4959349e2a7601df250ec16ed50f2283
[koha-ffzg.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
27 use t::lib::TestBuilder;
28 use t::lib::Mocks;
29
30 use Test::More tests => 22;
31 use Test::MockModule;
32 use Test::Warn;
33 use Carp;
34
35 my ( $email_object, $sendmail_params );
36
37 my $email_sender_module = Test::MockModule->new('Email::Stuffer');
38 $email_sender_module->mock(
39     'send_or_die',
40     sub {
41         ( $email_object, $sendmail_params ) = @_;
42         warn 'Fake sendmail';
43     }
44 );
45
46 use_ok('Koha::Patron::Password::Recovery');
47
48 t::lib::Mocks::mock_preference('KohaAdminEmailAddress', 'test@koha-community.org');
49
50 my $schema = Koha::Database->new()->schema();
51 $schema->storage->txn_begin();
52
53 #
54 # Start with fresh data
55 #
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";
69
70 my $patron_category = $builder->build({ source => 'Category' });
71 my $branch = $builder->build({
72     source => 'Branch',
73     value => {
74         branchemail      => undef,
75         branchreplyto    => undef,
76         branchreturnpath => $email1,
77     },
78 });
79
80 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
81
82 $schema->resultset('Borrower')->create(
83     {
84         borrowernumber  => $borrowernumber1,
85         surname         => '',
86         address         => '',
87         city            => '',
88         userid          => $userid1,
89         email           => $email1,
90         categorycode    => $patron_category->{categorycode},
91         branchcode      => $branch->{branchcode},
92     }
93 );
94 $schema->resultset('Borrower')->create(
95     {
96         borrowernumber  => $borrowernumber2,
97         surname         => '',
98         address         => '',
99         city            => '',
100         userid          => $userid2,
101         email           => $email2,
102         categorycode    => $patron_category->{categorycode},
103         branchcode      => $branch->{branchcode},
104     }
105 );
106 $schema->resultset('Borrower')->create(
107     {
108         borrowernumber => $borrowernumber3,
109         surname        => '',
110         address        => '',
111         city           => '',
112         userid         => $userid3,
113         email          => $email3,
114         categorycode   => $patron_category->{categorycode},
115         branchcode     => $branch->{branchcode},
116     }
117 );
118
119 $schema->resultset('BorrowerPasswordRecovery')->create(
120     {
121         borrowernumber => $borrowernumber1,
122         uuid           => $uuid1,
123         valid_until    => dt_from_string()->add( days => 2 )->datetime()
124     }
125 );
126 $schema->resultset('BorrowerPasswordRecovery')->create(
127     {
128         borrowernumber => $borrowernumber2,
129         uuid           => $uuid2,
130         valid_until    => dt_from_string()->subtract( days => 2 )->datetime()
131     }
132 );
133 $schema->resultset('BorrowerPasswordRecovery')->create(
134     {
135         borrowernumber => $borrowernumber3,
136         uuid           => $uuid3,
137         valid_until    => dt_from_string()->subtract( days => 3 )->datetime()
138     }
139 );
140
141
142 can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
143
144 ############################################################
145 # Koha::Patron::Password::Recovery::ValidateBorrowernumber #
146 ############################################################
147
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" );
151
152 ######################################################
153 # Koha::Patron::Password::Recovery::GetValidLinkInfo #
154 ######################################################
155
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");
159
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" );
164
165 ##############################################################
166 # Koha::Patron::Password::Recovery::CompletePasswordRecovery #
167 ##############################################################
168
169 is( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1), 3, "[CompletePasswordRecovery] Completing a password recovery deletes the used entry" );
170
171 $schema->resultset('BorrowerPasswordRecovery')->create(
172     {
173         borrowernumber => $borrowernumber2,
174         uuid           => $uuid2,
175         valid_until    => dt_from_string()->subtract( days => 2 )->datetime()
176     }
177 );
178
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" );
181
182 ###################################################################
183 # Koha::Patron::Password::Recovery::DeleteExpiredPasswordRecovery #
184 ###################################################################
185
186 $schema->resultset('BorrowerPasswordRecovery')->create(
187     {
188         borrowernumber => $borrowernumber3,
189         uuid           => $uuid3,
190         valid_until    => dt_from_string()->subtract( days => 3 )->datetime()
191     }
192 );
193
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" );
196
197 ###############################################################
198 # Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
199 ###############################################################
200
201 my $borrower = Koha::Patrons->search( { userid => $userid1 } )->next;
202 my $success;
203 warning_is {
204     $success = Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0); }
205     "Fake sendmail",
206     '[SendPasswordRecoveryEmail] expecting fake sendmail';
207 ok( $success == 1, '[SendPasswordRecoveryEmail] Returns 1 on success');
208
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");
211
212 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
213 my $tempuuid1 = $bpr->next->uuid;
214
215 warning_is {
216     Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1); }
217     "Fake sendmail",
218     '[SendPasswordRecoveryEmail] expecting fake sendmail';
219
220 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
221 my $tempuuid2 = $bpr->next->uuid;
222
223 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
224
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");
227
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.' );
231 }
232
233 $schema->storage->txn_rollback();
234