| 
      
            Line 0
          
      
      
        Link Here
      
     | 
  
          
            
              | 0 | 
              -   | 
              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 t::lib::TestBuilder;  | 
            
            
              | 24 | 
              use t::lib::Mocks;  | 
            
            
              | 25 | 
               | 
            
            
              | 26 | 
              use MIME::Base64;  | 
            
            
              | 27 | 
               | 
            
            
              | 28 | 
              my $schema  = Koha::Database->new->schema;  | 
            
            
              | 29 | 
              my $builder = t::lib::TestBuilder->new;  | 
            
            
              | 30 | 
               | 
            
            
              | 31 | 
              my $t = Test::Mojo->new('Koha::REST::V1'); | 
            
            
              | 32 | 
              my $tx;  | 
            
            
              | 33 | 
               | 
            
            
              | 34 | 
              subtest 'success tests' => sub { | 
            
            
              | 35 | 
               | 
            
            
              | 36 | 
                  plan tests => 5;  | 
            
            
              | 37 | 
               | 
            
            
              | 38 | 
                  $schema->storage->txn_begin;  | 
            
            
              | 39 | 
               | 
            
            
              | 40 | 
                  t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );  | 
            
            
              | 41 | 
               | 
            
            
              | 42 | 
                  my $password = 'AbcdEFG123';  | 
            
            
              | 43 | 
               | 
            
            
              | 44 | 
                  my $patron = $builder->build_object(  | 
            
            
              | 45 | 
                      { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); | 
            
            
              | 46 | 
                  $patron->set_password($password);  | 
            
            
              | 47 | 
               | 
            
            
              | 48 | 
                  my $credentials = encode_base64( $patron->userid . ':' . $password );  | 
            
            
              | 49 | 
               | 
            
            
              | 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 | 
               | 
            
            
              | 54 | 
                  $patron->flags(undef)->store;  | 
            
            
              | 55 | 
               | 
            
            
              | 56 | 
                  $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );  | 
            
            
              | 57 | 
                  $tx->req->headers->authorization("Basic $credentials"); | 
            
            
              | 58 | 
                  $t->request_ok($tx)->status_is( 403, 'Successful authentication and not enough permissions' )  | 
            
            
              | 59 | 
                      ->json_is(  | 
            
            
              | 60 | 
                      '/error' => 'Authorization failure. Missing required permission(s).',  | 
            
            
              | 61 | 
                      'Error message returned'  | 
            
            
              | 62 | 
                      );  | 
            
            
              | 63 | 
               | 
            
            
              | 64 | 
                  $schema->storage->txn_rollback;  | 
            
            
              | 65 | 
              };  | 
            
            
              | 66 | 
               | 
            
            
              | 67 | 
              subtest 'failure tests' => sub { | 
            
            
              | 68 | 
               | 
            
            
              | 69 | 
                  plan tests => 8;  | 
            
            
              | 70 | 
               | 
            
            
              | 71 | 
                  $schema->storage->txn_begin;  | 
            
            
              | 72 | 
               | 
            
            
              | 73 | 
                  my $password     = 'AbcdEFG123';  | 
            
            
              | 74 | 
                  my $bad_password = '123456789';  | 
            
            
              | 75 | 
               | 
            
            
              | 76 | 
                  t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );  | 
            
            
              | 77 | 
               | 
            
            
              | 78 | 
                  my $patron = $builder->build_object(  | 
            
            
              | 79 | 
                      { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } ); | 
            
            
              | 80 | 
                  $patron->set_password($password);  | 
            
            
              | 81 | 
               | 
            
            
              | 82 | 
                  $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );  | 
            
            
              | 83 | 
                  $tx->req->headers->authorization("Basic "); | 
            
            
              | 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 | 
               | 
            
            
              | 95 | 
               | 
            
            
              | 96 | 
                  t::lib::Mocks::mock_preference( 'RESTBasicAuth', 0 );  | 
            
            
              | 97 | 
               | 
            
            
              | 98 | 
                  $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );  | 
            
            
              | 99 | 
                  $tx->req->headers->authorization("Basic $credentials"); | 
            
            
              | 100 | 
                  $t->request_ok($tx)  | 
            
            
              | 101 | 
                    ->status_is( 401, 'Basic authentication is disabled' )  | 
            
            
              | 102 | 
                    ->json_is( '/error' => 'Basic authentication disabled', 'Expected error message rendered' );  | 
            
            
              | 103 | 
               | 
            
            
              | 104 | 
                  $schema->storage->txn_rollback;  | 
            
            
              | 105 | 
              };  | 
            
            
              | 106 | 
               | 
            
            
              | 107 | 
              1;  |