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

(-)a/t/db_dependent/api/v1/patrons.t (-127 / +167 lines)
Lines 26-31 use t::lib::Mocks; Link Here
26
26
27
use C4::Auth;
27
use C4::Auth;
28
use Koha::Database;
28
use Koha::Database;
29
use Koha::Patron::Debarments qw/AddDebarment/;
29
30
30
my $schema  = Koha::Database->new->schema;
31
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
32
my $builder = t::lib::TestBuilder->new;
Lines 41-144 subtest 'list() tests' => sub { Link Here
41
    plan tests => 2;
42
    plan tests => 2;
42
43
43
    $schema->storage->txn_begin;
44
    $schema->storage->txn_begin;
44
45
    unauthorized_access_tests('GET', undef, undef);
45
    unauthorized_access_tests('GET', undef, undef);
46
    $schema->storage->txn_rollback;
46
47
47
    subtest 'librarian access tests' => sub {
48
    subtest 'librarian access tests' => sub {
48
        plan tests => 8;
49
        plan tests => 13;
49
50
50
        my ($borrowernumber, $sessionid) = create_user_and_session({
51
        $schema->storage->txn_begin;
51
            authorized => 1 });
52
52
        my $patron = Koha::Patrons->find($borrowernumber);
53
        Koha::Patrons->search->delete;
53
        Koha::Patrons->search({
54
54
            borrowernumber => { '!=' => $borrowernumber},
55
        my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 1 });
55
            cardnumber => { LIKE => $patron->cardnumber . "%" }
56
        my $patron = Koha::Patrons->find($patron_id);
56
        })->delete;
57
        Koha::Patrons->search({
58
            borrowernumber => { '!=' => $borrowernumber},
59
            address2 => { LIKE => $patron->address2 . "%" }
60
        })->delete;
61
57
62
        my $tx = $t->ua->build_tx(GET => '/api/v1/patrons');
58
        my $tx = $t->ua->build_tx(GET => '/api/v1/patrons');
63
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
59
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
64
        $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
60
        $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
65
        $t->request_ok($tx)
61
        $t->request_ok($tx)
66
          ->status_is(200);
62
          ->status_is(200);
67
63
68
        $tx = $t->ua->build_tx(GET => '/api/v1/patrons?cardnumber='.
64
        $tx = $t->ua->build_tx(GET => '/api/v1/patrons?cardnumber=' . $patron->cardnumber);
69
                                  $patron->cardnumber);
65
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
70
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
66
        $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
71
        $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
72
        $t->request_ok($tx)
67
        $t->request_ok($tx)
73
          ->status_is(200)
68
          ->status_is(200)
74
          ->json_is('/0/cardnumber' => $patron->cardnumber);
69
          ->json_is('/0/cardnumber' => $patron->cardnumber);
75
70
76
        $tx = $t->ua->build_tx(GET => '/api/v1/patrons?address2='.
71
        $tx = $t->ua->build_tx(GET => '/api/v1/patrons?address2='.
77
                                  $patron->address2);
72
                                  $patron->address2);
78
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
73
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
79
        $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
74
        $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
80
        $t->request_ok($tx)
75
        $t->request_ok($tx)
81
          ->status_is(200)
76
          ->status_is(200)
82
          ->json_is('/0/address2' => $patron->address2);
77
          ->json_is('/0/address2' => $patron->address2);
83
    };
84
78
85
    $schema->storage->txn_rollback;
79
        my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
80
        AddDebarment({ borrowernumber => $patron_2->id });
81
        # re-read from DB
82
        $patron_2->discard_changes;
83
        my $ub = $patron_2->unblessed;
84
85
        $tx = $t->ua->build_tx( GET => '/api/v1/patrons?restricted=' . Mojo::JSON->true );
86
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
87
        $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
88
        $t->request_ok($tx)
89
          ->status_is(200)
90
          ->json_has('/0/restricted')
91
          ->json_is( '/0/restricted' => Mojo::JSON->true )
92
          ->json_hasnt('/1');
93
94
        $schema->storage->txn_rollback;
95
    };
