From 8a0fbbd35c13054a947ea0d27ca117542bc452b9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 18 May 2018 15:33:38 +0300 Subject: JPA WIP --- juick-www/src/test/java/com/juick/WebAppTests.java | 38 ++++++++++++---------- 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'juick-www/src/test/java/com') diff --git a/juick-www/src/test/java/com/juick/WebAppTests.java b/juick-www/src/test/java/com/juick/WebAppTests.java index 74be2c92..3b85b585 100644 --- a/juick-www/src/test/java/com/juick/WebAppTests.java +++ b/juick-www/src/test/java/com/juick/WebAppTests.java @@ -23,6 +23,7 @@ import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.css.StyleElement; import com.gargoylesoftware.htmlunit.html.DomElement; import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.juick.server.helpers.AnonymousUser; import com.juick.service.*; import com.juick.util.MessageUtils; import com.juick.www.Utils; @@ -126,8 +127,7 @@ public class WebAppTests { userService.createUser(ugnichName, ugnichPassword); ugnich = userService.getUserByName(ugnichName); - int freefdId = userService.createUser(freefdName, freefdPassword); - freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new); + freefd = userService.createUser(freefdName, freefdPassword); isSetUp = true; } @@ -148,7 +148,7 @@ public class WebAppTests { @Test public void postWithoutTagsShouldNotHaveAsteriskInTitle() throws Exception { String msgText = "Привет, я - Угнич"; - int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); + int mid = messagesService.createMessage(ugnich, msgText, null, null); HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); assertThat(threadPage.getTitleText(), equalTo("ugnich:")); } @@ -159,8 +159,9 @@ public class WebAppTests { String msgText = "автор этого поста был забанен"; String hash = "12345678"; - User isilmine = userService.getUserByUID(userService.createUser(userName, userPassword)).orElseThrow(IllegalStateException::new); - int mid = messagesService.createMessage(isilmine.getUid(), msgText, null, null); + User isilmine = userService.createUser(userName, userPassword); + assertThat(isilmine, not(AnonymousUser.INSTANCE)); + int mid = messagesService.createMessage(isilmine, msgText, null, null); jdbcTemplate.update("UPDATE users SET banned=1 WHERE id=?", isilmine.getUid()); Page blogPage = webClient.getPage("http://localhost:8080/isilmine"); Page threadPage = webClient.getPage(String.format("http://localhost:8080/isilmine/%d", mid)); @@ -169,7 +170,7 @@ public class WebAppTests { } @Test public void repliesList() throws IOException { - int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, null); + int mid = messagesService.createMessage(ugnich, "hello", null, null); IntStream.range(1, 15).forEach(i -> messagesService.createReply(mid, i-1, freefd, String.valueOf(i-1), null )); @@ -184,7 +185,7 @@ public class WebAppTests { } @Test public void userShouldNotSeeReplyButtonToBannedUser() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "freefd bl me", null, null); + int mid = messagesService.createMessage(ugnich, "freefd bl me", null, null); messagesService.createReply(mid, 0, ugnich, "yo", null); MvcResult loginResult = mockMvc.perform(post("/login") .param("username", freefdName) @@ -201,8 +202,9 @@ public class WebAppTests { assertThat(threadPage.querySelectorAll(".a-thread-comment").isEmpty(), equalTo(false)); privacyQueriesService.blacklistUser(freefd, ugnich); assertThat(userService.isInBLAny(freefd.getUid(), ugnich.getUid()), equalTo(true)); - int renhaId = userService.createUser("renha", "secret"); - messagesService.createReply(mid, 0, userService.getUserByUID(renhaId).orElseThrow(IllegalStateException::new), + User renha = userService.createUser("renha", "secret"); + assertThat(renha, not(AnonymousUser.INSTANCE)); + messagesService.createReply(mid, 0, renha, "people", null); threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200)); @@ -229,11 +231,11 @@ public class WebAppTests { @Test public void testTwitterCards() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "without image", null, null); + int mid = messagesService.createMessage(ugnich, "without image", null, null); assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid), "twitter:card") .getAttribute("content"), equalTo("summary")); - int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", null); + int mid2 = messagesService.createMessage(ugnich, "with image", "png", null); Message message = messagesService.getMessage(mid2); assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card") .getAttribute("content"), equalTo("summary_large_image")); @@ -266,7 +268,7 @@ public class WebAppTests { mockMvc.perform(multipart("/post2") .file(file) .cookie(loginResult.getResponse().getCookies())).andExpect(status().isFound()); - int mid = messagesService.createMessage(ugnich.getUid(), "dummy message", null, null); + int mid = messagesService.createMessage(ugnich, "dummy message", null, null); mockMvc.perform(post("/comment") .param("mid", String.valueOf(mid)) .param("body", "yo")).andExpect(redirectedUrl("http://localhost/login")); @@ -297,7 +299,7 @@ public class WebAppTests { } @Test public void hashLoginShouldNotUseSession() throws Exception { - String hash = userService.getHashByUID(ugnich.getUid()); + String hash = userService.getHashForUser(ugnich); MvcResult hashLoginResult = mockMvc.perform(get("/?show=my&hash=" + hash)) .andExpect(status().isOk()) .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash)))) @@ -322,8 +324,8 @@ public class WebAppTests { @Test public void discussionsShouldBePageableByTimestamp() throws Exception { String msgText = "Привет, я снова Угнич"; - int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); - int midNew = messagesService.createMessage(ugnich.getUid(), "Я более новый Угнич", null, null); + int mid = messagesService.createMessage(ugnich, msgText, null, null); + int midNew = messagesService.createMessage(ugnich, "Я более новый Угнич", null, null); MvcResult loginResult = mockMvc.perform(post("/login") .param("username", freefdName) .param("password", freefdPassword)).andReturn(); @@ -351,7 +353,7 @@ public class WebAppTests { HtmlPage discussionsOld = webClient.getPage(discussionsUrl + "&to=" + msg.getUpdated().toEpochMilli()); assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); - List newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed().collect(Collectors.toList()); + List newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich, String.valueOf(i), null, null)).boxed().collect(Collectors.toList()); for (Integer m : newMids) { subscriptionService.subscribeMessage(messagesService.getMessage(m), freefd); } @@ -384,7 +386,7 @@ public class WebAppTests { } @Test public void anythingRedirects() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, null); + int mid = messagesService.createMessage(ugnich, "yo", null, null); mockMvc.perform(get(String.format("/%d", mid))) .andExpect(status().isMovedPermanently()) .andExpect(redirectedUrl(String.format("/%s/%d", ugnich.getName(), mid))); @@ -407,7 +409,7 @@ public class WebAppTests { new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(), loginCookie.getName(), loginCookie.getValue())); - int mid = messagesService.createMessage(ugnich.getUid(), "new test", null, null); + int mid = messagesService.createMessage(ugnich, "new test", null, null); subscriptionService.subscribeMessage(messagesService.getMessage(mid), freefd); int rid = messagesService.createReply(mid, 0, ugnich, "new reply", null); HtmlPage discussionsPage = webClient.getPage("http://localhost:8080/?show=discuss"); -- cgit v1.2.3