]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/29-lp1817645-remoteauth-patron-api.t
e2c7bb28b7053e1d2f71459a7a8bd4a30c37cbe4
[Evergreen.git] / Open-ILS / src / perlmods / live_t / 29-lp1817645-remoteauth-patron-api.t
1 #!perl
2
3 use Test::More tests => 10; # XXX
4
5 diag("Tests RemoteAuth patron auth/retrieval");
6
7 use strict; use warnings;
8 use OpenILS::Utils::TestUtils;
9 use MIME::Base64;
10 use HTTP::Request;
11 use LWP::UserAgent;
12 use OpenILS::Utils::CStoreEditor qw/:funcs/;
13 use OpenILS::Application::AppUtils;
14 our $U = "OpenILS::Application::AppUtils";
15
16 OpenILS::Utils::TestUtils->new->bootstrap;
17
18 my $not_found = { barcode => '99999393000', password => 'nonexistentbarcode' };
19 my $expired = { barcode => '99999393001', password => 'marges1234' };
20 my $deleted = { barcode => '99999393002', password => 'homers1234' };
21 my $barred = { barcode => '99999393003', password => 'barts1234' };
22 my $valid = { barcode => '99999393004', password => 'lisas1234' };
23 my $inactive = { barcode => '99999393005', password => 'maggies1234' };
24 my $external = { barcode => '99999393100', password => 'shelbyvillem1234' };
25
26 # context org is SYS1, test user's home OU is BR1;
27 # use BR3 (under SYS2) to test external users
28 my $external_org = 6; # BR3
29
30 my $staff_login = $U->simplereq(
31     'open-ils.auth',
32     'open-ils.auth.login', {
33         username => 'admin',
34         password => 'demo123',
35         type => 'staff'
36     }
37 );
38 is($staff_login->{textcode}, 'SUCCESS', 'Staff login OK');
39 my $e = new_editor( authtoken => $staff_login->{payload}->{authtoken} );
40 $e->init;
41
42 my $client = LWP::UserAgent->new;
43 $client->ssl_opts(
44     SSL_verify_mode => 0,
45     verify_hostname => 0
46 );
47
48 # my $res = $client->request( $method, $uri, $headers, $content, $request_timeout );
49
50
51 ######################################################################
52
53 # requests:
54 # - validate barcode only?
55 # - validate barcode + PIN
56 # - retrieve user and check for required fields in response
57
58 # test cases:
59 # - valid user with username or barcode (opac.barcode_regex)
60 # - valid user with barcode prefix
61 # - valid user with barcode
62 # - invalid password
63 # - barcode not found
64 # - user is deleted
65 # - user is expired
66 # - user is barred
67 # - user has non-blocking penalties, auth/retrieval succeeds
68 # - user has blocking penalties
69 # - user exists, but home OU is not in scope
70
71 # not currently supported:
72 # - AuthProxy: services should use the remote auth server directly
73 # - opted-in patrons: these are external patrons and thus not auth'd
74
75 ######################################################################
76
77 #---------------------------------------------------------------------
78 # Basic access authentication (RFC 7617)
79 #---------------------------------------------------------------------
80 # - endpoint: /api/basicauth
81 # - client auth: none
82 # - request: includes "Authorization: Basic <credentials>" header,
83 #   credentials = Base64-encoded "id:password" string
84 # - response: HTTP 200 on success, 401 with WWW-Authenticate header 
85 #   field on failure
86 # - does not return patron info
87 #---------------------------------------------------------------------
88 sub basic_request {
89     my ($u, $password) = @_;
90     my $barcode = $u->{barcode};
91     $password ||= $u->{password};
92     my $resp = $client->get(
93         "https://localhost/api/basicauth",
94         'Authorization' => 'Basic ' . encode_base64("$barcode:$password")
95     );
96     return $resp->code;
97 }
98
99 my $basic_not_found = basic_request($not_found);
100 is ( $basic_not_found, '403', 'Basic request for nonexistent barcode correctly returned 403' );
101
102 my $basic_success = basic_request($valid);
103 is( $basic_success, '200', 'Basic request for valid patron OK' );
104
105 # invalid password
106 my $basic_invalid_pw = basic_request($valid, 'badpassword');
107 is( $basic_invalid_pw, '403', 'Basic request with invalid password correctly returned 403' );
108
109 # user is deleted
110 my $basic_deleted = basic_request($deleted);
111 is( $basic_deleted, '403', 'Basic request for deleted user correctly returned 403' );
112
113 # user is expired
114 my $basic_expired = basic_request($expired);
115 is( $basic_expired, '403', 'Basic request for expired user correctly returned 403' );
116
117 # user is inactive
118 my $basic_inactive = basic_request($inactive);
119 is( $basic_inactive, '403', 'Basic request for inactive user correctly returned 403' );
120
121 # user is barred
122 my $basic_barred = basic_request($barred);
123 is( $basic_barred, '403', 'Basic request for barred user correctly returned 403' );
124
125 # home OU is not in scope
126 my $basic_external = basic_request($external);
127 is( $basic_external, '403', 'Basic request for external user correctly returned 403' );
128
129 # TODO: user has blocking penalties
130
131 # TODO: user has non-blocking penalties, auth/retrieval succeeds
132
133
134
135 # TODO: EZProxy external script authentication:
136 # - endpoint: /remoteauth/ezproxy/<shortname>/<id>/<password>
137 # - client auth: none
138 # - request: GET with user and pass params, as above
139 # - response: "+VALID" if auth succeeds
140
141
142 # verify user activity based on the above tests
143 my $user = $U->fetch_user_by_barcode( $valid->{barcode} );
144 my $basic_activity = $e->search_actor_usr_activity([{usr => $user->id, etype => 1001}]);
145 ok(scalar(@$basic_activity) > 0, 'Basic request for valid patron is recorded in user activity');
146