X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2Fpages%2Fadmin%2FTestSEAdminPageDetails.java;h=228528b7074cafaa338ef8628603a8f504be739e;hp=39e7c5de3c30ac4df605bfbc1be23aa119e96b72;hb=9def69bd08ea69eb27786d5b34f00e154e09e9f3;hpb=410465079ef6887884118bee366a3f9931563602 diff --git a/tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java index 39e7c5de..228528b7 100644 --- a/tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java +++ b/tests/org/cacert/gigi/pages/admin/TestSEAdminPageDetails.java @@ -5,13 +5,17 @@ import static org.junit.Assert.*; import java.io.IOException; import java.net.MalformedURLException; -import java.net.URL; import java.net.URLConnection; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.EmailAddress; import org.cacert.gigi.dbObjects.Group; -import org.cacert.gigi.pages.account.MyDetails; +import org.cacert.gigi.dbObjects.ObjectCache; +import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.pages.account.History; import org.cacert.gigi.pages.admin.support.SupportEnterTicketPage; import org.cacert.gigi.pages.admin.support.SupportUserDetailsPage; import org.cacert.gigi.testUtils.ClientTest; @@ -31,55 +35,105 @@ public class TestSEAdminPageDetails extends ClientTest { String fname = "Först"; String lname = "Secönd"; int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD); - URLConnection uc = new URL("https://" + getServerName() + SupportUserDetailsPage.PATH + id).openConnection(); - uc.addRequestProperty("Cookie", cookie); + URLConnection uc = get(SupportUserDetailsPage.PATH + id); uc.setDoOutput(true); String res = IOUtils.readURL(uc); - assertThat(res, containsString("")); - assertThat(res, containsString("")); + assertThat(res, containsString(fname)); + assertThat(res, containsString(lname)); assertThat(res, containsString(email)); } @Test - public void testUserDetailsEdit() throws MalformedURLException, IOException { + public void testUserDetailsEmail() throws MalformedURLException, IOException, GigiApiException { String email = createUniqueName() + "@example.com"; String fname = "Först"; String lname = "Secönd"; int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD); + String email2 = createUniqueName() + "@example.com"; + EmailAddress ea = new EmailAddress(User.getById(id), email2, Locale.ENGLISH); + getMailReceiver().receive().verify(); + // Refresh email Object + ObjectCache.clearAllCaches(); + ea = EmailAddress.getById(ea.getId()); + assertTrue(ea.isVerified()); - String userCookie = login(email, TEST_PASSWORD); - assertEquals("Först", getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0)))); - // User can change his name - assertNull(executeBasicWebInteraction(userCookie, MyDetails.PATH, "fname=Kurti&lname=Hansel&mname=&suffix=&day=1&month=1&year=2000&processDetails", 0)); - assertEquals("Kurti", getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0)))); - // But when assurer - makeAssurer(id); - // User cannot change his name, and the form changed - assertNotNull(executeBasicWebInteraction(userCookie, MyDetails.PATH, "fname=Kurti2&lname=Hansel&mname=&suffix=&day=1&month=1&year=2000&processDetails", 0)); - assertNull(getFname(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0)))); - assertEquals("Kurti", getFnamePlain(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0)))); - - // but support still can - assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "fname=Kurti3&lname=Hansel&mname=&suffix=&dobd=1&dobm=2&doby=2000&detailupdate", 0)); - assertEquals("Kurti3", getFnamePlain(IOUtils.readURL(get(userCookie, MyDetails.PATH, 0)))); + String res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id)); + assertEquals(2, countRegex(res, Pattern.quote(email))); + assertEquals(1, countRegex(res, Pattern.quote(email2))); + User.getById(id).updateDefaultEmail(ea); + clearCaches(); + res = IOUtils.readURL(get(SupportUserDetailsPage.PATH + id)); + assertEquals(1, countRegex(res, Pattern.quote(email))); + assertEquals(2, countRegex(res, Pattern.quote(email2))); } - private String getFname(String res) { - Pattern p = Pattern.compile(""); - Matcher m = p.matcher(res); - if (m.find()) { - return m.group(1); + @Test + public void testUserDetailsEditToLog() throws MalformedURLException, IOException { + String email = createUniqueName() + "@example.com"; + String fname = "Först"; + String lname = "Secönd"; + int id = createVerifiedUser(fname, lname, email, TEST_PASSWORD); + String clientCookie = login(email, TEST_PASSWORD); + + assertEquals(0, logCountAdmin(id)); + assertEquals(0, logCountUser(clientCookie)); + // chaniging both leads to 2 entries + assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=1&dobm=2&doby=2000&detailupdate", 0)); + assertEquals(1, logCountAdmin(id)); + assertEquals(1, logCountUser(clientCookie)); + + // Sending same data keeps same + assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=1&dobm=2&doby=2000&detailupdate", 0)); + assertEquals(1, logCountAdmin(id)); + assertEquals(1, logCountUser(clientCookie)); + + // changing one leads to one entry + assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=1&dobm=3&doby=2000&detailupdate", 0)); + assertEquals(2, logCountAdmin(id)); + assertEquals(2, logCountUser(clientCookie)); + + // changing one leads to one entry + assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=2&dobm=3&doby=2000&detailupdate", 0)); + assertEquals(3, logCountAdmin(id)); + assertEquals(3, logCountUser(clientCookie)); + + // changing none -> no entry + assertNull(executeBasicWebInteraction(cookie, SupportUserDetailsPage.PATH + id, "dobd=2&dobm=3&doby=2000&detailupdate", 0)); + assertEquals(3, logCountAdmin(id)); + assertEquals(3, logCountUser(clientCookie)); + + } + + private int logCountAdmin(int id) throws IOException { + return getLogEntryCount(IOUtils.readURL(get(History.SUPPORT_PATH.replace("*", Integer.toString(id))))); + } + + private int logCountUser(String cookie) throws IOException { + return getLogEntryCount(IOUtils.readURL(get(cookie, History.PATH))); + } + + private int getLogEntryCount(String readURL) { + String s = "Support actions"; + int start = readURL.indexOf(s); + int end = readURL.indexOf("", start); + String logs = readURL.substring(start + s.length(), end); + int i = 0; + int c = -1; + while (i != -1) { + i = logs.indexOf("", i + 1); + c++; } - return null; + return c; } - private String getFnamePlain(String res) { - Pattern p = Pattern.compile("\\s*First Name: \\s*([^<]*)"); + private String getFname(String res) { + Pattern p = Pattern.compile("([^<]*)"); Matcher m = p.matcher(res); if (m.find()) { return m.group(1); } return null; } + }