86
};
96
};
87
97
88
subtest 'get() tests' => sub {
98
subtest 'get() tests' => sub {
89
    plan tests => 3;
99
    plan tests => 3;
90
100
91
    $schema->storage->txn_begin;
101
    $schema->storage->txn_begin;
92
93
    unauthorized_access_tests('GET', -1, undef);
102
    unauthorized_access_tests('GET', -1, undef);
103
    $schema->storage->txn_rollback;
94
104
95
    subtest 'access own object tests' => sub {
105
    subtest 'access own object tests' => sub {
96
        plan tests => 4;
106
        plan tests => 4;
97
107
98
        my ($patronid, $patronsessionid) = create_user_and_session({
108
        $schema->storage->txn_begin;
99
            authorized => 0 });
109
110
        my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 });
100
111
101
        # Access patron's own data even though they have no borrowers flag
112
        # Access patron's own data even though they have no borrowers flag
102
        my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patronid");
113
        my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron_id);
103
        $tx->req->cookies({name => 'CGISESSID', value => $patronsessionid});
114
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
104
        $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
115
        $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
105
        $t->request_ok($tx)
116
        $t->request_ok($tx)
106
          ->status_is(200);
117
          ->status_is(200);
107
118
108
        my $guarantee = $builder->build({
119
        my $guarantee = $builder->build_object({
109
            source => 'Borrower',
120
            class => 'Koha::Patrons',
110
            value  => {
121
            value => {
111
                guarantorid => $patronid,
122
                guarantorid => $patron_id,
112
            }
123
            }
113
        });
124
        });
114
125
115
        # Access guarantee's data even though guarantor has no borrowers flag
126
        # Access guarantee's data even though guarantor has no borrowers flag
116
        my $guaranteenumber = $guarantee->{borrowernumber};
127
        $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $guarantee->id );
117
        $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$guaranteenumber");
128
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
118
        $tx->req->cookies({name => 'CGISESSID', value => $patronsessionid});
129
        $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
119
        $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
120
        $t->request_ok($tx)
130
        $t->request_ok($tx)
121
          ->status_is(200);
131
          ->status_is(200);
132
133
        $schema->storage->txn_rollback;
122
    };
134
    };
123
135
124
    subtest 'librarian access tests' => sub {
136
    subtest 'librarian access tests' => sub {
125
        plan tests => 5;
137
        plan tests => 6;
126
138
127
        my ($patron_id) = create_user_and_session({
139
        $schema->storage->txn_begin;
128
            authorized => 0 });
140
129
        my $patron = Koha::Patrons->find($patron_id);
141
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
130
        my ($borrowernumber, $sessionid) = create_user_and_session({
142
        my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
131
            authorized => 1 });
143
132
        my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id");
144
        my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $patron->id);
133
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
145
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
134
        $t->request_ok($tx)
146
        $t->request_ok($tx)
135
          ->status_is(200)
147
          ->status_is(200)
136
          ->json_is('/borrowernumber' => $patron_id)
148
          ->json_is('/patron_id'        => $patron->id)
137
          ->json_is('/surname' => $patron->surname)
149
          ->json_is('/category_id'      => $patron->categorycode )
138
          ->json_is('/lost' => Mojo::JSON->false );
150
          ->json_is('/surname'          => $patron->surname)
139
    };
151
          ->json_is('/patron_card_lost' => Mojo::JSON->false );
140
152
141
    $schema->storage->txn_rollback;
153
        $schema->storage->txn_rollback;
154
    };
