Bug 23019: Add 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 => 4;
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
31 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
32
33 my $t = Test::Mojo->new('Koha::REST::V1');
34
35 subtest 'list profiles' => sub {
36     plan tests => 4;
37
38     $schema->storage->txn_begin;
39
40     Koha::ImportBatchProfiles->search()->delete;
41
42     my $ibp1 = $builder->build_object({ class => 'Koha::ImportBatchProfiles', value => { name => 'a_ibp' } });
43     my $ibp2 = $builder->build_object({ class => 'Koha::ImportBatchProfiles', value => { name => 'b_ibp' } });
44
45     my $patron = $builder->build_object(
46         {
47             class => 'Koha::Patrons',
48             value => {
49                 flags => 1
50             }
51         }
52     );
53
54     my $pwd = 'thePassword123';
55     $patron->set_password( { password => $pwd, skip_validation => 1 } );
56
57     my $uid = $patron->userid;
58
59     $t->get_ok("//$uid:$pwd@/api/v1/import_batch_profiles?_order_by=+name")
60       ->status_is(200)
61       ->json_is('/0/name', $ibp1->name)
62       ->json_is('/1/name', $ibp2->name);
63
64
65     $schema->storage->txn_rollback;
66
67 };
68
69 subtest 'add profile' => sub {
70     plan tests => 5;
71
72     $schema->storage->txn_begin;
73
74     Koha::ImportBatchProfiles->search()->delete;
75
76     my $patron = $builder->build_object(
77         {
78             class => 'Koha::Patrons',
79             value => {
80                 flags => 1
81             }
82         }
83     );
84
85     my $pwd = 'thePassword123';
86     $patron->set_password( { password => $pwd, skip_validation => 1 } );
87
88     my $uid = $patron->userid;
89     my $post_data = {
90         name => 'profileName',
91         overlay_action => 'overlay_action'
92     };
93     $t->post_ok("//$uid:$pwd@/api/v1/import_batch_profiles", json => $post_data)
94       ->status_is(201)
95       ->json_has('/profile_id')
96       ->json_is('/name', $post_data->{name})
97       ->json_is('/overlay_action', $post_data->{overlay_action});
98
99
100     $schema->storage->txn_rollback;
101
102 };
103
104 subtest 'edit profile' => sub {
105     plan tests => 5;
106
107     $schema->storage->txn_begin;
108
109     Koha::ImportBatchProfiles->search()->delete;
110
111     my $patron = $builder->build_object(
112         {
113             class => 'Koha::Patrons',
114             value => {
115                 flags => 1
116             }
117         }
118     );
119
120     my $pwd = 'thePassword123';
121     $patron->set_password( { password => $pwd, skip_validation => 1 } );
122
123     my $uid = $patron->userid;
124
125     my $ibp = $builder->build_object({class => 'Koha::ImportBatchProfiles', value => { name => 'someProfile' }});
126
127     my $post_data = {
128         name => 'theProfile'
129     };
130
131     $t->put_ok("//$uid:$pwd@/api/v1/import_batch_profiles/".$ibp->id, json => $post_data)
132       ->status_is(200)
133       ->json_is('/profile_id', $ibp->id)
134       ->json_is('/name', $post_data->{name});
135
136     $ibp->discard_changes;
137
138     is($ibp->name, $post_data->{name}, 'profile name should be the updated one');
139
140
141     $schema->storage->txn_rollback;
142
143 };
144
145 subtest 'delete profile' => sub {
146     plan tests => 3;
147
148     $schema->storage->txn_begin;
149
150     Koha::ImportBatchProfiles->search()->delete;
151
152     my $patron = $builder->build_object(
153         {
154             class => 'Koha::Patrons',
155             value => {
156                 flags => 1
157             }
158         }
159     );
160
161     my $pwd = 'thePassword123';
162     $patron->set_password( { password => $pwd, skip_validation => 1 } );
163
164     my $uid = $patron->userid;
165
166     my $ibp = $builder->build_object({class => 'Koha::ImportBatchProfiles'});
167
168     $t->delete_ok("//$uid:$pwd@/api/v1/import_batch_profiles/".$ibp->id)
169       ->status_is(204);
170
171     my $search = Koha::ImportBatchProfiles->find($ibp->id);
172
173     is($search, undef, 'profile should be erased');
174
175
176     $schema->storage->txn_rollback;
177
178 };