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