Debian Codegraph @

t/01-basic.t

raw symbols SCIP
1 use strict;
2 use warnings;
3
4 use Test::More import => ['!pass'];
5 use t::lib::TestApp;
6 use Dancer ':syntax';
7
8 my $dancer_version;
9 BEGIN {
10 $dancer_version = (exists &dancer_version) ? int(dancer_version()) : 1;
11 require Dancer::Test;
12 if ($dancer_version == 1) {
13 Dancer::Test->import();
14 } else {
15 Dancer::Test->import('t::lib::TestApp');
16 }
17 }
18
19 diag sprintf "Testing DPAE version %s under Dancer %s",
20 $Dancer::Plugin::Auth::Extensible::VERSION,
21 $Dancer::VERSION;
22
23 # First, without being logged in, check we can access the index page, but not
24 # stuff we need to be logged in for:
25
26 response_content_is [ GET => '/' ], 'Index always accessible',
27 'Index accessible while not logged in';
28
29 response_redirect_location_is [ GET => '/loggedin' ],
30 'http://localhost/login?return_url=%2Floggedin',
31 '/loggedin redirected to login page when not logged in';
32
33 response_redirect_location_is [ GET => '/beer' ],
34 'http://localhost/login?return_url=%2Fbeer',
35 '/beer redirected to login page when not logged in';
36
37 response_redirect_location_is [ GET => '/regex/a' ],
38 'http://localhost/login?return_url=%2Fregex%2Fa',
39 '/regex/a redirected to login page when not logged in';
40
41 # OK, now check we can't log in with fake details
42
43 response_status_is [
44 POST => '/login', { body => { username => 'foo', password => 'bar' } }
45 ], 401, 'Login with fake details fails';
46
47 # ... and that we can log in with real details
48 response_status_is [
49 POST => '/login', { body => { username => 'dave', password => 'beer' } }
50 ], 302, 'Login with real details succeeds';
51
52
53 # Now we're logged in, check we can access stuff we should...
54
55 response_status_is [ GET => '/loggedin' ], 200,
56 'Can access /loggedin now we are logged in';
57
58
59 response_content_is [ GET => '/loggedin' ], 'You are logged in',
60 'Correct page content while logged in, too';;
61
62 response_content_is [ GET => '/name' ], 'Hello, David Precious',
63 'Logged in user details via logged_in_user work';
64
65
66 response_content_is [ GET => '/roles' ], 'BeerDrinker,Motorcyclist',
67 'Correct roles for logged in user';
68
69 response_content_is [ GET => '/roles/bob'], 'CiderDrinker', 'Correct roles for other user in current realm';
70
71
72 # Check we can request something which requires a role we have....
73 response_status_is [ GET => '/beer' ], 200,
74 'We can request a route (/beer) requiring a role we have...';
75
76 # Check we can request a route that requires any of a list of roles, one of
77 # which we have:
78 response_status_is [ GET => '/anyrole' ], 200,
79 "We can request a multi-role route requiring with any one role";
80
81 response_status_is [ GET => '/allroles' ], 200,
82 "We can request a multi-role route with all roles required";
83
84
85 # And also a route declared as a regex (this should be no different, but
86 # melmothX was seeing issues with routes not requiring login when they should...
87 response_status_is [ GET => '/regex/a' ], 200,
88 "We can request a regex route when logged in";
89
90 response_status_is [ GET => '/piss/regex' ], 200,
91 "We can request a route requiring a regex role we have";
92
93 # ... but can't request something requiring a role we don't have
94 response_redirect_location_is [ GET => '/piss' ],
95 'http://localhost/login/denied?return_url=%2Fpiss',
96 "We cannot request a route requiring a role we don't have";
97
98 # Check the realm we authenticated against is what we expect
99 response_content_is [ GET => '/realm' ], 'config1',
100 'Authenticated against expected realm';
101
102 # Now, log out
103 response_status_is [
104 POST => '/logout', {},
105 ], 200, 'Logging out returns 200';
106
107
108 # Check we can't access protected pages now we logged out:
109 response_redirect_location_is [ GET => '/loggedin' ],
110 'http://localhost/login?return_url=%2Floggedin',
111 '/loggedin redirected to login page after logging out';
112
113 response_redirect_location_is [ GET => '/beer' ],
114 'http://localhost/login?return_url=%2Fbeer',
115 '/beer redirected to login page after logging out';
116
117 # OK, log back in, this time as a user from the second realm
118 response_status_is [
119 POST => '/login', { body => { username => 'burt', password => 'bacharach' } }
120 ], 302, 'Login as user from second realm succeeds';
121
122 # And that now we're logged in again, we can access protected pages
123 response_status_is [ GET => '/loggedin' ], 200,
124 'Can access /loggedin now we are logged in again';
125
126 # And that the realm we authenticated against is what we expect
127 response_content_is [ GET => '/realm' ], 'config2',
128 'Authenticated against expected realm';
129
130 response_content_is [ GET => '/roles/bob/config1'], 'CiderDrinker', 'Correct roles for other user in current realm';
131
132 # Now, log out again
133 response_status_is [
134 POST => '/logout', {},
135 ], 200, 'Logged out again';
136
137
138 # Now check we can log in as a user whose password is stored hashed:
139 response_status_is [
140 POST => '/login', {
141 body => { username => 'hashedpassword', password => 'password' }
142 }
143 ], 302, 'Login as user with hashed password succeeds';
144
145 # And that now we're logged in again, we can access protected pages
146 response_status_is [ GET => '/loggedin' ], 200,
147 'Can access /loggedin now we are logged in again';
148
149 # Check that the redirect URL can be set when logging in
150 response_redirect_location_is(
151 [
152 POST => '/login', {
153 body => {
154 username => 'dave',
155 password => 'beer',
156 return_url => '/foobar',
157 },
158 },
159 ],
160 'http://localhost/foobar',
161 'Redirect after login to given return_url works',
162 );
163
164 # Test that we are in fact caching fetches of user details, and that we do get
165 # the same details back from the cache. (The numbers are the number of times
166 # the provider was asked for the user's details, and we check the result from
167 # logged_in_user is usable both when initially fetched and when fetched from
168 # cache.)
169 response_content_is [ GET => '/testcaching' ], "0:dave:1:dave:1",
170 "Repeated calls to logged_in_user are cached";
171
172
173
174
175 done_testing();
176
177