1 package org.cacert.gigi;
3 import java.io.IOException;
4 import static org.junit.Assert.*;
5 import java.io.OutputStream;
6 import java.net.HttpURLConnection;
8 import java.net.URLEncoder;
10 import org.cacert.gigi.testUtils.ManagedTest;
11 import org.junit.Test;
13 public class LoginTest extends ManagedTest {
14 public static final String secureReference = "/account/certs/email";
16 public void testLoginUnverified() throws IOException {
17 long uniq = System.currentTimeMillis();
18 String email = "system" + uniq + "@testmail.org";
20 registerUser("an", "bn", email, pw);
22 assertFalse(isLoggedin(login(email, pw)));
25 public void testLoginVerified() throws IOException {
26 long uniq = System.currentTimeMillis();
27 String email = "system2" + uniq + "@testmail.org";
29 createVerifiedUser("an", "bn", email, pw);
30 assertTrue(isLoggedin(login(email, pw)));
32 public boolean isLoggedin(String cookie) throws IOException {
33 URL u = new URL("https://" + getServerName() + secureReference);
34 HttpURLConnection huc = (HttpURLConnection) u.openConnection();
35 huc.addRequestProperty("Cookie", cookie);
36 return huc.getResponseCode() == 200;
38 public String login(String email, String pw) throws IOException {
39 URL u = new URL("https://" + getServerName() + "/login");
40 HttpURLConnection huc = (HttpURLConnection) u.openConnection();
41 huc.setDoOutput(true);
42 OutputStream os = huc.getOutputStream();
43 String data = "username=" + URLEncoder.encode(email, "UTF-8")
44 + "&password=" + URLEncoder.encode(pw, "UTF-8");
45 os.write(data.getBytes());
47 String headerField = huc.getHeaderField("Set-Cookie");
48 headerField = headerField.substring(0, headerField.indexOf(';'));