add: testcase for successful certificate login
[gigi.git] / tests / org / cacert / gigi / LoginTest.java
1 package org.cacert.gigi;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
6 import java.io.IOException;
7 import java.net.HttpURLConnection;
8 import java.net.URL;
9 import java.net.URLConnection;
10 import java.security.GeneralSecurityException;
11 import java.security.KeyPair;
12 import java.security.PrivateKey;
13 import java.security.cert.X509Certificate;
14
15 import org.cacert.gigi.dbObjects.Certificate;
16 import org.cacert.gigi.dbObjects.Certificate.CSRType;
17 import org.cacert.gigi.dbObjects.CertificateProfile;
18 import org.cacert.gigi.dbObjects.Digest;
19 import org.cacert.gigi.dbObjects.User;
20 import org.cacert.gigi.testUtils.IOUtils;
21 import org.cacert.gigi.testUtils.ManagedTest;
22 import org.junit.Test;
23
24 public class LoginTest extends ManagedTest {
25
26     @Test
27     public void testLoginUnverified() throws IOException {
28         String email = createUniqueName() + "@testmail.org";
29         registerUser("an", "bn", email, TEST_PASSWORD);
30         getMailReceiver().receive();
31         assertFalse(isLoggedin(login(email, TEST_PASSWORD)));
32     }
33
34     @Test
35     public void testLoginVerified() throws IOException {
36         String email = createUniqueName() + "@testmail.org";
37         createVerifiedUser("an", "bn", email, TEST_PASSWORD);
38         assertTrue(isLoggedin(login(email, TEST_PASSWORD)));
39     }
40
41     @Test
42     public void testLoginWrongPassword() throws IOException {
43         String email = createUniqueName() + "@testmail.org";
44         createVerifiedUser("an", "bn", email, TEST_PASSWORD);
45         assertFalse(isLoggedin(login(email, TEST_PASSWORD + "b")));
46     }
47
48     @Test
49     public void testLogoutVerified() throws IOException {
50         String email = createUniqueName() + "@testmail.org";
51         createVerifiedUser("an", "bn", email, TEST_PASSWORD);
52         String cookie = login(email, TEST_PASSWORD);
53         assertTrue(isLoggedin(cookie));
54         logout(cookie);
55         assertFalse(isLoggedin(cookie));
56     }
57
58     private void logout(String cookie) throws IOException {
59         get(cookie, "/logout").getHeaderField("Location");
60     }
61
62     @Test
63     public void testLoginMethodDisplay() throws IOException {
64         String email = createUniqueName() + "@testmail.org";
65         createVerifiedUser("an", "bn", email, TEST_PASSWORD);
66         String l = login(email, TEST_PASSWORD);
67         URLConnection c = get(l, "");
68         String readURL = IOUtils.readURL(c);
69         assertThat(readURL, containsString("Password"));
70     }
71
72     @Test
73     public void testLoginCertificate() throws IOException, GeneralSecurityException, GigiApiException, InterruptedException {
74         String email = createUniqueName() + "@testmail.org";
75         int user = createVerifiedUser("an", "bn", email, TEST_PASSWORD);
76         KeyPair kp = generateKeypair();
77         String csr = generatePEMCSR(kp, "CN=hans");
78         User u = User.getById(user);
79         Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "hans"), Digest.SHA256, csr, CSRType.CSR, CertificateProfile.getById(1));
80         final PrivateKey pk = kp.getPrivate();
81         await(c.issue(null, "2y", u));
82         final X509Certificate ce = c.cert();
83         c.setLoginEnabled(true);
84         String cookie = login(pk, ce);
85         URL u2 = new URL("https://" + getServerName().replaceFirst("^www.", "secure.") + SECURE_REFERENCE);
86         HttpURLConnection huc = (HttpURLConnection) u2.openConnection();
87         huc.addRequestProperty("Cookie", cookie);
88         authenticateClientCert(pk, ce, huc);
89         assertEquals(200, huc.getResponseCode());
90     }
91 }