Bug 14097 : Changing AddReserve prototype call
[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::Borrowers;
24
25 use Test::More tests => 16;
26
27 use_ok('C4::Passwordrecovery');
28
29 my $schema = Koha::Database->new()->schema();
30 $schema->storage->txn_begin();
31
32 my $dbh = C4::Context->dbh;
33 $dbh->{RaiseError} = 1;
34
35 #
36 # Start with fresh data
37 #
38
39 my $borrowernumber1 = '2000000000';
40 my $borrowernumber2 = '2000000001';
41 my $userid1 = "I83MFItzRpGPxD3vW0";
42 my $userid2 = "Gh5t43980hfSAOcvne";
43 my $email1  = $userid1 . '@koha-community.org';
44 my $email2  = $userid2 . '@koha-community.org';
45 my $uuid1   = "ABCD1234";
46 my $uuid2   = "WXYZ0987";
47
48 my $categorycode = 'S'; #  staff
49 my $branch       = $schema->resultset('Branch')->first(); #  legit branch from your db
50
51 $schema->resultset('BorrowerPasswordRecovery')->delete_all();
52
53 $schema->resultset('Borrower')->create(
54     {
55         borrowernumber  => $borrowernumber1,
56         surname         => '',
57         address         => '',
58         city            => '',
59         userid          => $userid1,
60         email           => $email1,
61         categorycode    => $categorycode,
62         branchcode      => $branch,
63     }
64 );
65 $schema->resultset('Borrower')->create(
66     {
67         borrowernumber  => $borrowernumber2,
68         surname         => '',
69         address         => '',
70         city            => '',
71         userid          => $userid2,
72         email           => $email2,
73         categorycode    => $categorycode,
74         branchcode      => $branch,
75     }
76 );
77
78 $schema->resultset('BorrowerPasswordRecovery')->create(
79     {
80         borrowernumber => $borrowernumber1,
81         uuid           => $uuid1,
82         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->add( days => 2 )->datetime()
83     }
84 );
85 $schema->resultset('BorrowerPasswordRecovery')->create(
86     {
87         borrowernumber => $borrowernumber2,
88         uuid           => $uuid2,
89         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
90     }
91 );
92
93 can_ok( "C4::Passwordrecovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
94
95 ################################################
96 # C4::Passwordrecovery::ValidateBorrowernumber #
97 ################################################
98
99 ok(   C4::Passwordrecovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
100 ok( ! C4::Passwordrecovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
101 ok( ! C4::Passwordrecovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
102
103 ##########################################
104 # C4::Passwordrecovery::GetValidLinkInfo #
105 ##########################################
106
107 my ($bnum1, $uname1) = C4::Passwordrecovery::GetValidLinkInfo($uuid1);
108 my ($bnum2, $uname2) = C4::Passwordrecovery::GetValidLinkInfo($uuid2);
109 my ($bnum3, $uname3) = C4::Passwordrecovery::GetValidLinkInfo("THISISANINVALIDUUID");
110
111 is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
112 is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
113 ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
114 ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
115
116 ##################################################
117 # C4::Passwordrecovery::CompletePasswordRecovery #
118 ##################################################
119
120 ok( C4::Passwordrecovery::CompletePasswordRecovery($uuid1) == 2, "[CompletePasswordRecovery] Completing a password recovery deletes the entry and expired entries" );
121
122 $schema->resultset('BorrowerPasswordRecovery')->create(
123     {
124         borrowernumber => $borrowernumber2,
125         uuid           => $uuid2,
126         valid_until    => DateTime->now( time_zone => C4::Context->tz() )->subtract( days => 2 )->datetime()
127     }
128 );
129
130 ok( C4::Passwordrecovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
131 ok( C4::Passwordrecovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
132
133 ###################################################
134 # C4::Passwordrecovery::SendPasswordRecoveryEmail #
135 ###################################################
136
137 my $borrower = shift [ Koha::Borrowers->search( { userid => $userid1 } ) ];
138 ok( C4::Passwordrecovery::SendPasswordRecoveryEmail($borrower, $email1, 0) == 1, "[SendPasswordRecoveryEmail] Returns 1 on success" );
139 my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
140 ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
141
142 my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
143 my $tempuuid1 = $bpr->next->uuid;
144
145 C4::Passwordrecovery::SendPasswordRecoveryEmail($borrower, $email1, 1);
146
147 $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
148 my $tempuuid2 = $bpr->next->uuid;
149
150 $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
151
152 ok( $tempuuid1 ne $tempuuid2, "[SendPasswordRecoveryEmail] UPDATE == ON changes uuid in the database and updates the expirydate");
153 ok( scalar @$letters == 2, "[SendPasswordRecoveryEmail] UPDATE == ON sends a new letter with updated uuid");
154
155 $schema->storage->txn_rollback();
156
157 1;