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

(-)a/t/db_dependent/api/v1/accountlines.t (-6 / +2 lines)
Lines 300-311 sub create_user_and_session { Link Here
300
        }
300
        }
301
    });
301
    });
302
302
303
    my $session = C4::Auth::get_session('');
303
    # Create a session for the authorized user
304
    $session->param('number', $user->{ borrowernumber });
304
    my $session = t::lib::Mocks::mock_session({borrower => $user});
305
    $session->param('id', $user->{ userid });
306
    $session->param('ip', '127.0.0.1');
307
    $session->param('lasttime', time());
308
    $session->flush;
309
305
310
    return ($user, $session);
306
    return ($user, $session);
311
}
307
}
(-)a/t/db_dependent/api/v1/auth.t (-6 / +1 lines)
Lines 237-248 sub create_user_and_session { Link Here
237
    );
237
    );
238
238
239
    # Create a session for the authorized user
239
    # Create a session for the authorized user
240
    my $session = C4::Auth::get_session('');
240
    my $session = t::lib::Mocks::mock_session({borrower => $user});
241
    $session->param( 'number',   $user->{borrowernumber} );
242
    $session->param( 'id',       $user->{userid} );
243
    $session->param( 'ip',       '127.0.0.1' );
244
    $session->param( 'lasttime', time() );
245
    $session->flush;
246
241
247
    return ( $user->{borrowernumber}, $session->id );
242
    return ( $user->{borrowernumber}, $session->id );
248
}
243
}
(-)a/t/db_dependent/api/v1/availability.t (-6 / +1 lines)
Lines 392-403 sub create_user_and_session { Link Here
392
    );
392
    );
393
393
394
    # Create a session for the authorized user
394
    # Create a session for the authorized user
395
    my $session = C4::Auth::get_session('');
395
    my $session = t::lib::Mocks::mock_session({borrower => $user});
396
    $session->param( 'number',   $user->{borrowernumber} );
397
    $session->param( 'id',       $user->{userid} );
398
    $session->param( 'ip',       '127.0.0.1' );
399
    $session->param( 'lasttime', time() );
400
    $session->flush;
401
396
402
    if ( $args->{authorized} ) {
397
    if ( $args->{authorized} ) {
403
        $dbh->do( "
398
        $dbh->do( "
(-)a/t/db_dependent/api/v1/biblios.t (-6 / +1 lines)
Lines 169-180 sub create_user_and_session { Link Here
169
        },
169
        },
170
    });
170
    });
171
171
172
    my $session = C4::Auth::get_session('');
172
    my $session = t::lib::Mocks::mock_session({borrower => $user});
173
    $session->param('number', $user->{ borrowernumber });
174
    $session->param('id', $user->{ userid });
175
    $session->param('ip', '127.0.0.1');
176
    $session->param('lasttime', time());
177
    $session->flush;
178
173
179
    my $patron = Koha::Patrons->find($user->{borrowernumber});
174
    my $patron = Koha::Patrons->find($user->{borrowernumber});
180
    if ( $flags ) {
175
    if ( $flags ) {
(-)a/t/db_dependent/api/v1/checkouts.t (-12 / +2 lines)
Lines 61-72 Koha::Auth::PermissionManager->grantPermission( Link Here
61
    'circulate', 'circulate_remaining_permissions'
61
    'circulate', 'circulate_remaining_permissions'
62
);
62
);
63
63
64
my $session = C4::Auth::get_session('');
64
my $session = t::lib::Mocks::mock_session({borrower => $loggedinuser});
65
$session->param('number', $loggedinuser->{ borrowernumber });
66
$session->param('id', $loggedinuser->{ userid });
67
$session->param('ip', '127.0.0.1');
68
$session->param('lasttime', time());
69
$session->flush;
70
65
71
my $patron = $builder->build({ source => 'Borrower',
66
my $patron = $builder->build({ source => 'Borrower',
72
    value => {
67
    value => {
Lines 78-89 my $patron = $builder->build({ source => 'Borrower', Link Here
78
    }
73
    }
79
});
74
});
80
my $borrowernumber = $patron->{borrowernumber};
75
my $borrowernumber = $patron->{borrowernumber};
81
my $patron_session = C4::Auth::get_session('');
76
my $patron_session = t::lib::Mocks::mock_session({borrower => $patron});
82
$patron_session->param('number', $borrowernumber);
83
$patron_session->param('id', $patron->{ userid });
84
$patron_session->param('ip', '127.0.0.1');
85
$patron_session->param('lasttime', time());
86
$patron_session->flush;
87
77
88
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
78
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
89
my $module = new Test::MockModule('C4::Context');
79
my $module = new Test::MockModule('C4::Context');
(-)a/t/db_dependent/api/v1/checkoutsexpanded.t (-6 / +1 lines)
Lines 162-173 sub create_user_and_session { Link Here
162
        }
162
        }
163
    });
