summaryrefslogtreecommitdiff
path: root/rt/t/web/redirect-after-login.t
blob: d39bb58c8fc2f4f20d2b9c5c8ac01a002eab463d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/perl

use strict;
use warnings;

use RT::Test tests => 120;

my ($baseurl, $agent) = RT::Test->started_ok;

my $url = $agent->rt_base_url;
diag $url if $ENV{TEST_VERBOSE};

# test a login from the main page
{
    $agent->get_ok($url);
    is($agent->{'status'}, 200, "Loaded a page");
    is($agent->uri, $url, "didn't redirect to /NoAuth/Login.html for base URL");
    ok($agent->current_form->find_input('user'));
    ok($agent->current_form->find_input('pass'));
    like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");

    ok($agent->content =~ /username:/i);
    $agent->field( 'user' => 'root' );
    $agent->field( 'pass' => 'password' );

    # the field isn't named, so we have to click link 0
    $agent->click(0);
    is( $agent->status, 200, "Fetched the page ok");
    ok( $agent->content =~ /Logout/i, "Found a logout link");
    is( $agent->uri, $url, "right URL" );
    like( $agent->{redirected_uri}, qr{/NoAuth/Login\.html$}, "We redirected from login");
    $agent->logout();
}

# test a bogus login from the main page
{
    $agent->get_ok($url);
    is($agent->{'status'}, 200, "Loaded a page");
    is($agent->uri, $url, "didn't redirect to /NoAuth/Login.html for base URL");
    ok($agent->current_form->find_input('user'));
    ok($agent->current_form->find_input('pass'));
    like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");

    ok($agent->content =~ /username:/i);
    $agent->field( 'user' => 'root' );
    $agent->field( 'pass' => 'wrongpass' );

    # the field isn't named, so we have to click link 0
    $agent->click(0);
    is( $agent->status, 200, "Fetched the page ok");

    ok( $agent->content =~ /Your username or password is incorrect/i, "Found the error message");
    like( $agent->uri, qr{/NoAuth/Login\.html$}, "now on /NoAuth/Login.html" );
    $agent->logout();

    # Handle the warning after we're done with the page, since this leaves us
    # with a completely different $mech
    $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
}

# test a login from a non-front page, both with a double leading slash and without
for my $path (qw(Prefs/Other.html /Prefs/Other.html)) {
    my $requested = $url.$path;
    $agent->get_ok($requested);
    is($agent->status, 200, "Loaded a page");
    like($agent->uri, qr'/NoAuth/Login\.html\?next=[a-z0-9]{32}', "on login page, with next page hash");
    is($agent->{redirected_uri}, $requested, "redirected from our requested page");

    ok($agent->current_form->find_input('user'));
    ok($agent->current_form->find_input('pass'));
    ok($agent->current_form->find_input('next'));
    like($agent->value('next'), qr/^[a-z0-9]{32}$/i, "next page argument is a hash");
    like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");

    ok($agent->content =~ /username:/i);
    $agent->field( 'user' => 'root' );
    $agent->field( 'pass' => 'password' );

    # the field isn't named, so we have to click link 0
    $agent->click(0);
    is( $agent->status, 200, "Fetched the page ok");
    ok( $agent->content =~ /Logout/i, "Found a logout link");

    if ($path =~ m{/}) {
        (my $collapsed = $path) =~ s{^/}{};
        is( $agent->uri, $url.$collapsed, "right URL, with leading slashes in path collapsed" );
    } else {
        is( $agent->uri, $requested, "right URL" );
    }

    like( $agent->{redirected_uri}, qr{/NoAuth/Login\.html}, "We redirected from login");
    $agent->logout();
}

