Lines 23-35
use Test::Mojo;
Link Here
|
23 |
use t::lib::TestBuilder; |
23 |
use t::lib::TestBuilder; |
24 |
use t::lib::Mocks; |
24 |
use t::lib::Mocks; |
25 |
|
25 |
|
26 |
use MIME::Base64; |
|
|
27 |
|
28 |
my $schema = Koha::Database->new->schema; |
26 |
my $schema = Koha::Database->new->schema; |
29 |
my $builder = t::lib::TestBuilder->new; |
27 |
my $builder = t::lib::TestBuilder->new; |
30 |
|
28 |
|
31 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
29 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
32 |
my $tx; |
|
|
33 |
|
30 |
|
34 |
subtest 'success tests' => sub { |
31 |
subtest 'success tests' => sub { |
35 |
|
32 |
|
Lines 44-65
subtest 'success tests' => sub {
Link Here
|
44 |
my $patron = $builder->build_object( |
41 |
my $patron = $builder->build_object( |
45 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
42 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
46 |
$patron->set_password($password); |
43 |
$patron->set_password($password); |
|
|
44 |
my $userid = $patron->userid; |
47 |
|
45 |
|
48 |
my $credentials = encode_base64( $patron->userid . ':' . $password ); |
46 |
$t->get_ok("//$userid:$password@/api/v1/patrons") |
49 |
|
47 |
->status_is( 200, 'Successful authentication and permissions check' ); |
50 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
|
|
51 |
$tx->req->headers->authorization("Basic $credentials"); |
52 |
$t->request_ok($tx)->status_is( 200, 'Successful authentication and permissions check' ); |
53 |
|
48 |
|
54 |
$patron->flags(undef)->store; |
49 |
$patron->flags(undef)->store; |
55 |
|
50 |
|
56 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
51 |
$t->get_ok("//$userid:$password@/api/v1/patrons") |
57 |
$tx->req->headers->authorization("Basic $credentials"); |
52 |
->status_is( 403, 'Successful authentication and not enough permissions' ) |
58 |
$t->request_ok($tx)->status_is( 403, 'Successful authentication and not enough permissions' ) |
53 |
->json_is( |
59 |
->json_is( |
|
|
60 |
'/error' => 'Authorization failure. Missing required permission(s).', |
54 |
'/error' => 'Authorization failure. Missing required permission(s).', |
61 |
'Error message returned' |
55 |
'Error message returned' |
62 |
); |
56 |
); |
63 |
|
57 |
|
64 |
$schema->storage->txn_rollback; |
58 |
$schema->storage->txn_rollback; |
65 |
}; |
59 |
}; |
Lines 78-103
subtest 'failure tests' => sub {
Link Here
|
78 |
my $patron = $builder->build_object( |
72 |
my $patron = $builder->build_object( |
79 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
73 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
80 |
$patron->set_password($password); |
74 |
$patron->set_password($password); |
|
|
75 |
my $userid = $patron->userid; |
81 |
|
76 |
|
82 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
77 |
$t->get_ok("//$userid:@/api/v1/patrons") |
83 |
$tx->req->headers->authorization("Basic "); |
78 |
->status_is( 401, 'No credentials passed' ); |
84 |
$t->request_ok($tx)->status_is( 401, 'No credentials passed' ); |
|
|
85 |
|
86 |
$patron->flags(undef)->store; |
87 |
|
88 |
my $credentials = encode_base64( $patron->userid . ':' . $bad_password ); |
89 |
|
90 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
91 |
$tx->req->headers->authorization("Basic $credentials"); |
92 |
$t->request_ok($tx)->status_is( 403, 'Successful authentication and not enough permissions' ) |
93 |
->json_is( '/error' => 'Invalid password', 'Error message returned' ); |
94 |
|
79 |
|
|
|
80 |
$t->get_ok("//$userid:$bad_password@/api/v1/patrons") |
81 |
->status_is( 403, 'Failed authentication, invalid password' ) |
82 |
->json_is( '/error' => 'Invalid password', 'Error message returned' ); |
95 |
|
83 |
|
96 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 0 ); |
84 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 0 ); |
97 |
|
85 |
|
98 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
86 |
$t->get_ok("//$userid:$password@/api/v1/patrons") |
99 |
$tx->req->headers->authorization("Basic $credentials"); |
|
|
100 |
$t->request_ok($tx) |
101 |
->status_is( 401, 'Basic authentication is disabled' ) |
87 |
->status_is( 401, 'Basic authentication is disabled' ) |
102 |
->json_is( '/error' => 'Basic authentication disabled', 'Expected error message rendered' ); |
88 |
->json_is( '/error' => 'Basic authentication disabled', 'Expected error message rendered' ); |
103 |
|
89 |
|
104 |
- |
|
|