163
    });
164
164
165
    my $borrowersession = C4::Auth::get_session('');
165
    my $borrowersession = t::lib::Mocks::mock_session({borrower => $borrower});
166
    $borrowersession->param('number', $borrower->{ borrowernumber });
167
    $borrowersession->param('id', $borrower->{ userid });
168
    $borrowersession->param('ip', '127.0.0.1');
169
    $borrowersession->param('lasttime', time());
170
    $borrowersession->flush;
171
166
172
    return ($borrower->{borrowernumber}, $borrowersession->id);
167
    return ($borrower->{borrowernumber}, $borrowersession->id);
173
}
168
}
(-)a/t/db_dependent/api/v1/checkoutshistory.t (-12 / +2 lines)
Lines 58-69 Koha::Auth::PermissionManager->grantPermission( Link Here
58
    'circulate', 'circulate_remaining_permissions'
58
    'circulate', 'circulate_remaining_permissions'
59
);
59
);
60
60
61
my $session = C4::Auth::get_session('');
61
my $session = t::lib::Mocks::mock_session({borrower => $loggedinuser});
62
$session->param('number', $loggedinuser->{ borrowernumber });
63
$session->param('id', $loggedinuser->{ userid });
64
$session->param('ip', '127.0.0.1');
65
$session->param('lasttime', time());
66
$session->flush;
67
62
68
my $nopermission = $builder->build({
63
my $nopermission = $builder->build({
69
    source => 'Borrower',
64
    source => 'Borrower',
Lines 75-86 my $nopermission = $builder->build({ Link Here
75
    }
70
    }
76
});
71
});
77
72
78
my $session_nopermission = C4::Auth::get_session('');
73
my $session_nopermission = t::lib::Mocks::mock_session({borrower => $nopermission});
79
$session_nopermission->param('number', $nopermission->{ borrowernumber });
80
$session_nopermission->param('id', $nopermission->{ userid });
81
$session_nopermission->param('ip', '127.0.0.1');
82
$session_nopermission->param('lasttime', time());
83
$session_nopermission->flush;
84
74
85
my $borrower = $builder->build({ source => 'Borrower' });
75
my $borrower = $builder->build({ source => 'Borrower' });
86
my $borrowernumber = $borrower->{borrowernumber};
76
my $borrowernumber = $borrower->{borrowernumber};
(-)a/t/db_dependent/api/v1/cities.t (-6 / +1 lines)
Lines 360-371 sub create_user_and_session { Link Here
360
    );
360
    );
361
361
362
    # Create a session for the authorized user
362
    # Create a session for the authorized user
363
    my $session = C4::Auth::get_session('');
363
    my $session = t::lib::Mocks::mock_session({borrower => $user});
364
    $session->param( 'number',   $user->{borrowernumber} );
365
    $session->param( 'id',       $user->{userid} );
366
    $session->param( 'ip',       '127.0.0.1' );
367
    $session->param( 'lasttime', time() );
368
    $session->flush;
