View | Details | Raw Unified | Return to bug 29523
Collapse All | Expand All

(-)a/Koha/Patron.pm (-5 / +4 lines)
Lines 41-46 use Koha::CurbsidePickups; Link Here
41
use Koha::Database;
41
use Koha::Database;
42
use Koha::DateUtils qw( dt_from_string );
42
use Koha::DateUtils qw( dt_from_string );
43
use Koha::Encryption;
43
use Koha::Encryption;
44
use Koha::Exceptions;
44
use Koha::Exceptions::Password;
45
use Koha::Exceptions::Password;
45
use Koha::Holds;
46
use Koha::Holds;
46
use Koha::Old::Checkouts;
47
use Koha::Old::Checkouts;
Lines 2288-2298 Returns 0 if the I<user> parameter is missing. Link Here
2288
sub is_accessible {
2289
sub is_accessible {
2289
    my ( $self, $params ) = @_;
2290
    my ( $self, $params ) = @_;
2290
2291
2291
    # FIXME? It felt tempting to return 0 instead
2292
    unless ( defined( $params->{user} ) ) {
2292
    # but it would mean needing to explicitly add the 'user'
2293
        Koha::Exceptions::MissingParameter->throw( error => "The `user` parameter is mandatory" );
2293
    # param in all tests...
2294
    }
2294
    return 1
2295
      unless $params->{user};
2296
2295
2297
    my $consumer = $params->{user};
2296
    my $consumer = $params->{user};
2298
    return $consumer->can_see_patron_infos($self);
2297
    return $consumer->can_see_patron_infos($self);
(-)a/Koha/REST/V1/Patrons.pm (-4 / +9 lines)
Lines 103-109 Controller function that handles adding a new Koha::Patron object Link Here
103
=cut
103
=cut
104
104
105
sub add {
105
sub add {
106
    my $c = shift->openapi->valid_input or return;
106
    my $c            = shift->openapi->valid_input or return;
107
    my $current_user = $c->stash('koha.user');
107
108
108
    return try {
109
    return try {
109
110
Lines 168-174 sub add { Link Here
168
                $c->res->headers->location($c->req->url->to_string . '/' . $patron->borrowernumber);
169
                $c->res->headers->location($c->req->url->to_string . '/' . $patron->borrowernumber);
169
                return $c->render(
170
                return $c->render(
170
                    status  => 201,
171
                    status  => 201,
171
                    openapi => $patron->to_api
172
                    openapi => $patron->to_api( { user => $current_user } )
172
                );
173
                );
173
            }
174
            }
174
        );
175
        );
Lines 260-266 Controller function that handles updating a Koha::Patron object Link Here
260
=cut
261
=cut
261
262
262
sub update {
263
sub update {
263
    my $c = shift->openapi->valid_input or return;
264
    my $c            = shift->openapi->valid_input or return;
265
    my $current_user = $c->stash('koha.user');
264
266
265
    my $patron = Koha::Patrons->find( $c->param('patron_id') );
267
    my $patron = Koha::Patrons->find( $c->param('patron_id') );
266
268
Lines 307-313 sub update { Link Here
307
309
308
        $patron->set_from_api($body)->store;
310
        $patron->set_from_api($body)->store;
309
        $patron->discard_changes;
311
        $patron->discard_changes;
310
        return $c->render( status => 200, openapi => $patron->to_api );
312
        return $c->render(
313
            status  => 200,
314
            openapi => $patron->to_api( { user => $current_user } )
315
        );
311
    }
316
    }
312
    catch {
317
    catch {
313
        unless ( blessed $_ && $_->can('rethrow') ) {
318
        unless ( blessed $_ && $_->can('rethrow') ) {
(-)a/t/db_dependent/Koha/Patron.t (-8 / +12 lines)
Lines 682-703 subtest 'to_api() tests' => sub { Link Here
682
    my $patron = $builder->build_object(
682
    my $patron = $builder->build_object(
683
        {
683
        {
684
            class => 'Koha::Patrons',
684
            class => 'Koha::Patrons',
685
            value => {
685
            value => { debarred => undef }
686
                debarred => undef
686
        }
687
            }
687
    );
688
689
    my $consumer = $builder->build_object(
690
        {
691
            class => 'Koha::Patrons',
688
        }
692
        }
689
    );
693
    );
690
694
691
    my $restricted = $patron->to_api->{restricted};
695
    my $restricted = $patron->to_api( { user => $consumer } )->{restricted};
692
    ok( defined $restricted, 'restricted is defined' );
696
    ok( defined $restricted, 'restricted is defined' );
693
    ok( !$restricted, 'debarred is undef, restricted evaluates to false' );
697
    ok( !$restricted,        'debarred is undef, restricted evaluates to false' );
694
698
695
    $patron->debarred( dt_from_string->add( days => 1 ) )->store->discard_changes;
699
    $patron->debarred( dt_from_string->add( days => 1 ) )->store->discard_changes;
696
    $restricted = $patron->to_api->{restricted};
700
    $restricted = $patron->to_api( { user => $consumer } )->{restricted};
697
    ok( defined $restricted, 'restricted is defined' );
701
    ok( defined $restricted, 'restricted is defined' );
698
    ok( $restricted, 'debarred is defined, restricted evaluates to true' );
702
    ok( $restricted,         'debarred is defined, restricted evaluates to true' );
699
703
700
    my $patron_json = $patron->to_api({ embed => { algo => {} } });
704
    my $patron_json = $patron->to_api( { embed => { algo => {} }, user => $consumer } );
701
    ok( exists $patron_json->{algo} );
705
    ok( exists $patron_json->{algo} );
702
    is( $patron_json->{algo}, 'algo' );
706
    is( $patron_json->{algo}, 'algo' );
703
707
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (+14 lines)
Lines 422-427 subtest 'objects.search helper with query parameter' => sub { Link Here
422
    my $suggestion3 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio3->biblionumber} } );
422
    my $suggestion3 = $builder->build_object( { class => "Koha::Suggestions", value => { suggestedby => $patron2->borrowernumber, biblionumber => $biblio3->biblionumber} } );
423
423
424
    my $t = Test::Mojo->new;
424
    my $t = Test::Mojo->new;
425
426
    # Set valid koha.user in stash for to_api
427
    my $superlibrarian =
428
      $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1 } } );
429
    $t->app->hook(before_dispatch => sub {
430
       my $c = shift;
431
       $c->stash('koha.user' => $superlibrarian);
432
    });
433
    t::lib::Mocks::mock_userenv({ patron => $superlibrarian });
434
425
    $t->get_ok('/biblios' => json => {"suggestions.suggester.patron_id" => $patron1->borrowernumber })
435
    $t->get_ok('/biblios' => json => {"suggestions.suggester.patron_id" => $patron1->borrowernumber })
426
      ->json_is('/count' => 1, 'there should be 1 biblio with suggestions of patron 1');
436
      ->json_is('/count' => 1, 'there should be 1 biblio with suggestions of patron 1');
427
437
Lines 906-911 subtest 'objects.find_rs helper' => sub { Link Here
906
916
907
    $schema->storage->txn_begin;
917
    $schema->storage->txn_begin;
908
918
919
    my $superlibrarian =
920
      $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1 } } );
921
    t::lib::Mocks::mock_userenv({ patron => $superlibrarian });
922
909
    # Remove existing cities to have more control on the search results
923
    # Remove existing cities to have more control on the search results
910
    Koha::Cities->delete;
924
    Koha::Cities->delete;
911
925
(-)a/t/db_dependent/api/v1/acquisitions_baskets.t (-2 / +10 lines)
Lines 57-64 subtest 'list_managers() tests' => sub { Link Here
57
        }
57
        }
58
    );
