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

(-)a/t/db_dependent/api/v1/import_record_matches.t (-95 / +55 lines)
Lines 19-41 use Modern::Perl; Link Here
19
19
20
use Test::More tests => 1;
20
use Test::More tests => 1;
21
use Test::Mojo;
21
use Test::Mojo;
22
use Test::Warn;
23
22
24
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
25
use t::lib::Mocks;
24
use t::lib::Mocks;
26
25
27
use C4::Auth;
28
use Koha::Import::Record::Matches;
29
30
my $schema  = Koha::Database->new->schema;
26
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
27
my $builder = t::lib::TestBuilder->new;
32
28
33
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
29
my $t = Test::Mojo->new('Koha::REST::V1');
34
# this affects the other REST api tests
30
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
36
37
my $remote_address = '127.0.0.1';
38
my $t              = Test::Mojo->new('Koha::REST::V1');
39
31
40
subtest 'import record matches tests' => sub {
32
subtest 'import record matches tests' => sub {
41
33
Lines 43-52 subtest 'import record matches tests' => sub { Link Here
43
35
44
    $schema->storage->txn_begin;
36
    $schema->storage->txn_begin;
45
37
46
    my ( $unauthorized_borrowernumber, $unauthorized_session_id ) =
38
    my $librarian = $builder->build_object(
47
      create_user_and_session( { authorized => 0 } );
39
        {
48
    my ( $authorized_borrowernumber, $authorized_session_id ) =
40
            class => 'Koha::Patrons',
49
      create_user_and_session( { authorized => 1 } );
41
            value => { flags => 13 }
42
        }
43
    );
44
    my $password = 'thePassword123';
45
    $librarian->set_password( { password => $password, skip_validation => 1 } );
46
    my $userid = $librarian->userid;
47
48
    my $patron = $builder->build_object(
49
        {
50
            class => 'Koha::Patrons',
51
            value => { flags => 0 }
52
        }
53
    );
54
55
    $patron->set_password( { password => $password, skip_validation => 1 } );
56
    my $unauth_userid = $patron->userid;
50
57
51
    my $match_1 = $builder->build_object({
58
    my $match_1 = $builder->build_object({
52
        class => 'Koha::Import::Record::Matches',
59
        class => 'Koha::Import::Record::Matches',
Lines 65-124 subtest 'import record matches tests' => sub { Link Here
65
    my $del_import_batch_id = $del_match->import_record->import_batch_id;
72
    my $del_import_batch_id = $del_match->import_record->import_batch_id;
66
    my $del_match_id = $del_match->import_record_id;
73
    my $del_match_id = $del_match->import_record_id;
67
74
68
    # Unauthorized attempt to update
75
    $t->put_ok(
69
    my $tx = $t->ua->build_tx(
76
            "//$unauth_userid:$password@/api/v1/import_batches/"
70
      PUT => "/api/v1/import_batches/".$match_1->import_record->import_batch_id."/records/".$match_1->import_record_id."/matches/chosen"=>
77
          . $match_1->import_record->import_batch_id
71
      json => {
78
          . "/records/"
72
          candidate_match_id => $match_1->candidate_match_id
79
          . $match_1->import_record_id
73
      }
80
          . "/matches/chosen" => json => {
74
    );
81
            candidate_match_id => $match_1->candidate_match_id
75
    $tx->req->cookies(
82
          }
76
        { name => 'CGISESSID', value => $unauthorized_session_id } );
83
    )->status_is(403);
77
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
78
    $t->request_ok($tx)->status_is(403);
79
84
80
    # Invalid attempt to allow match on a non-existent record
85
    # Invalid attempt to allow match on a non-existent record
81
    $tx = $t->ua->build_tx(
82
      PUT => "/api/v1/import_batches/".$del_import_batch_id."/records/".$del_match_id."/matches/chosen" =>
83
      json => {
84
          candidate_match_id => $match_1->candidate_match_id
85
      }
86
    );
87
88
    $tx->req->cookies(
89
        { name => 'CGISESSID', value => $authorized_session_id } );
90
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
91
    $del_match->delete();
86
    $del_match->delete();
92
    $t->request_ok($tx)->status_is(404)
87
93
      ->json_is( '/error' => "Match not found" );
88
    $t->put_ok(
89
            "//$userid:$password@/api/v1/import_batches/"
90
          . $del_import_batch_id
91
          . "/records/"
92
          . $del_match_id
93
          . "/matches/chosen" => json => {
94
            candidate_match_id => $match_1->candidate_match_id
95
          }
96
    )->status_is(404)->json_is( '/error' => "Match not found" );
94
97
95
    # Valid, authorised update
98
    # Valid, authorised update
96
    $tx = $t->ua->build_tx(
99
    $t->put_ok(
97
      PUT => "/api/v1/import_batches/".$match_1->import_record->import_batch_id."/records/".$match_1->import_record_id."/matches/chosen" =>
100
            "//$userid:$password@/api/v1/import_batches/"
98
      json => {
101
          . $match_1->import_record->import_batch_id
99
          candidate_match_id => $match_1->candidate_match_id
102
          . "/records/"
100
      }
103
          . $match_1->import_record_id
101
    );
104
          . "/matches/chosen" => json => {
102
    $tx->req->cookies(
105
            candidate_match_id => $match_1->candidate_match_id
103
        { name => 'CGISESSID', value => $authorized_session_id } );
106
          }
104
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
107
    )->status_is(200);
105
    $t->request_ok($tx)->status_is(200);
106
108
107
    $match_1->discard_changes;
109
    $match_1->discard_changes;
108
    $match_2->discard_changes;
110
    $match_2->discard_changes;
111
109
    ok( $match_1->chosen,"Match 1 is correctly set to chosen");
112
    ok( $match_1->chosen,"Match 1 is correctly set to chosen");
110
    ok( !$match_2->chosen,"Match 2 correctly unset when match 1 is set");
113
    ok( !$match_2->chosen,"Match 2 correctly unset when match 1 is set");
111
114
112
    # Valid unsetting
115
    # Valid unsetting
113
    $tx = $t->ua->build_tx(
116
    $t->delete_ok( "//$userid:$password@/api/v1/import_batches/"
114
      DELETE => "/api/v1/import_batches/".$match_1->import_record->import_batch_id."/records/".$match_1->import_record_id."/matches/chosen" =>
117
          . $match_1->import_record->import_batch_id
115
      json => {
118
          . "/records/"
116
      }
119
          . $match_1->import_record_id
117
    );
120
          . "/matches/chosen" )->status_is(204);
118
    $tx->req->cookies(
119
        { name => 'CGISESSID', value => $authorized_session_id } );
120
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
121
    $t->request_ok($tx)->status_is(204);
122
121
123
    $match_1->discard_changes;
122
    $match_1->discard_changes;
124
    $match_2->discard_changes;
123
    $match_2->discard_changes;
Lines 127-167 subtest 'import record matches tests' => sub { Link Here
127
126
128
    $schema->storage->txn_rollback;
127
    $schema->storage->txn_rollback;
129
};
128
};
130
131
sub create_user_and_session {
132
133
    my $args  = shift;
134
    my $dbh   = C4::Context->dbh;
135
136
    my $user = $builder->build(
137
        {
138
            source => 'Borrower',
139
            value  => {
140
                flags => 0
141
            }
142
        }
143
    );
144
145
    # Create a session for the authorized user
146
    my $session = C4::Auth::get_session('');
147
    $session->param( 'number',   $user->{borrowernumber} );
148
    $session->param( 'id',       $user->{userid} );
149
    $session->param( 'ip',       '127.0.0.1' );
150
    $session->param( 'lasttime', time() );
151
    $session->flush;
152
153
    if ( $args->{authorized} ) {
154
        $builder->build({
155
            source => 'UserPermission',
156
            value  => {
157
                borrowernumber => $user->{borrowernumber},
158
                module_bit     => 13,
159
                code           => 'manage_staged_marc',
160
            }
161
        });
162
    }
163
164
    return ( $user->{borrowernumber}, $session->id );
165
}
166
167
1;
168
- 

Return to bug 30855