From 3a3adaeea6980f1bd175939f2215cd2422af8573 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Mon, 11 Aug 2014 21:09:54 +0200 Subject: [PATCH] Put more intelligence into the "outputError"-function of "Form". --- src/org/cacert/gigi/output/Form.java | 32 +++++++++++++++++-- src/org/cacert/gigi/pages/main/Signup.java | 28 +++------------- .../cacert/gigi/pages/wot/AssuranceForm.java | 17 ++-------- .../gigi/pages/main/RegisterPageTest.java | 4 +-- .../cacert/gigi/pages/wot/TestAssurance.java | 2 +- .../cacert/gigi/testUtils/ManagedTest.java | 4 +-- 6 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/org/cacert/gigi/output/Form.java b/src/org/cacert/gigi/output/Form.java index 176c7046..a4c65a48 100644 --- a/src/org/cacert/gigi/output/Form.java +++ b/src/org/cacert/gigi/output/Form.java @@ -34,6 +34,7 @@ public abstract class Form implements Outputable { @Override public void output(PrintWriter out, Language l, Map vars) { out.println("
"); + failed = false; outputContent(out, l, vars); out.print(""); + } out.print("
"); - out.print(Page.translate(req, text)); + if (contents.length == 0) { + out.print(Page.translate(req, text)); + } else { + out.print(String.format(Page.translate(req, text), contents)); + } out.println("
"); } + protected void outputErrorPlain(PrintWriter out, String text) { + if ( !failed) { + failed = true; + out.println("
"); + } + out.print("
"); + out.print(text); + out.println("
"); + } + + public boolean isFailed(PrintWriter out) { + if (failed) { + out.println("
"); + } + return failed; + } + protected String getCSRFToken() { return csrf; } diff --git a/src/org/cacert/gigi/pages/main/Signup.java b/src/org/cacert/gigi/pages/main/Signup.java index e956aa43..c9b77f61 100644 --- a/src/org/cacert/gigi/pages/main/Signup.java +++ b/src/org/cacert/gigi/pages/main/Signup.java @@ -89,40 +89,30 @@ public class Signup extends Form { @Override public synchronized boolean submit(PrintWriter out, HttpServletRequest req) { update(req); - boolean failed = false; - out.println("
"); if (buildup.getFname().equals("") || buildup.getLname().equals("")) { outputError(out, req, "First and/or last names were blank."); - failed = true; } if ( !myDoB.isValid()) { outputError(out, req, "Invalid date of birth"); - failed = true; } if ( !"1".equals(req.getParameter("cca_agree"))) { outputError(out, req, "You have to agree to the CAcert Community agreement."); - failed = true; } if (buildup.getEmail().equals("")) { outputError(out, req, "Email Address was blank"); - failed = true; } String pw1 = req.getParameter("pword1"); String pw2 = req.getParameter("pword2"); if (pw1 == null || pw1.equals("")) { outputError(out, req, "Pass Phrases were blank"); - failed = true; } else if ( !pw1.equals(pw2)) { outputError(out, req, "Pass Phrases don't match"); - failed = true; } int pwpoints = PasswordStrengthChecker.checkpw(pw1, buildup); if (pwpoints < 3) { outputError(out, req, "The Pass Phrase you submitted failed to contain enough" + " differing characters and/or contained words from" + " your name and/or email address."); - failed = true; } - if (failed) { - out.println("
"); + if (isFailed(out)) { return false; } try { @@ -134,7 +124,6 @@ public class Signup extends Form { ResultSet r2 = q2.executeQuery(); if (r1.next() || r2.next()) { outputError(out, req, "This email address is currently valid in the system."); - failed = true; } r1.close(); r2.close(); @@ -144,15 +133,12 @@ public class Signup extends Form { ResultSet r3 = q3.executeQuery(); if (r3.next()) { String domain = r3.getString(1); - out.print("
"); - out.print(String.format(Page.translate(req, "We don't allow signups from people using email addresses from %s"), domain)); - out.println("
"); - failed = true; + outputError(out, req, "We don't allow signups from people using email addresses from %s", domain); } r3.close(); } catch (SQLException e) { e.printStackTrace(); - failed = true; + outputError(out, req, "an internal error happened"); } String mailResult = EmailProvider.FAIL; try { @@ -168,15 +154,11 @@ public class Signup extends Form { if (mailResult.equals(EmailProvider.FAIL)) { outputError(out, req, "Failed to make a connection to the mail server"); } else { - out.print("
"); - out.print(mailResult); - out.println("
"); + outputErrorPlain(out, mailResult); } - failed = true; } - out.println(""); - if (failed) { + if (isFailed(out)) { return false; } try { diff --git a/src/org/cacert/gigi/pages/wot/AssuranceForm.java b/src/org/cacert/gigi/pages/wot/AssuranceForm.java index 3d68716e..e3dfe54c 100644 --- a/src/org/cacert/gigi/pages/wot/AssuranceForm.java +++ b/src/org/cacert/gigi/pages/wot/AssuranceForm.java @@ -50,45 +50,34 @@ public class AssuranceForm extends Form { @Override public boolean submit(PrintWriter out, HttpServletRequest req) { - out.println("
"); - boolean failed = false; - if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("CCAAgreed")) || !"1".equals(req.getParameter("assertion"))) { outputError(out, req, "You failed to check all boxes to validate" + " your adherence to the rules and policies of CAcert"); - failed = true; } if (req.getParameter("date") == null || req.getParameter("date").equals("")) { outputError(out, req, "You must enter the date when you met the assuree."); - failed = true; } else { try { Date d = sdf.parse(req.getParameter("date")); if (d.getTime() > System.currentTimeMillis()) { outputError(out, req, "You must not enter a date in the future."); - failed = true; } } catch (ParseException e) { outputError(out, req, "You must enter the date in this format: YYYY-MM-DD."); - failed = true; } } // check location, min 3 characters if (req.getParameter("location") == null || req.getParameter("location").equals("")) { outputError(out, req, "You failed to enter a location of your meeting."); - failed = true; } else if (req.getParameter("location").length() <= 2) { outputError(out, req, "You must enter a location with at least 3 characters eg town and country."); - failed = true; } // TODO checkPoints String points = req.getParameter("points"); if (points == null || "".equals(points)) { - // TODO message - failed = true; + outputError(out, req, "For an assurance, you need to enter points."); } - if (failed) { - out.println("
"); + if (isFailed(out)) { return false; } try { @@ -96,13 +85,11 @@ public class AssuranceForm extends Form { if (success != AssuranceResult.ASSURANCE_SUCCEDED) { outputError(out, req, success.getMessage()); } - out.println(""); return success == AssuranceResult.ASSURANCE_SUCCEDED; } catch (SQLException e) { e.printStackTrace(); } - out.println(""); return false; } diff --git a/tests/org/cacert/gigi/pages/main/RegisterPageTest.java b/tests/org/cacert/gigi/pages/main/RegisterPageTest.java index be0124a7..c21d7a26 100644 --- a/tests/org/cacert/gigi/pages/main/RegisterPageTest.java +++ b/tests/org/cacert/gigi/pages/main/RegisterPageTest.java @@ -33,14 +33,14 @@ public class RegisterPageTest extends ManagedTest { String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1"; String data = fetchStartErrorMessage(runRegister(query)); - assertTrue(data, data.startsWith("")); + assertNull(data); assertSuccessfullRegMail(); getMailReciever().setEmailCheckError("400 Greylisted"); getMailReciever().setApproveRegex(Pattern.compile("a")); query = defaultSignup + URLEncoder.encode("correct4_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1"; data = fetchStartErrorMessage(runRegister(query)); - assertFalse(data, data.startsWith("")); + assertNotNull(data); assertNull(getMailReciever().recieve()); diff --git a/tests/org/cacert/gigi/pages/wot/TestAssurance.java b/tests/org/cacert/gigi/pages/wot/TestAssurance.java index 94f194a3..6e4d5ea2 100644 --- a/tests/org/cacert/gigi/pages/wot/TestAssurance.java +++ b/tests/org/cacert/gigi/pages/wot/TestAssurance.java @@ -79,7 +79,7 @@ public class TestAssurance extends ManagedTest { @Test public void testAssureForm() throws IOException { String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10"); - assertTrue(error, error.startsWith("")); + assertNull(error); } @Test diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index 56301787..2d48a014 100644 --- a/tests/org/cacert/gigi/testUtils/ManagedTest.java +++ b/tests/org/cacert/gigi/testUtils/ManagedTest.java @@ -229,7 +229,7 @@ public class ManagedTest { try { String query = "fname=" + URLEncoder.encode(firstName, "UTF-8") + "&lname=" + URLEncoder.encode(lastName, "UTF-8") + "&email=" + URLEncoder.encode(email, "UTF-8") + "&pword1=" + URLEncoder.encode(password, "UTF-8") + "&pword2=" + URLEncoder.encode(password, "UTF-8") + "&day=1&month=1&year=1910&cca_agree=1"; String data = fetchStartErrorMessage(runRegister(query)); - assertTrue(data, data.startsWith("")); + assertNull(data); } catch (UnsupportedEncodingException e) { throw new Error(e); } catch (IOException e) { @@ -297,7 +297,7 @@ public class ManagedTest { static int count = 0; public static String createUniqueName() { - return "test" + System.currentTimeMillis() + "a" + (count++); + return "test" + System.currentTimeMillis() + "a" + (count++) + "u"; } private String stripCookie(String headerField) { -- 2.39.2