3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 5;
22 use t::lib::TestBuilder;
26 use Koha::ImportBatchProfiles;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new();
30 my $dbh = C4::Context->dbh;
32 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34 my $t = Test::Mojo->new('Koha::REST::V1');
36 subtest 'unauth access' => sub {
39 $schema->storage->txn_begin;
41 # Patron without specific flag
42 my $patron1 = $builder->build_object(
44 class => 'Koha::Patrons',
51 # Patron with correct flag, but without specific permission
52 my $patron2 = $builder->build_object(
54 class => 'Koha::Patrons',
61 my $uid = $patron1->userid;
62 my $pwd = $patron1->password;
63 $t->get_ok("//$uid:$pwd@/api/v1/import_batch_profiles?_order_by=+name")
66 $uid = $patron1->userid;
67 $pwd = $patron1->password;
68 $t->get_ok("//$uid:$pwd@/api/v1/import_batch_profiles?_order_by=+name")
71 $schema->storage->txn_rollback;
74 subtest 'list profiles' => sub {
77 $schema->storage->txn_begin;
79 Koha::ImportBatchProfiles->search()->delete;
81 my $ibp1 = $builder->build_object({ class => 'Koha::ImportBatchProfiles', value => { name => 'a_ibp' } });
82 my $ibp2 = $builder->build_object({ class => 'Koha::ImportBatchProfiles', value => { name => 'b_ibp' } });
84 my $patron = $builder->build_object(
86 class => 'Koha::Patrons',
93 my $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
97 $sth->execute($patron->borrowernumber, 'stage_marc_import', 'tools');
99 my $pwd = 'thePassword123';
100 $patron->set_password( { password => $pwd, skip_validation => 1 } );
102 my $uid = $patron->userid;
104 $t->get_ok("//$uid:$pwd@/api/v1/import_batch_profiles?_order_by=+name")
106 ->json_is('/0/name', $ibp1->name)
107 ->json_is('/1/name', $ibp2->name);
109 $schema->storage->txn_rollback;
113 subtest 'add profile' => sub {
116 $schema->storage->txn_begin;
118 Koha::ImportBatchProfiles->search()->delete;
120 my $patron = $builder->build_object(
122 class => 'Koha::Patrons',
129 my $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
133 $sth->execute($patron->borrowernumber, 'stage_marc_import', 'tools');
135 my $pwd = 'thePassword123';
136 $patron->set_password( { password => $pwd, skip_validation => 1 } );
138 my $uid = $patron->userid;
140 name => 'profileName',
141 overlay_action => 'overlay_action'
143 $t->post_ok("//$uid:$pwd@/api/v1/import_batch_profiles", json => $post_data)
145 ->json_has('/profile_id')
146 ->json_is('/name', $post_data->{name})
147 ->json_is('/overlay_action', $post_data->{overlay_action});
150 $schema->storage->txn_rollback;
154 subtest 'edit profile' => sub {
157 $schema->storage->txn_begin;
159 Koha::ImportBatchProfiles->search()->delete;
161 my $patron = $builder->build_object(
163 class => 'Koha::Patrons',
170 my $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
174 $sth->execute($patron->borrowernumber, 'stage_marc_import', 'tools');
176 my $pwd = 'thePassword123';
177 $patron->set_password( { password => $pwd, skip_validation => 1 } );
179 my $uid = $patron->userid;
181 my $ibp = $builder->build_object({class => 'Koha::ImportBatchProfiles', value => { name => 'someProfile' }});
187 $t->put_ok("//$uid:$pwd@/api/v1/import_batch_profiles/".$ibp->id, json => $post_data)
189 ->json_is('/profile_id', $ibp->id)
190 ->json_is('/name', $post_data->{name});
192 $ibp->discard_changes;
194 is($ibp->name, $post_data->{name}, 'profile name should be the updated one');
197 $schema->storage->txn_rollback;
201 subtest 'delete profile' => sub {
204 $schema->storage->txn_begin;
206 Koha::ImportBatchProfiles->search()->delete;
208 my $patron = $builder->build_object(
210 class => 'Koha::Patrons',
217 my $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
221 $sth->execute($patron->borrowernumber, 'stage_marc_import', 'tools');
223 my $pwd = 'thePassword123';
224 $patron->set_password( { password => $pwd, skip_validation => 1 } );
226 my $uid = $patron->userid;
228 my $ibp = $builder->build_object({class => 'Koha::ImportBatchProfiles'});
230 $t->delete_ok("//$uid:$pwd@/api/v1/import_batch_profiles/".$ibp->id)
233 my $search = Koha::ImportBatchProfiles->find($ibp->id);
235 is($search, undef, 'profile should be erased');
238 $schema->storage->txn_rollback;