369
364
370
    if ( $args->{authorized} ) {
365
    if ( $args->{authorized} ) {
371
        my $patron = Koha::Patrons->find($user->{borrowernumber});
366
        my $patron = Koha::Patrons->find($user->{borrowernumber});
(-)a/t/db_dependent/api/v1/holds.t (-24 / +4 lines)
Lines 72-83 my $nopermission = $builder->build({ Link Here
72
});
72
});
73
my $nopermission_patron = Koha::Patrons->find($nopermission->{borrowernumber});
73
my $nopermission_patron = Koha::Patrons->find($nopermission->{borrowernumber});
74
74
75
my $session_nopermission = C4::Auth::get_session('');
75
my $session_nopermission = t::lib::Mocks::mock_session({borrower => $nopermission_patron});
76
$session_nopermission->param('number', $nopermission->{ borrowernumber });
77
$session_nopermission->param('id', $nopermission->{ userid });
78
$session_nopermission->param('ip', '127.0.0.1');
79
$session_nopermission->param('lasttime', time());
80
$session_nopermission->flush;
81
76
82
my $borrower = Koha::Patrons->find($builder->build({
77
my $borrower = Koha::Patrons->find($builder->build({
83
    source => 'Borrower',
78
    source => 'Borrower',
Lines 131-154 my $borrower3 = Koha::Patrons->find($builder->build({ Link Here
131
my $borrowernumber3 = $borrower3->borrowernumber;
126
my $borrowernumber3 = $borrower3->borrowernumber;
132
127
133
# Get sessions
128
# Get sessions
134
my $session = C4::Auth::get_session('');
129
my $session =  t::lib::Mocks::mock_session({borrower => $borrower});
135
$session->param('number', $borrower->borrowernumber);
130
my $session2 = t::lib::Mocks::mock_session({borrower => $borrower2});
136
$session->param('id', $borrower->userid);
131
my $session3 = t::lib::Mocks::mock_session({borrower => $borrower3});
137
$session->param('ip', '127.0.0.1');
138
$session->param('lasttime', time());
139
$session->flush;
140
my $session2 = C4::Auth::get_session('');
141
$session2->param('number', $borrower2->borrowernumber);
142
$session2->param('id', $borrower2->userid);
143
$session2->param('ip', '127.0.0.1');
144
$session2->param('lasttime', time());
145
$session2->flush;
146
my $session3 = C4::Auth::get_session('');
147
$session3->param('number', $borrower3->borrowernumber);
148
$session3->param('id', $borrower3->userid);
149
$session3->param('ip', '127.0.0.1');
150
$session3->param('lasttime', time());
151
$session3->flush;
152
132
153
my $biblionumber = create_biblio('RESTful Web APIs');
133
my $biblionumber = create_biblio('RESTful Web APIs');
154
my $itemnumber = create_item($biblionumber, 'TEST000001');
134
my $itemnumber = create_item($biblionumber, 'TEST000001');
(-)a/t/db_dependent/api/v1/issuingrules.t (-6 / +1 lines)
Lines 367-378 sub create_user_and_session { Link Here
367
    );
367
    );
368
368
369
    # Create a session for the authorized user
369
    # Create a session for the authorized user
370
    my $session = C4::Auth::get_session('');
370
    my $session = t::lib::Mocks::mock_session({borrower => $user});
371
    $session->param( 'number',   $user->{borrowernumber} );
372
    $session->param( 'id',       $user->{userid} );
373
    $session->param( 'ip',       '127.0.0.1' );
374
    $session->param( 'lasttime', time() );
375
    $session->flush;
376
371
377
    if ( $args->{authorized} ) {
372
    if ( $args->{authorized} ) {
378
        my $patron = Koha::Patrons->find($user->{borrowernumber});
373
        my $patron = Koha::Patrons->find($user->{borrowernumber});
(-)a/t/db_dependent/api/v1/items.t (-6 / +1 lines)
Lines 110-121 sub create_session { Link Here
110
110
111
    my @sessions;
111
    my @sessions;
112
    foreach $borrower (@borrowers) {
112
    foreach $borrower (@borrowers) {
113
        my $session = C4::Auth::get_session('');
113
        my $session = t::lib::Mocks::mock_session({borrower => $borrower});
114
        $session->param('number', $borrower->{borrowernumber});
115
        $session->param('id', $borrower->{userid});
116
        $session->param('ip', '127.0.0.1');
117
        $session->param('lasttime', time());
118
        $session->flush;
119
        push @sessions, $session;
114
        push @sessions, $session;
120
    }
115
    }
121
116
(-)a/t/db_dependent/api/v1/labels.t (-6 / +1 lines)
Lines 283-294 sub create_user_and_session { Link Here
283
        }
283
        }
284
    });
284
    });
285
285
286
    my $borrowersession = C4::Auth::get_session('');
286
    my $borrowersession = t::lib::Mocks::mock_session({borrower => $borrower});
287
    $borrowersession->param('number', $borrower->{ borrowernumber });
288
    $borrowersession->param('id', $borrower->{ userid });
289
    $borrowersession->param('ip', '127.0.0.1');
290
    $borrowersession->param('lasttime', time());
291
    $borrowersession->flush;
292
287
293
    return ($borrower->{borrowernumber}, $borrowersession->id);
288
    return ($borrower->{borrowernumber}, $borrowersession->id);
294
}
289
}
(-)a/t/db_dependent/api/v1/messagingpreferences.t (-6 / +1 lines)
Lines 292-303 sub create_user_and_session { Link Here
292
        }
292
        }
293
    });
293
    });
294
294
295
    my $session = C4::Auth::get_session('');
