From 4839e26459ef687d10226dd4cda6f291bfe62e1a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Wed, 25 Jun 2014 15:31:37 +0200 Subject: [PATCH] Test login with email verification. --- doc/tableStructure.sql | 37 ++++++++++++- tests/org/cacert/gigi/LoginTest.java | 52 +++++++++++++++++++ .../cacert/gigi/testUtils/ManagedTest.java | 9 +++- 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 tests/org/cacert/gigi/LoginTest.java diff --git a/doc/tableStructure.sql b/doc/tableStructure.sql index e132aec0..24007a87 100644 --- a/doc/tableStructure.sql +++ b/doc/tableStructure.sql @@ -83,4 +83,39 @@ CREATE TABLE `user_agreements` ( `method` varchar(100) NOT NULL, `comment` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=61411 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1; + +DROP TABLE IF EXISTS `emailcerts`; +CREATE TABLE `emailcerts` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `memid` int(11) NOT NULL DEFAULT '0', + `serial` varchar(50) NOT NULL DEFAULT '', + `CN` varchar(255) NOT NULL DEFAULT '', + `subject` text NOT NULL, + `keytype` char(2) NOT NULL DEFAULT 'NS', + `codesign` tinyint(1) NOT NULL DEFAULT '0', + `csr_name` varchar(255) NOT NULL DEFAULT '', + `crt_name` varchar(255) NOT NULL DEFAULT '', + `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + `revoked` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + `expire` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + `warning` tinyint(1) NOT NULL DEFAULT '0', + `renewed` tinyint(1) NOT NULL DEFAULT '0', + `rootcert` int(2) NOT NULL DEFAULT '1', + `md` enum('md5','sha1','sha256','sha512') NOT NULL DEFAULT 'sha512', + `type` tinyint(4) DEFAULT NULL, + `disablelogin` int(1) NOT NULL DEFAULT '0', + `pkhash` char(40) DEFAULT NULL, + `certhash` char(40) DEFAULT NULL, + `coll_found` tinyint(1) NOT NULL, + `description` varchar(100) NOT NULL DEFAULT '', + PRIMARY KEY (`id`), + KEY `emailcerts_pkhash` (`pkhash`), + KEY `revoked` (`revoked`), + KEY `created` (`created`), + KEY `memid` (`memid`), + KEY `serial` (`serial`), + KEY `stats_emailcerts_expire` (`expire`), + KEY `emailcrt` (`crt_name`) +) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1; \ No newline at end of file diff --git a/tests/org/cacert/gigi/LoginTest.java b/tests/org/cacert/gigi/LoginTest.java new file mode 100644 index 00000000..9aa24c76 --- /dev/null +++ b/tests/org/cacert/gigi/LoginTest.java @@ -0,0 +1,52 @@ +package org.cacert.gigi; + +import java.io.IOException; +import static org.junit.Assert.*; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; + +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; + } + +} diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index f591e772..c9a44b8a 100644 --- a/tests/org/cacert/gigi/testUtils/ManagedTest.java +++ b/tests/org/cacert/gigi/testUtils/ManagedTest.java @@ -19,6 +19,7 @@ import java.util.Properties; import org.cacert.gigi.DevelLauncher; import org.cacert.gigi.IOUtils; +import org.cacert.gigi.InitTruststore; import org.cacert.gigi.testUtils.TestEmailReciever.TestMail; import org.junit.After; import org.junit.AfterClass; @@ -35,6 +36,11 @@ public class ManagedTest { return url; } static Properties testProps = new Properties(); + static { + InitTruststore.run(); + HttpURLConnection.setFollowRedirects(false); + } + @BeforeClass public static void connectToServer() { try { @@ -171,7 +177,8 @@ public class ManagedTest { TestMail tm = ter.recieve(); String verifyLink = tm.extractLink(); String[] parts = verifyLink.split("\\?"); - URL u = new URL("https://" + getServerName() + "/verify" + parts[1]); + URL u = new URL("https://" + getServerName() + "/verify?" + + parts[1]); u.openStream().close();; } catch (InterruptedException e) { throw new Error(e); -- 2.39.2