Bug 22435: Update unit tests
[koha.git] / t / db_dependent / api / v1 / import_batch_profiles.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 5;
21 use Test::Mojo;
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
24
25 use Koha::Database;
26 use Koha::ImportBatchProfiles;
27
28 my $schema  = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new();
30 my $dbh = C4::Context->dbh;
31
32 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33
34 my $t = Test::Mojo->new('Koha::REST::V1');
35
36 subtest 'unauth access' => sub {
37     plan tests => 4;
38
39     $schema->storage->txn_begin;
40
41     # Patron without specific flag
42     my $patron1 = $builder->build_object(
43         {
44             class => 'Koha::Patrons',
45             value => {
46                 flags => 4
47             }
48         }
49     );
50
51     # Patron with correct flag, but without specific permission
52     my $patron2 = $builder->build_object(
53         {
54             class => 'Koha::Patrons',
55             value => {
56                 flags => 4096
57             }
58         }
59     );
60
61     my $uid = $patron1->userid;
62     my $pwd = $patron1->password;
63     $t->get_ok("//$uid:$pwd@/api/v1/import_batch_profiles?_order_by=+name")
64       ->status_is(403);
65
66     $uid = $patron1->userid;
67     $pwd = $patron1->password;
68     $t->get_ok("//$uid:$pwd@/api/v1/import_batch_profiles?_order_by=+name")
69       ->status_is(403);
70
71     $schema->storage->txn_rollback;
72 };
73
74 subtest 'list profiles' => sub {
75     plan tests => 4;
76
77     $schema->storage->txn_begin;
78
79     Koha::ImportBatchProfiles->search()->delete;
80
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' } });
83
84     my $patron = $builder->build_object(
85         {
86             class => 'Koha::Patrons',
87             value => {
88                 flags => 4096
89             }
90         }
91     );
92
93     my $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
94                         SELECT ?, bit, ?
95                         FROM userflags
96                         WHERE flag = ?");
97     $sth->execute($patron->borrowernumber, 'stage_marc_import', 'tools');
98
99     my $pwd = 'thePassword123';
100     $patron->set_password( { password => $pwd, skip_validation => 1 } );
101
102     my $uid = $patron->userid;
103
104     $t->get_ok("//$uid:$pwd@/api/v1/import_batch_profiles?_order_by=+name")
105       ->status_is(200)
106       ->json_is('/0/name', $ibp1->name)
107       ->json_is('/1/name', $ibp2->name);
108
109     $schema->storage->txn_rollback;
110
111 };
112
113 subtest 'add profile' => sub {
114     plan tests => 5;
115
116     $schema->storage->txn_begin;
117
118     Koha::ImportBatchProfiles->search()->delete;
119
120     my $patron = $builder->build_object(
121         {
122             class => 'Koha::Patrons',
123             value => {
124                 flags => 4096
125             }
126         }
127     );
128
129     my $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
130                         SELECT ?, bit, ?
131                         FROM userflags
132                         WHERE flag = ?");
133     $sth->execute($patron->borrowernumber, 'stage_marc_import', 'tools');
134
135     my $pwd = 'thePassword123';
136     $patron->set_password( { password => $pwd, skip_validation => 1 } );
137
138     my $uid = $patron->userid;
139     my $post_data = {
140         name => 'profileName',
141         overlay_action => 'overlay_action'
142     };
143     $t->post_ok("//$uid:$pwd@/api/v1/import_batch_profiles", json => $post_data)
144       ->status_is(201)
145       ->json_has('/profile_id')
146       ->json_is('/name', $post_data->{name})
147       ->json_is('/overlay_action', $post_data->{overlay_action});
148
149
150     $schema->storage->txn_rollback;
151
152 };
153
154 subtest 'edit profile' => sub {
155     plan tests => 5;
156
157     $schema->storage->txn_begin;
158
159     Koha::ImportBatchProfiles->search()->delete;
160
161     my $patron = $builder->build_object(
162         {
163             class => 'Koha::Patrons',
164             value => {
165                 flags => 4096
166             }
167         }
168     );
169
170     my $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
171                         SELECT ?, bit, ?
172                         FROM userflags
173                         WHERE flag = ?");
174     $sth->execute($patron->borrowernumber, 'stage_marc_import', 'tools');
175
176     my $pwd = 'thePassword123';
177     $patron->set_password( { password => $pwd, skip_validation => 1 } );
178
179     my $uid = $patron->userid;
180
181     my $ibp = $builder->build_object({class => 'Koha::ImportBatchProfiles', value => { name => 'someProfile' }});
182
183     my $post_data = {
184         name => 'theProfile'
185     };
186
187     $t->put_ok("//$uid:$pwd@/api/v1/import_batch_profiles/".$ibp->id, json => $post_data)
188       ->status_is(200)
189       ->json_is('/profile_id', $ibp->id)
190       ->json_is('/name', $post_data->{name});
191
192     $ibp->discard_changes;
193
194     is($ibp->name, $post_data->{name}, 'profile name should be the updated one');
195
196
197     $schema->storage->txn_rollback;
198
199 };
200
201 subtest 'delete profile' => sub {
202     plan tests => 3;
203
204     $schema->storage->txn_begin;
205
206     Koha::ImportBatchProfiles->search()->delete;
207
208     my $patron = $builder->build_object(
209         {
210             class => 'Koha::Patrons',
211             value => {
212                 flags => 4096
213             }
214         }
215     );
216
217     my $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
218                         SELECT ?, bit, ?
219                         FROM userflags
220                         WHERE flag = ?");
221     $sth->execute($patron->borrowernumber, 'stage_marc_import', 'tools');
222
223     my $pwd = 'thePassword123';
224     $patron->set_password( { password => $pwd, skip_validation => 1 } );
225
226     my $uid = $patron->userid;
227
228     my $ibp = $builder->build_object({class => 'Koha::ImportBatchProfiles'});
229
230     $t->delete_ok("//$uid:$pwd@/api/v1/import_batch_profiles/".$ibp->id)
231       ->status_is(204);
232
233     my $search = Koha::ImportBatchProfiles->find($ibp->id);
234
235     is($search, undef, 'profile should be erased');
236
237
238     $schema->storage->txn_rollback;
239
240 };