X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2FLoginTest.java;h=cb575caf78190d3fb97bf33e0326beb242af57b6;hb=18ca4013b27f49c023cade364c46e5f2ef77b65d;hp=9aa24c763c5de083eaf18ae48ede7620f15fc6c5;hpb=7cf984749cf0027ccae90a53ebef07ab97ff164b;p=gigi.git diff --git a/tests/org/cacert/gigi/LoginTest.java b/tests/org/cacert/gigi/LoginTest.java index 9aa24c76..cb575caf 100644 --- a/tests/org/cacert/gigi/LoginTest.java +++ b/tests/org/cacert/gigi/LoginTest.java @@ -1,52 +1,61 @@ package org.cacert.gigi; -import java.io.IOException; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLEncoder; +import java.io.IOException; +import java.net.URLConnection; + +import org.cacert.gigi.testUtils.IOUtils; import org.cacert.gigi.testUtils.ManagedTest; import org.junit.Test; public class LoginTest extends ManagedTest { - public static final String secureReference = "/account/certs/email"; - @Test - public void testLoginUnverified() throws IOException { - long uniq = System.currentTimeMillis(); - String email = "system" + uniq + "@testmail.org"; - String pw = "1'aAaA"; - registerUser("an", "bn", email, pw); - waitForMail(); - assertFalse(isLoggedin(login(email, pw))); - } - @Test - public void testLoginVerified() throws IOException { - long uniq = System.currentTimeMillis(); - String email = "system2" + uniq + "@testmail.org"; - String pw = "1'aAaA"; - createVerifiedUser("an", "bn", email, pw); - assertTrue(isLoggedin(login(email, pw))); - } - public boolean isLoggedin(String cookie) throws IOException { - URL u = new URL("https://" + getServerName() + secureReference); - HttpURLConnection huc = (HttpURLConnection) u.openConnection(); - huc.addRequestProperty("Cookie", cookie); - return huc.getResponseCode() == 200; - } - public String login(String email, String pw) throws IOException { - URL u = new URL("https://" + getServerName() + "/login"); - HttpURLConnection huc = (HttpURLConnection) u.openConnection(); - huc.setDoOutput(true); - OutputStream os = huc.getOutputStream(); - String data = "username=" + URLEncoder.encode(email, "UTF-8") - + "&password=" + URLEncoder.encode(pw, "UTF-8"); - os.write(data.getBytes()); - os.flush(); - String headerField = huc.getHeaderField("Set-Cookie"); - headerField = headerField.substring(0, headerField.indexOf(';')); - return headerField; - } + + @Test + public void testLoginUnverified() throws IOException { + String email = createUniqueName() + "@testmail.org"; + registerUser("an", "bn", email, TEST_PASSWORD); + getMailReciever().receive(); + assertFalse(isLoggedin(login(email, TEST_PASSWORD))); + } + + @Test + public void testLoginVerified() throws IOException { + String email = createUniqueName() + "@testmail.org"; + createVerifiedUser("an", "bn", email, TEST_PASSWORD); + assertTrue(isLoggedin(login(email, TEST_PASSWORD))); + } + + @Test + public void testLoginWrongPassword() throws IOException { + String email = createUniqueName() + "@testmail.org"; + createVerifiedUser("an", "bn", email, TEST_PASSWORD); + assertFalse(isLoggedin(login(email, TEST_PASSWORD + "b"))); + } + + @Test + public void testLogoutVerified() throws IOException { + String email = createUniqueName() + "@testmail.org"; + createVerifiedUser("an", "bn", email, TEST_PASSWORD); + String cookie = login(email, TEST_PASSWORD); + assertTrue(isLoggedin(cookie)); + logout(cookie); + assertFalse(isLoggedin(cookie)); + } + + private void logout(String cookie) throws IOException { + get(cookie, "/logout").getHeaderField("Location"); + } + + @Test + public void testLoginMethodDisplay() throws IOException { + String email = createUniqueName() + "@testmail.org"; + createVerifiedUser("an", "bn", email, TEST_PASSWORD); + String l = login(email, TEST_PASSWORD); + URLConnection c = get(l, ""); + String readURL = IOUtils.readURL(c); + assertThat(readURL, containsString("Password")); + } }