Bug 32030: ERM - Add more API tests
[koha-ffzg.git] / t / db_dependent / api / v1 / erm_users.t
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::Patron::Attributes;
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
34 subtest 'list() tests' => sub {
35
36     plan tests => 11;
37
38     $schema->storage->txn_begin;
39
40     Koha::Patrons->search->delete;
41
42     my $librarian = $builder->build_object(
43         {
44             class => 'Koha::Patrons',
45             value => { flags => 2**28 }
46         }
47     );
48
49     my $password = 'thePassword123';
50     $librarian->set_password( { password => $password, skip_validation => 1 } );
51     my $userid = $librarian->userid;
52
53
54
55     ## Authorized user tests
56     # One erm_user created, should get returned
57     $librarian->discard_changes;
58     $t->get_ok("//$userid:$password@/api/v1/erm/users")->status_is(200)
59       ->json_is( [ $librarian->to_api ] );
60
61     my $another_erm_user = $builder->build_object(
62         {
63             class => 'Koha::Patrons',
64             value => { flags => 2**28 }
65         }
66     );
67
68     # Two erm_users created, they should both be returned
69     $t->get_ok("//$userid:$password@/api/v1/erm/users")->status_is(200)
70       ->json_is( [ $librarian->to_api ] );
71
72     # Warn on unsupported query parameter
73     $t->get_ok("//$userid:$password@/api/v1/erm/users?blah=blah")
74       ->status_is(400)
75       ->json_is(
76         [ { path => '/query/blah', message => 'Malformed query string' } ] );
77
78     my $patron = $builder->build_object(
79         {
80             class => 'Koha::Patrons',
81             value => { flags => 0 }
82         }
83     );
84
85     $patron->set_password( { password => $password, skip_validation => 1 } );
86     my $unauth_userid = $patron->userid;
87     # Unauthorized access
88     $t->get_ok("//$unauth_userid:$password@/api/v1/erm/users")->status_is(403);
89
90     $schema->storage->txn_rollback;
91 };