Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# Copyright 2016 Koha-Suomi |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 38; |
23 |
use Test::Mojo; |
24 |
|
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
use C4::Auth; |
28 |
use C4::Context; |
29 |
use Koha::AuthUtils; |
30 |
|
31 |
my $builder = t::lib::TestBuilder->new(); |
32 |
|
33 |
my $dbh = C4::Context->dbh; |
34 |
$dbh->{AutoCommit} = 0; |
35 |
$dbh->{RaiseError} = 1; |
36 |
|
37 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
38 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
39 |
|
40 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
41 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
42 |
my $password = "2anxious? if someone finds out"; |
43 |
|
44 |
my $borrower = $builder->build({ |
45 |
source => 'Borrower', |
46 |
value => { |
47 |
branchcode => $branchcode, |
48 |
categorycode => $categorycode, |
49 |
password => Koha::AuthUtils::hash_password($password), |
50 |
} |
51 |
}); |
52 |
|
53 |
my $auth_by_userid = { |
54 |
userid => $borrower->{userid}, |
55 |
password => $password, |
56 |
}; |
57 |
my $auth_by_cardnumber = { |
58 |
cardnumber => $borrower->{cardnumber}, |
59 |
password => $password, |
60 |
}; |
61 |
my $invalid_login = { |
62 |
userid => $borrower->{userid}, |
63 |
password => "please let me in", |
64 |
}; |
65 |
my $invalid_login2 = { |
66 |
cardnumber => $borrower->{cardnumber}, |
67 |
password => "my password is password, don't tell anyone", |
68 |
}; |
69 |
|
70 |
my $tx = $t->ua->build_tx(POST => '/api/v1/auth/session' => form => $auth_by_userid); |
71 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
72 |
$t->request_ok($tx) |
73 |
->status_is(201) |
74 |
->json_is('/firstname', $borrower->{firstname}) |
75 |
->json_is('/surname', $borrower->{surname}) |
76 |
->json_is('/borrowernumber', $borrower->{borrowernumber}) |
77 |
->json_is('/email', $borrower->{email}) |
78 |
->json_has('/sessionid'); |
79 |
|
80 |
$tx = $t->ua->build_tx(POST => '/api/v1/auth/session' => form => $auth_by_cardnumber); |
81 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
82 |
$t->request_ok($tx) |
83 |
->status_is(201) |
84 |
->json_is('/firstname', $borrower->{firstname}) |
85 |
->json_is('/surname', $borrower->{surname}) |
86 |
->json_is('/borrowernumber', $borrower->{borrowernumber}) |
87 |
->json_is('/email', $borrower->{email}) |
88 |
->json_has('/sessionid'); |
89 |
my $sessionid = $tx->res->json->{sessionid}; |
90 |
|
91 |
$tx = $t->ua->build_tx(POST => '/api/v1/auth/session' => form => $invalid_login); |
92 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
93 |
$t->request_ok($tx) |
94 |
->status_is(401) |
95 |
->json_is('/error', "Login failed."); |
96 |
|
97 |
$tx = $t->ua->build_tx(POST => '/api/v1/auth/session' => form => $invalid_login2); |
98 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
99 |
$t->request_ok($tx) |
100 |
->status_is(401) |
101 |
->json_is('/error', "Login failed."); |
102 |
|
103 |
$tx = $t->ua->build_tx(DELETE => '/api/v1/auth/session' => json => { sessionid => $sessionid."123" }); |
104 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
105 |
$t->request_ok($tx) |
106 |
->status_is(401) |
107 |
->json_is('/error', "Invalid session id."); |
108 |
|
109 |
my ($sess_status, $sid) = C4::Auth::check_cookie_auth($sessionid); |
110 |
is($sess_status, "ok", "Session is valid before logging out."); |
111 |
$tx = $t->ua->build_tx(DELETE => '/api/v1/auth/session' => json => { sessionid => $sessionid }); |
112 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
113 |
$t->request_ok($tx) |
114 |
->status_is(200); |
115 |
|
116 |
($sess_status, $sid) = C4::Auth::check_cookie_auth($sessionid); |
117 |
isnt($sess_status, "ok", "Session is not valid after logging out."); |
118 |
|
119 |
$tx = $t->ua->build_tx(POST => '/api/v1/auth/session' => form => $auth_by_cardnumber); |
120 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
121 |
$t->request_ok($tx) |
122 |
->status_is(201) |
123 |
->json_is('/firstname', $borrower->{firstname}) |
124 |
->json_is('/surname', $borrower->{surname}) |
125 |
->json_is('/borrowernumber', $borrower->{borrowernumber}) |
126 |
->json_is('/email', $borrower->{email}) |
127 |
->json_has('/sessionid'); |
128 |
$sessionid = $tx->res->json->{sessionid}; |
129 |
|
130 |
($sess_status, $sid) = C4::Auth::check_cookie_auth($sessionid); |
131 |
is($sess_status, "ok", "Session is valid before logging out."); |
132 |
$tx = $t->ua->build_tx(DELETE => '/api/v1/auth/session'); |
133 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
134 |
$t->request_ok($tx) |
135 |
->status_is(200); |
136 |
|
137 |
($sess_status, $sid) = C4::Auth::check_cookie_auth($sessionid); |
138 |
isnt($sess_status, "ok", "Session is not valid after logging out."); |
139 |
|
140 |
$dbh->rollback; |