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

(-)a/t/db_dependent/api/v1/illrequests.t (-81 / +42 lines)
Lines 18-42 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 1;
20
use Test::More tests => 1;
21
21
use Test::MockModule;
22
use Test::MockModule;
22
use Test::MockObject;
23
use Test::MockObject;
23
use Test::Mojo;
24
use Test::Mojo;
24
use Test::Warn;
25
25
26
use t::lib::TestBuilder;
26
use t::lib::TestBuilder;
27
use t::lib::Mocks;
27
use t::lib::Mocks;
28
28
29
use C4::Auth;
30
use Koha::Illrequests;
29
use Koha::Illrequests;
31
use Koha::DateUtils qw( format_sqldatetime );
30
use Koha::DateUtils qw( format_sqldatetime );
32
31
33
my $schema  = Koha::Database->new->schema;
32
my $schema  = Koha::Database->new->schema;
34
my $builder = t::lib::TestBuilder->new;
33
my $builder = t::lib::TestBuilder->new;
35
34
36
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
36
38
my $remote_address = '127.0.0.1';
37
my $t = Test::Mojo->new('Koha::REST::V1');
39
my $t              = Test::Mojo->new('Koha::REST::V1');
40
38
41
subtest 'list() tests' => sub {
39
subtest 'list() tests' => sub {
42
40
Lines 70-88 subtest 'list() tests' => sub { Link Here
70
    $schema->storage->txn_begin;
68
    $schema->storage->txn_begin;
71
69
72
    Koha::Illrequests->search->delete;
70
    Koha::Illrequests->search->delete;
73
    # ill => 22 (userflags.sql)
71
74
    my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 });
72
    # create an authorized user
73
    my $patron = $builder->build_object({
74
        class => 'Koha::Patrons',
75
        value => { flags => 2 ** 22 } # 22 => ill
76
    });
77
    my $password = 'thePassword123';
78
    $patron->set_password({ password => $password, skip_validation => 1 });
79
    my $userid = $patron->userid;
75
80
76
    ## Authorized user tests
81
    ## Authorized user tests
77
    # No requests, so empty array should be returned
82
    # No requests, so empty array should be returned
78
    my $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
83
    $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
79
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
84
      ->status_is(200)
80
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
85
      ->json_is( [] );
81
    $t->request_ok($tx)->status_is(200)->json_is( [] );
82
86
83
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
87
    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
84
    my $patron_1  = $builder->build_object( { class => 'Koha::Patrons' } );
88
    my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
85
    my $patron_2  = $builder->build_object( { class => 'Koha::Patrons' } );
89
    my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
86
90
87
    # Create an ILL request
91
    # Create an ILL request
88
    my $illrequest = $builder->build_object(
92
    my $illrequest = $builder->build_object(
Lines 104-127 subtest 'list() tests' => sub { Link Here
104
    my $req_formatted = add_formatted($response);
108
    my $req_formatted = add_formatted($response);
105
109
106
    # One illrequest created, should get returned
110
    # One illrequest created, should get returned
107
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
111
    $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
108
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
112
      ->status_is(200)
109
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
113
      ->json_is( [ $req_formatted ] );
110
    $t->request_ok($tx)->status_is(200)->json_is( [ $req_formatted ] );
111
114
112
    # One illrequest created, returned with augmented data
115
    # One illrequest created, returned with augmented data
113
    $tx = $t->ua->build_tx( GET =>
116
    $t->get_ok( "//$userid:$password@/api/v1/illrequests?embed=patron,library,capabilities,metadata,requested_partners" )
114
          '/api/v1/illrequests?embed=patron,library,capabilities,metadata,requested_partners' );
117
      ->status_is(200)
115
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
118
      ->json_has( '/0/patron', 'patron embedded' )
116
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
119
      ->json_is( '/0/patron/patron_id', $patron_1->borrowernumber, 'The right patron is embeded')
117
    $t->request_ok($tx)->status_is(200)
120
      ->json_has( '/0/requested_partners', 'requested_partners embedded' )
118
        ->json_has( '/0/patron', 'patron embedded' )
121
      ->json_has( '/0/capabilities', 'capabilities embedded' )
119
        ->json_is( '/0/patron/patron_id', $patron_1->borrowernumber, 'The right patron is embeded')
122
      ->json_has( '/0/library', 'library embedded'  )
120
        ->json_has( '/0/requested_partners', 'requested_partners embedded' )
123
      ->json_has( '/0/metadata', 'metadata embedded'  )
121
        ->json_has( '/0/capabilities', 'capabilities embedded' )
124
      ->json_hasnt( '/1', 'Only one request was created' );
122
        ->json_has( '/0/library', 'library embedded'  )
123
        ->json_has( '/0/metadata', 'metadata embedded'  )
124
        ->json_hasnt( '/1', 'Only one request was created' );
125
125
126
    # Create another ILL request
126
    # Create another ILL request
127
    my $illrequest2 = $builder->build_object(
127
    my $illrequest2 = $builder->build_object(
Lines 143-182 subtest 'list() tests' => sub { Link Here
143
    my $req2_formatted = add_formatted($response2);
143
    my $req2_formatted = add_formatted($response2);
144
144
145
    # Two illrequest created, should get returned
145
    # Two illrequest created, should get returned
146
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
146
    $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
147
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
147
      ->status_is(200)
148
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
149
    $t->request_ok($tx)->status_is(200)
150
      ->json_is( [ $req_formatted, $req2_formatted ] );
148
      ->json_is( [ $req_formatted, $req2_formatted ] );
151
149
152
    # Warn on unsupported query parameter
150
    # Warn on unsupported query parameter
153
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
151
    $t->get_ok( "//$userid:$password@/api/v1/illrequests?request_blah=blah" )
154
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
152
      ->status_is(400)
155
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
153
      ->json_is(
156
    $t->request_ok($tx)->status_is(400)->json_is(
157
        [{ path => '/query/request_blah', message => 'Malformed query string'}]
154
        [{ path => '/query/request_blah', message => 'Malformed query string'}]
158
    );
155
    );
159
156
160
    # Test the borrowernumber parameter
157
    # Test the borrowernumber parameter
161
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?borrowernumber=' .
158
    $t->get_ok( "//$userid:$password@/api/v1/illrequests?borrowernumber=" . $patron_2->borrowernumber )
162
        $patron_2->borrowernumber );
159
      ->status_is(200)
163
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
160
      ->json_is( [ $response2 ] );
164
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
165
    $t->request_ok($tx)->status_is(200)->json_is( [ $response2 ] );
166
161
167
    # Test the ILLHiddenRequestStatuses syspref
162
    # Test the ILLHiddenRequestStatuses syspref
168
    t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS1' );
163
    t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS1' );
169
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
164
    $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
170
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
165
      ->status_is(200)
171
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
172
    $t->request_ok($tx)->status_is(200)
173
      ->json_is( [ $req2_formatted ] );
166
      ->json_is( [ $req2_formatted ] );
174
167
175
    t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS2' );
168
    t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS2' );
176
    $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
169
    $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
177
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
170
      ->status_is(200)
178
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
179
    $t->request_ok($tx)->status_is(200)
180
      ->json_is( [ $req_formatted ] );
171
      ->json_is( [ $req_formatted ] );
181
172
182
    $schema->storage->txn_rollback;
173
    $schema->storage->txn_rollback;
Lines 201-232 sub add_formatted { Link Here
201
    }
192
    }
202
    return $req;
193
    return $req;
203
}
194
}
204
205
sub create_user_and_session {
206
207
    my $args = shift;
208
    my $dbh  = C4::Context->dbh;
209
210
    my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
211
212
    my $user = $builder->build(
213
        {
214
            source => 'Borrower',
215
            value  => {
216
                flags => $flags
217
            }
218
        }
219
    );
220
221
    # Create a session for the authorized user
222
    my $session = C4::Auth::get_session('');
223
    $session->param( 'number',   $user->{borrowernumber} );
224
    $session->param( 'id',       $user->{userid} );
225
    $session->param( 'ip',       '127.0.0.1' );
226
    $session->param( 'lasttime', time() );
227
    $session->flush;
228
229
    return ( $user->{borrowernumber}, $session->id );
230
}
231
232
1;
233
- 

Return to bug 27587