LP1929593 UPDATE_COPY_BARCODE permission
[evergreen-equinox.git] / Open-ILS / src / perlmods / live_t / lp1929593-update-copy-barcode-perm.t
1 #!perl
2 use strict; use warnings;
3 use Test::More tests => 17;
4 use OpenILS::Utils::TestUtils;
5 use OpenILS::Const qw(:const);
6
7 my $U = 'OpenILS::Application::AppUtils';
8 my $script = OpenILS::Utils::TestUtils->new();
9
10 diag("Test LP1929593 Wishlist: need separate permission for editing barcode");
11
12 use constant {
13     BR1_ID => 4,
14     BR3_ID => 6,
15     WORKSTATION => 'BR1-lp1929593-ebarc'
16 };
17
18 # We are deliberately NOT using the admin user to check for a perm failure.
19 my $credentials = {
20     username => 'br1mtownsend',
21     password => 'maryt1234',
22     type => 'staff'
23 };
24
25 # Log in as staff.
26 my $authtoken = $script->authenticate($credentials);
27 ok(
28     $authtoken,
29     'Logged in'
30 ) or BAIL_OUT('Must log in');
31
32 # Find or register workstation.
33 my $ws = $script->find_or_register_workstation(WORKSTATION, BR1_ID);
34 ok(
35     ! ref $ws,
36     'Found or registered workstation'
37 ) or BAIL_OUT('Need Workstation');
38
39 # Logout.
40 $script->logout();
41 ok(
42     ! $script->authtoken,
43     'Logged out'
44 );
45
46 # Login with workstation.
47 $credentials->{workstation} = WORKSTATION;
48 $credentials->{password} = 'maryt1234';
49 $authtoken = $script->authenticate($credentials);
50 ok(
51     $script->authtoken,
52     'Logged in with workstation'
53 ) or BAIL_OUT('Must log in');
54
55 # Find available copy at BR1
56 my $acps = $U->simplereq(
57     'open-ils.pcrud',
58     'open-ils.pcrud.search.acp.atomic',
59     $authtoken,
60     {circ_lib => BR1_ID, status => OILS_COPY_STATUS_AVAILABLE},
61     {limit => 1}
62 );
63 my $copy = $acps->[0];
64 isa_ok(
65     ref $copy,
66     'Fieldmapper::asset::copy',
67     'Got available copy from BR1'
68 );
69
70 sub test_barcode_rename_expecting_success {
71     my $copy = shift;
72
73     diag('Testing re-barcoding of ' . $copy->barcode() . ', expecting successful re-barcoding.');
74     my $original_barcode = $copy->barcode();
75
76     # Re-barcode it
77     my $result = $U->simplereq(
78         'open-ils.cat',
79         'open-ils.cat.update_copy_barcode',
80         $authtoken,
81         $copy->id(),
82         'new' . $original_barcode
83     );
84     is(
85         $result,
86         $copy->id(),
87         'open-ils.cat.update_copy_barcode indicates success'
88     );
89
90     # Double-check to be sure
91     $copy = $U->simplereq(
92         'open-ils.pcrud',
93         'open-ils.pcrud.retrieve.acp',
94         $authtoken,
95         $copy->id()
96     );
97     is(
98         $copy->barcode(),
99         'new' . $original_barcode,
100         'Copy was indeed re-barcoded'
101     );
102     diag('Current barcode: ' . $copy->barcode());
103
104     # Change it back
105     $result = $U->simplereq(
106         'open-ils.cat',
107         'open-ils.cat.update_copy_barcode',
108         $authtoken,
109         $copy->id(),
110         $original_barcode
111     );
112     is(
113         $result,
114         $copy->id(),
115         'open-ils.cat.update_copy_barcode indicates success'
116     );
117
118     # Double-check to be sure
119     $copy = $U->simplereq(
120         'open-ils.pcrud',
121         'open-ils.pcrud.retrieve.acp',
122         $authtoken,
123         $copy->id()
124     );
125     is(
126         $copy->barcode(),
127         $original_barcode,
128         'Copy was indeed re-barcoded'
129     );
130     diag('Current barcode: ' . $copy->barcode());
131 }
132
133 diag('Testing when user has both UPDATE_COPY and UPDATE_COPY_BARCODE');
134 test_barcode_rename_expecting_success($copy);
135
136 sub changePermCode {
137     my $from = shift;
138     my $to = shift;
139     diag('Changing ' . $from . ' permission to ' . $to);
140
141     # stateful cstore session
142     my $cstore_ses = $script->session('open-ils.cstore');
143     $cstore_ses->connect;
144
145     # Now let's fetch the $from perm
146     my $xact = $cstore_ses->request('open-ils.cstore.transaction.begin')->gather(1);
147     my $retrieve_req = $cstore_ses->request(
148         'open-ils.cstore.direct.permission.perm_list.search',
149         { 'code' => $from }
150     );
151     my $perm = $retrieve_req->gather(1);
152     is(
153         $perm->code(),
154         $from,
155         "Fetched $from permission"
156     );
157
158     # now let's change the code
159     $perm->code($to);
160     my $update_req = $cstore_ses->request(
161         'open-ils.cstore.direct.permission.perm_list.update',
162         $perm
163     );
164     my $update_resp = $update_req->gather(1);
165     is(
166         $update_resp,
167         $perm->id(),
168         'cstore update successful'
169     );
170
171     $cstore_ses->request('open-ils.cstore.transaction.commit')->gather(1);
172     $cstore_ses->disconnect();
173 }
174
175 changePermCode('UPDATE_COPY', 'WAS_UPDATE_COPY');
176 diag('Testing when user only has UPDATE_COPY_BARCODE');
177 test_barcode_rename_expecting_success($copy);
178
179 sub test_barcode_rename_expecting_failure {
180     my $copy = shift;
181
182     diag('Testing re-barcoding of ' . $copy->barcode() . ', expecting unsuccessful re-barcoding.');
183     my $original_barcode = $copy->barcode();
184
185     # Re-barcode it
186     my $result = $U->simplereq(
187         'open-ils.cat',
188         'open-ils.cat.update_copy_barcode',
189         $authtoken,
190         $copy->id(),
191         'new' . $original_barcode
192     );
193     isnt(
194         $result,
195         $copy->id(),
196         'open-ils.cat.update_copy_barcode indicates failure'
197     );
198
199     # Double-check to be sure
200     $copy = $U->simplereq(
201         'open-ils.pcrud',
202         'open-ils.pcrud.retrieve.acp',
203         $authtoken,
204         $copy->id()
205     );
206     is(
207         $copy->barcode(),
208         $original_barcode,
209         'Copy was indeed not re-barcoded'
210     );
211     diag('Current barcode: ' . $copy->barcode());
212
213     # Attempt to change it back, just in case things are succeeding when they're not supposed to
214     $result = $U->simplereq(
215         'open-ils.cat',
216         'open-ils.cat.update_copy_barcode',
217         $authtoken,
218         $copy->id(),
219         $original_barcode
220     );
221     isnt(
222         $result,
223         $copy->id(),
224         'open-ils.cat.update_copy_barcode indicates failure'
225     );
226
227     # Double-check to be sure
228     $copy = $U->simplereq(
229         'open-ils.pcrud',
230         'open-ils.pcrud.retrieve.acp',
231         $authtoken,
232         $copy->id()
233     );
234     is(
235         $copy->barcode(),
236         $original_barcode,
237         'Copy was indeed not re-barcoded'
238     );
239     diag('Current barcode: ' . $copy->barcode());
240 }
241
242 changePermCode('UPDATE_COPY_BARCODE', 'WAS_UPDATE_COPY_BARCODE');
243
244 diag('Testing when user has neither UPDATE_COPY_BARCODE nor UPDATE_COPY');
245 test_barcode_rename_expecting_failure($copy);
246
247 # back to the way they were
248 changePermCode('WAS_UPDATE_COPY', 'UPDATE_COPY');
249 changePermCode('WAS_UPDATE_COPY_BARCODE', 'UPDATE_COPY_BARCODE');
250
251 # Logout
252 $script->logout(); # Not a test, just to be pedantic.