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

(-)a/t/db_dependent/api/v1/libraries.t (-152 / +81 lines)
Lines 24-62 use Test::Warn; Link Here
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
25
use t::lib::Mocks;
26
26
27
use C4::Auth;
28
use Koha::Libraries;
27
use Koha::Libraries;
29
use Koha::Database;
28
use Koha::Database;
30
29
31
my $schema  = Koha::Database->new->schema;
30
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
31
my $builder = t::lib::TestBuilder->new;
33
32
34
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
# this affects the other REST api tests
36
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
37
34
38
my $remote_address = '127.0.0.1';
35
my $t = Test::Mojo->new('Koha::REST::V1');
39
my $t              = Test::Mojo->new('Koha::REST::V1');
40
36
41
subtest 'list() tests' => sub {
37
subtest 'list() tests' => sub {
42
    plan tests => 8;
38
    plan tests => 8;
43
39
44
    $schema->storage->txn_begin;
40
    $schema->storage->txn_begin;
45
41
42
    my $patron = $builder->build_object({
43
        class => 'Koha::Patrons',
44
        value => { flags => 4 }
45
    });
46
    my $password = 'thePassword123';
47
    $patron->set_password({ password => $password, skip_validation => 1 });
48
    my $userid = $patron->userid;
49
46
    # Create test context
50
    # Create test context
47
    my $library = $builder->build_object({ class => 'Koha::Libraries' });
51
    my $library = $builder->build_object({ class => 'Koha::Libraries' });
48
    my $another_library = $library->unblessed; # create a copy of $library but make
52
    my $another_library = $library->unblessed; # create a copy of $library but make
49
    delete $another_library->{branchcode};     # sure branchcode will be regenerated
53
    delete $another_library->{branchcode};     # sure branchcode will be regenerated
50
    $another_library = $builder->build_object({ class => 'Koha::Libraries', value => $another_library });
54
    $another_library = $builder->build_object({ class => 'Koha::Libraries', value => $another_library });
51
    my ( $borrowernumber, $session_id ) = create_user_and_session( { authorized => 1 } );
52
55
53
    ## Authorized user tests
56
    ## Authorized user tests
54
    my $count_of_libraries = Koha::Libraries->search->count;
57
    my $count_of_libraries = Koha::Libraries->search->count;
55
    # Make sure we are returned with the correct amount of libraries
58
    # Make sure we are returned with the correct amount of libraries
56
    my $tx = $t->ua->build_tx( GET => '/api/v1/libraries' );
59
    $t->get_ok( "//$userid:$password@/api/v1/libraries" )
57
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
58
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
59
    $t->request_ok($tx)
60
      ->status_is( 200, 'SWAGGER3.2.2' )
60
      ->status_is( 200, 'SWAGGER3.2.2' )
61
      ->json_has('/'.($count_of_libraries-1).'/library_id')
61
      ->json_has('/'.($count_of_libraries-1).'/library_id')
62
      ->json_hasnt('/'.($count_of_libraries).'/library_id');
62
      ->json_hasnt('/'.($count_of_libraries).'/library_id');
Lines 89-110 subtest 'list() tests' => sub { Link Here
89
89
90
        foreach my $field ( keys %{$fields} ) {
90
        foreach my $field ( keys %{$fields} ) {
91
            my $model_field = $fields->{ $field };
91
            my $model_field = $fields->{ $field };
92
            $tx = $t->ua->build_tx( GET =>
93
                         "/api/v1/libraries?$field=" . $library->$model_field );
94
            $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
95
            $tx->req->env( { REMOTE_ADDR => $remote_address } );
96
            my $result =
92
            my $result =
97
            $t->request_ok($tx)
93
            $t->get_ok("//$userid:$password@/api/v1/libraries?$field=" . $library->$model_field)
98
              ->status_is(200)
94
              ->status_is(200)
99
              ->json_has( [ $library, $another_library ] );
95
              ->json_has( [ $library, $another_library ] );
100
        }
96
        }
101
    };
97
    };
102
98
103
    # Warn on unsupported query parameter
99
    # Warn on unsupported query parameter
104
    $tx = $t->ua->build_tx( GET => '/api/v1/libraries?library_blah=blah' );
100
    $t->get_ok( "//$userid:$password@/api/v1/libraries?library_blah=blah" )
105
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
106
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
107
    $t->request_ok($tx)
108
      ->status_is(400)
101
      ->status_is(400)
109
      ->json_is( [{ path => '/query/library_blah', message => 'Malformed query string'}] );
102
      ->json_is( [{ path => '/query/library_blah', message => 'Malformed query string'}] );
110
103
Lines 118-140 subtest 'get() tests' => sub { Link Here
118
    $schema->storage->txn_begin;
111
    $schema->storage->txn_begin;
119
112
120
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
113
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
121
    my ( $borrowernumber, $session_id ) =
114
    my $patron = $builder->build_object({
122
      create_user_and_session( { authorized => 1 } );
115
        class => 'Koha::Patrons',
123
116
        value => { flags => 4 }
124
    my $tx = $t->ua->build_tx( GET => "/api/v1/libraries/" . $library->branchcode );
117
    });
125
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
118
    my $password = 'thePassword123';
126
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
119
    $patron->set_password({ password => $password, skip_validation => 1 });
127
    $t->request_ok($tx)
120
    my $userid = $patron->userid;
121
122
    $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode )
128
      ->status_is( 200, 'SWAGGER3.2.2' )
123
      ->status_is( 200, 'SWAGGER3.2.2' )
129
      ->json_is( '' => Koha::REST::V1::Library::_to_api( $library->TO_JSON ), 'SWAGGER3.3.2' );
124
      ->json_is( '' => Koha::REST::V1::Library::_to_api( $library->TO_JSON ), 'SWAGGER3.3.2' );
130
125
131
    my $non_existent_code = $library->branchcode;
126
    my $non_existent_code = $library->branchcode;
132
    $library->delete;
127
    $library->delete;
133
128
134
    $tx = $t->ua->build_tx( GET => "/api/v1/libraries/" . $non_existent_code );
129
    $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $non_existent_code )