142
};
155
};
143
156
144
subtest 'add() tests' => sub {
157
subtest 'add() tests' => sub {
Lines 146-269 subtest 'add() tests' => sub { Link Here
146
159
147
    $schema->storage->txn_begin;
160
    $schema->storage->txn_begin;
148
161
149
    my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
162
    my $patron = Koha::REST::V1::Patrons::_to_api(
150
    my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
163
        $builder->build_object( { class => 'Koha::Patrons' } )->TO_JSON );
151
    my $newpatron = {
152
        address      => 'Street',
153
        branchcode   => $branchcode,
154
        cardnumber   => $branchcode.$categorycode,
155
        categorycode => $categorycode,
156
        city         => 'Joenzoo',
157
        surname      => "TestUser",
158
        userid       => $branchcode.$categorycode,
159
    };
160
164
161
    unauthorized_access_tests('POST', undef, $newpatron);
165
    unauthorized_access_tests('POST', undef, $patron);
166
167
    $schema->storage->txn_rollback;
162
168
163
    subtest 'librarian access tests' => sub {
169
    subtest 'librarian access tests' => sub {
164
        plan tests => 20;
170
        plan tests => 20;
165
171
166
        my ($borrowernumber, $sessionid) = create_user_and_session({
172
        $schema->storage->txn_begin;
167
            authorized => 1 });
173
174
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
175
        my $newpatron = Koha::REST::V1::Patrons::_to_api( $patron->TO_JSON );
176
        delete $newpatron->{patron_id};
177
178
        # Create a library just to make sure its ID doesn't exist on the DB
179
        my $library_to_delete = $builder->build_object({ class => 'Koha::Libraries' });
180
        my $deleted_library_id = $library_to_delete->id;
181
        # Delete library
182
        $library_to_delete->delete;
168
183
169
        $newpatron->{branchcode} = "nonexistent"; # Test invalid branchcode
184
        my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
185
186
        $newpatron->{library_id} = $deleted_library_id;
170
        my $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron );
187
        my $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron );
171
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
188
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
172
        warning_like {
189
        warning_like {
173
            $t->request_ok($tx)
190
            $t->request_ok($tx)
174
              ->status_is(400)
191
              ->status_is(409)
175
              ->json_is('/error' => "Given branchcode does not exist"); }
192
              ->json_is('/error' => "Duplicate ID"); }
176
            qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/;
193
            qr/^DBD::mysql::st execute failed: Duplicate entry/;
194
195
        $newpatron->{library_id} = $patron->branchcode;
177
196
178
        $newpatron->{branchcode} = $branchcode;
197
        # Create a library just to make sure its ID doesn't exist on the DB
198
        my $category_to_delete = $builder->build_object({ class => 'Koha::Patron::Categories' });
199
        my $deleted_category_id = $category_to_delete->id;
200
        # Delete library
201
        $category_to_delete->delete;
179
202
180
        $newpatron->{categorycode} = "nonexistent"; # Test invalid patron category
203
        $newpatron->{category_id} = $deleted_category_id; # Test invalid patron category
181
        $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
204
        $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
182
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
205
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
183
        $t->request_ok($tx)
206
        $t->request_ok($tx)
184
          ->status_is(400)
207
          ->status_is(400)
185
          ->json_is('/error' => "Given categorycode does not exist");
208
          ->json_is('/error' => "Given category_id does not exist");
186
        $newpatron->{categorycode} = $categorycode;
209
        $newpatron->{category_id} = $patron->categorycode;
187
210
188
        $newpatron->{falseproperty} = "Non existent property";
211
        $newpatron->{falseproperty} = "Non existent property";
189
        $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
212
        $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
190
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
213
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
191
        $t->request_ok($tx)
214
        $t->request_ok($tx)
192
          ->status_is(400);
215
          ->status_is(400);
193
        delete $newpatron->{falseproperty};
216
        delete $newpatron->{falseproperty};
194
217
218
        my $patron_to_delete = $builder->build_object({ class => 'Koha::Patrons' });
219
        $newpatron = Koha::REST::V1::Patrons::_to_api($patron_to_delete->TO_JSON);
220
        delete $newpatron->{patron_id};
221
        $patron_to_delete->delete;
222
195
        $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
223
        $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
196
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
224
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
197
        $t->request_ok($tx)
225
        $t->request_ok($tx)
198
          ->status_is(201, 'Patron created successfully')
226
          ->status_is(201, 'Patron created successfully')
199
          ->json_has('/borrowernumber', 'got a borrowernumber')
227
          ->json_has('/patron_id', 'got a patron_id')
200
          ->json_is('/cardnumber', $newpatron->{ cardnumber })
228
          ->json_is( '/cardnumber' => $newpatron->{ cardnumber })
201
          ->json_is('/surname' => $newpatron->{ surname })
229
          ->json_is( '/surname'    => $newpatron->{ surname })
202
          ->json_is('/firstname' => $newpatron->{ firstname });
230
          ->json_is( '/firstname'  => $newpatron->{ firstname });
203
        $newpatron->{borrowernumber} = $tx->res->json->{borrowernumber};
204
231
205
        $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
232
        $tx = $t->ua->build_tx(POST => "/api/v1/patrons" => json => $newpatron);
206
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
233
        $tx->req->cookies({name => 'CGISESSID', value => $session_id});
207
        warning_like {
234
        warning_like {
208
            $t->request_ok($tx)
235
            $t->request_ok($tx)
209
              ->status_is(409)
236
              ->status_is(409)
210
              ->json_has( '/error', 'Fails when trying to POST duplicate cardnumber' )
237
              ->json_has( '/error', 'Fails when trying to POST duplicate cardnumber' )
211
              ->json_has( '/conflict', 'cardnumber' ); }
238
              ->json_has( '/conflict', 'cardnumber' ); }
212
            qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/;
239
            qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/;
213
    };
214
240
215
    $schema->storage->txn_rollback;
241
        $schema->storage->txn_rollback;
242
    };
216
};
243
};
217
244
218
subtest 'update() tests' => sub {
245
subtest 'update() tests' => sub {
219
    plan tests => 2;
246
    plan tests => 2;
220
247
221
    $schema->storage->txn_begin;
248
    $schema->storage->txn_begin;
222
223
    unauthorized_access_tests('PUT', 123, {email => 'nobody@example.com'});
249
    unauthorized_access_tests('PUT', 123, {email => 'nobody@example.com'});
250
    $schema->storage->txn_rollback;
224
251
225
    subtest 'librarian access tests' => sub {
252
    subtest 'librarian access tests' => sub {
226
        plan tests => 23;
253
        plan tests => 23;
227
254
255
        $schema->storage->txn_begin;
256
228
        t::lib::Mocks::mock_preference('minPasswordLength', 1);
257
        t::lib::Mocks::mock_preference('minPasswordLength', 1);
229
        my ($borrowernumber, $sessionid) = create_user_and_session({ authorized => 1 });
258
        my ( $patron_id_1, $session_id ) = create_user_and_session({ authorized => 1 });
230
        my ($borrowernumber2, undef) = create_user_and_session({ authorized => 0 });
259
        my ( $patron_id_2, undef )       = create_user_and_session({ authorized => 0 });
231
260
232
        my $patron_1  = Koha::Patrons->find($borrowernumber);
261
        my $patron_1  = Koha::Patrons->find($patron_id_1);
233
        my $patron_2  = Koha::Patrons->find($borrowernumber2);
262
        my $patron_2  = Koha::Patrons->find($patron_id_2);
234
        my $newpatron = $patron_2->TO_JSON;
263
        my $newpatron = Koha::REST::V1::Patrons::_to_api($patron_2->TO_JSON);
235
        # borrowernumber should not be passed in the request body for PUT
264
        # borrowernumber should not be passed in the request body for PUT
236
        delete $newpatron->{borrowernumber};
265
        delete $newpatron->{borrowernumber};
237
266
238
        my $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/-1" => json => $newpatron );
267
        my $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/-1" => json => $newpatron );
239
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
268
        $tx->req->cookies({name => 'CGISESSID', value => $session_id});
240
        $t->request_ok($tx)
269
        $t->request_ok($tx)
241
          ->status_is(404)
270
          ->status_is(404)
242
          ->json_has('/error', 'Fails when trying to PUT nonexistent patron');
271
          ->json_has('/error', 'Fails when trying to PUT nonexistent patron');
243
272
244
        $newpatron->{categorycode} = 'nonexistent';
273
        # Create a library just to make sure its ID doesn't exist on the DB
245
        $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron );
274
        my $category_to_delete = $builder->build_object({ class => 'Koha::Patron::Categories' });
246
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
275
        my $deleted_category_id = $category_to_delete->id;
276
        # Delete library
277
        $category_to_delete->delete;
278
279
        $newpatron->{category_id} = $deleted_category_id;
280
        $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$patron_id_2" => json => $newpatron );
281
        $tx->req->cookies({name => 'CGISESSID', value => $session_id});
247
        warning_like {
282
        warning_like {
248
            $t->request_ok($tx)
283
            $t->request_ok($tx)
249
              ->status_is(400)
284
              ->status_is(400)
250
              ->json_is('/error' => "Given categorycode does not exist"); }
285
              ->json_is('/error' => "Given category_id does not exist"); }
251
            qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/;
286
            qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/;
252
        $newpatron->{categorycode} = $patron_2->categorycode;
287
        $newpatron->{category_id} = $patron_2->categorycode;
288
289
        # Create a library just to make sure its ID doesn't exist on the DB
290
        my $library_to_delete = $builder->build_object({ class => 'Koha::Libraries' });
291
        my $deleted_library_id = $library_to_delete->id;
292
        # Delete library
293
        $library_to_delete->delete;
253
294
254
        $newpatron->{branchcode} = 'nonexistent';
295
        $newpatron->{library_id} = $deleted_library_id;
255
        $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron );
296
        $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron );
256
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
297
        $tx->req->cookies({name => 'CGISESSID', value => $session_id});
257
        warning_like {
298
        warning_like {
258
            $t->request_ok($tx)
299
            $t->request_ok($tx)
259
              ->status_is(400)
300
              ->status_is(400)
260
              ->json_is('/error' => "Given branchcode does not exist"); }
301
              ->json_is('/error' => "Given library_id does not exist"); }
261
            qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/;
302
            qr/^DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/;
262
        $newpatron->{branchcode} = $patron_2->branchcode;
303
        $newpatron->{library_id} = $patron_2->branchcode;
263
304
264
        $newpatron->{falseproperty} = "Non existent property";
305
        $newpatron->{falseproperty} = "Non existent property";
265
        $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron );
