RT 4.0.13
[freeside.git] / rt / t / web / redirect-after-login.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 122;
6
7 RT->Config->Set( GnuPG => Enable => 0 );
8
9 my ($baseurl, $agent) = RT::Test->started_ok;
10
11 my $url = $agent->rt_base_url;
12 diag $url if $ENV{TEST_VERBOSE};
13
14 # test a login from the main page
15 {
16     $agent->get_ok($url);
17     is($agent->{'status'}, 200, "Loaded a page");
18     is($agent->uri, $url, "didn't redirect to /NoAuth/Login.html for base URL");
19     ok($agent->current_form->find_input('user'));
20     ok($agent->current_form->find_input('pass'));
21     like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");
22
23     ok($agent->content =~ /username:/i);
24     $agent->field( 'user' => 'root' );
25     $agent->field( 'pass' => 'password' );
26
27     # the field isn't named, so we have to click link 0
28     $agent->click(0);
29     is( $agent->status, 200, "Fetched the page ok");
30     ok( $agent->content =~ /Logout/i, "Found a logout link");
31     is( $agent->uri, $url, "right URL" );
32     like( $agent->{redirected_uri}, qr{/NoAuth/Login\.html$}, "We redirected from login");
33     $agent->logout();
34 }
35
36 # test a bogus login from the main page
37 {
38     $agent->get_ok($url);
39     is($agent->{'status'}, 200, "Loaded a page");
40     is($agent->uri, $url, "didn't redirect to /NoAuth/Login.html for base URL");
41     ok($agent->current_form->find_input('user'));
42     ok($agent->current_form->find_input('pass'));
43     like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");
44
45     ok($agent->content =~ /username:/i);
46     $agent->field( 'user' => 'root' );
47     $agent->field( 'pass' => 'wrongpass' );
48
49     # the field isn't named, so we have to click link 0
50     $agent->click(0);
51     is( $agent->status, 200, "Fetched the page ok");
52
53     ok( $agent->content =~ /Your username or password is incorrect/i, "Found the error message");
54     like( $agent->uri, qr{/NoAuth/Login\.html$}, "now on /NoAuth/Login.html" );
55     $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
56
57     $agent->logout();
58 }
59
60 # test a login from a non-front page, both with a double leading slash and without
61 for my $path (qw(Prefs/Other.html /Prefs/Other.html)) {
62     my $requested = $url.$path;
63     $agent->get_ok($requested);
64     is($agent->status, 200, "Loaded a page");
65     like($agent->uri, qr'/NoAuth/Login\.html\?next=[a-z0-9]{32}', "on login page, with next page hash");
66     is($agent->{redirected_uri}, $requested, "redirected from our requested page");
67
68     ok($agent->current_form->find_input('user'));
69     ok($agent->current_form->find_input('pass'));
70     ok($agent->current_form->find_input('next'));
71     like($agent->value('next'), qr/^[a-z0-9]{32}$/i, "next page argument is a hash");
72     like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");
73
74     ok($agent->content =~ /username:/i);
75     $agent->field( 'user' => 'root' );
76     $agent->field( 'pass' => 'password' );
77
78     # the field isn't named, so we have to click link 0
79     $agent->click(0);
80     is( $agent->status, 200, "Fetched the page ok");
81     ok( $agent->content =~ /Logout/i, "Found a logout link");
82
83     if ($path =~ m{/}) {
84         (my $collapsed = $path) =~ s{^/}{};
85         is( $agent->uri, $url.$collapsed, "right URL, with leading slashes in path collapsed" );
86     } else {
87         is( $agent->uri, $requested, "right URL" );
88     }
89
90     like( $agent->{redirected_uri}, qr{/NoAuth/Login\.html}, "We redirected from login");
91     $agent->logout();
92 }
93
94 # test a bogus login from a non-front page
95 {
96     my $requested = $url.'Prefs/Other.html';
97     $agent->get_ok($requested);
98     is($agent->status, 200, "Loaded a page");
99     like($agent->uri, qr'/NoAuth/Login\.html\?next=[a-z0-9]{32}', "on login page, with next page hash");
100     is($agent->{redirected_uri}, $requested, "redirected from our requested page");
101
102     ok($agent->current_form->find_input('user'));
103     ok($agent->current_form->find_input('pass'));
104     ok($agent->current_form->find_input('next'));
105     like($agent->value('next'), qr/^[a-z0-9]{32}$/i, "next page argument is a hash");
106     like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");
107
108     ok($agent->content =~ /username:/i);
109     $agent->field( 'user' => 'root' );
110     $agent->field( 'pass' => 'wrongpass' );
111
112     # the field isn't named, so we have to click link 0
113     $agent->click(0);
114     is( $agent->status, 200, "Fetched the page ok");
115
116     ok( $agent->content =~ /Your username or password is incorrect/i, "Found the error message");
117     like( $agent->uri, qr{/NoAuth/Login\.html$}, "still on /NoAuth/Login.html" );
118     $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
119
120     # try to login again
121     ok($agent->current_form->find_input('user'));
122     ok($agent->current_form->find_input('pass'));
123     ok($agent->current_form->find_input('next'));
124     like($agent->value('next'), qr/^[a-z0-9]{32}$/i, "next page argument is a hash");
125     like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");
126
127     ok($agent->content =~ /username:/i);
128     $agent->field( 'user' => 'root' );
129     $agent->field( 'pass' => 'password' );
130
131     # the field isn't named, so we have to click link 0
132     $agent->click(0);
133     is( $agent->status, 200, "Fetched the page ok");
134
135     # check out where we got to
136     is( $agent->uri, $requested, "right URL" );
137     like( $agent->{redirected_uri}, qr{/NoAuth/Login\.html}, "We redirected from login");
138     $agent->logout();
139 }
140
141 # test a login from the main page with query params
142 {
143     my $requested = $url."?user=root;pass=password";
144     $agent->get_ok($requested);
145     is($agent->{'status'}, 200, "Loaded a page");
146     is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for base URL");
147     ok($agent->content =~ /Logout/i, "Found a logout link - we're logged in");
148     $agent->logout();
149 }
150
151 # test a bogus login from the main page with query params
152 {
153     my $requested = $url."?user=root;pass=wrongpass";
154     $agent->get_ok($requested);
155     is($agent->{'status'}, 200, "Loaded a page");
156     is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for base URL");
157     
158     ok($agent->content =~ /Your username or password is incorrect/i, "Found the error message");
159     ok($agent->current_form->find_input('user'));
160     ok($agent->current_form->find_input('pass'));
161     like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");
162     $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
163 }
164
165 # test a bogus login from a non-front page with query params
166 {
167     my $requested = $url."Prefs/Other.html?user=root;pass=wrongpass";
168     $agent->get_ok($requested);
169     is($agent->status, 200, "Loaded a page");
170     like($agent->uri, qr'/NoAuth/Login\.html\?next=[a-z0-9]{32}', "on login page, with next page hash");
171     is($agent->{redirected_uri}, $requested, "redirected from our requested page");
172     ok( $agent->content =~ /Your username or password is incorrect/i, "Found the error message");
173
174     ok($agent->current_form->find_input('user'));
175     ok($agent->current_form->find_input('pass'));
176     ok($agent->current_form->find_input('next'));
177     like($agent->value('next'), qr/^[a-z0-9]{32}$/i, "next page argument is a hash");
178     like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");
179     $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
180
181     # Try to login again
182     ok($agent->content =~ /username:/i);
183     $agent->field( 'user' => 'root' );
184     $agent->field( 'pass' => 'password' );
185
186     # the field isn't named, so we have to click link 0
187     $agent->click(0);
188     is( $agent->status, 200, "Fetched the page ok");
189
190     # check out where we got to
191     is( $agent->uri, $requested, "right URL" );
192     like( $agent->{redirected_uri}, qr{/NoAuth/Login\.html}, "We redirected from login");
193     $agent->logout();
194 }
195
196 # test REST login response
197 {
198     $agent = RT::Test::Web->new;
199     my $requested = $url."REST/1.0/?user=root;pass=password";
200     $agent->get($requested);
201     is($agent->status, 200, "Loaded a page");
202     is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for REST");
203     $agent->get_ok($url."REST/1.0");
204 }
205
206 # test REST login response for wrong pass
207 {
208     $agent = RT::Test::Web->new;
209     my $requested = $url."REST/1.0/?user=root;pass=passwrong";
210     $agent->get_ok($requested);
211     is($agent->status, 200, "Loaded a page");
212     is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for REST");
213     like($agent->content, qr/401 Credentials required/i, "got error status");
214     like($agent->content, qr/Your username or password is incorrect/, "got error message");
215     $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
216 }
217
218 # test REST login response for no creds
219 {
220     $agent = RT::Test::Web->new;
221     my $requested = $url."REST/1.0/";
222     $agent->get_ok($requested);
223     is($agent->status, 200, "Loaded a page");
224     is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for REST");
225     like($agent->content, qr/401 Credentials required/i, "got error status");
226     unlike($agent->content, qr/Your username or password is incorrect/, "didn't get any error message");
227 }
228
229 # XXX TODO: we should also be testing WebExternalAuth here, but we don't have
230 # the framework for dealing with that
231
232 1;