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

(-)a/t/db_dependent/api/v1/acquisitions_funds.t (-64 / +22 lines)
Lines 22-46 use Test::Mojo; Link Here
22
use t::lib::TestBuilder;
22
use t::lib::TestBuilder;
23
use t::lib::Mocks;
23
use t::lib::Mocks;
24
24
25
use C4::Auth;
26
use C4::Context;
27
use C4::Budgets;
25
use C4::Budgets;
28
26
29
use Koha::Database;
27
use Koha::Database;
30
use Koha::Patron;
31
28
32
my $schema  = Koha::Database->new->schema;
29
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new();
30
my $builder = t::lib::TestBuilder->new();
34
31
35
$schema->storage->txn_begin;
32
$schema->storage->txn_begin;
36
33
37
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
34
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
38
# this affects the other REST api tests
39
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
40
35
41
$ENV{REMOTE_ADDR} = '127.0.0.1';
42
my $t = Test::Mojo->new('Koha::REST::V1');
36
my $t = Test::Mojo->new('Koha::REST::V1');
43
37
38
my $librarian = $builder->build_object({
39
    class => 'Koha::Patrons',
40
    value => { flags => 2052 }
41
});
42
my $password = 'thePassword123';
43
$librarian->set_password({ password => $password, skip_validation => 1 });
44
my $userid = $librarian->userid;
45
46
my $patron = $builder->build_object({
47
    class => 'Koha::Patrons',
48
    value => { flags => 0 }
49
});
50
my $unauth_password = 'thePassword123';
51
$patron->set_password({ password => $unauth_password, skip_validation => 1 });
52
my $unauth_userid = $patron->userid;
53
44
my $fund1 = {
54
my $fund1 = {
45
    budget_code      => 'ABCD',
55
    budget_code      => 'ABCD',
46
    budget_amount    => '123.132000',
56
    budget_amount    => '123.132000',
Lines 56-125 $t->get_ok('/api/v1/acquisitions/funds') Link Here
56
$t->get_ok('/api/v1/acquisitions/funds/?name=testFund')
66
$t->get_ok('/api/v1/acquisitions/funds/?name=testFund')
57
  ->status_is(401);
67
  ->status_is(401);
58
68
59
my ( $borrowernumber, $session_id )
69
$t->get_ok("//$unauth_userid:$unauth_password@/api/v1/acquisitions/funds")
60
        #= create_user_and_session( { authorized => 1 } );
61
        = create_user_and_session(  );
62
63
my $tx = $t->ua->build_tx(GET => '/api/v1/acquisitions/funds');
64
$tx->req->cookies({name => 'CGISESSID', value => $session_id});
65
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
66
$t->request_ok($tx)
67
  ->status_is(403);
70
  ->status_is(403);
68
71
69
$tx = $t->ua->build_tx(GET => "/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name });
72
$t->get_ok("//$unauth_userid:$unauth_password@/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name })
70
$tx->req->cookies({name => 'CGISESSID', value => $session_id});
71
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
72
$t->request_ok($tx)
73
  ->status_is(403);
73
  ->status_is(403);
74
74
75
( $borrowernumber, $session_id )
75
$t->get_ok("//$userid:$password@/api/v1/acquisitions/funds")
76
        = create_user_and_session( { authorized => 1 } );
77
78
$tx = $t->ua->build_tx(GET => '/api/v1/acquisitions/funds');
79
$tx->req->cookies({name => 'CGISESSID', value => $session_id});
80
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
81
$t->request_ok($tx)
82
  ->status_is(200);
76
  ->status_is(200);
83
77
84
$tx = $t->ua->build_tx(GET => "/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name });
78
$t->get_ok("//$userid:$password@/api/v1/acquisitions/funds/?name=" . $fund1->{ budget_name })
85
$tx->req->cookies({name => 'CGISESSID', value => $session_id});
86
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
87
$t->request_ok($tx)
88
  ->status_is(200)
79
  ->status_is(200)
89
  ->json_like('/0/name' => qr/$fund1->{ budget_name }/);
80
  ->json_like('/0/name' => qr/$fund1->{ budget_name }/);
90
81
91
$schema->storage->txn_rollback;
82
$schema->storage->txn_rollback;
92
83
93
sub create_user_and_session {
84
1;
94
95
    my $args = shift;
96
    my $flags = ( $args->{authorized} ) ? 2052 : 0;
97
98
    # my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0;
99
    my $dbh = C4::Context->dbh;
100
101
    my $user = $builder->build(
102
        {   source => 'Borrower',
103
            value  => { flags => $flags }
104
        }
105
    );
106
107
    # Create a session for the authorized user
108
    my $session = C4::Auth::get_session('');
109
    $session->param( 'number',   $user->{borrowernumber} );
110
    $session->param( 'id',       $user->{userid} );
111
    $session->param( 'ip',       '127.0.0.1' );
112
    $session->param( 'lasttime', time() );
113
    $session->flush;
114
115
    if ( $args->{authorized} ) {
116
        $dbh->do(
117
            q{
118
            INSERT INTO user_permissions (borrowernumber,module_bit,code)
119
            VALUES (?,11,'budget_manage_all')},
120
            undef, $user->{borrowernumber}
121
        );
122
    }
123
124
    return ( $user->{borrowernumber}, $session->id );
125
}
126
- 

Return to bug 19661