306
        $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron );
266
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
307
        $tx->req->cookies({name => 'CGISESSID', value => $session_id});
267
        $t->request_ok($tx)
308
        $t->request_ok($tx)
268
          ->status_is(400)
309
          ->status_is(400)
269
          ->json_is('/errors/0/message' =>
310
          ->json_is('/errors/0/message' =>
Lines 274-281 subtest 'update() tests' => sub { Link Here
274
        $newpatron->{cardnumber} = $patron_1->cardnumber;
315
        $newpatron->{cardnumber} = $patron_1->cardnumber;
275
        $newpatron->{userid}     = $patron_1->userid;
316
        $newpatron->{userid}     = $patron_1->userid;
276
317
277
        $tx = $t->ua->build_tx( PUT => "/api/v1/patrons/$borrowernumber2" => json => $newpatron );
318
        $tx = $t->ua->build_tx( PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron );
278
        $tx->req->cookies({ name => 'CGISESSID', value => $sessionid });
319
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
279
        warning_like {
320
        warning_like {
280
            $t->request_ok($tx)
321
            $t->request_ok($tx)
281
              ->status_is(409)
322
              ->status_is(409)
Lines 283-340 subtest 'update() tests' => sub { Link Here
283
              ->json_is(  '/conflict', 'cardnumber' ); }
324
              ->json_is(  '/conflict', 'cardnumber' ); }
284
            qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/;
325
            qr/^DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key 'cardnumber'/;
285
326
286
        $newpatron->{ cardnumber } = $borrowernumber.$borrowernumber2;
327
        $newpatron->{ cardnumber } = $patron_id_1.$patron_id_2;
287
        $newpatron->{ userid } = "user".$borrowernumber.$borrowernumber2;
328
        $newpatron->{ userid }     = "user".$patron_id_1.$patron_id_2;
288
        $newpatron->{ surname } = "user".$borrowernumber.$borrowernumber2;
329
        $newpatron->{ surname }    = "user".$patron_id_1.$patron_id_2;
289
330
290
        $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron);
331
        $tx = $t->ua->build_tx(PUT => "/api/v1/patrons/" . $patron_2->id => json => $newpatron);
291
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
332
        $tx->req->cookies({name => 'CGISESSID', value => $session_id});
292
        $t->request_ok($tx)
333
        $t->request_ok($tx)
293
          ->status_is(200, 'Patron updated successfully')
334
          ->status_is(200, 'Patron updated successfully')
294
          ->json_has($newpatron);
335
          ->json_has($newpatron);
295
        is(Koha::Patrons->find( $patron_2->id )->cardnumber,
336
        is(Koha::Patrons->find( $patron_2->id )->cardnumber,
296
           $newpatron->{ cardnumber }, 'Patron is really updated!');
337
           $newpatron->{ cardnumber }, 'Patron is really updated!');
297
    };
298
338
299
    $schema->storage->txn_rollback;
339
        $schema->storage->txn_rollback;
340
    };
300
};
341
};
301
342
302
subtest 'delete() tests' => sub {
343
subtest 'delete() tests' => sub {
303
    plan tests => 2;
344
    plan tests => 2;
304
345
305
    $schema->storage->txn_begin;
346
    $schema->storage->txn_begin;
306
307
    unauthorized_access_tests('DELETE', 123, undef);
347
    unauthorized_access_tests('DELETE', 123, undef);
348
    $schema->storage->txn_rollback;
308
349
309
    subtest 'librarian access test' => sub {
350
    subtest 'librarian access test' => sub {
310
        plan tests => 4;
351
        plan tests => 4;
311
352
312
        my ($borrowernumber, $sessionid) = create_user_and_session({
353
        $schema->storage->txn_begin;
313
            authorized => 1 });
354
314
        my ($borrowernumber2, $sessionid2) = create_user_and_session({
355
        my ( undef, $session_id ) = create_user_and_session({ authorized => 1 });
315
            authorized => 0 });
356
        my ( $patron_id, undef )  = create_user_and_session({ authorized => 0 });
316
357
317
        my $tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/-1");
358
        my $tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/-1");
318
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
359
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
319
        $t->request_ok($tx)
360
        $t->request_ok($tx)
320
          ->status_is(404, 'Patron not found');
361
          ->status_is(404, 'Patron not found');
321
362
322
        $tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/$borrowernumber2");
363
        $tx = $t->ua->build_tx(DELETE => "/api/v1/patrons/$patron_id");
323
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
364
        $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
324
        $t->request_ok($tx)
365
        $t->request_ok($tx)
325
          ->status_is(200, 'Patron deleted successfully');
366
          ->status_is(200, 'Patron deleted successfully');
326
    };
327
367
328
    $schema->storage->txn_rollback;
368
        $schema->storage->txn_rollback;
369
    };
329
};
370
};
330
371
331
# Centralized tests for 401s and 403s assuming the endpoint requires
372
# Centralized tests for 401s and 403s assuming the endpoint requires
332
# borrowers flag for access
373
# borrowers flag for access
333
sub unauthorized_access_tests {
374
sub unauthorized_access_tests {
334
    my ($verb, $patronid, $json) = @_;
375
    my ($verb, $patron_id, $json) = @_;
335
376
336
    my $endpoint = '/api/v1/patrons';
377
    my $endpoint = '/api/v1/patrons';
337
    $endpoint .= ($patronid) ? "/$patronid" : '';
378
    $endpoint .= ($patron_id) ? "/$patron_id" : '';
338
379
339
    subtest 'unauthorized access tests' => sub {
380
    subtest 'unauthorized access tests' => sub {
340
        plan tests => 5;
381
        plan tests => 5;
Lines 343-353 sub unauthorized_access_tests { Link Here
343
        $t->request_ok($tx)
384
        $t->request_ok($tx)
344
          ->status_is(401);
385
          ->status_is(401);
345
386
346
        my ($borrowernumber, $sessionid) = create_user_and_session({
387
        my ($borrowernumber, $session_id) = create_user_and_session({
347
            authorized => 0 });
388
            authorized => 0 });
348
389
349
        $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
390
        $tx = $t->ua->build_tx($verb => $endpoint => json => $json);
350
        $tx->req->cookies({name => 'CGISESSID', value => $sessionid});
391
        $tx->req->cookies({name => 'CGISESSID', value => $session_id});
351
        $t->request_ok($tx)
392
        $t->request_ok($tx)
352
          ->status_is(403)
393
          ->status_is(403)
353
          ->json_is('/required_permissions', {"borrowers" => "1"});
394
          ->json_is('/required_permissions', {"borrowers" => "1"});
354
- 

Return to bug 19784