# test a bogus login from a non-front page
{
    my $requested = $url.'Prefs/Other.html';
    $agent->get_ok($requested);
    is($agent->status, 200, "Loaded a page");
    like($agent->uri, qr'/NoAuth/Login\.html\?next=[a-z0-9]{32}', "on login page, with next page hash");
    is($agent->{redirected_uri}, $requested, "redirected from our requested page");

    ok($agent->current_form->find_input('user'));
    ok($agent->current_form->find_input('pass'));
    ok($agent->current_form->find_input('next'));
    like($agent->value('next'), qr/^[a-z0-9]{32}$/i, "next page argument is a hash");
    like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");

    ok($agent->content =~ /username:/i);
    $agent->field( 'user' => 'root' );
    $agent->field( 'pass' => 'wrongpass' );

    # the field isn't named, so we have to click link 0
    $agent->click(0);
    is( $agent->status, 200, "Fetched the page ok");

    ok( $agent->content =~ /Your username or password is incorrect/i, "Found the error message");
    like( $agent->uri, qr{/NoAuth/Login\.html$}, "still on /NoAuth/Login.html" );

    # try to login again
    ok($agent->current_form->find_input('user'));
    ok($agent->current_form->find_input('pass'));
    ok($agent->current_form->find_input('next'));
    like($agent->value('next'), qr/^[a-z0-9]{32}$/i, "next page argument is a hash");
    like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");

    ok($agent->content =~ /username:/i);
    $agent->field( 'user' => 'root' );
    $agent->field( 'pass' => 'password' );

    # the field isn't named, so we have to click link 0
    $agent->click(0);
    is( $agent->status, 200, "Fetched the page ok");

    # check out where we got to
    is( $agent->uri, $requested, "right URL" );
    like( $agent->{redirected_uri}, qr{/NoAuth/Login\.html}, "We redirected from login");
    $agent->logout();

    # Handle the warning after we're done with the page, since this leaves us
    # with a completely different $mech
    $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
}

# test a login from the main page with query params
{
    my $requested = $url."?user=root;pass=password";
    $agent->get_ok($requested);
    is($agent->{'status'}, 200, "Loaded a page");
    is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for base URL");
    ok($agent->content =~ /Logout/i, "Found a logout link - we're logged in");
    $agent->logout();
}

# test a bogus login from the main page with query params
{
    my $requested = $url."?user=root;pass=wrongpass";
    $agent->get_ok($requested);
    is($agent->{'status'}, 200, "Loaded a page");
    is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for base URL");
    
    ok($agent->content =~ /Your username or password is incorrect/i, "Found the error message");
    ok($agent->current_form->find_input('user'));
    ok($agent->current_form->find_input('pass'));
    like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");
    
    # Handle the warning after we're done with the page, since this leaves us
    # with a completely different $mech
    $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
}

# test a bogus login from a non-front page with query params
{
    my $requested = $url."Prefs/Other.html?user=root;pass=wrongpass";
    $agent->get_ok($requested);
    is($agent->status, 200, "Loaded a page");
    like($agent->uri, qr'/NoAuth/Login\.html\?next=[a-z0-9]{32}', "on login page, with next page hash");
    is($agent->{redirected_uri}, $requested, "redirected from our requested page");
    ok( $agent->content =~ /Your username or password is incorrect/i, "Found the error message");

    ok($agent->current_form->find_input('user'));
    ok($agent->current_form->find_input('pass'));
    ok($agent->current_form->find_input('next'));
    like($agent->value('next'), qr/^[a-z0-9]{32}$/i, "next page argument is a hash");
    like($agent->current_form->action, qr{/NoAuth/Login\.html$}, "login form action is correct");

    # Try to login again
    ok($agent->content =~ /username:/i);
    $agent->field( 'user' => 'root' );
    $agent->field( 'pass' => 'password' );

    # the field isn't named, so we have to click link 0
    $agent->click(0);
    is( $agent->status, 200, "Fetched the page ok");

    # check out where we got to
    is( $agent->uri, $requested, "right URL" );
    like( $agent->{redirected_uri}, qr{/NoAuth/Login\.html}, "We redirected from login");
    $agent->logout();

    # Handle the warning after we're done with the page, since this leaves us
    # with a completely different $mech
    $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
}

# test REST login response
{
    my $requested = $url."REST/1.0/?user=root;pass=password";
    $agent->get($requested);
    is($agent->status, 200, "Loaded a page");
    is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for REST");
    $agent->get_ok($url);
    $agent->logout();
}

# test REST login response for wrong pass
{
    my $requested = $url."REST/1.0/?user=root;pass=passwrong";
    $agent->get_ok($requested);
    is($agent->status, 200, "Loaded a page");
    is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for REST");
    like($agent->content, qr/401 Credentials required/i, "got error status");
    like($agent->content, qr/Your username or password is incorrect/, "got error message");
    
    # Handle the warning after we're done with the page, since this leaves us
    # with a completely different $mech
    $agent->warning_like(qr/FAILED LOGIN for root/, "got failed login warning");
}

# test REST login response for no creds
{
    my $requested = $url."REST/1.0/";
    $agent->get_ok($requested);
    is($agent->status, 200, "Loaded a page");
    is($agent->uri, $requested, "didn't redirect to /NoAuth/Login.html for REST");
    like($agent->content, qr/401 Credentials required/i, "got error status");
    unlike($agent->content, qr/Your username or password is incorrect/, "didn't get any error message");
}

# XXX TODO: we should also be testing WebExternalAuth here, but we don't have
# the framework for dealing with that

1;