Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 3 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 2; |
21 |
use Test::Mojo; |
22 |
|
23 |
use t::lib::TestBuilder; |
24 |
use t::lib::Mocks; |
25 |
|
26 |
use MIME::Base64; |
27 |
|
28 |
my $schema = Koha::Database->new->schema; |
29 |
my $builder = t::lib::TestBuilder->new; |
30 |
|
31 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
32 |
my $tx; |
33 |
|
34 |
subtest 'success tests' => sub { |
35 |
|
36 |
plan tests => 5; |
37 |
|
38 |
$schema->storage->txn_begin; |
39 |
|
40 |
my $password = 'AbcdEFG123'; |
41 |
|
42 |
my $patron = $builder->build_object( |
43 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
44 |
$patron->set_password($password); |
45 |
|
46 |
my $credentials = encode_base64( $patron->userid . ':' . $password ); |
47 |
|
48 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
49 |
$tx->req->headers->authorization("Basic $credentials"); |
50 |
$t->request_ok($tx)->status_is( 200, 'Successful authentication and permissions check' ); |
51 |
|
52 |
$patron->flags(undef)->store; |
53 |
|
54 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
55 |
$tx->req->headers->authorization("Basic $credentials"); |
56 |
$t->request_ok($tx)->status_is( 403, 'Successful authentication and not enough permissions' ) |
57 |
->json_is( |
58 |
'/error' => 'Authorization failure. Missing required permission(s).', |
59 |
'Error message returned' |
60 |
); |
61 |
|
62 |
$schema->storage->txn_rollback; |
63 |
}; |
64 |
|
65 |
subtest 'failure tests' => sub { |
66 |
|
67 |
plan tests => 5; |
68 |
|
69 |
$schema->storage->txn_begin; |
70 |
|
71 |
my $password = 'AbcdEFG123'; |
72 |
my $bad_password = '123456789'; |
73 |
|
74 |
my $patron = $builder->build_object( |
75 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
76 |
$patron->set_password($password); |
77 |
|
78 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
79 |
$tx->req->headers->authorization("Basic "); |
80 |
$t->request_ok($tx)->status_is( 401, 'No credentials passed' ); |
81 |
|
82 |
$patron->flags(undef)->store; |
83 |
|
84 |
my $credentials = encode_base64( $patron->userid . ':' . $bad_password ); |
85 |
|
86 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
87 |
$tx->req->headers->authorization("Basic $credentials"); |
88 |
$t->request_ok($tx)->status_is( 403, 'Successful authentication and not enough permissions' ) |
89 |
->json_is( '/error' => 'Invalid password', 'Error message returned' ); |
90 |
|
91 |
$schema->storage->txn_rollback; |
92 |
}; |
93 |
|
94 |
1; |