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',
57 value => { flags => 1 },
60 t::lib::Mocks::mock_preference('RESTOAuth2ClientCredentials', 1);
62 my $api_key = Koha::ApiKey->new({ patron_id => $patron->id, description => 'blah' })->store;
65 grant_type => 'client_credentials',
66 client_id => $api_key->client_id,
67 client_secret => $api_key->secret
69 $t->post_ok('/api/v1/oauth/token', form => $formData)
71 ->json_is('/expires_in' => 3600)
72 ->json_is('/token_type' => 'Bearer')
73 ->json_has('/access_token');
75 my $access_token = $t->tx->res->json->{access_token};
77 # With access token and permissions, it returns 200
78 #$patron->flags(2**4)->store;
82 my $tx = $t->ua->build_tx(GET => '/api/v1/acquisitions/orders');
83 $tx->req->headers->authorization("Bearer $access_token");
84 $tx->req->headers->header( 'x-koha-embed' => 'fund' );
86 $t->app->hook(after_dispatch => sub { $stash = shift->stash });
87 $t->request_ok($tx)->status_is(200);
89 my $user = $stash->{'koha.user'};
90 ok( defined $user, 'The \'koha.user\' object is defined in the stash') and
91 is( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron') and
92 is( $user->borrowernumber, $patron->borrowernumber, 'The stashed user is the right one' );
94 my $embed = $stash->{'koha.embed'};
95 ok( defined $embed, 'The embed hashref is generated and stashed' );
96 is_deeply( $embed, { fund => {} }, 'The embed data structure is correct' );
98 $schema->storage->txn_rollback;
101 subtest 'cookie-based tests' => sub {
105 $schema->storage->txn_begin;
107 my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 });
109 $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
110 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
111 $tx->req->env( { REMOTE_ADDR => $remote_address } );
114 $t->app->hook(after_dispatch => sub { $stash = shift->stash });
115 $t->request_ok($tx)->status_is(200);
117 my $user = $stash->{'koha.user'};
118 ok( defined $user, 'The \'koha.user\' object is defined in the stash') and
119 is( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron') and
120 is( $user->borrowernumber, $borrowernumber, 'The stashed user is the right one' );
122 $schema->storage->txn_rollback;
125 sub create_user_and_session {
128 my $flags = ( $args->{authorized} ) ? 16 : 0;
130 my $user = $builder->build(
132 source => 'Borrower',
139 # Create a session for the authorized user
140 my $session = C4::Auth::get_session('');
141 $session->param( 'number', $user->{borrowernumber} );
142 $session->param( 'id', $user->{userid} );
143 $session->param( 'ip', '127.0.0.1' );
144 $session->param( 'lasttime', time() );
147 return ( $user->{borrowernumber}, $session->id );