]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/24-lp1710949-login-api.t
LP#1830642: add tests for authenticating users when password contains percent sign
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 24-lp1710949-login-api.t
1 #!perl
2
3 use Test::More tests => 27;
4
5 diag("Tests open-ils.auth.login");
6
7 use strict; use warnings;
8 use OpenILS::Utils::TestUtils;
9 use OpenILS::Application::AppUtils;
10 use OpenSRF::Utils::Cache;
11 use Digest::MD5 qw/md5_hex/;
12 our $U = "OpenILS::Application::AppUtils";
13
14 OpenILS::Utils::TestUtils->new->bootstrap;
15
16 my $resp = $U->simplereq(
17     'open-ils.auth',
18     'open-ils.auth.login', {
19         username => 'admin',
20         password => 'demo123',
21         type => 'staff'
22     }
23 );
24
25 is($resp->{textcode}, 'SUCCESS', 'Admin username login OK');
26
27 my $authtoken = $resp->{payload}->{authtoken};
28 ok($authtoken, 'Have an authtoken');
29
30 $resp = $U->simplereq(
31     'open-ils.auth',
32     'open-ils.auth.session.retrieve', $authtoken);
33
34 ok( 
35     (ref($resp) && !$U->event_code($resp) && $resp->usrname eq 'admin'), 
36     'Able to retrieve session'
37 );
38
39 $resp = $U->simplereq(
40     'open-ils.auth',
41     'open-ils.auth.login', {
42         username => 'admin',
43         password => 'demo123x', # bad password
44         type => 'staff'
45     }
46 );
47
48 isnt($resp->{textcode}, 'SUCCESS', 'Admin bad password rejected');
49
50 $resp = $U->simplereq(
51     'open-ils.auth',
52     'open-ils.auth.login', {
53         barcode => '99999381970',
54         password => 'montyc1234',
55         type => 'staff'
56     }
57 );
58
59 is($resp->{textcode}, 'SUCCESS', '99999381970 login OK');
60
61 $resp = $U->simplereq(
62     'open-ils.auth',
63     'open-ils.auth.login', {
64         identifier => 'br1mclark',
65         password => 'montyc1234',
66         type => 'staff'
67     }
68 );
69
70 is($resp->{textcode}, 'SUCCESS', 'Identifier check for br1mclark OK');
71
72 foreach my $i (1..15) {
73     $resp = $U->simplereq(
74         'open-ils.auth',
75         'open-ils.auth.login', {
76             identifier => 'br1mclark',
77             password => 'justplainwrong',
78             type => 'staff'
79         }
80     );
81     isnt($resp->{textcode}, 'SUCCESS', "Attempt $i: wrong password br1mclark does not work");
82 }
83
84 $resp = $U->simplereq(
85     'open-ils.auth',
86     'open-ils.auth.login', {
87         identifier => 'br1mclark',
88         password => 'montyc1234',
89         type => 'staff'
90     }
91 );
92 isnt($resp->{textcode}, 'SUCCESS', '... and consequently multiple failed attempts block');
93
94 # and clean up
95 my $cache = OpenSRF::Utils::Cache->new("global", 0);
96 $cache->delete_cache('oils_auth_br1mclark_count');
97
98 # test for LP#1830642
99 my $new_pwd = 'password%';
100
101 my $user = $U->simplereq(
102     'open-ils.actor',
103     'open-ils.actor.user.fleshed.retrieve_by_barcode',
104     $authtoken,
105     '99999381970'
106 );
107 $user->passwd($new_pwd);
108 $resp = $U->simplereq(
109     'open-ils.actor',
110     'open-ils.actor.patron.update',
111     $authtoken,
112     $user
113 );
114 isa_ok($resp, 'Fieldmapper::actor::user', 'test password updated');
115
116 my $seed = $U->simplereq(
117     'open-ils.auth',
118     'open-ils.auth.authenticate.init',
119     'br1mclark'
120 );
121 ok(defined $seed, 'Got an auth seed');
122
123 my $hashed_pwd = md5_hex($seed . md5_hex($new_pwd));
124 $resp = $U->simplereq(
125     'open-ils.auth',
126     'open-ils.auth.authenticate.complete',
127     {
128         username => 'br1mclark',
129         password => $hashed_pwd,
130         type => 'staff'
131     }
132 );
133 is($resp->{textcode}, 'SUCCESS', '.complete succeeds when password contains %');
134
135 $resp = $U->simplereq(
136     'open-ils.auth',
137     'open-ils.auth.login', {
138         identifier => 'br1mclark',
139         password => $new_pwd,
140         type => 'staff'
141     }
142 );
143 is($resp->{textcode}, 'SUCCESS', '.login succeeds when password contains %');
144
145 # cleanup
146 my $restored_user = $U->simplereq(
147     'open-ils.actor',
148     'open-ils.actor.user.fleshed.retrieve_by_barcode',
149     $authtoken,
150     '99999381970'
151 );
152 $restored_user->passwd('montyc1234');
153 $resp = $U->simplereq(
154     'open-ils.actor',
155     'open-ils.actor.patron.update',
156     $authtoken,
157     $restored_user
158 );
159 isa_ok($resp, 'Fieldmapper::actor::user', 'test password reverted');
160