135
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
136
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
137
    $t->request_ok($tx)
138
      ->status_is(404)
130
      ->status_is(404)
139
      ->json_is( '/error' => 'Library not found' );
131
      ->json_is( '/error' => 'Library not found' );
140
132
Lines 147-179 subtest 'add() tests' => sub { Link Here
147
139
148
    $schema->storage->txn_begin;
140
    $schema->storage->txn_begin;
149
141
150
    my ( $unauthorized_borrowernumber, $unauthorized_session_id ) =
142
    my $authorized_patron = $builder->build_object({
151
      create_user_and_session( { authorized => 0 } );
143
        class => 'Koha::Patrons',
152
    my ( $authorized_borrowernumber, $authorized_session_id ) =
144
        value => { flags => 1 }
153
      create_user_and_session( { authorized => 1 } );
145
    });
146
    my $password = 'thePassword123';
147
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
148
    my $auth_userid = $authorized_patron->userid;
149
150
    my $unauthorized_patron = $builder->build_object({
151
        class => 'Koha::Patrons',
152
        value => { flags => 4 }
153
    });
154
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
155
    my $unauth_userid = $unauthorized_patron->userid;
154
156
155
    my $library_obj = $builder->build_object({ class => 'Koha::Libraries' });
157
    my $library_obj = $builder->build_object({ class => 'Koha::Libraries' });
156
    my $library     = Koha::REST::V1::Library::_to_api( $library_obj->TO_JSON );
158
    my $library     = Koha::REST::V1::Library::_to_api( $library_obj->TO_JSON );
157
    $library_obj->delete;
159
    $library_obj->delete;
158
160
159
    # Unauthorized attempt to write
161
    # Unauthorized attempt to write
160
    my $tx = $t->ua->build_tx( POST => "/api/v1/libraries" => json => $library );
162
    $t->post_ok( "//$unauth_userid:$password@/api/v1/libraries" => json => $library )
161
    $tx->req->cookies(
162
        { name => 'CGISESSID', value => $unauthorized_session_id } );
163
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
164
    $t->request_ok($tx)
165
      ->status_is(403);
163
      ->status_is(403);
166
164
167
    # Authorized attempt to write invalid data
165
    # Authorized attempt to write invalid data
168
    my $library_with_invalid_field = { %$library };
166
    my $library_with_invalid_field = { %$library };
169
    $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
167
    $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
170
168
171
    $tx = $t->ua->build_tx(
169
    $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library_with_invalid_field )
172
        POST => "/api/v1/libraries" => json => $library_with_invalid_field );
