1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/*
* Copyright (C) 2008-2017, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.juick.www;
import com.gargoylesoftware.htmlunit.CookieManager;
import com.gargoylesoftware.htmlunit.Page;
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.Message;
import com.juick.Tag;
import com.juick.User;
import com.juick.configuration.DataConfiguration;
import com.juick.service.*;
import com.juick.util.MessageUtils;
import com.juick.www.configuration.SapeConfiguration;
import com.juick.www.configuration.WebSecurityConfig;
import com.juick.www.configuration.WwwAppConfiguration;
import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import org.apache.commons.text.StringEscapeUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.util.FileSystemUtils;
import javax.inject.Inject;
import javax.servlet.http.Cookie;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Created by vitalyster on 12.01.2017.
*/
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = {"xmpp_disabled=true"})
public class WebAppTests {
@MockBean
private ImagesService imagesService;
@Inject
private WebApp webApp;
@Inject
private MockMvc mockMvc;
@Inject
private WebClient webClient;
@Inject
private UserService userService;
@Inject
private MessagesService messagesService;
@Inject
private PrivacyQueriesService privacyQueriesService;
@Inject
private JdbcTemplate jdbcTemplate;
@Inject
private SubscriptionService subscriptionService;
@Inject
private PebbleEngine pebbleEngine;
@Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
private String imgPath;
private static User ugnich, freefd;
private static String ugnichName, ugnichPassword, freefdName, freefdPassword;
private static boolean isSetUp = false;
@Before
public void setup() throws IOException {
if (!isSetUp) {
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
ugnichName = "ugnich";
ugnichPassword = "secret";
freefdName = "freefd";
freefdPassword = "MyPassw0rd!";
userService.createUser(ugnichName, ugnichPassword);
ugnich = userService.getUserByName(ugnichName);
int freefdId = userService.createUser(freefdName, freefdPassword);
freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new);
isSetUp = true;
}
Files.createDirectory(Paths.get(imgPath, "p"));
Files.createDirectory(Paths.get(imgPath, "photos-1024"));
Files.createDirectory(Paths.get(imgPath, "photos-512"));
Files.createDirectory(Paths.get(imgPath, "ps"));
}
@After
public void teardown() throws IOException {
FileSystemUtils.deleteRecursively(Paths.get(imgPath, "p"));
FileSystemUtils.deleteRecursively(Paths.get(imgPath, "photos-1024"));
FileSystemUtils.deleteRecursively(Paths.get(imgPath, "photos-512"));
FileSystemUtils.deleteRecursively(Paths.get(imgPath, "ps"));
}
@Test
public void postWithoutTagsShouldNotHaveAsteriskInTitle() throws Exception {
String msgText = "Привет, я - Угнич";
int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null);
HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
assertThat(threadPage.getTitleText(), equalTo("ugnich:"));
}
@Test
public void bannedUserBlogandPostShouldReturn404() throws IOException {
String userName = "isilmine";
String userPassword = "secret";
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);
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));
assertThat(blogPage.getWebResponse().getStatusCode(), equalTo(404));
assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(404));
}
@Test
public void repliesList() throws IOException {
int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, null);
IntStream.range(1, 15).forEach(i ->
messagesService.createReply(mid, i-1, freefd.getUid(), String.valueOf(i-1), null ));
HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200));
Long visibleItems = StreamSupport.stream(threadPage.getHtmlElementById("replies")
.getChildElements().spliterator(), false).filter(e -> {
StyleElement display = e.getStyleElement("display");
return display == null || !display.getValue().equals("none");
}).count();
assertThat(visibleItems, equalTo(14L));
}
@Test
public void userShouldNotSeeReplyButtonToBannedUser() throws Exception {
int mid = messagesService.createMessage(ugnich.getUid(), "freefd bl me", null, null);
messagesService.createReply(mid, 0, ugnich.getUid(), "yo", null);
MvcResult loginResult = mockMvc.perform(post("/login")
.param("username", freefdName)
.param("password", freefdPassword)).andReturn();
Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me");
webClient.setCookieManager(new CookieManager());
webClient.getCookieManager().addCookie(
new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(),
loginCookie.getName(),
loginCookie.getValue()));
HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200));
assertThat(threadPage.querySelectorAll(".msg-comment-target").isEmpty(), equalTo(false));
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, renhaId, "people", null);
threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200));
assertThat(threadPage.querySelectorAll(".msg-comment-target").isEmpty(), equalTo(true));
assertThat(threadPage.querySelectorAll(".a-thread-comment").isEmpty(), equalTo(true));
}
@Test
public void correctTagsEscaping() throws PebbleException, IOException {
PebbleTemplate template = pebbleEngine.getTemplate("views/test");
Writer writer = new StringWriter();
template.evaluate(writer,
Collections.singletonMap("tagsList",
Collections.singletonList(StringEscapeUtils.escapeHtml4(new Tag(">_<").getName()))));
String output = writer.toString().trim();
assertThat(output, equalTo("<a href=\"/ugnich/?tag=%26gt%3B_%26lt%3B\">>_<</a>"));
}
public DomElement fetchMeta(String url, String name) throws IOException {
HtmlPage page = webClient.getPage(url);
DomElement emptyMeta = new DomElement("", "meta", null, null);
return page.getElementsByTagName("meta").stream()
.filter(t -> t.getAttribute("name").equals(name)).findFirst().orElse(emptyMeta);
}
@Test
public void testTwitterCards() throws Exception {
int mid = messagesService.createMessage(ugnich.getUid(), "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);
Message message = messagesService.getMessage(mid2);
assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card")
.getAttribute("content"), equalTo("summary_large_image"));
assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "og:description")
.getAttribute("content"),
startsWith(StringEscapeUtils.escapeHtml4(MessageUtils.getMessageHashTags(message))));
}
@Test
public void postMessageTests() throws Exception {
mockMvc.perform(post("/post").param("body", "yo")).andExpect(redirectedUrl("http://localhost/login"));
MvcResult loginResult = mockMvc.perform(post("/login")
.param("username", ugnichName)
.param("password", ugnichPassword)).andReturn();
mockMvc.perform(post("/post")
.cookie(loginResult.getResponse().getCookies())
.param("wrong_param", "yo")).andExpect(status().isBadRequest());
mockMvc.perform(post("/post")
.cookie(loginResult.getResponse().getCookies())
.param("body", "yo")).andExpect(status().isOk());
mockMvc.perform(post("/post")
.cookie(loginResult.getResponse().getCookies())
.param("img", "http://static.juick.com/settings/facebook.png")).andExpect(status().isOk());
mockMvc.perform(post("/post")
.cookie(loginResult.getResponse().getCookies())
.param("img", "bad_url")).andExpect(status().isBadRequest());
FileInputStream fi = new FileInputStream(new ClassPathResource("tagscloud.png").getFile());
MockMultipartFile file = new MockMultipartFile("attach", fi);
mockMvc.perform(multipart("/post")
.file(file)
.cookie(loginResult.getResponse().getCookies())).andExpect(status().isOk());
int mid = messagesService.createMessage(ugnich.getUid(), "dummy message", null, null);
mockMvc.perform(post("/comment")
.param("mid", String.valueOf(mid))
.param("body", "yo")).andExpect(redirectedUrl("http://localhost/login"));
mockMvc.perform(post("/comment")
.cookie(loginResult.getResponse().getCookies())
.param("wrong_param", "yo")).andExpect(status().isBadRequest());
mockMvc.perform(post("/comment")
.cookie(loginResult.getResponse().getCookies())
.param("mid", String.valueOf(mid))
.param("wrong_param", "yo")).andExpect(status().isBadRequest());
mockMvc.perform(post("/comment")
.cookie(loginResult.getResponse().getCookies())
.param("mid", String.valueOf(mid))
.param("img", "http://static.juick.com/settings/facebook.png")).andExpect(status().isFound());
mockMvc.perform(multipart("/comment")
.file(file)
.cookie(loginResult.getResponse().getCookies())
.param("mid", String.valueOf(mid))).andExpect(status().isFound());
mockMvc.perform(post("/comment")
.cookie(loginResult.getResponse().getCookies())
.param("mid", String.valueOf(mid))
.param("body", "yo")).andExpect(redirectedUrl(String.format("/%s/%d#%d", ugnichName, mid, 3)));
}
@Test
public void hashLoginShouldNotUseSession() throws Exception {
String hash = userService.getHashByUID(ugnich.getUid());
MvcResult hashLoginResult = mockMvc.perform(get("/?show=my&hash=" + hash))
.andExpect(status().isOk())
.andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash))))
.andExpect(content().string(containsString(hash)))
.andReturn();
Cookie rememberMeFromHash = hashLoginResult.getResponse().getCookie("juick-remember-me");
MvcResult formLoginResult = mockMvc.perform(post("/login")
.param("username", ugnichName)
.param("password", ugnichPassword)).andReturn();
Cookie rememberMeFromForm = formLoginResult.getResponse().getCookie("juick-remember-me");
mockMvc.perform(get("/?show=my").cookie(rememberMeFromForm)).andExpect(status().isOk())
.andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash))))
.andExpect(content().string(containsString(hash)));
mockMvc.perform(get("/?show=my").cookie(rememberMeFromHash)).andExpect(status().isOk())
.andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash))))
.andExpect(content().string(containsString(hash)));
}
@Test
public void nonExistentBlogShouldReturn404() throws Exception {
mockMvc.perform(get("/ololoe/")).andExpect(status().isNotFound());
}
@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);
MvcResult loginResult = mockMvc.perform(post("/login")
.param("username", freefdName)
.param("password", freefdPassword)).andReturn();
Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me");
webClient.setCookieManager(new CookieManager());
webClient.getCookieManager().addCookie(
new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(),
loginCookie.getName(),
loginCookie.getValue()));
String discussionsUrl = "http://localhost:8080/?show=discuss";
HtmlPage discussions = webClient.getPage(discussionsUrl);
assertThat(discussions.querySelectorAll("article").size(), is(0));
subscriptionService.subscribeMessage(mid, freefd.getUid());
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article").size(), is(1));
subscriptionService.subscribeMessage(midNew, freefd.getUid());
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article").size(), is(2));
assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew)));
messagesService.createReply(mid, 0, freefd.getUid(), "I'm replied", null);
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article").size(), is(2));
assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid)));
Message msg = messagesService.getMessage(mid);
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<Integer> newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed().collect(Collectors.toList());
for (Integer m : newMids) {
subscriptionService.subscribeMessage(m, freefd.getUid());
}
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article").size(), is(20));
assertThat(discussions.querySelectorAll("article")
.get(19).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid)));
messagesService.createReply(midNew, 0, freefd.getUid(), "I'm replied", null);
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article")
.get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew)));
Message old = messagesService.getMessage(newMids.get(0));
discussionsOld = webClient.getPage(discussionsUrl + "&to=" + old.getUpdated().toEpochMilli());
assertThat(discussionsOld.querySelectorAll("article").size(), is(1));
assertThat(discussionsOld.querySelectorAll("article")
.get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid)));
}
@Test
public void redirectParamShouldCorrectlyRedirectLoggedUser() throws Exception {
MvcResult formLoginResult = mockMvc.perform(post("/login")
.param("username", ugnichName)
.param("password", ugnichPassword)).andReturn();
Cookie rememberMeFromForm = formLoginResult.getResponse().getCookie("juick-remember-me");
mockMvc.perform(get("/login").cookie(rememberMeFromForm))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/"));
mockMvc.perform(get("/login?redirect=false").cookie(rememberMeFromForm))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/login/success"));
}
}
|