58
    );
59
59
60
    $t->get_ok("//$userid:$password@/api/v1/acquisitions/baskets/managers?q=$api_filter")
60
    $t->get_ok(
61
      ->status_is(200)->json_is( [ $patron_with_permission->to_api, $superlibrarian->to_api ] );
61
        "//$userid:$password@/api/v1/acquisitions/baskets/managers?q=$api_filter"
62
    )->status_is(200)->json_is(
63
        [
64
            $patron_with_permission->to_api(
65
                { user => $patron_with_permission }
66
            ),
67
            $superlibrarian->to_api( { user => $superlibrarian } )
68
        ]
69
    );
62
70
63
    $schema->storage->txn_rollback;
71
    $schema->storage->txn_rollback;
64
};
72
};
(-)a/t/db_dependent/api/v1/acquisitions_funds.t (-5 / +21 lines)
Lines 113-123 subtest 'list_owners() and list_users() tests' => sub { Link Here
113
        }
113
        }
114
    );
114
    );
115
115
116
    $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds/owners?q=$api_filter")
116
    $t->get_ok(
117
      ->status_is(200)->json_is( [ $patron_with_permission->to_api, $superlibrarian->to_api ] );
117
        "//$userid:$password@/api/v1/acquisitions/funds/owners?q=$api_filter")
118
118
      ->status_is(200)->json_is(
119
    $t->get_ok("//$userid:$password@/api/v1/acquisitions/funds/users?q=$api_filter")
119
        [
120
      ->status_is(200)->json_is( [ $patron_with_permission->to_api, $superlibrarian->to_api ] );
120
            $patron_with_permission->to_api(
121
                { user => $patron_with_permission }
122
            ),
123
            $superlibrarian->to_api( { user => $superlibrarian } )
124
        ]
125
      );
126
127
    $t->get_ok(
128
        "//$userid:$password@/api/v1/acquisitions/funds/users?q=$api_filter")
129
      ->status_is(200)->json_is(
130
        [
131
            $patron_with_permission->to_api(
132
                { user => $patron_with_permission }
133
            ),
134
            $superlibrarian->to_api( { user => $superlibrarian } )
135
        ]
136
      );
121
137
122
    $schema->storage->txn_rollback;
