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

(-)a/t/db_dependent/api/v1/patrons_account_links.t (-1 / +236 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 <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::NoWarnings;
21
use Test::More tests => 5;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use Koha::Patron::AccountLinks;
28
use Koha::Database;
29
30
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
32
33
my $t = Test::Mojo->new('Koha::REST::V1');
34
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36
subtest 'list() tests' => sub {
37
    plan tests => 11;
38
39
    $schema->storage->txn_begin;
40
41
    my $librarian = $builder->build_object(
42
        {
43
            class => 'Koha::Patrons',
44
            value => { flags => 2**4 }    # borrowers flag
45
        }
46
    );
47
    my $password = 'thePassword123';
48
    $librarian->set_password( { password => $password, skip_validation => 1 } );
49
    my $userid = $librarian->userid;
50
51
    my $patron = $builder->build_object(
52
        {
53
            class => 'Koha::Patrons',
54
            value => { flags => 0 }
55
        }
56
    );
57
    $patron->set_password( { password => $password, skip_validation => 1 } );
58
    my $unauth_userid = $patron->userid;
59
60
    my $test_patron = $builder->build_object( { class => 'Koha::Patrons' } );
61
62
    $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $test_patron->borrowernumber . "/account_links" )
63
        ->status_is(200)
64
        ->json_is( [] );
65
66
    my $linked_patron = $builder->build_object( { class => 'Koha::Patrons' } );
67
    my $link_group_id = Koha::Patron::AccountLinks->get_next_group_id();
68
69
    Koha::Patron::AccountLink->new(
70
        { link_group_id => $link_group_id, borrowernumber => $test_patron->borrowernumber } )->store;
71
    Koha::Patron::AccountLink->new(
72
        { link_group_id => $link_group_id, borrowernumber => $linked_patron->borrowernumber } )->store;
73
74
    $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $test_patron->borrowernumber . "/account_links" )
75
        ->status_is(200)
76
        ->json_has('/0')
77
        ->json_has('/1');
78
79
    $t->get_ok( "//$unauth_userid:$password@/api/v1/patrons/" . $test_patron->borrowernumber . "/account_links" )
80
        ->status_is(403);
81
82
    $t->get_ok("//$userid:$password@/api/v1/patrons/99999999/account_links")->status_is(404);
83
84
    $schema->storage->txn_rollback;
85
};
86
87
subtest 'add() tests' => sub {
88
    plan tests => 13;
89
90
    $schema->storage->txn_begin;
91
92
    my $librarian = $builder->build_object(
93
        {
94
            class => 'Koha::Patrons',
95
            value => { flags => 2**4 }
96
        }
97
    );
98
    my $password = 'thePassword123';
99
    $librarian->set_password( { password => $password, skip_validation => 1 } );
100
    my $userid = $librarian->userid;
101
102
    my $patron = $builder->build_object(
103
        {
104
            class => 'Koha::Patrons',
105
            value => { flags => 0 }
106
        }
107
    );
108
    $patron->set_password( { password => $password, skip_validation => 1 } );
109
    my $unauth_userid = $patron->userid;
110
111
    my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
112
    my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } );
113
114
    $t->post_ok( "//$unauth_userid:$password@/api/v1/patrons/"
115
            . $patron1->borrowernumber
116
            . "/account_links" => json => { linked_patron_id => $patron2->borrowernumber } )->status_is(403);
117
118
    $t->post_ok( "//$userid:$password@/api/v1/patrons/"
119
            . $patron1->borrowernumber
120
            . "/account_links" => json => { linked_patron_id => $patron2->borrowernumber } )
121
        ->status_is(201)
122
        ->json_has('/account_link_id')
123
        ->json_has('/link_group_id');
124
125
    ok( $patron1->account_link, 'patron1 now has account link' );
126
    ok( $patron2->account_link, 'patron2 now has account link' );
127
    is(
128
        $patron1->account_link->link_group_id, $patron2->account_link->link_group_id,
129
        'Both patrons in same link group'
130
    );
131
132
    $t->post_ok( "//$userid:$password@/api/v1/patrons/"
133
            . $patron1->borrowernumber
134
            . "/account_links" => json => { linked_patron_id => $patron2->borrowernumber } )->status_is(409);
