]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/15-lp1533329-opt-in.t
LP#1670407 Add tests for xact_finish close/re-open
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 15-lp1533329-opt-in.t
1 #!perl
2
3 use Test::More tests => 12;
4
5 diag("Test checking for, creating, and restricting patron opt-in.");
6
7 use constant WORKSTATION_NAME => 'BR1-test-12-lp1533329-opt-in.t';
8 use constant WORKSTATION_LIB => 4; # BR1, a branch of SYS1
9 use constant PATRON_LIB => 6; # BR3, a branch of SYS2
10 use constant PATRON_SYS => 3; # SYS2
11 use constant SYS_DEPTH => 1; # depth of "System" org type
12 use constant PATRON_BARCODE => '99999359616';
13
14 use strict; use warnings;
15
16 use OpenILS::Utils::TestUtils;
17 use OpenILS::Utils::CStoreEditor qw/:funcs/;
18 use OpenILS::Utils::Fieldmapper;
19
20 my $script = OpenILS::Utils::TestUtils->new();
21 $script->bootstrap;
22
23 our $U = "OpenILS::Application::AppUtils";
24
25 my $e = new_editor(xact => 1);
26 $e->init;
27
28 # initialize a new aous object for insertion into the db
29 sub new_org_setting {
30     my ($org_unit, $name, $value) = @_;
31     my $set = Fieldmapper::actor::org_unit_setting->new();
32     $set->org_unit($org_unit);
33     $set->name($name);
34     $set->value($value);
35     return $set;
36 }
37
38 sub opt_in_enabled {
39     my $resp = $U->simplereq(
40         'open-ils.actor',
41         'open-ils.actor.user.org_unit_opt_in.enabled'
42     );
43     return $resp;
44 }
45
46 # do an opt-in check
47 sub opt_in_check {
48     my ($authtoken, $usr_id) = @_;
49     my $resp = $U->simplereq(
50         'open-ils.actor',
51         'open-ils.actor.user.org_unit_opt_in.check',
52         $authtoken, $usr_id);
53     return $resp;
54 }
55
56 SKIP: {
57     skip 'cannot test opt-in unless enabled in opensrf.xml', 12 unless(opt_in_enabled());
58
59     #----------------------------------------------------------------
60     # 1. Login, register workstation, get authtoken.
61     #----------------------------------------------------------------
62     $script->authenticate({
63         username => 'admin',
64         password => 'demo123',
65         type => 'staff'});
66     ok(
67         $script->authtoken,
68         'Have an authtoken'
69     );
70     my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
71     ok(
72         ! ref $ws,
73         'Registered a new workstation'
74     );
75     $script->logout();
76     $script->authenticate({
77         username => 'admin',
78         password => 'demo123',
79         type => 'staff',
80         workstation => WORKSTATION_NAME});
81     ok(
82         $script->authtoken,
83         'Have an authtoken associated with the workstation'
84     );
85
86     #----------------------------------------------------------------
87     # 2. Set org.patron_opt_boundary for SYS2, so that BR1 is outside
88     # the boundary.
89     #----------------------------------------------------------------
90     $e->xact_begin;
91     my $boundary = new_org_setting(PATRON_SYS, 'org.patron_opt_boundary', SYS_DEPTH);
92     my $boundary_stat = $e->create_actor_org_unit_setting($boundary);
93     ok($boundary_stat, 'Opt boundary setting created successfully');
94     $e->xact_commit;
95
96     #----------------------------------------------------------------
97     # 3. Check opt-in for test patron.  It should return 0.
98     #----------------------------------------------------------------
99     my $patron = $U->fetch_user_by_barcode(PATRON_BARCODE);
100     is(
101         opt_in_check($script->authtoken, $patron->id),
102         '0',
103         'Opt-in check for non-opted-in patron correctly returned 0'
104     );
105
106     #----------------------------------------------------------------
107     # 4. Set org.restrict_opt_to_depth at SYS2, so that BR1 is
108     # outside SYS2's section of the tree at the specified depth (thus
109     # preventing opt-in).
110     #----------------------------------------------------------------
111     $e->xact_begin;
112     my $restrict = new_org_setting(PATRON_SYS, 'org.restrict_opt_to_depth', SYS_DEPTH);
113     my $restrict_stat = $e->create_actor_org_unit_setting($restrict);
114     ok($restrict_stat, 'Opt restrict depth setting created successfully');
115     $e->xact_commit;
116
117     #----------------------------------------------------------------
118     # 5. Check opt-in for test patron.  It should return 2.
119     #----------------------------------------------------------------
120     is(
121         opt_in_check($script->authtoken, $patron->id),
122         '2',
123         'Opt-in check for patron at restricted opt-in library correctly returned 2'
124     );
125
126     #----------------------------------------------------------------
127     # 6. Remove the org.restrict_opt_to_depth setting for SYS2.
128     #----------------------------------------------------------------
129     $e->xact_begin;
130     my $delete_restrict_stat = $e->delete_actor_org_unit_setting($restrict);
131     ok($delete_restrict_stat, 'Opt restrict depth setting deleted successfully');
132     $e->xact_commit;
133
134     #----------------------------------------------------------------
135     # 7. Create opt-in for test patron.
136     #----------------------------------------------------------------
137     my $opt_id = $U->simplereq(
138         'open-ils.actor',
139         'open-ils.actor.user.org_unit_opt_in.create',
140         $script->authtoken, $patron->id, WORKSTATION_LIB);
141     ok($opt_id, 'Patron successfully opted in');
142
143     #----------------------------------------------------------------
144     # 8. Check opt-in for test patron.  It should return 1.
145     #----------------------------------------------------------------
146     is(
147         opt_in_check($script->authtoken, $patron->id),
148         '1',
149         'Opt-in check for opted-in patron correctly returned 1'
150     );
151
152     #----------------------------------------------------------------
153     # 9. Delete opt-in.
154     #----------------------------------------------------------------
155     my $opt = $U->simplereq(
156         'open-ils.cstore',
157         'open-ils.cstore.direct.actor.usr_org_unit_opt_in.retrieve',
158         $opt_id
159     );
160     $e->xact_begin;
161     my $delete_opt_stat = $e->delete_actor_usr_org_unit_opt_in($opt);
162     ok($delete_opt_stat, 'Opt-in deleted successfully');
163     $e->xact_commit;
164
165     #----------------------------------------------------------------
166     # 10. Remove opt boundary setting.
167     #----------------------------------------------------------------
168     $e->xact_begin;
169     my $delete_setting_stat = $e->delete_actor_org_unit_setting($boundary);
170     ok($delete_setting_stat, 'Opt boundary setting deleted successfully');
171     $e->xact_commit;
172 }
173