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

(-)a/t/db_dependent/api/v1/public/lists.t (-1 / +123 lines)
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
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
use Test::Mojo;
22
23
use JSON qw(encode_json);
24
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
27
28
use Koha::Virtualshelves;
29
use Koha::Database;
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
my $t = Test::Mojo->new('Koha::REST::V1');
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
subtest 'list_public() tests' => sub {
38
39
    plan tests => 30;
40
41
    $schema->storage->txn_begin;
42
43
    my $password = 'thePassword123';
44
45
    my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
46
    my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
47
48
    $patron_1->set_password( { password => $password, skip_validation => 1 } );
49
    my $patron_1_userid = $patron_1->userid;
50
51
    $patron_2->set_password( { password => $password, skip_validation => 1 } );
52
    my $patron_2_userid = $patron_2->userid;
53
54
    my $list_1 =
55
        $builder->build_object( { class => 'Koha::Virtualshelves', value => { owner => $patron_1->id, public => 1 } } );
56
    my $list_2 =
57
        $builder->build_object( { class => 'Koha::Virtualshelves', value => { owner => $patron_1->id, public => 0 } } );
58
    my $list_3 =
59
        $builder->build_object( { class => 'Koha::Virtualshelves', value => { owner => $patron_2->id, public => 1 } } );
60
    my $list_4 =
61
        $builder->build_object( { class => 'Koha::Virtualshelves', value => { owner => $patron_2->id, public => 0 } } );
62
63
    my $q = encode_json(
64
        {
65
            list_id => {
66
                -in => [
67
                    $list_1->id,
68
                    $list_2->id,
69
                    $list_3->id,
70
                    $list_4->id,
71
                ]
72
            }
73
        }
74
    );
75
76
    # anonymous
77
    $t->get_ok("/api/v1/public/lists?q=$q")->status_is( 200, "Anonymous users can only fetch public lists" )
78
        ->json_is( [ $list_1->to_api( { public => 1 } ), $list_3->to_api( { public => 1 } ) ] );
79
80
    $t->get_ok("/api/v1/public/lists?q=$q&only_public=1")
81
        ->status_is( 200, "Anonymous users can only fetch public lists" )
82
        ->json_is( [ $list_1->to_api( { public => 1 } ), $list_3->to_api( { public => 1 } ) ] );
83
84
    $t->get_ok("/api/v1/public/lists?q=$q&only_mine=1")
85
        ->status_is( 400, "Passing only_mine on an anonymous session generates a 400 code" )
86
        ->json_is( '/error_code' => q{only_mine_forbidden} );
87
88
    $t->get_ok("//$patron_1_userid:$password@/api/v1/public/lists?q=$q&only_mine=1")
89
        ->status_is( 200, "Passing only_mine with a logged in user makes it return only their lists" )
90
        ->json_is( [ $list_1->to_api( { public => 1 } ), $list_2->to_api( { public => 1 } ) ] );
91
92
    $t->get_ok("//$patron_2_userid:$password@/api/v1/public/lists?q=$q&only_mine=1")
93
        ->status_is( 200, "Passing only_mine with a logged in user makes it return only their lists" )
94
        ->json_is( [ $list_3->to_api( { public => 1 } ), $list_4->to_api( { public => 1 } ) ] );
95
96
    # only public
97
    $t->get_ok("//$patron_1_userid:$password@/api/v1/public/lists?q=$q&only_public=1")
98
        ->status_is( 200, "Passing only_public with a logged in user makes it return only public lists" )
99
        ->json_is( [ $list_1->to_api( { public => 1 } ), $list_3->to_api( { public => 1 } ) ] );
100
101
    $t->get_ok("//$patron_2_userid:$password@/api/v1/public/lists?q=$q&only_public=1")
102
        ->status_is( 200, "Passing only_public with a logged in user makes it return only public lists" )
103
        ->json_is( [ $list_1->to_api( { public => 1 } ), $list_3->to_api( { public => 1 } ) ] );
104
105
    $t->get_ok("//$patron_1_userid:$password@/api/v1/public/lists?q=$q")
106
        ->status_is( 200, "Not filtering with only_mine or only_public makes it return all accessible lists" )
107
        ->json_is(
108
        [ $list_1->to_api( { public => 1 } ), $list_2->to_api( { public => 1 } ), $list_3->to_api( { public => 1 } ) ]
109
        );
110
111
    $t->get_ok("//$patron_2_userid:$password@/api/v1/public/lists?q=$q")
112
        ->status_is( 200, "Not filtering with only_mine or only_public makes it return all accessible lists" )
113
        ->json_is(
114
        [ $list_1->to_api( { public => 1 } ), $list_3->to_api( { public => 1 } ), $list_4->to_api( { public => 1 } ) ]
115
        );
116
117
    # conflicting params
118
    $t->get_ok("//$patron_1_userid:$password@/api/v1/public/lists?q=$q&only_public=1&only_mine=1")
119
        ->status_is( 200, "Passing only_public with a logged in user makes it return only public lists" )
120
        ->json_is( [ $list_1->to_api( { public => 1 } ) ] );
121
122
    $schema->storage->txn_rollback;
123
};

Return to bug 28965