135
136
    $t->post_ok( "//$userid:$password@/api/v1/patrons/"
137
            . $patron1->borrowernumber
138
            . "/account_links" => json => { linked_patron_id => 99999999 } )->status_is(404);
139
140
    $schema->storage->txn_rollback;
141
};
142
143
subtest 'add() to existing group' => sub {
144
    plan tests => 5;
145
146
    $schema->storage->txn_begin;
147
148
    my $librarian = $builder->build_object(
149
        {
150
            class => 'Koha::Patrons',
151
            value => { flags => 2**4 }
152
        }
153
    );
154
    my $password = 'thePassword123';
155
    $librarian->set_password( { password => $password, skip_validation => 1 } );
156
    my $userid = $librarian->userid;
157
158
    my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
159
    my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } );
160
    my $patron3 = $builder->build_object( { class => 'Koha::Patrons' } );
161
162
    my $link_group_id = Koha::Patron::AccountLinks->get_next_group_id();
163
    Koha::Patron::AccountLink->new( { link_group_id => $link_group_id, borrowernumber => $patron1->borrowernumber } )
164
        ->store;
165
    Koha::Patron::AccountLink->new( { link_group_id => $link_group_id, borrowernumber => $patron2->borrowernumber } )
166
        ->store;
167
168
    $t->post_ok( "//$userid:$password@/api/v1/patrons/"
169
            . $patron1->borrowernumber
170
            . "/account_links" => json => { linked_patron_id => $patron3->borrowernumber } )->status_is(201);
171
172
    ok( $patron3->account_link, 'patron3 now has account link' );
173
    is( $patron3->account_link->link_group_id, $link_group_id, 'patron3 added to existing group' );
174
175
    is(
176
        Koha::Patron::AccountLinks->search( { link_group_id => $link_group_id } )->count,
177
        3, 'Now 3 patrons in group'
178
    );
179
180
    $schema->storage->txn_rollback;
181
};
182
183
subtest 'delete() tests' => sub {
184
    plan tests => 10;
185
186
    $schema->storage->txn_begin;
187
188
    my $librarian = $builder->build_object(
189
        {
190
            class => 'Koha::Patrons',
191
            value => { flags => 2**4 }
192
        }
193
    );
194
    my $password = 'thePassword123';
195
    $librarian->set_password( { password => $password, skip_validation => 1 } );
196
    my $userid = $librarian->userid;
197
198
    my $patron = $builder->build_object(
199
        {
200
            class => 'Koha::Patrons',
201
            value => { flags => 0 }
202
        }
203
    );
204
    $patron->set_password( { password => $password, skip_validation => 1 } );
205
    my $unauth_userid = $patron->userid;
206
207
    my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
208
    my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } );
209
    my $patron3 = $builder->build_object( { class => 'Koha::Patrons' } );
210
211
    # Use 3-patron group so deleting one leaves 2 (no orphan cleanup triggered)
212
    my $link_group_id = Koha::Patron::AccountLinks->get_next_group_id();
213
    my $link1         = Koha::Patron::AccountLink->new(
214
        { link_group_id => $link_group_id, borrowernumber => $patron1->borrowernumber } )->store;
215
    Koha::Patron::AccountLink->new( { link_group_id => $link_group_id, borrowernumber => $patron2->borrowernumber } )
216
        ->store;
217
    Koha::Patron::AccountLink->new( { link_group_id => $link_group_id, borrowernumber => $patron3->borrowernumber } )
218
        ->store;
219
220
    $t->delete_ok(
221
        "//$unauth_userid:$password@/api/v1/patrons/" . $patron1->borrowernumber . "/account_links/" . $link1->id )
222
        ->status_is(403);
223
224
    $t->delete_ok( "//$userid:$password@/api/v1/patrons/" . $patron1->borrowernumber . "/account_links/" . $link1->id )
225
        ->status_is(204);
226
227
    ok( !$patron1->account_link, 'patron1 link removed' );
228
    ok( $patron2->account_link,  'patron2 link still exists (3-patron group, no orphan cleanup)' );
229
230
    $t->delete_ok( "//$userid:$password@/api/v1/patrons/" . $patron1->borrowernumber . "/account_links/" . $link1->id )
231
        ->status_is(404);
232
233
    $t->delete_ok("//$userid:$password@/api/v1/patrons/99999999/account_links/1")->status_is(404);
234
235
    $schema->storage->txn_rollback;
236
};

Return to bug 39658