138
    $schema->storage->txn_rollback;
123
};
139
};
(-)a/t/db_dependent/api/v1/patrons.t (-15 / +21 lines)
Lines 322-328 subtest 'add() tests' => sub { Link Here
322
322
323
    $schema->storage->txn_begin;
323
    $schema->storage->txn_begin;
324
324
325
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } )->to_api;
325
    my $librarian = $builder->build_object(
326
        {
327
            class => 'Koha::Patrons',
328
            value => { flags => 2**4 }    # borrowers flag = 4
329
        }
330
    );
331
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } )->to_api({ user => $librarian });
326
332
327
    unauthorized_access_tests('POST', undef, $patron);
333
    unauthorized_access_tests('POST', undef, $patron);
328
334
Lines 344-351 subtest 'add() tests' => sub { Link Here
344
        # Mock early, so existing mandatory attributes don't break all the tests
350
        # Mock early, so existing mandatory attributes don't break all the tests
345
        my $mocked_patron = Test::MockModule->new('Koha::Patron');
351
        my $mocked_patron = Test::MockModule->new('Koha::Patron');
346
352
347
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
353
        my $librarian = $builder->build_object(
348
        my $newpatron = $patron->to_api;
354
            {
355
                class => 'Koha::Patrons',
356
                value => { flags => 2**4 }    # borrowers flag = 4
357
            }
358
        );
359
360
        my $patron    = $builder->build_object( { class => 'Koha::Patrons' } );
361
        my $newpatron = $patron->to_api({ user => $librarian });
349
        # delete RO attributes
362
        # delete RO attributes
350
        delete $newpatron->{patron_id};
363
        delete $newpatron->{patron_id};
351
        delete $newpatron->{restricted};
364
        delete $newpatron->{restricted};
Lines 357-368 subtest 'add() tests' => sub { Link Here
357
        # Delete library
370
        # Delete library
358
        $library_to_delete->delete;
371
        $library_to_delete->delete;
359
372
360
        my $librarian = $builder->build_object(
361
            {
362
                class => 'Koha::Patrons',
363
                value => { flags => 2**4 }    # borrowers flag = 4
364
            }
365
        );
366
        my $password = 'thePassword123';
373
        my $password = 'thePassword123';
367
        $librarian->set_password( { password => $password, skip_validation => 1 } );
374
        $librarian->set_password( { password => $password, skip_validation => 1 } );
368
        my $userid = $librarian->userid;
375
        my $userid = $librarian->userid;
Lines 396-402 subtest 'add() tests' => sub { Link Here
396
        delete $newpatron->{falseproperty};
403
        delete $newpatron->{falseproperty};
397
404
398
        my $patron_to_delete = $builder->build_object({ class => 'Koha::Patrons' });
405
        my $patron_to_delete = $builder->build_object({ class => 'Koha::Patrons' });
399
        $newpatron = $patron_to_delete->to_api;
406
        $newpatron = $patron_to_delete->to_api({ user => $librarian });
400
        # delete RO attributes
407
        # delete RO attributes
401
        delete $newpatron->{patron_id};
408
        delete $newpatron->{patron_id};
402
        delete $newpatron->{restricted};
409
        delete $newpatron->{restricted};
Lines 646-652 subtest 'update() tests' => sub { Link Here
646
653
647
        my $patron_1  = $authorized_patron;
654
        my $patron_1  = $authorized_patron;
648
        my $patron_2  = $unauthorized_patron;
655
        my $patron_2  = $unauthorized_patron;
649
        my $newpatron = $unauthorized_patron->to_api;
656
        my $newpatron = $unauthorized_patron->to_api({ user => $authorized_patron });
650
        # delete RO attributes
657
        # delete RO attributes
651
        delete $newpatron->{patron_id};
658
        delete $newpatron->{patron_id};
652
        delete $newpatron->{restricted};
659
        delete $newpatron->{restricted};
Lines 723-731 subtest 'update() tests' => sub { Link Here
723
          ->status_is(200, 'Patron updated successfully');
730
          ->status_is(200, 'Patron updated successfully');
724
731
725
        # Put back the RO attributes
732
        # Put back the RO attributes
726
        $newpatron->{patron_id} = $unauthorized_patron->to_api->{patron_id};
733
        $newpatron->{patron_id} = $unauthorized_patron->to_api({ user => $authorized_patron })->{patron_id};
727
        $newpatron->{restricted} = $unauthorized_patron->to_api->{restricted};
734
        $newpatron->{restricted} = $unauthorized_patron->to_api({ user => $authorized_patron })->{restricted};
728
        $newpatron->{anonymized} = $unauthorized_patron->to_api->{anonymized};
735
        $newpatron->{anonymized} = $unauthorized_patron->to_api({ user => $authorized_patron })->{anonymized};
729
736
730
        my $got = $result->tx->res->json;
737
        my $got = $result->tx->res->json;
731
        my $updated_on_got = delete $got->{updated_on};
738
        my $updated_on_got = delete $got->{updated_on};
732
- 

Return to bug 29523