295
    my $session = t::lib::Mocks::mock_session({borrower => $borrower});
296
    $session->param('number', $borrower->{ borrowernumber });
297
    $session->param('id', $borrower->{ userid });
298
    $session->param('ip', '127.0.0.1');
299
    $session->param('lasttime', time());
300
    $session->flush;
301
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
296
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
302
297
303
    return ($patron, $session);
298
    return ($patron, $session);
(-)a/t/db_dependent/api/v1/notices.t (-6 / +1 lines)
Lines 467-478 sub create_user_and_session { Link Here
467
        }
467
        }
468
    });
468
    });
469
469
470
    my $session = C4::Auth::get_session('');
470
    my $session = t::lib::Mocks::mock_session({borrower => $borrower});
471
    $session->param('number', $borrower->{ borrowernumber });
472
    $session->param('id', $borrower->{ userid });
473
    $session->param('ip', '127.0.0.1');
474
    $session->param('lasttime', time());
475
    $session->flush;
476
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
471
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
477
    if ( $flags ) {
472
    if ( $flags ) {
478
        Koha::Auth::PermissionManager->grantPermissions($patron, $flags);
473
        Koha::Auth::PermissionManager->grantPermissions($patron, $flags);
(-)a/t/db_dependent/api/v1/ownerflag.t (-6 / +1 lines)
Lines 120-131 sub create_user_and_session { Link Here
120
            lost         => 0,
120
            lost         => 0,
121
        }
121
        }
122
    });
122
    });
123
    my $session = C4::Auth::get_session('');
123
    my $session = t::lib::Mocks::mock_session({borrower => $patron});
124
    $session->param('number', $patron->{ borrowernumber });
125
    $session->param('id', $patron->{ userid });
126
    $session->param('ip', '127.0.0.1');
127
    $session->param('lasttime', time());
128
    $session->flush;
129
124
130
    return ($patron, $session);
125
    return ($patron, $session);
131
}
126
}
(-)a/t/db_dependent/api/v1/passwordrecovery.t (-6 / +1 lines)
Lines 394-405 sub create_user_and_session { Link Here
394
        }
394
        }
395
    });
395
    });
396
396
397
    my $session = C4::Auth::get_session('');
397
    my $session = t::lib::Mocks::mock_session({borrower => $borrower});
398
    $session->param('number', $borrower->{ borrowernumber });
399
    $session->param('id', $borrower->{ userid });
400
    $session->param('ip', '127.0.0.1');
401
    $session->param('lasttime', time());
402
    $session->flush;
403
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
398
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
404
    if ( $flags ) {
399
    if ( $flags ) {
405
        Koha::Auth::PermissionManager->grantPermissions($patron, $flags);
400
        Koha::Auth::PermissionManager->grantPermissions($patron, $flags);
(-)a/t/db_dependent/api/v1/patrons.t (-6 / +1 lines)
Lines 660-671 sub create_user_and_session { Link Here
660
    );
660
    );
661
661
662
    # Create a session for the authorized user
662
    # Create a session for the authorized user
663
    my $session = C4::Auth::get_session('');
663
    my $session = t::lib::Mocks::mock_session({borrower => $user});
664
    $session->param( 'number',   $user->{borrowernumber} );
665
    $session->param( 'id',       $user->{userid} );
666
    $session->param( 'ip',       '127.0.0.1' );
667
    $session->param( 'lasttime', time() );
668
    $session->flush;
669
664
670
    return ( $user->{borrowernumber}, $session->id );
665
    return ( $user->{borrowernumber}, $session->id );
671
}
666
}
(-)a/t/db_dependent/api/v1/payments.t (-6 / +1 lines)
Lines 238-249 sub create_user_and_session { Link Here
238
        }
238
        }
239
    });
239
    });
240
240
241
    my $borrowersession = C4::Auth::get_session('');
241
    my $borrowersession = t::lib::Mocks::mock_session({borrower => $borrower});
242
    $borrowersession->param('number', $borrower->{ borrowernumber });
243
    $borrowersession->param('id', $borrower->{ userid });
244
    $borrowersession->param('ip', '127.0.0.1');
245
    $borrowersession->param('lasttime', time());
246
    $borrowersession->flush;
247
242
248
    return ($borrower->{borrowernumber}, $borrowersession->id);
243
    return ($borrower->{borrowernumber}, $borrowersession->id);
249
}
244
}
(-)a/t/db_dependent/api/v1/records.t (-6 / +1 lines)
Lines 383-394 sub create_user_and_session { Link Here
383
        }
