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 => 20; |
23 |
use Test::Mojo; |
24 |
|
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
use C4::Context; |
28 |
use Koha::AuthUtils; |
29 |
|
30 |
my $builder = t::lib::TestBuilder->new(); |
31 |
|
32 |
my $dbh = C4::Context->dbh; |
33 |
$dbh->{AutoCommit} = 0; |
34 |
$dbh->{RaiseError} = 1; |
35 |
|
36 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
37 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
38 |
|
39 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
40 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
41 |
my $password = "2anxious? if someone finds out"; |
42 |
|
43 |
my $borrower = $builder->build({ |
44 |
source => 'Borrower', |
45 |
value => { |
46 |
branchcode => $branchcode, |
47 |
categorycode => $categorycode, |
48 |
password => Koha::AuthUtils::hash_password($password), |
49 |
} |
50 |
}); |
51 |
|
52 |
my $auth_by_userid = { |
53 |
userid => $borrower->{userid}, |
54 |
password => $password, |
55 |
}; |
56 |
my $auth_by_cardnumber = { |
57 |
cardnumber => $borrower->{cardnumber}, |
58 |
password => $password, |
59 |
}; |
60 |
my $invalid_login = { |
61 |
userid => $borrower->{userid}, |
62 |
password => "please let me in", |
63 |
}; |
64 |
my $invalid_login2 = { |
65 |
cardnumber => $borrower->{cardnumber}, |
66 |
password => "my password is password, don't tell anyone", |
67 |
}; |
68 |
|
69 |
my $tx = $t->ua->build_tx(POST => '/api/v1/auth/session' => form => $auth_by_userid); |
70 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
71 |
$t->request_ok($tx) |
72 |
->status_is(201) |
73 |
->json_is('/firstname', $borrower->{firstname}) |
74 |
->json_is('/surname', $borrower->{surname}) |
75 |
->json_is('/borrowernumber', $borrower->{borrowernumber}) |
76 |
->json_is('/email', $borrower->{email}) |
77 |
->json_has('/sessionid'); |
78 |
|
79 |
$tx = $t->ua->build_tx(POST => '/api/v1/auth/session' => form => $auth_by_cardnumber); |
80 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
81 |
$t->request_ok($tx) |
82 |
->status_is(201) |
83 |
->json_is('/firstname', $borrower->{firstname}) |
84 |
->json_is('/surname', $borrower->{surname}) |
85 |
->json_is('/borrowernumber', $borrower->{borrowernumber}) |
86 |
->json_is('/email', $borrower->{email}) |
87 |
->json_has('/sessionid'); |
88 |
|
89 |
$tx = $t->ua->build_tx(POST => '/api/v1/auth/session' => form => $invalid_login); |
90 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
91 |
$t->request_ok($tx) |
92 |
->status_is(401) |
93 |
->json_is('/error', "Login failed."); |
94 |
|
95 |
$tx = $t->ua->build_tx(POST => '/api/v1/auth/session' => form => $invalid_login2); |
96 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
97 |
$t->request_ok($tx) |
98 |
->status_is(401) |
99 |
->json_is('/error', "Login failed."); |
100 |
|
101 |
$dbh->rollback; |