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