383
        }
384
    });
384
    });
385
385
386
    my $session = C4::Auth::get_session('');
386
    my $session = t::lib::Mocks::mock_session({borrower => $borrower});
387
    $session->param('number', $borrower->{ borrowernumber });
388
    $session->param('id', $borrower->{ userid });
389
    $session->param('ip', '127.0.0.1');
390
    $session->param('lasttime', time());
391
    $session->flush;
392
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
387
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
393
    if ( $flags ) {
388
    if ( $flags ) {
394
        foreach my $flag (keys %$flags) {
389
        foreach my $flag (keys %$flags) {
(-)a/t/db_dependent/api/v1/reports.t (-6 / +1 lines)
Lines 160-171 sub create_user_and_session { Link Here
160
        }
160
        }
161
    });
161
    });
162
162
163
    my $session = C4::Auth::get_session('');
163
    my $session = t::lib::Mocks::mock_session({borrower => $borrower});
164
    $session->param('number', $borrower->{ borrowernumber });
165
    $session->param('id', $borrower->{ userid });
166
    $session->param('ip', '127.0.0.1');
167
    $session->param('lasttime', time());
168
    $session->flush;
169
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
164
    my $patron = Koha::Patrons->find($borrower->{borrowernumber});
170
    if ( $flags ) {
165
    if ( $flags ) {
171
        Koha::Auth::PermissionManager->grantPermissions($patron, $flags);
166
        Koha::Auth::PermissionManager->grantPermissions($patron, $flags);
(-)a/t/lib/Mocks.pm (-1 / +58 lines)
Lines 6-11 use C4::Context; Link Here
6
use Koha::Schema;
6
use Koha::Schema;
7
use Test::MockModule;
7
use Test::MockModule;
8
8
9
use Koha::Exceptions;
10
use Koha::Exceptions::File;
11
9
my %configs;
12
my %configs;
10
sub mock_config {
13
sub mock_config {
11
    my $context = new Test::MockModule('C4::Context');
14
    my $context = new Test::MockModule('C4::Context');
Lines 41-44 sub mock_preference { Link Here
41
    });
44
    });
42
}
45
}
43
46
47
=head2 mock_session
48
49
    t::lib::Mocks::mock_session({
50
        borrower => { #One can give a borrower-hash or a Koha::Borrower-object to mint the session.
51
            borrowernumber => 0420,
52
            userid => 'OG Kush',
53
        },
54
        params => { #Extra parameters given directly to the CGI::Session as param(), overwrites previous defaults, such as the values inferred from borrower
55
            number   => $borrowernumber,
56
            id       => $userid,
57
            ip       => '127.0.0.1',
58
            lasttime => time(),
59
            ...
60
        },
61
    });
62
63
Mocks a CGI::Session used by C4::Auth.
64
Sets reasonable defaults for most values.
65
66
@PARAM1 Hashref of parameters to be given to CGI::Session
67
@RETURNS CGI::Session, a new session object serialized to the backing store, ready to be authenticated with
68
@THROWS Koha::Exception::File, if flushing the session to a file failed
69
70
=cut
71
72
sub mock_session {
73
    my ($args) = @_;
74
    my $params = $args->{params};
75
76
    my $session = C4::Auth::get_session('');
77
    if (my $borrower = $args->{borrower}) {
78
        $borrower = Koha::Patrons->cast($borrower);
79
        $session->param( 'number', $borrower->borrowernumber);
80
        $session->param( 'id',     $borrower->userid);
81
    }
82
    foreach my $k (keys %$params) {
83
        $session->param( $k, $params->{$k});
84
    }
85
    $session->param('ip', '127.0.0.1')  unless $session->param('ip');
86
    $session->param('lasttime', time()) unless $session->param('lasttime');
87
    Koha::Exceptions::BadParameter->throw(error => "Mandatory CGI::Session parameter 'number' is not defined!") unless $session->param('number');
88
    $session->flush;
89
90
    if ($session->errstr()) {
91
        if($session->errstr() =~ /couldn't open '(.+)'/) {
92
            Koha::Exceptions::File->throw(error => "While trying to mock a CGI::Session, got the following CGI::Session error string '".$session->errstr()."'", path => $1);
93
        }
94
        else {
95
            Koha::Exceptions::Exception->throw(error => "Getting a CGI::Session failed with an unexpected error    '$session->errstr()'    ");
96
        }
97
    }
98
99
    return $session;
100
}
101
44
1;
102
1;
45
- 

Return to bug 19515