Bug 22061: (follow-up) set_password expects a hashref
[koha_ffzg] / t / db_dependent / api / v1 / auth_authenticate_api_request.t
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 Module::Load::Conditional qw(can_load);
24
25 use Koha::ApiKeys;
26 use Koha::Database;
27 use Koha::Patrons;
28
29 use t::lib::Mocks;
30 use t::lib::TestBuilder;
31
32 my $t = Test::Mojo->new('Koha::REST::V1');
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new();
35
36 my $remote_address = '127.0.0.1';
37 my $tx;
38
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' );
43
44 subtest 'token-based tests' => sub {
45
46     if ( can_load( modules => { 'Net::OAuth2::AuthorizationServer' => undef } ) ) {
47         plan tests => 10;
48     }
49     else {
50         plan skip_all => 'Net::OAuth2::AuthorizationServer not available';
51     }
52
53     $schema->storage->txn_begin;
54
55     my $patron = $builder->build_object({
56         class => 'Koha::Patrons',
57         value  => {
58             flags => 16 # no permissions
59         },
60     });
61
62     t::lib::Mocks::mock_preference('RESTOAuth2ClientCredentials', 1);
63
64     my $api_key = Koha::ApiKey->new({ patron_id => $patron->id, description => 'blah' })->store;
65
66     my $formData = {
67         grant_type    => 'client_credentials',
68         client_id     => $api_key->client_id,
69         client_secret => $api_key->secret
70     };
71     $t->post_ok('/api/v1/oauth/token', form => $formData)
72         ->status_is(200)
73         ->json_is('/expires_in' => 3600)
74         ->json_is('/token_type' => 'Bearer')
75         ->json_has('/access_token');
76
77     my $access_token = $t->tx->res->json->{access_token};
78
79     # With access token and permissions, it returns 200
80     #$patron->flags(2**4)->store;
81
82     my $stash;
83
84     my $tx = $t->ua->build_tx(GET => '/api/v1/patrons');
85     $tx->req->headers->authorization("Bearer $access_token");
86
87     $t->app->hook(after_dispatch => sub { $stash = shift->stash });
88     $t->request_ok($tx)->status_is(200);
89
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' );
94
95     $schema->storage->txn_rollback;
96 };
97
98 subtest 'cookie-based tests' => sub {
99
100     plan tests => 5;
101
102     $schema->storage->txn_begin;
103
104     my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 });
105
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 } );
109
110     my $stash;
111     $t->app->hook(after_dispatch => sub { $stash = shift->stash });
112     $t->request_ok($tx)->status_is(200);
113
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' );
118
119     $schema->storage->txn_rollback;
120 };
121
122 sub create_user_and_session {
123
124     my $args  = shift;
125     my $flags = ( $args->{authorized} ) ? 16 : 0;
126
127     my $user = $builder->build(
128         {
129             source => 'Borrower',
130             value  => {
131                 flags => $flags
132             }
133         }
134     );
135
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() );
142     $session->flush;
143
144     return ( $user->{borrowernumber}, $session->id );
145 }