aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/juick/server/tests/ServerTests.java
blob: 9e1e68ea3df1fa3601e2d83fb4e16b72f9a24d47 (plain) (blame)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
/*
 * 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.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gargoylesoftware.htmlunit.CookieManager;
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.jayway.jsonpath.JsonPath;
import com.juick.*;
import com.juick.model.*;
import com.juick.server.*;
import com.juick.server.api.activity.model.Context;
import com.juick.server.api.activity.model.activities.Create;
import com.juick.server.api.activity.model.activities.Delete;
import com.juick.server.api.activity.model.activities.Follow;
import com.juick.server.api.activity.model.activities.Like;
import com.juick.server.api.activity.model.activities.Undo;
import com.juick.server.api.activity.model.objects.Note;
import com.juick.server.api.activity.model.objects.Person;
import com.juick.server.api.webfinger.model.Account;
import com.juick.server.api.xnodeinfo2.model.NodeInfo;
import com.juick.server.util.HttpUtils;
import com.juick.server.util.ImageUtils;
import com.juick.server.www.WebApp;
import com.juick.service.*;
import com.juick.service.component.MessageEvent;
import com.juick.util.DateFormattersHolder;
import com.juick.util.MessageUtils;
import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.ExpectedException;
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.web.client.TestRestTemplate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.session.Extension;
import rocks.xmpp.core.session.XmppSession;
import rocks.xmpp.core.session.XmppSessionConfiguration;

import javax.inject.Inject;
import javax.servlet.http.Cookie;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

import static com.juick.server.api.activity.model.Context.ACTIVITY_MEDIA_TYPE;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.client.ExpectedCount.times;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Created by vitalyster on 25.11.2016.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = {
        "broken_ssl_hosts=localhost,serverstorageisfull.tld",
        "ios_app_id=12345678.com.juick.ExampleApp",
        "spring.jackson.default-property-inclusion=non_default"
})
@AutoConfigureMockMvc
public class ServerTests {

    @Inject
    private MockMvc mockMvc;
    @Inject
    private WebClient webClient;
    @Inject
    private TestRestTemplate restTemplate;
    @Inject
    private MessagesService messagesService;
    @Inject
    private UserService userService;
    @Inject
    private TagService tagService;
    @Inject
    private ObjectMapper jsonMapper;
    @Inject
    private CommandsManager commandsManager;
    @Inject
    private SubscriptionService subscriptionService;
    @Inject
    private PrivacyQueriesService privacyQueriesService;
    @Inject
    private JdbcTemplate jdbcTemplate;
    @Inject
    private EmailService emailService;
    @Inject
    private PMQueriesService pmQueriesService;
    @Inject
    private TelegramService telegramService;
    @Inject
    private CrosspostService crosspostService;
    @Inject
    private ImagesService imagesService;
    @Inject
    private ServerManager serverManager;
    @Inject
    private KeystoreManager keystoreManager;
    @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
    private String tmpDir;
    @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
    private String imgDir;
    @Inject
    private PebbleEngine pebbleEngine;
    @Value("${ios_app_id:}")
    private String appId;
    @Inject
    private SignatureManager signatureManager;
    @Inject
    private ActivityPubManager activityPubManager;
    @Inject
    private WebApp webApp;
    @Inject
    private RestTemplate apClient;

    @Value("classpath:mocks/activity/testuser.json")
    private Resource testuserResponse;
    @Value("classpath:mocks/activity/testfollow.json")
    private Resource testfollowRequest;
    @Value("classpath:static/av-96.png")
    private Resource defaultAvatar;
    @Value("classpath:cmyk.jpg")
    private Resource cmykJpeg;
    @Value("classpath:nojfif.jpg")
    private Resource nojfif;

    @Inject
    private KeystoreManager testKeystoreManager;

    private static User ugnich, freefd, juick;
    static String ugnichName, ugnichPassword, freefdName, freefdPassword, juickName, juickPassword;
    URI emptyUri = URI.create(StringUtils.EMPTY);

    private static boolean isSetUp = false;

    @Before
    public void setUp() throws Exception {
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "p"));
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "photos-1024"));
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "photos-512"));
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "ps"));
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "a"));
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "ao"));
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "as"));
        Files.createDirectory(Paths.get(imgDir, "p"));
        Files.createDirectory(Paths.get(imgDir, "photos-1024"));
        Files.createDirectory(Paths.get(imgDir, "photos-512"));
        Files.createDirectory(Paths.get(imgDir, "ps"));
        Files.createDirectory(Paths.get(imgDir, "a"));
        Files.createDirectory(Paths.get(imgDir, "ao"));
        Files.createDirectory(Paths.get(imgDir, "as"));
        if (!isSetUp) {
            ugnichName = "ugnich";
            ugnichPassword = "secret";
            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);
            subscriptionService.subscribeUser(freefd, ugnich);
            webClient.getOptions().setJavaScriptEnabled(false);
            isSetUp = true;
        }
    }
    @After
    public void teardown() throws IOException {
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "p"));
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "photos-1024"));
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "photos-512"));
        FileSystemUtils.deleteRecursively(Paths.get(imgDir, "ps"));
    }
    @Test
    public void getMyFeed() {
        int mid0 = messagesService.createMessage(ugnich.getUid(), "test", null, null);
        int mid2 = messagesService.createMessage(ugnich.getUid(), "test2", null, null);
        List<Integer> freefdFeed = messagesService.getMyFeed(freefd.getUid(), 0, false);
        assertThat(freefdFeed.get(0), equalTo(mid2));
        int tonyaid = userService.createUser("Tonya", "secret");
        int mid3 = messagesService.createMessage(tonyaid, "test3", null, null);
        messagesService.recommendMessage(mid3, ugnich.getUid());
        assertThat(messagesService.getMyFeed(freefd.getUid(), 0, false).get(0), equalTo(mid2));
        assertThat(messagesService.getMyFeed(freefd.getUid(), 0, true).get(0), equalTo(mid3));
        assertThat(messagesService.getMyFeed(freefd.getUid(), mid2, true).get(0), equalTo(mid0));
        assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Added));
        assertThat(messagesService.getMessage(mid0).orElseThrow(IllegalStateException::new).getLikes(), equalTo(1));
        assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Deleted));
        assertThat(messagesService.getMessage(mid0).get().getLikes(), equalTo(0));
        assertThat(messagesService.getAll(ugnich.getUid(), 0).get(0), equalTo(mid3));
        Tag yoTag = tagService.getTag("yoyo", true);
        assertThat(tagService.getTag("YOYO", false), equalTo(yoTag));
        int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, Collections.singletonList(yoTag));
        Message msg = messagesService.getMessage(mid).get();
        List<User> subscribers = subscriptionService.getSubscribedUsers(ugnich.getUid(), msg);

        telegramService.createTelegramUser(12345, "freefd");
        String loginhash = jdbcTemplate.queryForObject("SELECT loginhash FROM telegram where tg_id=?",
                String.class, 12345);
        crosspostService.setTelegramUser(loginhash, freefd.getUid());

        List<Long> telegramSubscribers = telegramService.getTelegramIdentifiers(subscribers);
        assertThat(subscribers.size(), equalTo(1));
        assertThat(subscribers.size(), equalTo(telegramSubscribers.size()));
        assertThat(subscribers.get(0).getUid(), equalTo(freefd.getUid()));
        tagService.blacklistTag(freefd, yoTag);
        List<User> subscribers2 = subscriptionService.getSubscribedUsers(ugnich.getUid(), msg);
        assertThat(subscribers2.size(), equalTo(0));
        assertThat(telegramService.getTelegramIdentifiers(subscribers2).size(), equalTo(0));
        tagService.blacklistTag(freefd, yoTag);
        assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), msg).size(), equalTo(1));
        subscriptionService.unSubscribeUser(freefd, ugnich);
        assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), msg).size(), equalTo(0));
        Message mentionMessage = new Message();
        mentionMessage.setText("@freefd - dick");
        assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), mentionMessage).size(), equalTo(1));
        subscriptionService.subscribeUser(freefd, ugnich);
        assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), mentionMessage).size(), equalTo(1));
    }
    @Test
    public void pmTests() {
        pmQueriesService.createPM(freefd.getUid(), ugnich.getUid(), "hello");
        Message pm = pmQueriesService.getPMMessages(ugnich.getUid(), freefd.getUid()).get(0);
        assertThat(pm.getText(), equalTo("hello"));
        assertThat(pm.getUser().getUid(), equalTo(freefd.getUid()));
    }

    @Test
    public void messageTests() {
        int user_id = userService.createUser("mmmme", "secret");
        User user = userService.getUserByUID(user_id).orElse(AnonymousUser.INSTANCE);
        assertEquals("it should be me", "mmmme", user.getName());
        int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());
        Message msg = messagesService.getMessage(mid).get();
        assertEquals("yo", msg.getText());
        User me = msg.getUser();
        assertEquals("mmmme", me.getName());
        assertEquals("mmmme", messagesService.getMessageAuthor(mid).getName());
        int tagID = tagService.createTag("weather");
        Tag tag = tagService.getTag(tagID);
        List<Tag> tagList = new ArrayList<>();
        tagList.add(tag);
        int mid2 = messagesService.createMessage(user_id, "yo2", null, tagList);
        Message msg2 = messagesService.getMessage(mid2).get();
        assertEquals(1, msg2.getTags().size());
        assertEquals("we already have ugnich", -1, userService.createUser("ugnich", "x"));
        int ugnich_id = userService.createUser("hugnich", "x");
        User ugnich = userService.getUserByUID(ugnich_id).orElse(AnonymousUser.INSTANCE);
        int rid = messagesService.createReply(msg2.getMid(), 0, ugnich, "bla-bla", null);
        assertEquals(1, rid);
        assertThat(msg2.getTo(), equalTo(AnonymousUser.INSTANCE));
        Message reply = messagesService.getReply(msg2.getMid(), rid);
        assertThat(reply.getTo().getName(), equalTo(user.getName()));
        List<Message> replies = messagesService.getReplies(user, msg2.getMid());
        assertThat(replies.size(), equalTo(1));
        assertThat(replies.get(0), equalTo(reply));
        int ridToReply = messagesService.createReply(msg2.getMid(), 1, ugnich, "blax2", null);
        Message reply2 = messagesService.getReply(msg2.getMid(), ridToReply);
        assertThat(reply.getTo().getName(), equalTo(user.getName()));
        List<Message> replies2 = messagesService.getReplies(user, msg2.getMid());
        assertThat(replies2.size(), equalTo(2));
        assertThat(replies2.get(1), equalTo(reply2));

        Message msg3 = messagesService.getMessage(mid2).get();

        assertEquals(2, msg3.getReplies());
        assertEquals("weather", msg3.getTags().get(0).getName());
        assertEquals(ugnich.getUid(), userService.checkPassword(ugnich.getName(), "x"));
        assertEquals(-1, userService.checkPassword(ugnich.getName(), "xy"));

        subscriptionService.subscribeMessage(msg, user);
        subscriptionService.subscribeMessage(msg, ugnich);
        int reply_id = messagesService.createReply(msg.getMid(), 0, ugnich, "comment", null);
        assertEquals(1, subscriptionService.getUsersSubscribedToComments(msg,
                messagesService.getReply(msg.getMid(), reply_id)).size());
        assertThat(messagesService.getDiscussions(ugnich.getUid(), 0L).get(0),
                equalTo(msg.getMid()));
        messagesService.deleteMessage(user_id, mid);
        messagesService.deleteMessage(user_id, mid2);
        String htmlTagName = ">_<";
        Tag htmlTag = tagService.getTag(htmlTagName, true);
        TagStats htmlTagStats = new TagStats();
        htmlTagStats.setTag(htmlTag);
        String dbTagName = jdbcTemplate.queryForObject("select name from tags where name=?", String.class, htmlTagName);
        assertEquals("db tags should not be escaped", dbTagName, htmlTag.getName());
        int mid4 = messagesService.createMessage(user_id, "yoyoyo", null, null);
        Message msg4 = messagesService.getMessage(mid4).get();
        assertEquals("tags string should be empty", StringUtils.EMPTY, MessageUtils.getTagsString(msg4));
        messagesService.deleteMessage(user_id, mid4);
    }
    public ExpectedException exception = ExpectedException.none();

    @Test
    public void likeTypeStatsTests(){
        int user_id = userService.createUser("dsdss", "secret");
        final int freefdId = freefd.getUid();
        int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());
        messagesService.likeMessage(mid, freefdId , 2);
        messagesService.likeMessage(mid, freefdId,2);
        messagesService.likeMessage(mid, freefdId,3);
        messagesService.likeMessage(mid, freefdId,1);

        Message msg4 = messagesService.getMessage(mid).get();
        assertThat(msg4.getLikes(), equalTo(1));

        Assert.assertEquals(2, msg4.getReactions().stream().filter(r -> r.getId() == 2)
                .findFirst().orElseThrow(IllegalStateException::new).getCount());
        Assert.assertEquals(1,msg4.getReactions().stream().filter(r -> r.getId() == 3)
                .findFirst().orElseThrow(IllegalStateException::new).getCount());
    }
    @Test
    public void lastJidShouldNotBeDeleted() {
        int ugnich_id = userService.createUser("hugnich2", "x");
        jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", ugnich_id, "firstjid@localhost", 1);
        jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", ugnich_id, "secondjid@localhost", 1);
        assertThat(userService.deleteJID(ugnich_id, "secondjid@localhost"), equalTo(true));
        assertThat(userService.deleteJID(ugnich_id, "firstjid@localhost"), equalTo(false));
    }
    @Test
    public void lastEmailShouldNotBeDeleted() {
        int ugnich_id = userService.createUser("hugnich3", "x");
        jdbcTemplate.update("INSERT INTO emails(user_id,email) VALUES(?,?)", ugnich_id, "first@localhost");
        jdbcTemplate.update("INSERT INTO emails(user_id,email) VALUES(?,?)", ugnich_id, "second@localhost");
        assertThat(emailService.deleteEmail(ugnich_id, "second@localhost"), equalTo(true));
        assertThat(emailService.deleteEmail(ugnich_id, "first@localhost"), equalTo(false));
    }
    @Test
    public void messageUpdatedTimeShouldMatchLastReplyTime() throws InterruptedException {
        int ugnich_id = userService.createUser("hugnich4", "x");
        int mid = messagesService.createMessage(ugnich_id, "yo", null, null);
        Instant ts = jdbcTemplate.queryForObject("SELECT updated FROM messages WHERE message_id=?",
                Timestamp.class, mid).toInstant();
        Thread.sleep(1000);
        int rid = messagesService.createReply(mid, 0, ugnich, "people", null);
        Instant rts = jdbcTemplate.queryForObject("SELECT updated FROM messages WHERE message_id=?",
                Timestamp.class, mid).toInstant();
        assertThat(rts, greaterThan(ts));
        Message msg = messagesService.getReply(mid, rid);
        assertThat(rts, equalTo(msg.getTimestamp()));
        messagesService.deleteMessage(ugnich_id, mid);
    }

    @Test
    public void testAllUnAuthorized() throws Exception {
        mockMvc.perform(get("/api/"))
                .andExpect(status().isMovedPermanently());

        mockMvc.perform(get("/api/auth"))
                .andExpect(status().isUnauthorized());

        mockMvc.perform(get("/api/home"))
                .andExpect(status().isUnauthorized());

        mockMvc.perform(get("/api/messages/recommended"))
                .andExpect(status().isUnauthorized());

        mockMvc.perform(get("/api/messages/set_privacy"))
                .andExpect(status().isUnauthorized());
    }

    @Test
    public void homeTestWithMessages() throws Exception {
        String msgText = "Привет, я - Угнич";
        CommandResult result = commandsManager.processCommand(ugnich, msgText, URI.create("https://static.juick.com/settings/facebook.png"));
        int mid = result.getNewMessage().get().getMid();
        Message msg = messagesService.getMessage(mid).get();
        tagService.createTag("тест");
        String hash = DigestUtils.md5DigestAsHex(IOUtils.toByteArray(defaultAvatar.getInputStream()));
        mockMvc.perform(
                get("/api/home")
                        .with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("$[0].mid", is(msg.getMid())))
                .andExpect(jsonPath("$[0].timestamp",
                        is(DateFormattersHolder.getMessageFormatterInstance().format(msg.getTimestamp()))))
                .andExpect(jsonPath("$[0].body", is(msg.getText())))
                .andExpect(jsonPath("$[0].attachment.url",
                        is(String.format("https://i.juick.com/p/%d.png", msg.getMid()))))
                .andExpect(jsonPath("$[0].attachment.small.url",
                        is(String.format("https://i.juick.com/photos-512/%d.png", msg.getMid()))))
                .andExpect(jsonPath("$[0].user.avatar",
                        is(String.format("http://localhost:8080/av-96-%s.png", hash))));
    }

    @Test
    public void homeTestWithMessagesAndRememberMe() throws Exception {
        String ugnichHash = userService.getHashByUID(ugnich.getUid());
        mockMvc.perform(
                get("/api/home")
                        .with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isOk())
                .andReturn();

        mockMvc.perform(get("/api/home")
                .param("hash", ugnichHash))
                .andExpect(status().isOk());
    }

    @Test
    public void homeTestWithMessagesAndSimpleCors() throws Exception {
        mockMvc.perform(
                get("/api/home")
                        .with(httpBasic(ugnichName, ugnichPassword))
                        .header("Origin", "http://api.example.net"))
                .andExpect(status().isOk())
                .andExpect(header().string("Access-Control-Allow-Origin", "*"));
        mockMvc.perform(
                get("/u/ugnich")
                        .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("/api/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("/api/messages"))
                .andExpect(status().isOk());

        mockMvc.perform(get("/api/users")
                .param("uname", "ugnich")
                .param("uname", "freefd"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("$", hasSize(2)));
    }

    @Test
    public void messagesUrlTest() throws Exception {
        int user_id = userService.createUser("dsds4345", "secret");

        String freefdHash = userService.getHashByUID(freefd.getUid());
        System.out.println("user_id"+ user_id);
        String userIdHash = userService.getHashByUID(user_id);
        final int freefdId = freefd.getUid();
        int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());
        messagesService.likeMessage(mid, freefdId, 2 );
        messagesService.likeMessage(mid, freefdId, 2 );
        messagesService.likeMessage(mid, freefdId, 3 );

        mockMvc.perform(get("/api/messages?"+ "hash=" + userIdHash))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect((jsonPath("$[0].reactions[?(@.id == 3)].count",
                        is(Collections.singletonList(1)))))
                .andExpect((jsonPath("$[0].reactions[?(@.id == 2)].count",
                        is(Collections.singletonList(2)))));

        mockMvc.perform(get("/api/reactions?hash=" + userIdHash))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("$.length()", is(7)));
    }

    @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("/api/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("/api/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("/api/post")
                .param("body", "yo")
                .with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isOk());
    }
    @Test
    public void threadWithEphemeralNumberShouldReturn404() throws Exception {
        mockMvc.perform(get("/api/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("/api/home")).andExpect(status().isUnauthorized());
        mockMvc.perform(get("/api/auth"))
                .andExpect(status().isUnauthorized());
        mockMvc.perform(get("/api/auth").with(httpBasic(ugnichName, "wrongpassword")))
                .andExpect(status().isUnauthorized());
        MvcResult result = mockMvc.perform(get("/api/auth").with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isOk())
                .andReturn();
        String authHash = result.getResponse().getContentAsString();
        assertThat(authHash, equalTo(ugnichHash));
        mockMvc.perform(get("/api/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("/api/notifications").with(httpBasic(ugnichName, ugnichPassword))
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(jsonMapper.writeValueAsBytes(Collections.singletonList(registration))))
                .andExpect(status().isOk());
        MvcResult result = mockMvc.perform(get("/api/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/m/123456#23").build();
        assertThat(uriComponents.getPath().substring(3), 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("/api/notifications").with(httpBasic(ugnichName, ugnichPassword))
        .contentType(MediaType.APPLICATION_JSON_UTF8)
        .content(jsonMapper.writeValueAsBytes(tokens))).andExpect(status().isForbidden());
        mockMvc.perform(delete("/api/notifications").with(httpBasic(juickName, juickPassword))
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(jsonMapper.writeValueAsBytes(tokens))).andExpect(status().isOk());
    }
    @Test
    public void notificationsSettingsAllowedOnlyForServiceUser() throws Exception {
        CommandResult result = commandsManager.processCommand(ugnich, "yo", emptyUri);
        String stringValueOfMid = String.valueOf(result.getNewMessage().get().getMid());
        mockMvc.perform(get("/api/notifications").with(httpBasic(juickName, juickPassword))
        .param("mid", stringValueOfMid).param("uid", String.valueOf(ugnich.getUid()))).andExpect(status().isOk());
        mockMvc.perform(get("/api/notifications")
                .param("mid", stringValueOfMid).param("uid", String.valueOf(ugnich.getUid()))).andExpect(status().isUnauthorized());
    }
    @Test
    public void topTest() {
        int topmid = messagesService.createMessage(ugnich.getUid(), "top message", null, null);
        IntStream.rangeClosed(6, 12).forEach(i -> {
            User next = new User();
            next.setUid(i);
            messagesService.createReply(topmid, 0, next, "yo", null);
        });

        List<Integer> topCandidates = messagesService.getPopularCandidates();
        assertThat(topCandidates.size(), is(1));
        assertThat(topCandidates.get(0), is(topmid));
        Tag juickTag = tagService.getTag("juick", false);
        assertThat(juickTag.TID, is(2));
        tagService.updateTags(topmid, Collections.singletonList(juickTag));
        assertThat(messagesService.getPopularCandidates().isEmpty(), is(true));
        tagService.updateTags(topmid, Collections.singletonList(juickTag));
        assertThat(messagesService.getPopularCandidates().isEmpty(), is(false));
        jdbcTemplate.update("INSERT INTO tags(tag_id, name) VALUES(805, 'NSFW')");
        Tag nsfw = tagService.getTag("NSFW", false);
        assertThat(nsfw.TID, equalTo(805));
        tagService.updateTags(topmid, Collections.singletonList(nsfw));
        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;charset=UTF-8"))
                .andExpect(xpath("/rss/channel/description").string("The latest messages at Juick"));
    }
    @Test
    public void botCommandsTests() throws Exception {
        assertThat(commandsManager.processCommand(AnonymousUser.INSTANCE, "PING", emptyUri).getText(), is("PONG"));
        // subscription commands have two lines, others have 1
        assertThat(commandsManager.processCommand(AnonymousUser.INSTANCE, "help", emptyUri).getText().split("\n").length, is(32));
    }

    @Test
    public void protocolTests() throws Exception {
        int uid = userService.createUser("me", "secret");
        User user = userService.getUserByUID(uid).orElse(AnonymousUser.INSTANCE);
        Tag yo = tagService.getTag("yo", true);
        Message msg = commandsManager.processCommand(user, "*yo yoyo", URI.create("https://static.juick.com/settings/facebook.png")).getNewMessage().get();
        assertThat(msg.getAttachmentType(), is("png"));
        Message msgreply = commandsManager.processCommand(user, "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("https://static.juick.com/settings/xmpp.png").toURL(), tmpDir)).getNewMessage().get();
        assertThat(msgreply.getAttachmentType(), equalTo("png"));
        assertEquals("text should match", "yoyo",
                messagesService.getMessage(msg.getMid()).get().getText());
        assertEquals("tag should match", "yo",
                tagService.getMessageTags(msg.getMid()).get(0).getTag().getName());
        CommandResult yoyoMsg = commandsManager.processCommand(user, "*yo", URI.create("https://static.juick.com/settings/facebook.png"));
        assertTrue(yoyoMsg.getNewMessage().isPresent());
        assertThat(yoyoMsg.getNewMessage().get().getTags().get(0), is(yo));
        Message msg2 = yoyoMsg.getNewMessage().get();
        int mid = msg2.getMid();
        Timestamp last = jdbcTemplate.queryForObject("SELECT lastmessage FROM users WHERE id=?", Timestamp.class, user.getUid());
        assertThat(last.toInstant(), equalTo(yoyoMsg.getNewMessage().get().getTimestamp()));
        assertEquals("should be message", true,
                commandsManager.processCommand(user, String.format("#%d", mid), emptyUri).getText().startsWith("@me"));
        int readerUid = userService.createUser("dummyReader", "dummySecret");
        User readerUser = userService.getUserByUID(readerUid).orElse(AnonymousUser.INSTANCE);
        assertThat(commandsManager.processCommand(readerUser, "s", emptyUri).getText().startsWith("You are subscribed to"), is(true));
        assertThat(commandsManager.processCommand(readerUser, "S", emptyUri).getText().startsWith("You are subscribed to"), is(true));
        assertEquals("should be subscribed", "Subscribed",
                commandsManager.processCommand(readerUser, "S #" + mid, emptyUri).getText());
        assertEquals("should be favorited", "Message is added to your recommendations",
                commandsManager.processCommand(readerUser, "! #" + mid, emptyUri).getText());
        int rid = messagesService.createReply(mid, 0, user, "comment", null);
        assertEquals("number of subscribed users should match", 1,
                subscriptionService.getUsersSubscribedToComments(
                        messagesService.getMessage(mid).get(),
                        messagesService.getReply(mid, rid)).size());
        privacyQueriesService.blacklistUser(user, readerUser);
        assertEquals("number of subscribed users should match", 0,
                subscriptionService.getUsersSubscribedToComments(
                        messagesService.getMessage(mid).get(),
                        messagesService.getReply(mid, rid)).size());
        assertEquals("number of subscribed users should match", 1,
                subscriptionService.getUsersSubscribedToComments(
                        messagesService.getMessage(mid).get(),
                        messagesService.getReply(mid, rid), true).size());
        assertEquals("should be subscribed", "Subscribed to @" + user.getName(),
                commandsManager.processCommand(readerUser, "S @" + user.getName(), emptyUri)
                        .getText());
        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 expectedSecondReply = "Reply posted.\n#" + mid + "/2 "
                + "https://juick.com/m/" + mid + "#2";
        String expectedThirdReply = "Reply posted.\n#" + mid + "/3 "
                + "https://juick.com/m/" + mid + "#3";
        assertEquals("should be second reply", expectedSecondReply,
                commandsManager.processCommand(user, "#" + mid + " yoyo", URI.create("https://static.juick.com/settings/facebook.png")).getText());
        assertEquals("should be third reply", expectedThirdReply,
                commandsManager.processCommand(user, " \t\n #" + mid + "/2 ",
                        URI.create("https://static.juick.com/settings/facebook.png")).getText());
        Message reply = messagesService.getReplies(user, mid).stream().filter(m -> m.getRid() == 3).findFirst()
                .orElse(new Message());
        Timestamp lastreply = jdbcTemplate.queryForObject("SELECT lastmessage FROM users WHERE id=?", Timestamp.class, user.getUid());
        assertThat(lastreply.toInstant(), equalTo(reply.getTimestamp()));
        assertEquals("should be reply to second comment", 2, reply.getReplyto());
        assertThat(commandsManager.processCommand(readerUser, "#" + mid + " *yo *there", emptyUri)
                .getText(), startsWith("Reply posted"));
        assertEquals("tags should be updated", "Tags are updated",
                commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText());
        assertEquals("number of tags should match", 2,
                tagService.getMessageTags(mid).size());
        assertThat(messagesService.getMessage(mid).get().getTags().size(), is(2));
        assertEquals("should be blacklisted", "Tag added to your blacklist",
                commandsManager.processCommand(readerUser, "BL *there", emptyUri).getText());
        assertEquals("number of subscribed users should match", 0,
                subscriptionService.getSubscribedUsers(uid, msg2).size());
        assertEquals("tags should be updated", "Tags are updated",
                commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText());
        assertEquals("number of tags should match", 1,
                tagService.getMessageTags(mid).size());
        int taggerUid = userService.createUser("dummyTagger", "dummySecret");
        User taggerUser = userService.getUserByUID(taggerUid).orElse(AnonymousUser.INSTANCE);
        assertEquals("should be subscribed", "Subscribed",
                commandsManager.processCommand(taggerUser, "S *yo", emptyUri).getText());
        assertEquals("number of subscribed users should match", 2,
                subscriptionService.getSubscribedUsers(uid, msg2).size());
        assertEquals("should be unsubscribed", "Unsubscribed from yo",
                commandsManager.processCommand(taggerUser, "U *yo", emptyUri).getText());
        assertEquals("number of subscribed users should match", 1,
                subscriptionService.getSubscribedUsers(uid, msg2).size());
        assertEquals("number of readers should match", 1,
                userService.getUserReaders(uid).size());
        String readerFeed = commandsManager.processCommand(readerUser, "#", emptyUri).getText();
        assertTrue("description should match", readerFeed.startsWith("Your feed"));
        assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(),
                commandsManager.processCommand(readerUser, "U @" + user.getName(), emptyUri)
                        .getText());
        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,
                commandsManager.processCommand(readerUser, "u #" + mid, emptyUri).getText());
        assertEquals("number of subscribed users should match", 0,
                subscriptionService.getUsersSubscribedToComments(messagesService.getMessage(mid).get(),
                        messagesService.getReply(mid, rid)).size());
        assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid),
                commandsManager.processCommand(readerUser, "D #" + mid, emptyUri).getText());
        assertEquals("should be deleted", "Message deleted",
                commandsManager.processCommand(user, "D #" + mid, emptyUri).getText());
        assertEquals("should be not found", "Message not found",
                commandsManager.processCommand(user, "#" + mid, emptyUri).getText());

        String expectedCodeMessage = "some smelly code goes here\n" +
                "> void main(void** args) {\n" +
                "> }";
        String codeAndTags = "*code\n" + expectedCodeMessage;
        Message codeAndTagsMessage = commandsManager.processCommand(user, codeAndTags, emptyUri).getNewMessage().get();
        List<Tag> codeAndTagsTags = codeAndTagsMessage.getTags();
        assertEquals("expected single tag", 1,
                codeAndTagsTags.size());
        assertEquals("the single tag should be the 'code'", "code",
                codeAndTagsTags.get(0).getName());
        assertEquals("and the message should be with a C-code and without tags", expectedCodeMessage,
                codeAndTagsMessage.getText());
        CommandResult result = commandsManager.processCommand(user, "*one *two *three *four *five *six test", emptyUri);
        assertThat(result.getNewMessage(), is(Optional.empty()));
        assertThat(result.getText(), is("Sorry, 5 tags maximum."));
        result = commandsManager.processCommand(user, String.format("#%d *one *two *three *four *five *six", msg.getMid()), emptyUri);
        assertThat(result.getNewMessage(), is(Optional.empty()));
        assertThat(result.getText(), is("Tags are NOT updated (5 tags maximum?)"));
        result = commandsManager.processCommand(user, "I'm very smart to post my login url there" +
                "<https://juick.com/settings?hash=VTYZkKV8FWkmu6g1>", emptyUri);
        assertThat(result.getNewMessage().isPresent(), is(true));
        assertFalse(result.getNewMessage().get().getText().contains("VTYZkKV8FWkmu6g1"));
        result = commandsManager.processCommand(user, "*корм *juick_ppl *рационализм *? *мюсли а сколько микроморт в дневной порции сверхмюслей?", emptyUri);
        assertTrue(result.getNewMessage().isPresent());
        String tags = "*Juick *Google *Google Play";
        String data = "Вчера отправлял *NSFW постинг в топ :)";
        result = commandsManager.processCommand(user, String.format("%s %s", tags, data), emptyUri);
        assertThat(result.getNewMessage().get().getTags().size(), equalTo(3));
        assertThat(result.getNewMessage().get().getText(), equalTo(data));
        tags = "*\u041a\u0438\u0435\u0432 *\u044d\u043a\u043e\u043b\u043e\u0433\u0438\u044f";
        data = "* \u043c\u0443\u0441\u043e\u0440\\n\u0423 \u043c\u0435\u043d\u044f \u043a\u0430\u0436\u0434\u0443\u044e \u043d\u0435\u0434\u0435\u043b\u044e \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043f\u043e 4-5 \u0431\u0443\u0442\u044b\u043b\u043e\u043a 1,5\u043b \u041f\u0415\u0422. \u041c\u043d\u0435 \u0433\u0435\u043c\u043e\u0440\u043d\u043e \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043f\u043e \u043a\u0438\u043b\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0438\u043b\u0438 \u043f\u043e 5\u043a\u0433 \u044d\u0442\u043e\u0433\u043e \u043c\u0443\u0441\u043e\u0440\u0430, \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u0437\u0442\u0438 \u0435\u0433\u043e \u0435\u0449\u0435 \u043a\u0443\u0434\u0430-\u0442\u043e.\\n\u041d\u0435, \u043d\u0443 \u0435\u0441\u0442\u044c \u043b\u044e\u0434\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442 \u0432\u0442\u043e\u0440\u0441\u044b\u0440\u044c\u0435 \u043f\u043e \u043c\u0443\u0441\u043e\u0440\u043a\u0430\u043c, \u0441\u0432\u0430\u043b\u043a\u0430\u043c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0434\u0435\u043d\u044c\u0433\u0438 \u043d\u0443\u0436\u043d\u044b. \u0418 \u0431\u044b\u0432\u0430\u044e\u0442 \u0441\u0442\u043e\u044f\u0442 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b-\u043a\u043b\u0435\u0442\u043a\u0438 \u0434\u043b\u044f \u043f\u043b\u0430\u0441\u0442\u0438\u043a\u0430, \u043d\u043e \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u0443\u0441\u043e\u0440 \u0432\u044b\u0432\u043e\u0437\u044f\u0442 \u043d\u0435 \u0447\u0430\u0441\u0442\u043e \u0438\u043b\u0438 \u043b\u044e\u0434\u0438 \u0432\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u0432\u044b\u043a\u0438\u0434\u044b\u0432\u0430\u044e\u0442 \u043c\u043d\u043e\u0433\u043e \u043c\u0443\u0441\u043e\u0440\u0430, \u0442\u043e \u0432 \u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435 \u0442\u043e\u0442 \u043c\u0443\u0441\u043e\u0440, \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043b\u0441\u044f \u0432 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430\u0445.";
        result = commandsManager.processCommand(user, String.format("%s %s", tags, data), emptyUri);
        assertThat(result.getNewMessage().get().getTags().size(), equalTo(2));
        assertThat(result.getNewMessage().get().getTags().get(0).getName(), equalTo("Киев"));
        assertThat(result.getNewMessage().get().getText(), equalTo(data));
        result = commandsManager.processCommand(user, "S @unknown-user", emptyUri);
        assertThat(result.getNewMessage(), is(Optional.empty()));
        assertThat(result.getText(), is("User not found"));
    }
    @Test
    public void mailParserTest() throws Exception {
        String mail = "MIME-Version: 1.0\n" +
                "Received: by 10.176.0.242 with HTTP; Fri, 16 Mar 2018 05:31:50 -0700 (PDT)\n" +
                "In-Reply-To: <2891710.100@juick.com>\n" +
                "References: <2891710.0@juick.com> <2891710.100@juick.com>\n" +
                "Date: Fri, 16 Mar 2018 15:31:50 +0300\n" +
                "Delivered-To: vitalyster@gmail.com\n" +
                "Message-ID: <CAF+0zPD_YLVgYovajLqUFwkRAgJT+FzyQ2EzikQsPKsrnfKv-Q@mail.gmail.com>\n" +
                "Subject: Re: New reply to TJ\n" +
                "From: Vitaly Takmazov <vitalyster@gmail.com>\n" +
                "To: Juick <juick@juick.com>\n" +
                "Content-Type: multipart/alternative; boundary=\"001a11454886e42be5056786ca70\"\n" +
                "\n" +
                "--001a11454886e42be5056786ca70\n" +
                "Content-Type: text/plain; charset=\"UTF-8\"\n" +
                "\n" +
                "s2313334\n" +
                "\n" +
                "--001a11454886e42be5056786ca70\n" +
                "Content-Type: text/html; charset=\"UTF-8\"\n" +
                "\n" +
                "<div dir=\"ltr\">s2313334</div>\n" +
                "\n" +
                "--001a11454886e42be5056786ca70--";
        mockMvc.perform(post("/api/mail").with(httpBasic(juickName, juickPassword)).content(mail))
                .andExpect(status().isOk());
    }

    @Test
    public void recommendTests() throws Exception {

        int mid = messagesService.createMessage(ugnich.getUid(), "to be liked", null, null);
        String freefdHash = userService.getHashByUID(freefd.getUid());
        int freefdMid = messagesService.createMessage(freefd.getUid(), "to be not liked", null, null);

        mockMvc.perform(post("/api/like?mid=" + mid + "&hash=" + freefdHash))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.status", is("Message is added to your recommendations")));
        mockMvc.perform(get("/api/thread?mid=" + mid + "&hash=" + freefdHash))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].recommendations.length()", is(1)))
                .andExpect(jsonPath("$[0].recommendations[0]", is(freefdName)));
        mockMvc.perform(post("/api/like?mid=" + freefdMid + "&hash=" + freefdHash))
                .andExpect(status().isForbidden());
    }

    @Test
    public void likesTests() throws Exception{
         int user_id = userService.createUser("dsds", "secret");
         String freefdHash = userService.getHashByUID(freefd.getUid());
         int mid1 = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());

         mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=2"))
                 .andExpect(status().isOk());

         Message msg4 = messagesService.getMessage(mid1).get();
         assertThat(msg4.getLikes(), is(0));
         assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, Collections.singletonList(mid1)).get(0).getLikes(), is(0));
         Assert.assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2)
                 .findFirst().orElseThrow(IllegalStateException::new).getCount());
        mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=1"))
                .andExpect(status().isOk());
        mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=1"))
                .andExpect(status().isOk());
        assertThat(messagesService.getMessage(mid1).get().getLikes(), is(1));
    }

    @Test
    public void lastReadTests() throws Exception {
        jdbcTemplate.execute("DELETE FROM bl_users");
        jdbcTemplate.execute("DELETE FROM messages");
        assertThat(userService.isInBLAny(ugnich.getUid(), freefd.getUid()), is(false));
        int mid = messagesService.createMessage(ugnich.getUid(), "to be watched", null, null);
        subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), ugnich);
        messagesService.createReply(mid, 0, freefd, "new reply", null);
        BiFunction<User, Integer, Integer> lastRead = (user, m) -> jdbcTemplate.queryForObject(
                "SELECT last_read_rid FROM subscr_messages WHERE suser_id=? AND message_id=?",
                Integer.class, user.getUid(), m);
        assertThat(lastRead.apply(ugnich, mid), is(0));
        assertThat(messagesService.getUnread(ugnich).size(), is(1));
        assertThat(messagesService.getUnread(ugnich).get(0), is(mid));
        messagesService.getReplies(ugnich, mid);
        assertThat(lastRead.apply(ugnich, mid), is(1));
        assertThat(messagesService.getUnread(ugnich).size(), is(0));
        messagesService.setLastReadComment(ugnich, mid, 0);
        assertThat(lastRead.apply(ugnich, mid), is(1));
        String ugnichHash = userService.getHashByUID(ugnich.getUid());
        int freefdrid = messagesService.createReply(mid, 0, freefd, "again", null);
        mockMvc.perform(get(String.format("/api/thread/mark_read/%d-%d.gif?hash=%s", mid, freefdrid, ugnichHash)))
                .andExpect(status().isOk())
                .andExpect(content().bytes(IOUtils.toByteArray(
                        Objects.requireNonNull(getClass().getClassLoader().getResource("Transparent.gif")))));
        assertThat(lastRead.apply(ugnich, mid), is(freefdrid));
        privacyQueriesService.blacklistUser(ugnich, freefd);
        int newfreefdrid = messagesService.createReply(mid, 0, freefd, "from ban", null);
        serverManager.processMessageEvent(new MessageEvent(this, messagesService.getReply(mid, newfreefdrid),
                Collections.emptyList()));
        assertThat(userService.isReplyToBL(ugnich, messagesService.getReply(mid, newfreefdrid)), is(true));
        // TODO: test event listeners correctly
        Thread.sleep(2000L);
        assertThat(lastRead.apply(ugnich, mid), is(newfreefdrid));
        privacyQueriesService.blacklistUser(ugnich, freefd);
        newfreefdrid = messagesService.createReply(mid, 0, freefd, "after ban", null);
        assertThat(lastRead.apply(ugnich, mid), lessThan(newfreefdrid));
        mockMvc.perform(get(String.format("/api/thread?mid=%d&hash=%s", mid, ugnichHash)))
                .andExpect(status().isOk());
        assertThat(lastRead.apply(ugnich, mid), is(newfreefdrid));
    }
    @Test
    public void feedsShouldNotContainMessagesWithBannedTags() {
        Tag banned = tagService.getTag("banned", true);
        int mid = messagesService.createMessage(ugnich.getUid(), "yo", "jpg",
                Collections.singletonList(banned));
        privacyQueriesService.blacklistTag(freefd, banned);
        assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getAll(freefd.getUid(), 0))
                .stream().noneMatch(m -> m.getTags().contains(banned)));
        assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getAll(ugnich.getUid(), 0))
                .stream().noneMatch(m -> m.getTags().contains(banned)));
        assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPhotos(freefd.getUid(), 0))
                .stream().noneMatch(m -> m.getTags().contains(banned)));
        assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPhotos(ugnich.getUid(), 0))
                .stream().noneMatch(m -> m.getTags().contains(banned)));
        jdbcTemplate.update("UPDATE messages SET popular=1 WHERE message_id=?", mid);
        assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPopular(freefd.getUid(), 0))
                .stream().noneMatch(m -> m.getTags().contains(banned)));
        assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPopular(ugnich.getUid(), 0))
                .stream().noneMatch(m -> m.getTags().contains(banned)));
        assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true))
                .stream().noneMatch(m -> m.getTags().contains(banned)));
        int newUid = userService.createUser("newUser", "12345");
        int newMid = messagesService.createMessage(newUid, "people", null, Collections.singletonList(banned));
        messagesService.recommendMessage(newMid, ugnich.getUid());
        assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true))
                .stream().noneMatch(m -> m.getTags().contains(banned)));
        tagService.updateTags(newMid, Collections.singletonList(banned));
        assertThat(messagesService.getMessage(newMid).get().getTags().size(), is(0));
        privacyQueriesService.blacklistUser(freefd, userService.getUserByUID(newUid).orElse(AnonymousUser.INSTANCE));
        assertTrue(messagesService.getMyFeed(freefd.getUid(), 0, true)
                .stream().noneMatch(m -> m == newMid));
    }
    @Test
    public void tagsShouldBeDeserializedFromXml() throws JAXBException {
        XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
                .extensions(Extension.of(com.juick.Message.class))
                .build();
        XmppSession xmpp = new XmppSession("juick.com", configuration) {
            @Override
            public void connect(Jid from) {

            }

            @Override
            public Jid getConnectedResource() {
                return null;
            }
        };
        String tag = "<tag xmlns='http://juick.com/message'>yo</tag>";
        String xml = "<message xmlns='jabber:client' from='juick@juick.com' type='chat'><body>yo</body><juick mid='1' ts='2017-09-14' uid='1' uname='ugnich' xmlns='http://juick.com/message'><body>yo</body><user uid='1' uname='ugnich' xmlns='http://juick.com/user'/><tag>yo</tag><tag>people</tag></juick></message>";
        Unmarshaller unmarshaller = xmpp.createUnmarshaller();
        rocks.xmpp.core.stanza.model.Message xmppMessage = (rocks.xmpp.core.stanza.model.Message) unmarshaller.unmarshal(new StringReader(xml));
        Tag xmlTag = (Tag) unmarshaller.unmarshal(new StringReader(tag));
        assertThat(xmlTag.getName(), equalTo("yo"));
        Message juickMessage = xmppMessage.getExtension(Message.class);
        assertThat(juickMessage.getTags().get(0).getName(), equalTo("yo"));
    }
    @Test
    public void messageParserSerializer() throws ParserConfigurationException,
            IOException, SAXException, JAXBException {
        Message msg = new Message();
        msg.setTags(MessageUtils.parseTags("test test" + (char) 0xA0 + "2 test3"));
        assertEquals("First tag must be", "test", msg.getTags().get(0).getName());
        assertEquals("Third tag must be", "test3", msg.getTags().get(2).getName());
        assertEquals("Count of tags must be", 3, msg.getTags().size());
        Instant currentDate = Instant.now();
        msg.setTimestamp(currentDate);
        String jsonMessage = jsonMapper.writeValueAsString(msg);
        assertEquals("date should be in timestamp field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate),
                JsonPath.read(jsonMessage, "$.timestamp"));


        JAXBContext context = JAXBContext
                .newInstance(Message.class);
        Marshaller m = context.createMarshaller();

        StringWriter sw = new StringWriter();
        m.marshal(msg, sw);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(sw.toString().getBytes(StandardCharsets.UTF_8)));
        Node juickNode = doc.getDocumentElement();
        NamedNodeMap attrs = juickNode.getAttributes();
        assertEquals("date should be in ts field", DateFormattersHolder.getMessageFormatterInstance().format(currentDate),
                attrs.getNamedItem("ts").getNodeValue());
    }
    @Test
    public void restTemplateTests() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);

        map.add("body", "yo");
        map.add("hash", userService.getHashByUID(ugnich.getUid()));
        ResponseEntity<CommandResult> result = restTemplate.postForEntity(
                    "/api/post",
                    request, CommandResult.class);
        assertThat(result.getStatusCode(), is(HttpStatus.OK));
    }
    @Test
    public void emptyAuthenticatedPostShouldThrowBadRequest() throws Exception {
        mockMvc.perform(post("/api/post")
                .with(httpBasic(juickName, juickPassword)))
                .andExpect(status().isBadRequest());
    }
    @Test
    public void attachmentSizeTests() throws URISyntaxException, IOException {
        ImageUtils imageUtils = new ImageUtils(StringUtils.EMPTY, StringUtils.EMPTY);
        Attachment attachment = imageUtils.getAttachment(new File(getClass().getClassLoader().getResource("Transparent.gif").toURI()));
        assertThat(attachment.getHeight(), is(1));
        assertThat(attachment.getWidth(), is(1));
    }
    @Test
    public void meContainsAllInfo() throws Exception {
        jdbcTemplate.update("DELETE FROM subscr_users");
        assertThat(userService.getUserReaders(ugnich.getUid()).size(), is(0));
        assertThat(userService.getUserFriends(ugnich.getUid()).size(), is(0));
        commandsManager.processCommand(freefd, "S @ugnich", emptyUri);
        commandsManager.processCommand(ugnich, "S @freefd", emptyUri);
        assertThat(userService.getUserReaders(ugnich.getUid()).size(), is(1));
        String hash = userService.getHashByUID(ugnich.getUid());
        mockMvc.perform(get("/api/me")
                .with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(jsonPath("$.hash", is(hash)))
                .andExpect(jsonPath("$.readers.length()", is(1)))
                .andExpect(jsonPath("$.read.length()", is(1)));
    }
    @Test
    public void feedsShouldNotContainBannedUsers() throws Exception {
        commandsManager.processCommand(ugnich, "BL @freefd", emptyUri);
        CommandResult result = commandsManager.processCommand(ugnich, "freefd - dick", emptyUri);
        int mid = result.getNewMessage().get().getMid();
        commandsManager.processCommand(freefd, String.format("#%d ugnich - dick too", mid), emptyUri);
        commandsManager.processCommand(juick, String.format("#%d/1 ban for a hour!", mid), emptyUri);
        commandsManager.processCommand(juick, String.format("#%d freefd is here but it is hidden from you", mid), emptyUri);
        assertThat(messagesService.getMessage(mid).get().getReplies(), is(3));
        Message reply = messagesService.getReply(mid, 3);
        assertThat(userService.isReplyToBL(ugnich, reply), is(false));
        List<Message> replies = messagesService.getReplies(ugnich, mid);
        assertThat(replies.size(), is(1));
        commandsManager.processCommand(freefd, String.format("#%d/3 hahaha!", mid), emptyUri);
        assertThat(messagesService.getMessage(mid).get().getReplies(), is(4));
        replies = messagesService.getReplies(ugnich, mid);
        assertThat(replies.size(), is(1));
        commandsManager.processCommand(juick, String.format("#%d/4 mmm?!", mid), emptyUri);
        assertThat(messagesService.getMessage(mid).get().getReplies(), is(5));
        replies = messagesService.getReplies(ugnich, mid);
        reply = messagesService.getReply(mid, 5);
        assertThat(userService.isReplyToBL(ugnich, reply), is(true));
        assertThat(replies.size(), is(1));
        List<Message> msgs = messagesService.getMessages(ugnich, Collections.singletonList(mid));
        assertThat(msgs.get(0).getReplies(), is(1));
        commandsManager.processCommand(ugnich, "BL @freefd", emptyUri);
        messagesService.setRead(ugnich, mid);
        assertThat(messagesService.getReplies(ugnich, mid).size(), is(5));
        List<Message> nonblmsgs = messagesService.getMessages(ugnich, Collections.singletonList(mid));
        assertThat(nonblmsgs.get(0).getReplies(), is(5));
        commandsManager.processCommand(ugnich, "BL @freefd", emptyUri);
        Tag tag = tagService.getTag("linux", true);
        messagesService.createMessage(freefd.getUid(), "sux", null, Collections.singletonList(tag));
        assertThat(messagesService.getTag(tag.TID, freefd.getUid(), 0, 10).size(), is(1));
        assertThat(messagesService.getTag(tag.TID, ugnich.getUid(), 0, 10).size(), is(0));
        commandsManager.processCommand(ugnich, "BL @freefd", emptyUri);
    }
    @Test
    public void cmykJpegShouldBeProcessedCorrectly() throws Exception {
        CommandResult postJpgCmyk = commandsManager.processCommand(ugnich, "YO", cmykJpeg.getURI());
        assertThat(postJpgCmyk.getNewMessage().isPresent(), is(true));
        int mid = postJpgCmyk.getNewMessage().get().getMid();
        File originalFile = Paths.get(imgDir, "p", String.format("%d.jpg", mid)).toFile();
        assertThat(originalFile.exists(), is(true));
        File mediumFile = Paths.get(imgDir, "photos-1024", String.format("%d.jpg", mid)).toFile();
        assertThat(mediumFile.exists(), is(true));
        assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getWidth(), is(2585));
        assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getHeight(), is(3335));
        assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getMedium().getHeight(), is(1024));
        assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getSmall().getHeight(), is(512));
    }
    @Test
    public void JpegWithoutJfifShouldBeProcessedCorrectly() throws Exception {
        CommandResult postJpgCmyk = commandsManager.processCommand(ugnich, "YO", nojfif.getURI());
        assertThat(postJpgCmyk.getNewMessage().isPresent(), is(true));
        int mid = postJpgCmyk.getNewMessage().get().getMid();
        File originalFile = Paths.get(imgDir, "p", String.format("%d.jpg", mid)).toFile();
        assertThat(originalFile.exists(), is(true));
        File mediumFile = Paths.get(imgDir, "photos-1024", String.format("%d.jpg", mid)).toFile();
        assertThat(mediumFile.exists(), is(true));
        assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getWidth(), is(3264));
        assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getHeight(), is(2448));
        assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getMedium().getHeight(), is(768));
        assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getSmall().getHeight(), is(384));
    }
    @Test
    public void JpegFromJuickUriShouldBeProcessedCorrectly() throws Exception {
        Path tmpFile = Paths.get(tmpDir, "2915104.jpg");
        Files.copy(Paths.get(new ClassPathResource("2915104.jpg").getURI()), tmpFile, StandardCopyOption.REPLACE_EXISTING);
        assertThat(tmpFile.toFile().exists(), is(true));
        CommandResult postJpgiPhone = commandsManager.processCommand(ugnich, "YO", URI.create("juick://2915104.jpg"));
        assertThat(postJpgiPhone.getNewMessage().isPresent(), is(true));
        int mid = postJpgiPhone.getNewMessage().get().getMid();
        File originalFile = Paths.get(imgDir, "p", String.format("%d.jpg", mid)).toFile();
        assertThat(originalFile.exists(), is(true));
        File mediumFile = Paths.get(imgDir, "photos-1024", String.format("%d.jpg", mid)).toFile();
        assertThat(mediumFile.exists(), is(true));
        assertThat(postJpgiPhone.getNewMessage().get().getAttachment().getWidth(), is(1280));
        assertThat(postJpgiPhone.getNewMessage().get().getAttachment().getHeight(), is(1280));
        assertThat(postJpgiPhone.getNewMessage().get().getAttachment().getMedium().getHeight(), is(1024));
        assertThat(postJpgiPhone.getNewMessage().get().getAttachment().getSmall().getHeight(), is(512));
    }
    @Test
    public void changeExtensionWhenReceiveFileWithWrongContentType() throws Exception {
        Path pngOutput = Paths.get(tmpDir, "cmyk.png");
        Files.deleteIfExists(pngOutput);
        Files.copy(Paths.get(cmykJpeg.getURI()), pngOutput);
        assertThat(pngOutput.toFile().exists(), is(true));
        CommandResult postJpgCmyk = commandsManager.processCommand(ugnich, "YO", pngOutput.toUri());
        assertThat(postJpgCmyk.getNewMessage().isPresent(), is(true));
        assertThat(postJpgCmyk.getNewMessage().get().getAttachmentType(), is("jpg"));
        CommandResult replyJpgCmyk = commandsManager.processCommand(ugnich, String.format("#%d YO", postJpgCmyk.getNewMessage().get().getMid()), pngOutput.toUri());
        assertThat(replyJpgCmyk.getNewMessage().isPresent(), is(true));
        assertThat(replyJpgCmyk.getNewMessage().get().getAttachmentType(), is("jpg"));
    }
    @Test
    public void messageEditingSpec() throws Exception {
        MvcResult result = mockMvc.perform(post("/api/post").with(httpBasic(ugnichName, ugnichPassword))
        .param("body", "YO")).andExpect(status().is2xxSuccessful()).andReturn();
        Message original = jsonMapper.readValue(result.getResponse().getContentAsString(), CommandResult.class)
                .getNewMessage().get();
        assertThat(original.getText(), equalTo("YO"));
        assertThat(original.getUpdatedAt(), equalTo(original.getTimestamp()));
        // to have updated_at greater than ts
        Thread.sleep(1000);
        result = mockMvc.perform(post("/api/update").with(httpBasic(ugnichName, ugnichPassword))
        .param("mid", String.valueOf(original.getMid()))
        .param("body", "PEOPLE")).andExpect(status().is2xxSuccessful()).andReturn();
        Message edited = jsonMapper.readValue(result.getResponse().getContentAsString(), CommandResult.class)
                .getNewMessage().get();
        assertThat(edited.getText(), equalTo("PEOPLE"));
        assertThat(edited.getUpdatedAt(), greaterThan(edited.getTimestamp()));
        mockMvc.perform(post("/api/update").with(httpBasic(freefdName, freefdPassword))
                .param("mid", String.valueOf(original.getMid()))
                .param("body", "PEOPLE")).andExpect(status().is(403));
        result = mockMvc.perform(post("/api/comment").with(httpBasic(freefdName, freefdPassword))
        .param("mid", String.valueOf(original.getMid()))
        .param("body", "HEY")).andExpect(status().is2xxSuccessful()).andReturn();
        CommandResult comment = jsonMapper.readValue(result.getResponse().getContentAsString(), CommandResult.class);
        assertThat(comment.getNewMessage().get().getText(), is("HEY"));
        assertThat(comment.getNewMessage().get().getUpdatedAt(), is(comment.getNewMessage().get().getTimestamp()));
        // to have updated_at greater than ts
        Thread.sleep(1000);
        result = mockMvc.perform(post("/api/update").with(httpBasic(freefdName, freefdPassword))
                .param("mid", String.valueOf(comment.getNewMessage().get().getMid()))
                .param("rid", String.valueOf(comment.getNewMessage().get().getRid()))
                .param("body", "HEY, JOE")).andExpect(status().is2xxSuccessful()).andReturn();
        Message editedComment = jsonMapper.readValue(result.getResponse().getContentAsString(), CommandResult.class)
                .getNewMessage().get();
        assertThat(editedComment.getText(), is("HEY, JOE"));
        assertThat(editedComment.getUpdatedAt(), greaterThan(editedComment.getTimestamp()));
        messagesService.deleteMessage(ugnich.getUid(), original.getMid());
    }
    @Test
    public void subscribersToRecommendations() {
        int readerId = userService.createUser("reader", "123456");
        int recommenderId = userService.createUser("recommender", "123456");
        int lateRecommenderId = userService.createUser("lateRecommender", "123456");
        int posterId = userService.createUser("poster", "123456");
        User reader = userService.getUserByName("reader");
        User recommender = userService.getUserByName("recommender");
        User lateRecommender = userService.getUserByName("lateRecommender");
        User poster = userService.getUserByName("poster");
        subscriptionService.subscribeUser(reader, recommender);
        subscriptionService.subscribeUser(reader, lateRecommender);
        Tag sampleTag = tagService.getTag("banned", true);
        int posterMid = messagesService.createMessage(posterId, "YO", null, Collections.singletonList(sampleTag));
        messagesService.recommendMessage(posterMid, recommenderId);
        BiFunction<Integer, Message, List<User>> subscribers = (recommId, msg) ->
                subscriptionService.getUsersSubscribedToUserRecommendations(recommId, msg);
        List<User> recommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get());
        assertThat(recommendSubscribers.size(), is(1));
        assertThat(recommendSubscribers.get(0).getUid(), is(readerId));
        privacyQueriesService.blacklistUser(reader, poster);
        assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(0));
        privacyQueriesService.blacklistUser(reader, poster);
        assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(1));
        tagService.blacklistTag(reader, sampleTag);
        assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(0));
        tagService.blacklistTag(reader, sampleTag);
        assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(1));
        messagesService.recommendMessage(posterMid, lateRecommenderId);
        List<User> lateRecommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get());
        assertThat(lateRecommendSubscribers.size(), is(0));
        int readerMid = messagesService.createMessage(readerId, "PEOPLE", null, null);
        messagesService.recommendMessage(readerMid, recommenderId);
        assertThat(subscribers.apply(recommenderId, messagesService.getMessage(readerMid).get()).size(), is(0));
    }
    @Test
    public void mentionsInComments() {
        int posterId = userService.createUser("p", "secret");
        int commenterId = userService.createUser("cc", "secret");
        User commenter = userService.getUserByUID(commenterId).get();
        int mentionerId = userService.createUser("mmm", "secret");
        User mentioner = userService.getUserByUID(mentionerId).get();
        int mid = messagesService.createMessage(posterId, "who is dick?", null, null);
        Message msg = messagesService.getMessage(mid).get();
        int rid = messagesService.createReply(mid, 0, commenter,
                "@mmm is dick", null);
        Message reply = messagesService.getReply(mid, rid);
        assertThat(subscriptionService.getUsersSubscribedToComments(msg, reply).size(), is(1));
        subscriptionService.subscribeUser(mentioner, commenter);
        assertThat(subscriptionService.getUsersSubscribedToComments(msg, reply).size(), is(1));
        privacyQueriesService.blacklistUser(mentioner, commenter);
        assertThat(subscriptionService.getUsersSubscribedToComments(msg, reply).size(), is(0));
    }
    @Test
    public void credentialsShouldNeverBeSerialized() throws Exception {
        int uid = userService.createUser("yyy", "xxxx");
        User yyy = userService.getUserByUID(uid).get();
        assertThat(yyy.getCredentials(), is("xxxx"));
        ObjectMapper jsonMapper = new ObjectMapper();
        jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        String jsonUser = jsonMapper.writeValueAsString(yyy);
        Map<String, Object> user = JsonPath.read(jsonUser, "$");
        // only uid, name and uri
        assertThat(user.keySet().size(), is(3));

        JAXBContext context = JAXBContext
                .newInstance(User.class);
        Marshaller m = context.createMarshaller();

        StringWriter sw = new StringWriter();
        m.marshal(yyy, sw);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(sw.toString().getBytes(StandardCharsets.UTF_8)));
        Element juickNode = doc.getDocumentElement();
        NamedNodeMap attrs = juickNode.getAttributes();
        // uid, name, xmlns, xmlns:user
        assertThat(attrs.getLength(), is(4));
    }
    @Test
    public void bannedUserBlogandPostShouldReturn404() throws Exception {
        String userName = "isilmine";
        String userPassword = "secret";
        String msgText = "автор этого поста был забанен";

        User isilmine = userService.getUserByUID(userService.createUser(userName, userPassword)).orElseThrow(IllegalStateException::new);
        int mid = messagesService.createMessage(isilmine.getUid(), msgText, null, null);
        mockMvc.perform(get(String.format("/api/thread?mid=%d", mid)).with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isOk());
        jdbcTemplate.update("UPDATE users SET banned=1 WHERE id=?", isilmine.getUid());
        mockMvc.perform(get(String.format("/api/thread?mid=%d", mid)).with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isNotFound());
        mockMvc.perform(get("/api/messages?uname=isilmine").with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isNotFound());
        mockMvc.perform(get("/api/info/isilmine").with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isNotFound());
        mockMvc.perform(get("/api/info/ugnich").with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isOk());
    }

    @Test
    public void emptyPasswordMeansUserIsDisabled() throws Exception {
        String userName = "oldschooluser";
        String userPassword = "";

        userService.createUser(userName, userPassword);

        mockMvc.perform(get("/api/auth").with(httpBasic(userName, userPassword))).andExpect(status().isUnauthorized());
        mockMvc.perform(post("/login")
                .param("username", userName)
                .param("password", userPassword)).andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl("/login?error=1"));
    }
    @Test
    public void bannedUserShouldBeShadowedFromRecommendationsList() throws IOException {
        int ermineId = userService.createUser("ermine", "secret");
        int monstreekId = userService.createUser("monstreek", "secret");
        int pogoId = userService.createUser("pogo", "secret");
        int fmapId = userService.createUser("fmap", "secret");
        int mid = messagesService.createMessage(monstreekId, "KURWA", null, null);
        assertThat(messagesService.recommendMessage(mid, ermineId), is(MessagesService.RecommendStatus.Added));
        assertThat(messagesService.recommendMessage(mid, fmapId), is(MessagesService.RecommendStatus.Added));
        assertThat(messagesService.recommendMessage(mid, pogoId), is(MessagesService.RecommendStatus.Added));
        assertThat(messagesService.getMessage(mid).get().getLikes(), is(3));
        assertThat(CollectionUtils.isEqualCollection(messagesService.getMessageRecommendations(mid), Arrays.asList("fmap", "ermine", "pogo")), is(true));
        privacyQueriesService.blacklistUser(userService.getUserByName("monstreek"), userService.getUserByName("pogo"));
        assertThat(messagesService.getMessage(mid).get().getLikes(), is(3));
        assertThat(CollectionUtils.isEqualCollection(messagesService.getMessageRecommendations(mid), Arrays.asList("fmap", "ermine")), is(true));
    }
    @Test
    public void bannedUserShouldNotBeVisibleToOthers() {
        jdbcTemplate.execute("DELETE FROM messages");
        int casualUserId = userService.createUser("user", "secret");
        int bannedUserId = userService.createUser("banned", "banned");
        jdbcTemplate.update("UPDATE users SET banned=1 WHERE id=?", bannedUserId);
        messagesService.createMessage(bannedUserId, "KURWA", null, Collections.emptyList());
        assertThat(messagesService.getAll(casualUserId, 0).size(), is(0));
        assertThat(messagesService.getDiscussions(casualUserId, 0L).size(), is(0));
        assertThat(messagesService.getDiscussions(0, 0L).size(), is(0));
        assertThat(messagesService.getAll(bannedUserId, 0).size(), is(1));
        int mid = messagesService.createMessage(casualUserId, "PEACE", null, Collections.emptyList());
        User banned = userService.getUserByName("banned");
        int bannedRid = messagesService.createReply(mid, 0, banned, "KURWA", null);
        int casualRid = messagesService.createReply(mid, 0, userService.getUserByName("user"), "DOOR", null);
        assertThat(messagesService.getReplies(AnonymousUser.INSTANCE, mid).size(), is(1));
        assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, Collections.singletonList(mid)).get(0).getReplies(), is(1));
        assertThat(messagesService.getReplies(banned, mid).size(), is(2));
        assertThat(messagesService.getMessages(banned, Collections.singletonList(mid)).get(0).getReplies(), is(2));
    }

    @Test
    public void accountUrlShouldBeExposedOverWebfinger() throws Exception {
        mockMvc.perform(get("/.well-known/webfinger?resource=acct:ugnich@localhost"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.subject", is("acct:ugnich@localhost")))
                .andExpect(jsonPath("$.links", hasSize(1)))
                .andExpect(jsonPath("$.links[0].href", is("http://localhost:8080/u/ugnich")));
        mockMvc.perform(get("/.well-known/webfinger?resource=acct:durov@localhost"))
                .andExpect(status().isNotFound());
        Person ugnich = (Person) signatureManager.discoverPerson("ugnich@juick.com").get();
        assertThat(ugnich.getName(), is(ugnichName));
    }
    @Test
    public void userProfileAndBlogShouldBeExposedAsActivityStream() throws Exception {
        ClassPathResource defaultAvatar = new ClassPathResource("static/av-96.png");
        String hash = DigestUtils.md5DigestAsHex(IOUtils.toByteArray(defaultAvatar.getInputStream()));
        mockMvc.perform(get("/u/ugnich").accept(Context.LD_JSON_MEDIA_TYPE))
                .andExpect(status().isOk())
        .andExpect(jsonPath("$.icon.url", is(String.format("http://localhost:8080/av-96-%s.png", hash))))
        .andExpect(jsonPath("$.publicKey.publicKeyPem", is(keystoreManager.getPublicKeyPem())));
        jdbcTemplate.execute("DELETE FROM messages");
        List<Integer> mids = IteratorUtils.toList(IntStream.rangeClosed(1, 30)
                .mapToObj(i -> messagesService.createMessage(ugnich.getUid(),
                        String.format("message %d", i), null, null))
                .collect(Collectors.toCollection(ArrayDeque::new)).descendingIterator());
        List<Integer> midsPage = mids.stream().limit(20).collect(Collectors.toList());
        mockMvc.perform(get("/u/ugnich/blog").accept(Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.orderedItems", hasSize(20)))
        .andExpect(jsonPath("$.next", is("http://localhost:8080/u/ugnich/blog?before=" + midsPage.get(midsPage.size() - 1))));
    }
    @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 repliesList() throws IOException {
        int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, null);
        IntStream.range(1, 15).forEach(i ->
                messagesService.createReply(mid, i-1, freefd, 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, "yo", null);
        MvcResult loginResult = mockMvc.perform(post("/login")
                .param("username", freefdName)
                .param("password", freefdPassword))
                .andExpect(status().isFound()).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, userService.getUserByUID(renhaId).orElseThrow(IllegalStateException::new),
                "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\">&gt;_&lt;</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).get();
        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 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))
                .andExpect(status().is3xxRedirection()).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))
                .andExpect(status().is3xxRedirection()).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/";
        HtmlPage discussions = webClient.getPage(discussionsUrl);
        assertThat(discussions.querySelectorAll("article").size(), is(0));
        subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), freefd);
        discussions = (HtmlPage) discussions.refresh();
        assertThat(discussions.querySelectorAll("article").size(), is(1));
        subscriptionService.subscribeMessage(messagesService.getMessage(midNew).get(), freefd);
        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, "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).get();
        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(messagesService.getMessage(m).get(), freefd);
        }
        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, "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)).get();
        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))
                .andExpect(status().isFound()).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"));
    }
    @Test
    public void anythingRedirects() throws Exception {
        int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, null);
        mockMvc.perform(get(String.format("/%d", mid)))
                .andExpect(status().isMovedPermanently())
                .andExpect(redirectedUrl(String.format("/%s/%d", ugnich.getName(), mid)));
    }
    @Test
    public void appAssociationsTest() throws Exception {
        mockMvc.perform((get("/.well-known/apple-app-site-association")))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("$.webcredentials.apps[0]", is(appId)));
    }
    @Test
    public void notificationsTests() throws Exception {
        jdbcTemplate.execute("DELETE FROM messages");
        jdbcTemplate.execute("DELETE FROM replies");
        jdbcTemplate.execute("DELETE FROM subscr_messages");
        MvcResult loginResult = mockMvc.perform(post("/login")
                .param("username", freefdName)
                .param("password", freefdPassword))
                .andExpect(status().is3xxRedirection()).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()));
        int mid = messagesService.createMessage(ugnich.getUid(), "new test", null, null);
        subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), freefd);
        int rid = messagesService.createReply(mid, 0, ugnich, "new reply", null);
        HtmlPage discussionsPage = webClient.getPage("http://localhost:8080/?show=discuss");
        assertThat(discussionsPage.querySelectorAll("#global a .badge").size(), is(1));
        HtmlPage unreadThread = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid));
        assertThat(unreadThread.querySelectorAll("#global a .badge").size(), is(0));
    }
    @Test
    public void escapeSqlTests() {
        String sql = String.format("SELECT * FROM table WHERE data='%s'", Utils.encodeSphinx("';-- DROP TABLE table"));
        assertThat(sql, is("SELECT * FROM table WHERE data='\\';-- DROP TABLE table\'"));
    }
    @Test
    public void swaggerOutput() throws Exception {
        MvcResult result = mockMvc.perform(get("/v2/api-docs")
        .accept(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andReturn();
        String outputDir = System.getProperty("io.springfox.staticdocs.outputDir");
        if (StringUtils.isNotEmpty(outputDir)) {
            Files.createDirectories(Paths.get(outputDir));
            BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, "swagger.json"), StandardCharsets.UTF_8);
            writer.write(result.getResponse().getContentAsString());
            writer.flush();
        }
    }
    @Test
    public void newMessageShouldNotContainDuplicatedTags() throws Exception {
        CommandResult result = commandsManager.processCommand(ugnich, "*test1 *test2 *test1 test3", emptyUri);
        assertThat(result.getNewMessage().isPresent(), is(true));
        Message msg = result.getNewMessage().get();
        assertThat(msg.getTags().size(), is (2));
        assertThat(msg.getTags().get(0).getName(), is("test1"));
        assertThat(msg.getTags().get(1).getName(), is("test2"));
        assertThat(msg.getText(), is("test3"));
    }
    @Test
    public void oneClickUnsubscribe() throws Exception {
        mockMvc.perform(post("/settings/unsubscribe")
                .param("hash", "123456")
                .param("List-Unsubscribe", "One-Click")).andExpect(status().isBadRequest());
        mockMvc.perform(post("/settings/unsubscribe")
        .param("hash", userService.getHashByUID(ugnich.getUid()))
        .param("List-Unsubscribe", "One-Click")).andExpect(status().isOk());
    }
    @Test
    public void ActivityDeserialization() throws IOException {
        String followJsonStr = IOUtils.toString(new ClassPathResource("follow.json").getURI(), StandardCharsets.UTF_8);
        Follow follow = (Follow)jsonMapper.readValue(followJsonStr, Context.class);
        String personJsonStr = IOUtils.toString(new ClassPathResource("person.json").getURI(), StandardCharsets.UTF_8);
        Person person = (Person)jsonMapper.readValue(personJsonStr, Context.class);
        String undoJsonStr = IOUtils.toString(new ClassPathResource("undo.json").getURI(), StandardCharsets.UTF_8);
        Undo undo = jsonMapper.readValue(undoJsonStr, Undo.class);
        String undoFollower = (String)((Map)undo.getObject()).get("object");
        String createJsonStr = IOUtils.toString(new ClassPathResource("create.json").getURI(), StandardCharsets.UTF_8);
        Create create = jsonMapper.readValue(createJsonStr, Create.class);
        Map<String, Object> note = (Map<String, Object>) create.getObject();
        Map<String, Object> attachmentObj = (Map<String, Object> )((List<Object>) note.get("attachment")).get(0);
        String attachment = attachmentObj != null ? (String)attachmentObj.get("url") : StringUtils.EMPTY;
        String deleteJsonStr = IOUtils.toString(new ClassPathResource("delete.json").getURI(), StandardCharsets.UTF_8);
        Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class);
        int mid = messagesService.createMessage(ugnich.getUid(), "YO", "", null);
        User extUser = new User();
        extUser.setUri(URI.create("http://localhost:8080/users/xwatt"));
        int rid = messagesService.createReply(mid, 0, extUser, "PEOPLE", null);
        Message replyFromExt = messagesService.getReply(mid, rid);
        String extMessageUri = "http://localhost:8080/statuses/12345";
        messagesService.updateReplyUri(replyFromExt, URI.create(extMessageUri));
        int rid2 = messagesService.createReply(mid, rid, ugnich, "HI", null);

        Message replyToExt = messagesService.getReply(mid, rid2);
        Note replyNote = activityPubManager.makeNote(replyToExt);
        assertThat(replyNote.getInReplyTo(), equalTo(extMessageUri));
        String noteStr = IOUtils.toString(new ClassPathResource("mention.json").getURI(), StandardCharsets.UTF_8);
        Note create2 = jsonMapper.readValue(noteStr, Note.class);
        jsonMapper.readValue(IOUtils.toString(new ClassPathResource("webfinger.json").getURI(), StandardCharsets.UTF_8), Account.class);
        NodeInfo info = jsonMapper.readValue(IOUtils.toString(new ClassPathResource("xnodeinfo2.json").getURI(), StandardCharsets.UTF_8), NodeInfo.class);
        assertThat(info.getUsage().getUsers().getActiveHalfyear(), is(42));
        Like like = jsonMapper.readValue(IOUtils.toString(new ClassPathResource("like.json").getURI(), StandardCharsets.UTF_8), Like.class);
    }
    @Test
    public void activitySerialization() throws Exception {
        Message msgNoTags = commandsManager.processCommand(ugnich, "people", emptyUri).getNewMessage().get();
        String json = jsonMapper.writeValueAsString(Context.build(activityPubManager.makeNote(msgNoTags)));
        Message msg = commandsManager.processCommand(ugnich, "*shit happens", emptyUri).getNewMessage().get();
        Note note = activityPubManager.makeNote(msg);
        json = jsonMapper.writeValueAsString(Context.build(note));
        Note replyNote = new Note();
        replyNote.setId("http://localhost:8080/n/2-1");
        replyNote.setInReplyTo(activityPubManager.messageUri(msg));
        replyNote.setAttributedTo("http://localhost:8080/u/freefd");
        replyNote.setTo(Collections.singletonList(activityPubManager.personUri(ugnich)));
        replyNote.setContent("HI");
        Create create = new Create();
        create.setActor("http://localhost:8080/u/freefd");
        create.setObject(replyNote);
        signatureManager.post((Person) signatureManager.getContext(URI.create("http://localhost:8080/u/freefd")).get(),
                (Person) signatureManager.getContext(URI.create("http://localhost:8080/u/ugnich")).get(), create);
        Message replyToExt = commandsManager.processCommand(ugnich, String.format("#%d/1 PSSH YOBA ETO TI", msg.getMid()), emptyUri).getNewMessage().get();
        json = jsonMapper.writeValueAsString(Context.build(activityPubManager.makeNote(messagesService.getReply(replyToExt.getMid(), replyToExt.getRid()))));
    }
    @Test
    public void signingSpec() throws IOException {
        Person from = (Person) signatureManager.getContext(URI.create("http://localhost:8080/u/freefd")).get();
        Person to = (Person) signatureManager.getContext(URI.create("http://localhost:8080/u/ugnich")).get();
        Follow follow = new Follow();
        follow.setActor("http://localhost:8080/u/freefd");
        follow.setObject("http://localhost:8080/u/ugnich");
        signatureManager.post(from, to, follow);
    }
    @Test
    public void serviceSignatureAuth() throws Exception {
        String meUri = "/api/me";
        String testHost = "localhost:8080";
        Person ugnichPerson = (Person) signatureManager.discoverPerson("ugnich@localhost:8080").get();
        Instant now = Instant.now();
        String requestDate = DateFormattersHolder.getHttpDateFormatter().format(now);
        String signatureString = signatureManager.addSignature(ugnichPerson, testHost, "GET", meUri, requestDate);
        MvcResult me = mockMvc.perform(get("/api/me")
                .header("Host", testHost)
                .header("Date", requestDate)
                .header("Signature", signatureString))
                .andExpect(status().isOk())
                .andReturn();
        User meUser = jsonMapper.readValue(me.getResponse().getContentAsString(), User.class);
        assertThat(meUser, is(ugnich));
        String testuserResponseString = IOUtils.toString(testuserResponse.getInputStream(), StandardCharsets.UTF_8);
        ClientHttpRequestFactory originalRequestFactory = apClient.getRequestFactory();
        URI testuserUri = URI.create("https://example.com/u/testuser");
        URI testuserkeyUri = URI.create("https://example.com/u/testuser#main-key");
        MockRestServiceServer restServiceServer = MockRestServiceServer.createServer(apClient);
        restServiceServer.expect(times(3), requestTo(testuserUri))
                .andRespond(withSuccess(testuserResponseString, MediaType.APPLICATION_JSON_UTF8));
        restServiceServer.expect(times(3), requestTo(testuserkeyUri))
                .andRespond(withSuccess(testuserResponseString, MediaType.APPLICATION_JSON_UTF8));
        Person testuser = (Person)signatureManager.getContext(testuserUri).get();
        Assert.assertThat(testuser.getPublicKey().getPublicKeyPem(), is(testKeystoreManager.getPublicKeyPem()));
        Instant now2 = Instant.now();
        String testRequestDate = DateFormattersHolder.getHttpDateFormatter().format(now2);
        String inboxUri = "/api/inbox";
        String testSignatureString =
                signatureManager.addSignature(testuser, testHost, "POST",
                        inboxUri, testRequestDate, testKeystoreManager);
        mockMvc.perform(post(inboxUri)
                .header("Host", testHost)
                .header("Date", testRequestDate)
                .header("Signature", testSignatureString)
                .contentType(Context.LD_JSON_MEDIA_TYPE)
                .content(IOUtils.toByteArray(testfollowRequest.getInputStream())))
                .andExpect(status().isAccepted());
        mockMvc.perform(post(inboxUri)
                .header("Host", "wronghost")
                .header("Date", testRequestDate)
                .header("Signature", testSignatureString)
                .contentType(Context.LD_JSON_MEDIA_TYPE)
                .content(IOUtils.toByteArray(testfollowRequest.getInputStream())))
                .andExpect(status().isUnauthorized());
        apClient.setRequestFactory(originalRequestFactory);
    }
    @Test
    public void hostmeta() throws Exception {
        MvcResult result = mockMvc.perform(get("/.well-known/host-meta"))
                .andExpect(status().isOk()).andReturn();
        String xrd = result.getResponse().getContentAsString();
        result = mockMvc.perform(get("/.well-known/x-nodeinfo2"))
                .andExpect(status().isOk()).andReturn();
    }
    @Test
    public void pms() throws Exception {
        jdbcTemplate.execute("DELETE FROM pm");
        jdbcTemplate.execute("DELETE FROM bl_users");
        CommandResult res = commandsManager.processCommand(ugnich, "@freefd DICK", emptyUri);
        assertThat(res.getNewMessage(), is(Optional.empty()));
        assertThat(res.getText(), is("Private message sent"));
        MvcResult result = mockMvc.perform(get("/api/groups_pms")
        .with(httpBasic(freefdName, freefdPassword)))
                .andExpect(status().isOk())
                .andReturn();
        PrivateChats chats = jsonMapper.readValue(result.getResponse().getContentAsString(), PrivateChats.class);
        assertThat(chats.getUsers().size(), is(1));
    }
    @Test
    public void seenTests() {
        Instant now = Instant.now();
        int newUserUid = userService.createUser("newuser", "assword");
        assertThat(userService.getUserByUID(newUserUid).get().getSeen(), is(nullValue()));
        messagesService.createMessage(newUserUid, "YO", "", null);
        assertThat(userService.getUserByUID(newUserUid).get().getSeen(), greaterThanOrEqualTo(now));
    }
    @Test
    public void signupTest() throws Exception {
        emailService.addVerificationCode(null, "demo@email.com", "123456");
        MvcResult result = mockMvc.perform(post("/api/signup")
        .param("username", "testuser")
        .param("password", "demopassword")
        .param("verificationCode", "123456")).andExpect(status().isOk()).andReturn();
        com.juick.User testuser = jsonMapper.readValue(result.getResponse().getContentAsString(), User.class);
        assertThat(testuser.getName(), is("testuser"));
    }
    @Test
    public void doNotAllowRepliesToNonExistingReplyAndNotAllowToEditTagsOfComment() throws Exception {
        assertThat(commandsManager.processCommand(ugnich, "#23213213/2 BAD COMMENT", emptyUri).getText(), is("Message not found"));
        Message msg = commandsManager.processCommand(ugnich, "YO", emptyUri).getNewMessage().get();
        assertThat(commandsManager.processCommand(ugnich, String.format("#%d/1 BAD COMMENT", msg.getMid()), emptyUri).getText(), is("Reply not found"));
        CommandResult result = commandsManager.processCommand(freefd,
                String.format("#%d *GOOD *COMMENT", msg.getMid()), emptyUri);
        Message reply = result.getNewMessage().get();
        assertThat(commandsManager.processCommand(ugnich, String.format("#%d/%d *GOOD *BAD", reply.getMid(), reply.getRid()), emptyUri).getText(), startsWith("Reply posted"));
    }

    @Test
    public void XMPPSignupIsDisabled() throws Exception {
        jdbcTemplate.update("INSERT INTO jids(loginhash, jid) VALUES('1', 'test@jid.tld')");
        MvcResult formLoginResult = mockMvc.perform(post("/login")
                .param("username", ugnichName)
                .param("password", ugnichPassword))
                .andExpect(status().is3xxRedirection()).andReturn();
        Cookie rememberMeFromForm = formLoginResult.getResponse().getCookie("juick-remember-me");
        mockMvc.perform(post("/signup")
                .cookie(rememberMeFromForm)
                .param("hash", "1")
                .param("type", "xmpp")
                .param("action", "link"))
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("XMPP support is disabled")));
    }
    @Test
    public void ActivityPubLikesShouldNotMirrorMessage() throws Exception {
        jdbcTemplate.execute("DELETE FROM messages");
        Message msg = commandsManager.processCommand(ugnich, "YO", emptyUri).getNewMessage().get();
        messagesService.likeMessage(msg.getMid(), freefd.getUid(), Reaction.LIKE);
        messagesService.likeMessage(msg.getMid(), 0, Reaction.LIKE, "http://localhost:8080/u/test");
        jdbcTemplate.update("UPDATE messages SET popular=1 where message_id=?", msg.getMid());
        List<Message> top = messagesService.getMessages(ugnich, messagesService.getPopular(ugnich.getUid(), 0));
        assertThat(top.size(), is(1));
    }
    @Test
    public void verifiedUsersTest(){
        assertThat(userService.getUserByName("ugnich").isVerified(), is(false));
        jdbcTemplate.update("INSERT INTO facebook(user_id, fb_id) VALUES(?, ?)", ugnich.getUid(), "100001866137681");
        assertThat(userService.getUserByName("ugnich").isVerified(), is(true));
    }
    @Test
    public void avatarUploadOverApi() throws Exception {
        ClassPathResource defaultAvatar = new ClassPathResource("static/av-96.png");
        String hash = DigestUtils.md5DigestAsHex(IOUtils.toByteArray(defaultAvatar.getInputStream()));
        assertThat(webApp.getAvatarUrl(userService.getUserByName(freefdName)), is(String.format("http://localhost:8080/av-96-%s.png", hash)));

        ClassPathResource newAvatar = new ClassPathResource("static/durov.png");
        byte[] newAvatarData = IOUtils.toByteArray(newAvatar.getInputStream());
        mockMvc.perform(MockMvcRequestBuilders.multipart("/api/me/upload")
                .file("avatar", newAvatarData)
        .with(httpBasic(freefdName, freefdPassword))
        ).andExpect(status().isOk());
        String newHash = DigestUtils.md5DigestAsHex(newAvatarData);
        URI newUri = Paths.get(imgDir, "ao", String.format("%d.png", freefd.getUid())).toUri();
        assertThat(DigestUtils.md5DigestAsHex(IOUtils.toByteArray(newUri)), is(newHash));
    }
    @Test
    public void varyMvcResponse() throws Exception {
        mockMvc.perform(get("/"))
                .andExpect(status().isOk())
                .andExpect(header().string("Vary", "Accept-Language"));
        mockMvc.perform(get("/rss/ugnich/blog"))
                .andExpect(status().isOk())
                .andExpect(header().string("Vary", "Accept-Language"));
        mockMvc.perform(get("/api/messages"))
                .andExpect(status().isOk())
                .andExpect(header().string("Vary", "Accept-Language"));
    }
    @Test
    public void apiInfo() throws Exception {
        userService.createUser("tst", "tst");
        MvcResult result = mockMvc.perform(get("/api/info/tst"))
                .andExpect(status().isOk())
                .andReturn();
        User tst = jsonMapper.readValue(result.getResponse().getContentAsString(), User.class);
        assertThat(tst.getReaders(), is(nullValue()));
        commandsManager.processCommand(ugnich, "S @tst", emptyUri);
        result = mockMvc.perform(get("/api/info/tst"))
                .andExpect(status().isOk())
                .andReturn();
        tst = jsonMapper.readValue(result.getResponse().getContentAsString(), User.class);
        assertThat(tst.getReaders().size(), is(1));
    }
    @Test
    public void federatedUserDeletionFlow() throws Exception {
        String deleteJsonStr = IOUtils.toString(new ClassPathResource("delete_user.json").getURI(), StandardCharsets.UTF_8);
        Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class);
        ClientHttpRequestFactory originalRequestFactory = apClient.getRequestFactory();
        MockRestServiceServer restServiceServer = MockRestServiceServer.createServer(apClient);
        restServiceServer.expect(times(2), requestTo((String) delete.getObject()))
                .andRespond(withStatus(HttpStatus.GONE));
        restServiceServer.expect(requestTo((String) delete.getObject()))
                .andRespond(response -> {
                    throw new ResourceAccessException("Connection reset");
                });
        mockMvc.perform(post("/api/inbox")
                .contentType(ACTIVITY_MEDIA_TYPE)
                .content(deleteJsonStr))
                .andExpect(status().isAccepted());
        mockMvc.perform(post("/api/inbox")
                .contentType(ACTIVITY_MEDIA_TYPE)
                .content(deleteJsonStr)
                .header("Signature", "keyId=\"https://example.com/users/deleted#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date digest content-type\",signature=\"wHoU91JJBsIYcR1W1/57B0oG98t5Aa/TvGPw1B8KQlAp5KhpePnOzD1MZRgivBx7YKO6eYwDx+AX9dn6tjlAvzRLygv21H6UoDZFihWzeE1HM8pY2Pe4EhUgYBN0YuiKUi7W4TS9bDRAJ5vGNPUWATe+2o5Jcbux5cZYXFKKYbLBLD+/IlqPdHA2IXLZ52HFVVfBkPH5sSklV6XJtD/PHLK9R/I9w/mUpj9moUPQu44rR7KvxiGNuHla3vfDtJbkBqLMdScX91EG8373AulXPUiCCF7R2lJB0fFQedm2nSbcwBoJ32GEyOyOPFgPKG5zd9Fd5TfB1pmA8ZIE0sChfA==\""))
                .andExpect(status().isAccepted());
        apClient.setRequestFactory(originalRequestFactory);
    }
    @Test
    public void handleIncorrectCertificates() throws Exception {
        String deleteJsonStr = IOUtils.toString(new ClassPathResource("delete_user.json").getURI(), StandardCharsets.UTF_8);
        Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class);
        ClientHttpRequestFactory originalRequestFactory = apClient.getRequestFactory();
        MockRestServiceServer restServiceServer = MockRestServiceServer.createServer(apClient);
        restServiceServer.expect(requestTo((String) delete.getObject()))
                .andRespond(response -> {
                    throw new ResourceAccessException("Connection reset");
                });
        mockMvc.perform(post("/api/inbox")
                .contentType(ACTIVITY_MEDIA_TYPE)
                .content(deleteJsonStr)
                .header("Signature", "keyId=\"https://example.com/users/deleted#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date digest content-type\",signature=\"wHoU91JJBsIYcR1W1/57B0oG98t5Aa/TvGPw1B8KQlAp5KhpePnOzD1MZRgivBx7YKO6eYwDx+AX9dn6tjlAvzRLygv21H6UoDZFihWzeE1HM8pY2Pe4EhUgYBN0YuiKUi7W4TS9bDRAJ5vGNPUWATe+2o5Jcbux5cZYXFKKYbLBLD+/IlqPdHA2IXLZ52HFVVfBkPH5sSklV6XJtD/PHLK9R/I9w/mUpj9moUPQu44rR7KvxiGNuHla3vfDtJbkBqLMdScX91EG8373AulXPUiCCF7R2lJB0fFQedm2nSbcwBoJ32GEyOyOPFgPKG5zd9Fd5TfB1pmA8ZIE0sChfA==\""))
                .andExpect(status().isAccepted());
        apClient.setRequestFactory(originalRequestFactory);
    }
}