3 # This file is part of Koha.
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
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.
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.
20 use Test
::More tests
=> 2;
23 use Module
::Load
::Conditional
qw(can_load);
30 use t
::lib
::TestBuilder
;
32 my $t = Test
::Mojo
->new('Koha::REST::V1');
33 my $schema = Koha
::Database
->new->schema;
34 my $builder = t
::lib
::TestBuilder
->new();
36 my $remote_address = '127.0.0.1';
39 # FIXME: CGI::Session::Driver::DBI explicitly sets AutoCommit=1 [1] which breaks the rollback in out tests.
40 # Until we change into some other library, set SessionStorage to 'tmp'
41 # [1] https://metacpan.org/source/CGI::Session::Driver::DBI#L28
42 t
::lib
::Mocks
::mock_preference
( 'SessionStorage', 'tmp' );
44 subtest
'token-based tests' => sub {
46 if ( can_load
( modules
=> { 'Net::OAuth2::AuthorizationServer' => undef } ) ) {
50 plan skip_all
=> 'Net::OAuth2::AuthorizationServer not available';
53 $schema->storage->txn_begin;
55 my $patron = $builder->build_object({
56 class => 'Koha::Patrons',
58 flags
=> 16 # no permissions
62 t
::lib
::Mocks
::mock_preference
('RESTOAuth2ClientCredentials', 1);
64 my $api_key = Koha
::ApiKey
->new({ patron_id
=> $patron->id, description
=> 'blah' })->store;
67 grant_type
=> 'client_credentials',
68 client_id
=> $api_key->client_id,
69 client_secret
=> $api_key->secret
71 $t->post_ok('/api/v1/oauth/token', form
=> $formData)
73 ->json_is('/expires_in' => 3600)
74 ->json_is('/token_type' => 'Bearer')
75 ->json_has('/access_token');
77 my $access_token = $t->tx->res->json->{access_token
};
79 # With access token and permissions, it returns 200
80 #$patron->flags(2**4)->store;
84 my $tx = $t->ua->build_tx(GET
=> '/api/v1/patrons');
85 $tx->req->headers->authorization("Bearer $access_token");
87 $t->app->hook(after_dispatch
=> sub { $stash = shift->stash });
88 $t->request_ok($tx)->status_is(200);
90 my $user = $stash->{'koha.user'};
91 ok
( defined $user, 'The \'koha.user\' object is defined in the stash') and
92 is
( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron') and
93 is
( $user->borrowernumber, $patron->borrowernumber, 'The stashed user is the right one' );
95 $schema->storage->txn_rollback;
98 subtest
'cookie-based tests' => sub {
102 $schema->storage->txn_begin;
104 my ( $borrowernumber, $session_id ) = create_user_and_session
({ authorized
=> 1 });
106 $tx = $t->ua->build_tx( GET
=> "/api/v1/patrons" );
107 $tx->req->cookies( { name
=> 'CGISESSID', value
=> $session_id } );
108 $tx->req->env( { REMOTE_ADDR
=> $remote_address } );
111 $t->app->hook(after_dispatch
=> sub { $stash = shift->stash });
112 $t->request_ok($tx)->status_is(200);
114 my $user = $stash->{'koha.user'};
115 ok
( defined $user, 'The \'koha.user\' object is defined in the stash') and
116 is
( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron') and
117 is
( $user->borrowernumber, $borrowernumber, 'The stashed user is the right one' );
119 $schema->storage->txn_rollback;
122 sub create_user_and_session
{
125 my $flags = ( $args->{authorized
} ) ?
16 : 0;
127 my $user = $builder->build(
129 source
=> 'Borrower',
136 # Create a session for the authorized user
137 my $session = C4
::Auth
::get_session
('');
138 $session->param( 'number', $user->{borrowernumber
} );
139 $session->param( 'id', $user->{userid
} );
140 $session->param( 'ip', '127.0.0.1' );
141 $session->param( 'lasttime', time() );
144 return ( $user->{borrowernumber
}, $session->id );