Lines 23-30
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 |
|
Lines 44-65
subtest 'success tests' => sub {
Link Here
|
44 |
my $patron = $builder->build_object( |
42 |
my $patron = $builder->build_object( |
45 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
43 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
46 |
$patron->set_password($password); |
44 |
$patron->set_password($password); |
|
|
45 |
my $userid = $patron->userid; |
47 |
|
46 |
|
48 |
my $credentials = encode_base64( $patron->userid . ':' . $password ); |
47 |
$t->get_ok("//$userid:$password@/api/v1/patrons") |
49 |
|
48 |
->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 |
|
49 |
|
54 |
$patron->flags(undef)->store; |
50 |
$patron->flags(undef)->store; |
55 |
|
51 |
|
56 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
52 |
$t->get_ok("//$userid:$password@/api/v1/patrons") |
57 |
$tx->req->headers->authorization("Basic $credentials"); |
53 |
->status_is( 403, 'Successful authentication and not enough permissions' ) |
58 |
$t->request_ok($tx)->status_is( 403, 'Successful authentication and not enough permissions' ) |
54 |
->json_is( |
59 |
->json_is( |
|
|
60 |
'/error' => 'Authorization failure. Missing required permission(s).', |
55 |
'/error' => 'Authorization failure. Missing required permission(s).', |
61 |
'Error message returned' |
56 |
'Error message returned' |
62 |
); |
57 |
); |
63 |
|
58 |
|
64 |
$schema->storage->txn_rollback; |
59 |
$schema->storage->txn_rollback; |
65 |
}; |
60 |
}; |
Lines 78-103
subtest 'failure tests' => sub {
Link Here
|
78 |
my $patron = $builder->build_object( |
73 |
my $patron = $builder->build_object( |
79 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
74 |
{ class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); |
80 |
$patron->set_password($password); |
75 |
$patron->set_password($password); |
|
|
76 |
my $userid = $patron->userid; |
81 |
|
77 |
|
82 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
78 |
$t->get_ok("//$userid:@/api/v1/patrons") |
83 |
$tx->req->headers->authorization("Basic "); |
79 |
->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 |
|
80 |
|
|
|
81 |
$t->get_ok("//$userid:$bad_password@/api/v1/patrons") |
82 |
->status_is( 403, 'Failed authentication, invalid password' ); |
83 |
->json_is( '/error' => 'Invalid password', 'Error message returned' ); |
95 |
|
84 |
|
96 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 0 ); |
85 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 0 ); |
97 |
|
86 |
|
98 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons" ); |
87 |
$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' ) |
88 |
->status_is( 401, 'Basic authentication is disabled' ) |
102 |
->json_is( '/error' => 'Basic authentication disabled', 'Expected error message rendered' ); |
89 |
->json_is( '/error' => 'Basic authentication disabled', 'Expected error message rendered' ); |
103 |
|
90 |
|
104 |
- |
|
|