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

(-)a/t/db_dependent/api/v1/auth_basic.t (-26 / +59 lines)
Lines 32-38 my $t = Test::Mojo->new('Koha::REST::V1'); Link Here
32
32
33
subtest 'success tests' => sub {
33
subtest 'success tests' => sub {
34
34
35
    plan tests => 10;
35
    plan tests => 2;
36
36
37
    $schema->storage->txn_begin;
37
    $schema->storage->txn_begin;
38
38
Lines 40-78 subtest 'success tests' => sub { Link Here
40
40
41
    my $password = 'AbcdEFG123';
41
    my $password = 'AbcdEFG123';
42
42
43
    my $patron = $builder->build_object(
43
    my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { userid => 'tomasito' } } );
44
        { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } );
44
    $patron->set_password( { password => $password } );
45
    $patron->set_password({ password => $password });
45
46
    my $userid = $patron->userid;
46
    my $userid     = $patron->userid;
47
    my $cardnumber = $patron->cardnumber;
47
48
48
    my $stash;
49
    my $stash;
49
    my $interface;
50
    my $interface;
50
    my $userenv;
51
    my $userenv;
51
52
52
    $t->app->hook(after_dispatch => sub {
53
    $t->app->hook(
53
        $stash     = shift->stash;
54
        after_dispatch => sub {
54
        $interface = C4::Context->interface;
55
            $stash     = shift->stash;
55
        $userenv   = C4::Context->userenv;
56
            $interface = C4::Context->interface;
56
    });
57
            $userenv   = C4::Context->userenv;
58
        }
59
    );
57
60
58
    $t->get_ok("//$userid:$password@/api/v1/patrons")
61
    subtest '`userid` login' => sub {
59
      ->status_is( 200, 'Successful authentication and permissions check' );
60
62
61
    my $user = $stash->{'koha.user'};
63
        plan tests => 10;
62
    ok( defined $user, 'The \'koha.user\' object is defined in the stash') and
63
    is( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron') and
64
    is( $user->borrowernumber, $patron->borrowernumber, 'The stashed user is the right one' );
65
    is( $userenv->{number}, $patron->borrowernumber, 'userenv set correctly' );
66
    is( $interface, 'api', "Interface correctly set to \'api\'" );
67
64
68
    $patron->flags(undef)->store;
65
        $patron->flags( 2**4 )->store;
69
66
70
    $t->get_ok("//$userid:$password@/api/v1/patrons")
67
        $t->get_ok("//$userid:$password@/api/v1/patrons")
71
      ->status_is( 403, 'Successful authentication and not enough permissions' )
68
            ->status_is( 200, 'Successful authentication and permissions check' );
72
      ->json_is(
69
73
        '/error' => 'Authorization failure. Missing required permission(s).',
70
        my $user = $stash->{'koha.user'};
74
        'Error message returned'
71
        ok( defined $user, 'The \'koha.user\' object is defined in the stash' )
75
      );
72
            and is( ref($user),            'Koha::Patron',          'Stashed koha.user object type is Koha::Patron' )
73
            and is( $user->borrowernumber, $patron->borrowernumber, 'The stashed user is the right one' );
74
        is( $userenv->{number}, $patron->borrowernumber, 'userenv set correctly' );
75
        is( $interface,         'api',                   "Interface correctly set to \'api\'" );
76
77
        $patron->flags(undef)->store;
78
79
        $t->get_ok("//$userid:$password@/api/v1/patrons")
80
            ->status_is( 403, 'Successful authentication and not enough permissions' )->json_is(
81
            '/error' => 'Authorization failure. Missing required permission(s).',
82
            'Error message returned'
83
            );
84
    };
85
86
    subtest '`cardnumber` login' => sub {
87
88
        plan tests => 10;
89
90
        $patron->flags( 2**4 )->store;
91
92
        $t->get_ok("//$cardnumber:$password@/api/v1/patrons")
93
            ->status_is( 200, 'Successful authentication and permissions check' );
94
95
        my $user = $stash->{'koha.user'};
96
        ok( defined $user, 'The \'koha.user\' object is defined in the stash' )
97
            and is( ref($user),            'Koha::Patron',          'Stashed koha.user object type is Koha::Patron' )
98
            and is( $user->borrowernumber, $patron->borrowernumber, 'The stashed user is the right one' );
99
        is( $userenv->{number}, $patron->borrowernumber, 'userenv set correctly' );
100
        is( $interface,         'api',                   "Interface correctly set to \'api\'" );
101
102
        $patron->flags(undef)->store;
103
104
        $t->get_ok("//$cardnumber:$password@/api/v1/patrons")
105
            ->status_is( 403, 'Successful authentication and not enough permissions' )->json_is(
106
            '/error' => 'Authorization failure. Missing required permission(s).',
107
            'Error message returned'
108
            );
109
    };
76
110
77
    $schema->storage->txn_rollback;
111
    $schema->storage->txn_rollback;
78
};
112
};
79
- 

Return to bug 36420