173
    $tx->req->cookies(
174
        { name => 'CGISESSID', value => $authorized_session_id } );
175
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
176
    $t->request_ok($tx)
177
      ->status_is(400)
170
      ->status_is(400)
178
      ->json_is(
171
      ->json_is(
179
        "/errors" => [
172
        "/errors" => [
Lines 185-195 subtest 'add() tests' => sub { Link Here
185
    );
178
    );
186
179
187
    # Authorized attempt to write
180
    # Authorized attempt to write
188
    $tx = $t->ua->build_tx( POST => "/api/v1/libraries" => json => $library );
181
    $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
189
    $tx->req->cookies(
190
        { name => 'CGISESSID', value => $authorized_session_id } );
191
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
192
    $t->request_ok($tx)
193
      ->status_is( 201, 'SWAGGER3.2.1' )
182
      ->status_is( 201, 'SWAGGER3.2.1' )
194
      ->json_is( '' => $library, 'SWAGGER3.3.1' )
183
      ->json_is( '' => $library, 'SWAGGER3.3.1' )
195
      ->header_is( Location => '/api/v1/libraries/' . $library->{library_id}, 'SWAGGER3.4.1' );
184
      ->header_is( Location => '/api/v1/libraries/' . $library->{library_id}, 'SWAGGER3.4.1' );
Lines 198-222 subtest 'add() tests' => sub { Link Here
198
    my $library_id = $library->{library_id};
187
    my $library_id = $library->{library_id};
199
    # Authorized attempt to create with null id
188
    # Authorized attempt to create with null id
200
    $library->{library_id} = undef;
189
    $library->{library_id} = undef;
201
    $tx = $t->ua->build_tx(
190
202
        POST => "/api/v1/libraries" => json => $library );
191
    $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
203
    $tx->req->cookies(
204
        { name => 'CGISESSID', value => $authorized_session_id } );
205
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
206
    $t->request_ok($tx)
207
      ->status_is(400)
192
      ->status_is(400)
208
      ->json_has('/errors');
193
      ->json_has('/errors');
209
194
210
    # Authorized attempt to create with existing id
195
    # Authorized attempt to create with existing id
211
    $library->{library_id} = $library_id;
196
    $library->{library_id} = $library_id;
212
    $tx = $t->ua->build_tx(
213
        POST => "/api/v1/libraries" => json => $library );
214
    $tx->req->cookies(
215
        { name => 'CGISESSID', value => $authorized_session_id } );
216
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
217
197
218
    warning_like {
198
    warning_like {
219
        $t->request_ok($tx)
199
        $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
220
          ->status_is(409)
200
          ->status_is(409)
221
          ->json_has( '/error' => "Fails when trying to add an existing library_id")
201
          ->json_has( '/error' => "Fails when trying to add an existing library_id")
222
          ->json_is(  '/conflict', 'PRIMARY' ); } # WTF
202
          ->json_is(  '/conflict', 'PRIMARY' ); } # WTF
Lines 230-250 subtest 'update() tests' => sub { Link Here
230
210
231
    $schema->storage->txn_begin;
211
    $schema->storage->txn_begin;
232
212
233
    my ( $unauthorized_borrowernumber, $unauthorized_session_id ) =
213
    my $authorized_patron = $builder->build_object({
234
      create_user_and_session( { authorized => 0 } );
214
        class => 'Koha::Patrons',
235
    my ( $authorized_borrowernumber, $authorized_session_id ) =
215
        value => { flags => 1 }
236
      create_user_and_session( { authorized => 1 } );
216
    });
217
    my $password = 'thePassword123';
218
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
219
    my $auth_userid = $authorized_patron->userid;
220
221
    my $unauthorized_patron = $builder->build_object({
222
        class => 'Koha::Patrons',
223
        value => { flags => 4 }
224
    });
225
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
226
    my $unauth_userid = $unauthorized_patron->userid;
237
227
238
    my $library    = $builder->build_object({ class => 'Koha::Libraries' });
228
    my $library    = $builder->build_object({ class => 'Koha::Libraries' });
239
    my $library_id = $library->branchcode;
229
    my $library_id = $library->branchcode;
240
230
241
    # Unauthorized attempt to update
231
    # Unauthorized attempt to update
242
    my $tx = $t->ua->build_tx( PUT => "/api/v1/libraries/$library_id"
232
    $t->put_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id"
243
        => json => { branchname => 'New unauthorized name change' } );
233
                    => json => { name => 'New unauthorized name change' } )
244
    $tx->req->cookies(
245
        { name => 'CGISESSID', value => $unauthorized_session_id } );
246
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
247
    $t->request_ok($tx)
248
      ->status_is(403);
234
      ->status_is(403);
249
235
250
    # Attempt partial update on a PUT
236
    # Attempt partial update on a PUT
Lines 252-263 subtest 'update() tests' => sub { Link Here
252
        address1 => "New library address",
238
        address1 => "New library address",
253
    };
239
    };
254
240
255
    $tx = $t->ua->build_tx( PUT => "/api/v1/libraries/$library_id" =>
241
    $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_missing_field )
256
                            json => $library_with_missing_field );
257
    $tx->req->cookies(
258
        { name => 'CGISESSID', value => $authorized_session_id } );
259
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
260
    $t->request_ok($tx)
261
      ->status_is(400)
242
      ->status_is(400)
262
      ->json_has( "/errors" =>
243
      ->json_has( "/errors" =>
263
          [ { message => "Missing property.", path => "/body/address2" } ]
244
          [ { message => "Missing property.", path => "/body/address2" } ]
Lines 268-277 subtest 'update() tests' => sub { Link Here
268
    $library_with_updated_field->{library_id} = $library_id;
249
    $library_with_updated_field->{library_id} = $library_id;
269
    $deleted_library->delete;
250
    $deleted_library->delete;
270
251
271
    $tx = $t->ua->build_tx( PUT => "/api/v1/libraries/$library_id" => json => $library_with_updated_field );
252
    $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_updated_field )
272
    $tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } );
273
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
274
    $t->request_ok($tx)
275
      ->status_is(200, 'SWAGGER3.2.1')
253
      ->status_is(200, 'SWAGGER3.2.1')
276
      ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
254
      ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
277
255
Lines 279-290 subtest 'update() tests' => sub { Link Here
279
    my $library_with_invalid_field = { %$library_with_updated_field };
257
    my $library_with_invalid_field = { %$library_with_updated_field };
280
    $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
258
    $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
281
259
282
    $tx = $t->ua->build_tx(
260
    $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_invalid_field )
283
        PUT => "/api/v1/libraries/$library_id" => json => $library_with_invalid_field );
284
    $tx->req->cookies(
285
        { name => 'CGISESSID', value => $authorized_session_id } );
286
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
287
    $t->request_ok($tx)
288
      ->status_is(400)
261
      ->status_is(400)
289
      ->json_is(
262
      ->json_is(
290
        "/errors" => [
263
        "/errors" => [
Lines 296-308 subtest 'update() tests' => sub { Link Here
296
    );
269
    );
297
270
298
    my $non_existent_code = 'nope'.int(rand(10000));
271
    my $non_existent_code = 'nope'.int(rand(10000));
299
    $tx =
272
    $t->put_ok("//$auth_userid:$password@/api/v1/libraries/$non_existent_code" => json => $library_with_updated_field)
300
      $t->ua->build_tx( PUT => "/api/v1/libraries/$non_existent_code" => json =>
301
          $library_with_updated_field );
302
    $tx->req->cookies(
303
        { name => 'CGISESSID', value => $authorized_session_id } );
304
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
305
    $t->request_ok($tx)
306
      ->status_is(404);
273
      ->status_is(404);
307
274
308
    $schema->storage->txn_rollback;
275
    $schema->storage->txn_rollback;
Lines 313-382 subtest 'delete() tests' => sub { Link Here
313
280
314
    $schema->storage->txn_begin;
281
    $schema->storage->txn_begin;
315
282
316
    my ( $unauthorized_borrowernumber, $unauthorized_session_id ) =
283
    my $authorized_patron = $builder->build_object({
317
      create_user_and_session( { authorized => 0 } );
284
        class => 'Koha::Patrons',
318
    my ( $authorized_borrowernumber, $authorized_session_id ) =
285
        value => { flags => 1 }
319
      create_user_and_session( { authorized => 1 } );
286
    });
287
    my $password = 'thePassword123';
288
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
289
    my $auth_userid = $authorized_patron->userid;
320
290
321
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
291
    my $unauthorized_patron = $builder->build_object({
292
        class => 'Koha::Patrons',
293
        value => { flags => 4 }
294
    });
295
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
296
    my $unauth_userid = $unauthorized_patron->userid;
297
298
    my $library_id = $builder->build( { source => 'Branch' } )->{branchcode};
322
299
323
    # Unauthorized attempt to delete
300
    # Unauthorized attempt to delete
324
    my $tx = $t->ua->build_tx( DELETE => "/api/v1/libraries/$branchcode" );
301
    $t->delete_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id" )
325
    $tx->req->cookies(
326
        { name => 'CGISESSID', value => $unauthorized_session_id } );
327
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
328
    $t->request_ok($tx)
329
      ->status_is(403);
302
      ->status_is(403);
330
303
331
    $tx = $t->ua->build_tx( DELETE => "/api/v1/libraries/$branchcode" );
304
    $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
332
    $tx->req->cookies(
333
        { name => 'CGISESSID', value => $authorized_session_id } );
334
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
335
    $t->request_ok($tx)
336
      ->status_is(204, 'SWAGGER3.2.4')
305
      ->status_is(204, 'SWAGGER3.2.4')
337
      ->content_is('', 'SWAGGER3.3.4');
306
      ->content_is('', 'SWAGGER3.3.4');
338
307
339
    $tx = $t->ua->build_tx( DELETE => "/api/v1/libraries/$branchcode" );
308
    $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
340
    $tx->req->cookies(
341
        { name => 'CGISESSID', value => $authorized_session_id } );
342
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
343
    $t->request_ok($tx)
344
      ->status_is(404);
309
      ->status_is(404);
345
310
346
    $schema->storage->txn_rollback;
311
    $schema->storage->txn_rollback;
347
};
312
};
348
349
sub create_user_and_session {
350
351
    my $args  = shift;
352
    my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0;
353
    my $dbh   = C4::Context->dbh;
354
355
    my $user = $builder->build(
356
        {
357
            source => 'Borrower',
358
            value  => {
359
                flags => $flags
360
            }
361
        }
362
    );
363
364
    # Create a session for the authorized user
365
    my $session = C4::Auth::get_session('');
366
    $session->param( 'number',   $user->{borrowernumber} );
367
    $session->param( 'id',       $user->{userid} );
368
    $session->param( 'ip',       '127.0.0.1' );
369
    $session->param( 'lasttime', time() );
370
    $session->flush;
371
372
    if ( $args->{authorized} ) {
373
        $dbh->do( "
374
            INSERT INTO user_permissions (borrowernumber,module_bit,code)
375
            VALUES (?,3,'parameters_remaining_permissions')", undef,
376
            $user->{borrowernumber} );
377
    }
378
379
    return ( $user->{borrowernumber}, $session->id );
380
}
381
382
1;
383
- 

Return to bug 16497