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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/*
* 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.server.tests;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.ExternalToken;
import com.juick.Message;
import com.juick.Tag;
import com.juick.User;
import com.juick.configuration.DataConfiguration;
import com.juick.configuration.RepositoryConfiguration;
import com.juick.server.EmailManager;
import com.juick.server.XMPPBot;
import com.juick.server.XMPPServer;
import com.juick.server.configuration.ApiAppConfiguration;
import com.juick.server.configuration.ApiSecurityConfig;
import com.juick.server.helpers.TagStats;
import com.juick.service.*;
import com.juick.util.DateFormattersHolder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.Stanza;
import rocks.xmpp.core.stanza.model.server.ServerMessage;
import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.IntStream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Created by vitalyster on 25.11.2016.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApiAppConfiguration.class, ApiSecurityConfig.class, DataConfiguration.class, RepositoryConfiguration.class})
@TestPropertySource(properties = {"broken_ssl_hosts=localhost,serverstorageisfull.tld"})
@WebAppConfiguration
public class ServerTests extends AbstractJUnit4SpringContextTests {
private MockMvc mockMvc;
@Inject
private WebApplicationContext webApplicationContext;
@Inject
private MessagesService messagesService;
@Inject
private UserService userService;
@Inject
private TagService tagService;
@Inject
private ObjectMapper jsonMapper;
@Inject
private ImagesService imagesService;
@Inject
private WebApplicationContext wac;
@Inject
private XMPPServer server;
@Inject
private XMPPBot bot;
@Inject
private SubscriptionService subscriptionService;
@Inject
private JdbcTemplate jdbcTemplate;
@Value("${hostname:localhost}")
private Jid jid;
private static User ugnich, freefd, juick;
static String ugnichName, ugnichPassword, freefdName, freefdPassword, juickName, juickPassword;
static Message msg;
static int juickTagId;
private static boolean isSetUp = false;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(SecurityMockMvcConfigurers.springSecurity())
.dispatchOptions(true)
.build();
if (!isSetUp) {
ugnichName = "ugnich";
ugnichPassword = "MyPassw0rd!";
freefdName = "freefd";
freefdPassword = "MyPassw0rd!";
juickName = "juick";
juickPassword = "demo";
int ugnichId = userService.createUser(ugnichName, ugnichPassword);
ugnich = userService.getUserByUID(ugnichId).orElseThrow(IllegalStateException::new);
int freefdId = userService.createUser(freefdName, freefdPassword);
freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new);
int juickId = userService.createUser(juickName, juickPassword);
juick = userService.getUserByUID(juickId).orElseThrow(IllegalStateException::new);
String msgText = "Привет, я - Угнич";
int mid = messagesService.createMessage(ugnich.getUid(), msgText, "png", null);
msg = messagesService.getMessage(mid);
tagService.createTag("тест");
juickTagId = tagService.createTag("juick");
isSetUp = true;
}
}
@Test
public void testAllUnAuthorized() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isMovedPermanently());
mockMvc.perform(get("/auth"))
.andExpect(status().is4xxClientError());
mockMvc.perform(get("/home"))
.andExpect(status().is4xxClientError());
mockMvc.perform(get("/messages/recommended"))
.andExpect(status().is4xxClientError());
mockMvc.perform(get("/messages/set_privacy"))
.andExpect(status().is4xxClientError());
}
@Test
public void homeTestWithMessages() throws Exception {
mockMvc.perform(
get("/home")
.with(httpBasic(ugnichName, ugnichPassword)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$[-1].mid", is(msg.getMid())))
.andExpect(jsonPath("$[-1].timestamp",
is(DateFormattersHolder.getMessageFormatterInstance().format(msg.getTimestamp()))))
.andExpect(jsonPath("$[-1].body", is(msg.getText())))
.andExpect(jsonPath("$[-1].attachment.url", is("https://i.juick.com/p/1.png")))
.andExpect(jsonPath("$[-1].attachment.small.url", is("https://i.juick.com/photos-512/1.png")));
}
@Test
public void homeTestWithMessagesAndRememberMe() throws Exception {
String ugnichHash = userService.getHashByUID(ugnich.getUid());
mockMvc.perform(
get("/home")
.with(httpBasic(ugnichName, ugnichPassword)))
.andExpect(status().isOk())
.andReturn();
mockMvc.perform(get("/home")
.param("hash", ugnichHash))
.andExpect(status().isOk());
}
@Test
public void homeTestWithMessagesAndSimpleCors() throws Exception {
mockMvc.perform(
get("/home")
.with(httpBasic(ugnichName, ugnichPassword))
.header("Origin", "http://api.example.net"))
.andExpect(status().isOk())
.andExpect(header().string("Access-Control-Allow-Origin", "*"));
}
@Test
public void homeTestWithPreflightCors() throws Exception {
mockMvc.perform(
options("/home")
.with(httpBasic(ugnichName, ugnichPassword))
.header("Origin", "http://api.example.net")
.header("Access-Control-Request-Method", "POST")
.header("Access-Control-Request-Headers", "X-PINGOTHER, Content-Type"))
.andExpect(status().isOk())
.andExpect(header().string("Access-Control-Allow-Origin", "*"))
.andExpect(header().string("Access-Control-Allow-Methods", "POST,GET,PUT,OPTIONS,DELETE"))
.andExpect(header().string("Access-Control-Allow-Headers", "X-PINGOTHER, Content-Type"));
}
@Test
public void anonymousApis() throws Exception {
mockMvc.perform(get("/messages"))
.andExpect(status().isOk());
mockMvc.perform(get("/users")
.param("uname", "ugnich")
.param("uname", "freefd"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(2)));
}
@Test
public void tags() throws Exception {
Tag weather = tagService.getTag("weather", true);
Tag yo = tagService.getTag("yo", true);
messagesService.createMessage(ugnich.getUid(), "text", null, Arrays.asList(yo, weather));
messagesService.createMessage(freefd.getUid(), "text2", null, Collections.singletonList(yo));
MvcResult result = mockMvc.perform(get("/tags"))
.andExpect(status().isOk())
.andReturn();
List<TagStats> tagsFromApi = jsonMapper.readValue(result.getResponse().getContentAsString(),
new TypeReference<List<TagStats>>(){});
TagStats yoStats = tagsFromApi.stream().filter(t -> t.getTag().getName().equals("yo")).findFirst().get();
assertThat(yoStats.getUsageCount(), is(2));
MvcResult result2 = mockMvc.perform(get("/tags")
.param("user_id", String.valueOf(ugnich.getUid())))
.andExpect(status().isOk())
.andReturn();
List<TagStats> ugnichTagsFromApi = jsonMapper.readValue(result2.getResponse().getContentAsString(),
new TypeReference<List<TagStats>>(){});
TagStats yoUgnichStats = ugnichTagsFromApi.stream().filter(t -> t.getTag().getName().equals("yo")).findFirst().get();
assertThat(yoUgnichStats.getUsageCount(), is(1));
}
@Test
public void postWithReferer() throws Exception {
mockMvc.perform(post("/post")
.param("body", "yo")
.with(httpBasic(ugnichName, ugnichPassword)))
.andExpect(status().isOk());
}
@Test
public void threadWithEphemeralNumberShouldReturn404() throws Exception {
mockMvc.perform(get("/thread").param("mid", "999999999")
.with(httpBasic(ugnichName, ugnichPassword))).andExpect(status().is4xxClientError());
}
@Test
public void performRequestsWithIssuedToken() throws Exception {
String ugnichHash = userService.getHashByUID(ugnich.getUid());
mockMvc.perform(get("/home")).andExpect(status().isUnauthorized());
mockMvc.perform(get("/auth"))
.andExpect(status().isUnauthorized());
mockMvc.perform(get("/auth").with(httpBasic(ugnichName, "wrongpassword")))
.andExpect(status().isUnauthorized());
MvcResult result = mockMvc.perform(get("/auth").with(httpBasic(ugnichName, ugnichPassword)))
.andExpect(status().isOk())
.andReturn();
String authHash = jsonMapper.readValue(result.getResponse().getContentAsString(), String.class);
assertThat(authHash, equalTo(ugnichHash));
mockMvc.perform(get("/home").param("hash", ugnichHash)).andExpect(status().isOk());
}
@Test
public void registerForNotificationsTests() throws Exception {
String token = "123456";
ExternalToken registration = new ExternalToken(null, "apns", token, null);
mockMvc.perform(put("/notifications").with(httpBasic(ugnichName, ugnichPassword))
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(jsonMapper.writeValueAsBytes(Collections.singletonList(registration))))
.andExpect(status().isOk());
MvcResult result = mockMvc.perform(get("/notifications")
.param("uid", String.valueOf(ugnich.getUid()))
.with(httpBasic(juickName, juickPassword)))
.andExpect(status().isOk())
.andReturn();
List<User> user = jsonMapper.readValue(result.getResponse().getContentAsString(),
new TypeReference<List<User>>() {
});
assertThat(user.get(0).getTokens().get(0).getToken(), equalTo(token));
}
@Test
public void tg2juickLinks() {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://juick.com/123456#23").build();
assertThat(uriComponents.getPath().substring(1), is("123456"));
assertThat(uriComponents.getFragment(), is("23"));
}
@Test
public void notificationsTokensTest() throws Exception {
List<ExternalToken> tokens = Collections.singletonList(new ExternalToken(null, "gcm", "123456", null));
mockMvc.perform(delete("/notifications").with(httpBasic(ugnichName, ugnichPassword))
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(jsonMapper.writeValueAsBytes(tokens))).andExpect(status().isForbidden());
mockMvc.perform(delete("/notifications").with(httpBasic(juickName, juickPassword))
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(jsonMapper.writeValueAsBytes(tokens))).andExpect(status().isOk());
}
@Test
public void notificationsSettingsAllowedOnlyForServiceUser() throws Exception {
mockMvc.perform(get("/notifications").with(httpBasic(juickName, juickPassword))
.param("mid", "1").param("uid", String.valueOf(ugnich.getUid()))).andExpect(status().isOk());
mockMvc.perform(get("/notifications")
.param("mid", "1").param("uid", String.valueOf(ugnich.getUid()))).andExpect(status().isUnauthorized());
}
@Test
public void topTest() throws Exception {
int topmid = messagesService.createMessage(ugnich.getUid(), "top message", null, null);
IntStream.rangeClosed(6, 12).forEach(i -> {
messagesService.createReply(topmid, 0, i, "yo", null);
});
assertThat(messagesService.getPopularCandidates().get(0), is(topmid));
Tag juickTag = tagService.getTag(juickTagId);
assertThat(juickTag.TID, is(2));
tagService.updateTags(topmid, Collections.singletonList(juickTag));
assertThat(messagesService.getPopularCandidates().isEmpty(), is(true));
}
@Test
public void inReplyToScannerTest() {
String header = "<123456.56@juick.com>";
Scanner headerScanner = new Scanner(header).useDelimiter(EmailManager.MSGID_PATTERN);
int mid = Integer.parseInt(headerScanner.next());
int rid = Integer.parseInt(headerScanner.next());
assertThat(mid, equalTo(123456));
assertThat(rid, equalTo(56));
}
@Test
public void lastMessagesTest() throws Exception {
mockMvc.perform(
get("/rss/"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/rss+xml"))
.andExpect(xpath("/rss/channel/description").string("The latest messages at Juick"));
}
@Test
public void statusPageIsUp() throws Exception {
mockMvc.perform(get("http://localhost:8080/status").with(httpBasic(ugnichName, ugnichPassword))).andExpect(status().isOk());
assertThat(server.getJid(), equalTo(jid));
}
@Test
public void botIsUpAndProcessingResourceConstraints() {
int renhaId;
renhaId = userService.createUser("renha", "umnnbt");
Jid from = Jid.of("renha@serverstorageisfull.tld");
jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", renhaId, from.toEscapedString(), 1);
String xmlMessage = "<message xmlns=\"jabber:server\" from=\"renha@serverstorageisfull.tld\" to=\"juick@juick.com/Juick\" type=\"error\"><body>Reply by @LexX</body><juick xmlns=\"http://juick.com/message\" mid=\"2885759\" privacy=\"1\" replyto=\"0\" rid=\"8\" ts=\"2017-10-10 07:41:10\"><body>Похоже нынче можно публично заявлять о своем веганстве. </body><user xmlns=\"http://juick.com/user\" uname=\"LexX\" uid=\"6340\"></user></juick><error type=\"wait\"><resource-constraint xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"></resource-constraint><text xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">Your contact offline message queue is full. The message has been discarded.</text></error></message>";
Stanza msg = server.parse(xmlMessage);
assertThat(from, equalTo(msg.getFrom()));
boolean isActive = jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, renhaId) == 1;
assertThat(isActive, equalTo(true));
bot.incomingMessage((ServerMessage)msg);
isActive = jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, renhaId) == 1;
assertThat(isActive, equalTo(false));
}
@Test
public void botCommandsTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "PING").get(), is("PONG"));
// subscription commands have two lines, others have 1
assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "help").get().split("\n").length, is(24));
}
@Test
public void protocolTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException, JsonProcessingException {
int uid = userService.createUser("me", "secret");
User user = userService.getUserByUID(uid).orElse(new User());
Tag yo = tagService.getTag("yo", true);
int mid = messagesService.createMessage(uid, "yoyo", null, Collections.singletonList(yo));
assertEquals("should be message", true,
bot.processCommand(user, Jid.of("test@localhost"), String.format("#%d", mid)).get().startsWith("@me"));
mid = messagesService.getUserBlog(user.getUid(), -1, 0).stream().reduce((first, second) -> second).get();
assertEquals("text should match", "yoyo",
messagesService.getMessage(mid).getText());
assertEquals("tag should match", "yo",
tagService.getMessageTags(mid).get(0).getTag().getName());
int readerUid = userService.createUser("dummyReader", "dummySecret");
User readerUser = userService.getUserByUID(readerUid).orElse(new User());
assertEquals("should be subscribed", "Subscribed",
bot.processCommand(readerUser, Jid.of("dummy@localhost"), "S #" + mid).get());
/* TODO: move from juick-legacy
assertEquals("should be favorited", "Message added to your recommendations",
juickProtocol.getReply(readerUser, "! #" + mid));
*/
assertEquals("number of subscribed users should match", 1,
subscriptionService.getUsersSubscribedToComments(mid, uid).size());
/*
assertEquals("should be subscribed", "Subscribed",
bot.processCommand(readerUser, Jid.of("dummy@localhost"), "S @" + user.getName()).get());
List<User> friends = userService.getUserFriends(readerUid);
assertEquals("number of friend users should match", 2,
friends.size());
assertEquals("number of reader users should match", 1,
userService.getUserReaders(uid).size());
String expectedReply = "Reply posted.\n#" + mid + "/1 "
+ "http://juick.com/" + mid + "#1";
String expectedSecondReply = "Reply posted.\n#" + mid + "/2 "
+ "http://juick.com/" + mid + "#2";
assertEquals("should be reply", expectedReply,
bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + " yoyo").get());
assertEquals("should be second reply", expectedSecondReply,
bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + "/1 yoyo").get());
Message reply = messagesService.getReplies(mid).stream().filter(m -> m.getRid() == 2).findFirst()
.orElse(new Message());
assertEquals("should be reply to first comment", 1, reply.getReplyto());
assertNotEquals("tags should NOT be updated", "Tags are updated",
bot.processCommand(readerUser, Jid.of("dummy@localhost"), "#" + mid + " *yo *there").get());
assertEquals("tags should be updated", "Tags are updated",
bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there").get());
assertEquals("number of tags should match", 2,
tagService.getMessageTags(mid).size());
assertEquals("should be blacklisted", "Tag added to your blacklist",
bot.processCommand(readerUser, Jid.of("dummy@localhost"), "BL *there").get());
assertEquals("number of subscribed users should match", 0,
subscriptionService.getSubscribedUsers(uid, mid).size());
assertEquals("tags should be updated", "Tags are updated",
bot.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there").get());
assertEquals("number of tags should match", 1,
tagService.getMessageTags(mid).size());
int taggerUid = userService.createUser("dummyTagger", "dummySecret");
User taggerUser = userService.getUserByUID(taggerUid).orElse(new User());
assertEquals("should be subscribed", "Subscribed",
bot.processCommand(taggerUser, Jid.of("tagger@localhost"), "S *yo").get());
assertEquals("number of subscribed users should match", 2,
subscriptionService.getSubscribedUsers(uid, mid).size());
assertEquals("should be unsubscribed", "Unsubscribed from yo",
bot.processCommand(taggerUser, Jid.of("tagger@localhost"), "U *yo").get());
assertEquals("number of subscribed users should match", 1,
subscriptionService.getSubscribedUsers(uid, mid).size());
assertEquals("number of readers should match", 1,
userService.getUserReaders(uid).size());
String readerFeed = bot.processCommand(readerUser, Jid.of("dummy@localhost"), "#").get();
assertEquals("description should match", true, readerFeed.startsWith("Your feed"));
assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(),
bot.processCommand(readerUser, Jid.of("dummy@localhost"), "U @" + user.getName()).get());
assertEquals("number of readers should match", 0,
userService.getUserReaders(uid).size());
assertEquals("number of friends should match", 1,
userService.getUserFriends(uid).size());
assertEquals("should be unsubscribed", "Unsubscribed from #" + mid,
bot.processCommand(readerUser, Jid.of("dummy@localhost"), "u #" + mid).get());
assertEquals("number of subscribed users should match", 0,
subscriptionService.getUsersSubscribedToComments(mid, uid).size());
assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid),
bot.processCommand(readerUser, Jid.of("dummy@localhost"), "D #" + mid).get());
assertEquals("should be deleted", String.format("Message %s deleted", mid),
bot.processCommand(user, Jid.of("test@localhost"), "D #" + mid).get());
assertEquals("should not have messages", 0, messagesService.getAll(user.getUid(), 0).size());
*/
}
}
|