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

(-)a/t/db_dependent/api/v1/suggestions.t (-2 / +87 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 5;
20
use Test::More tests => 6;
21
use Test::Mojo;
21
use Test::Mojo;
22
22
23
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
Lines 420-422 subtest 'delete() tests' => sub { Link Here
420
420
421
    $schema->storage->txn_rollback;
421
    $schema->storage->txn_rollback;
422
};
422
};
423
- 
423
424
subtest 'Permissions tests' => sub {
425
    plan tests => 11;
426
427
    $schema->storage->txn_begin;
428
429
    my $librarian = $builder->build_object(
430
        {
431
            class => 'Koha::Patrons',
432
            value => { flags => 2**4 }    # 4 = catalogue
433
        }
434
    );
435
436
    my $password = 'thePassword123';
437
    $librarian->set_password( { password => $password, skip_validation => 1 } );
438
    my $userid = $librarian->userid;
439
440
    my $patron = $builder->build_object(
441
        {
442
            class => 'Koha::Patrons',
443
            value => { flags => 0 }
444
        }
445
    );
446
    my $patron_id = $patron->id;
447
448
    my $suggestion = $builder->build_object(
449
        {
450
            class => 'Koha::Suggestions',
451
            value => { suggestedby => $patron_id, STATUS => 'ASKED' }
452
        }
453
    );
454
    my $suggestion_data = $suggestion->to_api;
455
    delete $suggestion_data->{suggestion_id};
456
    $suggestion->delete;
457
458
    # Unauthorized attempt to write
459
    $t->post_ok(
460
        "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data )
461
      ->status_is( 403, 'SWAGGER3.2.1' );
462
463
    $builder->build(
464
        {
465
            source => 'UserPermission',
466
            value  => {
467
                borrowernumber => $librarian->id,
468
                module_bit     => 12,                     # 12 = suggestions
469
                code           => 'suggestions_create',
470
            },
471
        }
472
    );
473
474
    # Authorized attempt to write
475
    my $generated_suggestion =
476
      $t->post_ok(
477
        "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data )
478
      ->status_is( 201, 'SWAGGER3.2.1' )->header_like(
479
        Location => qr|^\/api\/v1\/suggestions\/\d*|,
480
        'SWAGGER3.4.1'
481
    )->tx->res->json;
482
483
    my $suggestion_id = $generated_suggestion->{suggestion_id};
484
    is_deeply(
485
        $generated_suggestion,
486
        Koha::Suggestions->find($suggestion_id)->to_api,
487
        'The object is returned'
488
    );
489
490
    $t->delete_ok("//$userid:$password@/api/v1/suggestions/$suggestion_id")
491
      ->status_is(403);
492
493
    $builder->build(
494
        {
495
            source => 'UserPermission',
496
            value  => {
497
                borrowernumber => $librarian->id,
498
                module_bit     => 12,                     # 12 = suggestions
499
                code           => 'suggestions_delete',
500
            },
501
        }
502
    );
503
504
    $t->delete_ok("//$userid:$password@/api/v1/suggestions/$suggestion_id")
505
      ->status_is( 204, 'SWAGGER3.2.4' )->content_is( q{}, 'SWAGGER3.3.4' );
506
507
    $schema->storage->txn_rollback;
508
};

Return to bug 33363