1
package hudson.security;
3
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
4
import com.gargoylesoftware.htmlunit.html.HtmlPage;
5
import org.jvnet.hudson.test.HudsonTestCase;
6
import org.jvnet.hudson.test.recipes.PresetData;
7
import org.jvnet.hudson.test.recipes.PresetData.DataSet;
8
import org.xml.sax.SAXException;
10
import java.io.IOException;
13
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
16
* @author Kohsuke Kawaguchi
18
public class LoginTest extends HudsonTestCase {
20
* Requesting a loginError page directly should result in a redirect,
21
* on a non-secured Hudson.
23
public void testLoginErrorRedirect() throws Exception {
24
verifyNotError(createWebClient());
27
private void verifyNotError(WebClient wc) throws IOException, SAXException {
28
HtmlPage p = wc.goTo("loginError");
29
URL url = p.getWebResponse().getUrl();
30
System.out.println(url);
31
assertFalse(url.toExternalForm().contains("login"));
35
* Same as {@link #testLoginErrorRedirect()} if the user has already successfully authenticated.
37
@PresetData(DataSet.ANONYMOUS_READONLY)
38
public void testLoginErrorRedirect2() throws Exception {
39
// in a secured Hudson, the error page should render.
40
WebClient wc = createWebClient();
42
wc.goTo("loginError");
43
fail("Expecting a 401 error");
44
} catch (FailingHttpStatusCodeException e) {
46
assertEquals(SC_UNAUTHORIZED,e.getStatusCode());
49
// but not once the user logs in.
50
verifyNotError(wc.login("alice"));