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
|
package com.juick.www;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.juick.Message;
import com.juick.User;
import com.juick.configuration.MockDataConfiguration;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import com.juick.tests.util.MockUtils;
import com.juick.www.configuration.SapeConfiguration;
import com.juick.www.configuration.WwwAppConfiguration;
import com.juick.www.configuration.WwwServletConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
import org.springframework.web.context.WebApplicationContext;
import javax.inject.Inject;
import java.io.IOException;
import java.util.Collections;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
/**
* Created by vitalyster on 12.01.2017.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
public class WebAppTests {
@Configuration
@Import(value = {
WwwServletConfiguration.class, WwwAppConfiguration.class, SapeConfiguration.class,
MockDataConfiguration.class
})
static class Config {}
@Inject
private WebApplicationContext wac;
@Inject
private WebApp webApp;
private WebClient webClient;
@Inject
UserService userService;
@Inject
MessagesService messagesService;
@Before
public void setup() {
webClient = MockMvcWebClientBuilder.webAppContextSetup(this.wac).build();
webClient.getOptions().setJavaScriptEnabled(false);
}
@Test
public void postWithoutTagsShouldNotHaveAsteriskInTitle() throws Exception {
String ugnichName = "ugnich";
String ugnichPassword = "MyPassw0rd!";
String msgText = "Привет, я - Угнич";
String hash = "12345678";
User user = MockUtils.mockUser(1, ugnichName, ugnichPassword);
Message msg = MockUtils.mockMessage(1, user, msgText);
when(userService.getUIDbyName(ugnichName))
.thenReturn(1);
when(userService.getUserByName(ugnichName))
.thenReturn(user);
when(userService.getUserByUID(1))
.thenReturn(Optional.of(user));
when(userService.getFullyUserByName(ugnichName))
.thenReturn(user);
when(messagesService.getMyFeed(1, 0))
.thenReturn(Collections.singletonList(1));
when(messagesService.getMessages(Collections.singletonList(1)))
.thenReturn(Collections.singletonList(msg));
when(userService.getUIDbyHash(hash))
.thenReturn(1);
when(messagesService.getMessageAuthor(1)).thenReturn(user);
when(messagesService.canViewThread(1, 0)).thenReturn(true);
when(messagesService.getMessage(1)).thenReturn(msg);
HtmlPage threadPage = webClient.getPage("http://localhost:8080/ugnich/1");
assertThat(threadPage.getTitleText(), equalTo("ugnich:"));
}
@Test
public void bannedUserBlogandPostShouldReturn404() throws IOException {
String userName = "isilmine";
String userPassword = "secret";
String msgText = "автор этого поста был забанен";
String hash = "12345678";
User user = MockUtils.mockUser(2, userName, userPassword);
user.setBanned(true);
Message msg = MockUtils.mockMessage(2, user, msgText);
when(userService.getUIDbyName(userName))
.thenReturn(2);
when(userService.getUserByName(userName))
.thenReturn(user);
when(userService.getUserByUID(2))
.thenReturn(Optional.of(user));
when(userService.getFullyUserByName(userName))
.thenReturn(user);
when(messagesService.getMyFeed(2, 0))
.thenReturn(Collections.singletonList(2));
when(messagesService.getMessages(Collections.singletonList(2)))
.thenReturn(Collections.singletonList(msg));
when(userService.getUIDbyHash(hash))
.thenReturn(1);
when(messagesService.getMessageAuthor(2)).thenReturn(user);
when(messagesService.canViewThread(2, 0)).thenReturn(true);
when(messagesService.getMessage(2)).thenReturn(msg);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
Page blogPage = webClient.getPage("http://localhost:8080/isilmine");
Page threadPage = webClient.getPage("http://localhost:8080/isilmine/2");
assertThat(blogPage.getWebResponse().getStatusCode(), equalTo(404));
assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(404));
}
}
|