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

(-)a/t/db_dependent/api/v1/additional_fields.t (-1 / +113 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 t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::AdditionalFields;
27
use Koha::Database;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35
subtest 'list() tests' => sub {
36
37
    plan tests => 17;
38
39
    $schema->storage->txn_begin;
40
41
    Koha::AdditionalFields->search->delete;
42
43
    my $librarian = $builder->build_object(
44
        {
45
            class => 'Koha::Patrons',
46
            value => { flags => 2**13 }
47
        }
48
    );
49
    my $password = 'thePassword123';
50
    $librarian->set_password( { password => $password, skip_validation => 1 } );
51
    my $userid = $librarian->userid;
52
53
    my $patron = $builder->build_object(
54
        {
55
            class => 'Koha::Patrons',
56
            value => { flags => 0 }
57
        }
58
    );
59
60
    $patron->set_password( { password => $password, skip_validation => 1 } );
61
    my $unauth_userid = $patron->userid;
62
63
    ## Authorized user tests
64
    # No additional fields, so empty array should be returned
65
    $t->get_ok("//$userid:$password@/api/v1/additional_fields")->status_is(200)->json_is( [] );
66
67
    my $additional_field = $builder->build_object(
68
        {
69
            class => 'Koha::AdditionalFields',
70
            value => { tablename => 'aqinvoices', name => 'af_name' },
71
        }
72
    );
73
74
    # One additional_field created, should get returned
75
    $t->get_ok("//$userid:$password@/api/v1/additional_fields")->status_is(200)
76
        ->json_is( [ $additional_field->to_api ] );
77
78
    my $another_additional_field = $builder->build_object(
79
        {
80
            class => 'Koha::AdditionalFields',
81
            value => { tablename => 'aqinvoices', name => 'second_af_name' },
82
        }
83
    );
84
85
    my $additional_field_different_tablename = $builder->build_object(
86
        {
87
            class => 'Koha::AdditionalFields',
88
            value => { tablename => 'aqbasket', name => 'third_af_name' },
89
        }
90
    );
91
92
    # Three additional fields created, they should both be returned
93
    $t->get_ok("//$userid:$password@/api/v1/additional_fields")->status_is(200)->json_is(
94
        [
95
            $additional_field->to_api,
96
            $another_additional_field->to_api,
97
            $additional_field_different_tablename->to_api
98
        ]
99
    );
100
101
    # Filtering works, two agreements sharing vendor_id
102
    $t->get_ok( "//$userid:$password@/api/v1/additional_fields?tablename=" . $additional_field->tablename )
103
        ->status_is(200)->json_is( [ $additional_field->to_api, $another_additional_field->to_api ] );
104
105
    # Warn on unsupported query parameter
106
    $t->get_ok("//$userid:$password@/api/v1/additional_fields?blah=blah")->status_is(400)
107
        ->json_is( [ { path => '/query/blah', message => 'Malformed query string' } ] );
108
109
    # Unauthorized access
110
    $t->get_ok("//$unauth_userid:$password@/api/v1/additional_fields")->status_is(403);
111
112
    $schema->storage->txn_rollback;
113
};

Return to bug 35197