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

(-)a/t/db_dependent/api/v1/import_record_matches.t (-95 / +65 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 => 0 }
42
        }
43
    );
44
45
    $builder->build({
46
        source => 'UserPermission',
47
        value  => {
48
            borrowernumber => $librarian->id,
49
            module_bit     => 13, # tools
50
            code           => 'manage_staged_marc',
51
        }
52
    });
53
54
    my $password = 'thePassword123';
55
    $librarian->set_password( { password => $password, skip_validation => 1 } );
56
    my $userid = $librarian->userid;
57
58
    my $patron = $builder->build_object(
59
        {
60
            class => 'Koha::Patrons',
61
            value => { flags => 0 }
62
        }
63
    );
64
65
    $patron->set_password( { password => $password, skip_validation => 1 } );
66
    my $unauth_userid = $patron->userid;
50
67
51
    my $match_1 = $builder->build_object({
68
    my $match_1 = $builder->build_object({
52
        class => 'Koha::Import::Record::Matches',
69
        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;
82
    my $del_import_batch_id = $del_match->import_record->import_batch_id;
66
    my $del_match_id = $del_match->import_record_id;
83
    my $del_match_id = $del_match->import_record_id;
67
84
68
    # Unauthorized attempt to update
85
    $t->put_ok(
69
    my $tx = $t->ua->build_tx(
86
            "//$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"=>
87
          . $match_1->import_record->import_batch_id
71
      json => {
88
          . "/records/"
72
          candidate_match_id => $match_1->candidate_match_id
89
          . $match_1->import_record_id
73
      }
90
          . "/matches/chosen" => json => {
74
    );
91
            candidate_match_id => $match_1->candidate_match_id
75
    $tx->req->cookies(
92
          }
76
        { name => 'CGISESSID', value => $unauthorized_session_id } );
93
    )->status_is(403);
77
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
78
    $t->request_ok($tx)->status_is(403);
79
94
80
    # Invalid attempt to allow match on a non-existent record
95
    # 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();
96
    $del_match->delete();
92
    $t->request_ok($tx)->status_is(404)
97
93
      ->json_is( '/error' => "Match not found" );
98
    $t->put_ok(
99
            "//$userid:$password@/api/v1/import_batches/"
100
          . $del_import_batch_id
101
          . "/records/"
102
          . $del_match_id
103
          . "/matches/chosen" => json => {
104
            candidate_match_id => $match_1->candidate_match_id
105
          }
106
    )->status_is(404)->json_is( '/error' => "Match not found" );
94
107
95
    # Valid, authorised update
108
    # Valid, authorised update
96
    $tx = $t->ua->build_tx(
109
    $t->put_ok(
97
      PUT => "/api/v1/import_batches/".$match_1->import_record->import_batch_id."/records/".$match_1->import_record_id."/matches/chosen" =>
110
            "//$userid:$password@/api/v1/import_batches/"
98
      json => {
111
          . $match_1->import_record->import_batch_id
99
          candidate_match_id => $match_1->candidate_match_id
112
          . "/records/"
100
      }
113
          . $match_1->import_record_id
101
    );
114
          . "/matches/chosen" => json => {
102
    $tx->req->cookies(
115
            candidate_match_id => $match_1->candidate_match_id
103
        { name => 'CGISESSID', value => $authorized_session_id } );
116
          }
104
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
117
    )->status_is(200);
105
    $t->request_ok($tx)->status_is(200);
106
118
107
    $match_1->discard_changes;
119
    $match_1->discard_changes;
108
    $match_2->discard_changes;
120
    $match_2->discard_changes;
121
109
    ok( $match_1->chosen,"Match 1 is correctly set to chosen");
122
    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");
123
    ok( !$match_2->chosen,"Match 2 correctly unset when match 1 is set");
111
124
112
    # Valid unsetting
125
    # Valid unsetting
113
    $tx = $t->ua->build_tx(
126
    $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" =>
127
          . $match_1->import_record->import_batch_id
115
      json => {
128
          . "/records/"
116
      }
129
          . $match_1->import_record_id
117
    );
130
          . "/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
131
123
    $match_1->discard_changes;
132
    $match_1->discard_changes;
124
    $match_2->discard_changes;
133
    $match_2->discard_changes;
Lines 127-167 subtest 'import record matches tests' => sub { Link Here
127
136
128
    $schema->storage->txn_rollback;
137
    $schema->storage->txn_rollback;
129
};
138
};
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