aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
blob: e0f9b840c2b665484e0b6271ec57007c0e78d7ff (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
package com.juick.api.tests;

import com.juick.Message;
import com.juick.Tag;
import com.juick.User;
import com.juick.api.configuration.ApiAppConfiguration;
import com.juick.api.configuration.ApiMvcConfiguration;
import com.juick.api.configuration.ApiSecurityConfig;
import com.juick.configuration.DataConfiguration;
import com.juick.server.helpers.TagStats;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
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.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.inject.Inject;
import java.util.*;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Created by vitalyster on 25.11.2016.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class MessagesTests {
    @Configuration
    @Import(value = {ApiMvcConfiguration.class, ApiAppConfiguration.class, ApiSecurityConfig.class, DataConfiguration.class})
    static class Config {
        @Bean
        @Primary
        MessagesService messagesService() {
            return Mockito.mock(MessagesService.class);
        }

        @Bean
        @Primary
        UserService userService() {
            return Mockito.mock(UserService.class);
        }

        @Bean
        @Primary
        TagService tagService() {
            return Mockito.mock(TagService.class);
        }
    }

    private MockMvc mockMvc;
    @Inject
    private WebApplicationContext webApplicationContext;

    @Inject
    private MessagesService messagesService;
    @Inject
    private UserService userService;
    @Inject
    private TagService tagService;

    private User ugnich, freefd;
    String ugnichName, ugnichPassword, freefdName, freefdPassword;

    private static Message getMessage(final User user, final String messageText) {
        Message msg = new Message();

        msg.setMid(1);
        msg.setUser(user);
        msg.setText(messageText == null ? RandomStringUtils.randomAlphanumeric(24) : messageText);
        msg.setTags(Collections.singletonList(new Tag(RandomStringUtils.randomAlphabetic(4))));

        return msg;
    }

    private static User getUser(final int uid, final String name, final String password) {
        User user = new User();

        user.setName(name);
        user.setUid(uid);
        user.setCredentials(password);
        user.setBanned(false);

        return user;
    }

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .apply(SecurityMockMvcConfigurers.springSecurity())
                .dispatchOptions(true)
                .build();
        ugnichName = "ugnich";
        ugnichPassword = "MyPassw0rd!";
        freefdName = "freefd";
        freefdPassword = "MyPassw0rd!";

        ugnich = getUser(1, ugnichName, ugnichPassword);
        freefd = getUser(2, freefdName, freefdPassword);

        List<String> users = new ArrayList<>(2);
        users.add(ugnichName);
        users.add(freefdName);

        when(userService.getUsersByName(users))
                .thenReturn(Arrays.asList(ugnich, freefd));
        when(userService.getUserByName(ugnichName))
                .thenReturn(ugnich);
        when(userService.getFullyUserByName(ugnichName))
                .thenReturn(ugnich);
        when(userService.getUserByName(null))
                .thenReturn(new User());
    }

    @Test
    public void testAllUnAuthorized() throws Exception {

        mockMvc.perform(get("/"))
                .andExpect(status().is4xxClientError());

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

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

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

    @Test
    public void homeTestWithMessages() throws Exception {
        String msgText = "Привет, я - Угнич";

        Message msg = getMessage(ugnich, msgText);


        when(messagesService.getMyFeed(1, 0))
                .thenReturn(Collections.singletonList(1));
        when(messagesService.getMessages(Collections.singletonList(1)))
                .thenReturn(Collections.singletonList(msg));

        mockMvc.perform(
                get("/home")
                        .with(httpBasic(ugnichName, ugnichPassword)))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$[0].mid", is(1)))
                .andExpect(jsonPath("$[0].body", is(msgText)));
    }

    @Test
    public void homeTestWithMessagesAndRememberMe() throws Exception {
        String ugnichName = "ugnich";
        String uginchPassword = "MyPassw0rd!";
        String msgText = "Привет, я - Угнич";
        String hash = "12345678";

        User user = getUser(1, ugnichName, uginchPassword);
        Message msg = getMessage(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);

        mockMvc.perform(
                get("/home")
                        .with(httpBasic(ugnichName, uginchPassword)))
                .andExpect(status().isOk())
                .andReturn();

        when(userService.getUserByHash(hash))
                .thenReturn(user);

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

    @Test
    public void homeTestWithMEssagesAndSimpleCors() throws Exception {
        String ugnichName = "ugnich";
        String uginchPassword = "MyPassw0rd!";

        User user = getUser(1, ugnichName, uginchPassword);
        Message msg = getMessage(user, null);

        when(userService.getFullyUserByName(ugnichName))
                .thenReturn(user);
        when(userService.getUserByName(ugnichName))
                .thenReturn(user);
        when(messagesService.getMyFeed(1, 0))
                .thenReturn(Collections.singletonList(1));
        when(messagesService.getMessages(Collections.singletonList(1)))
                .thenReturn(Collections.singletonList(msg));

        mockMvc.perform(
                get("/home")
                        .with(httpBasic(ugnichName, uginchPassword))
                        .header("Origin", "http://api.example.net"))
                .andExpect(status().isOk())
                .andExpect(header().string("Access-Control-Allow-Origin", "*"));
    }

    @Test
    public void homeTestWithPreflightCors() throws Exception {
        String ugnichName = "ugnich";
        String uginchPassword = "MyPassw0rd!";

        User user = getUser(1, ugnichName, uginchPassword);
        when(userService.getFullyUserByName(ugnichName))
                .thenReturn(user);

        mockMvc.perform(
                options("/home")
                        .with(httpBasic(ugnichName, uginchPassword))
                        .header("Origin", "http://api.example.net")
                        .header("Access-Control-Request-Method", "POST")
                        .header("Access-Control-Request-Headers", "X-PINGOTHER, Content-Type"))
                .andExpect(status().isOk())
                .andExpect(header().string("Access-Control-Allow-Origin", "*"))
                .andExpect(header().string("Access-Control-Allow-Methods", "POST,GET,PUT,OPTIONS,DELETE"))
                .andExpect(header().string("Access-Control-Allow-Headers", "X-PINGOTHER, Content-Type"));
    }

    @Test
    public void anonymousApis() throws Exception {


        mockMvc.perform(get("/messages"))
                .andExpect(status().isOk());

        mockMvc.perform(get("/users")
                .param("uname", "ugnich")
                .param("uname", "freefd"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("$", hasSize(2)));
    }
    @Test
    public void tags() throws Exception {
        Tag weather = new Tag("weather");
        TagStats sw = new TagStats();
        sw.setTag(weather);
        sw.setUsageCount(15);
        Tag yo = new Tag("yo");
        TagStats sy = new TagStats();
        sy.setTag(yo);
        sy.setUsageCount(5);
        when(tagService.getUserTagStats(1)).thenReturn(Collections.singletonList(sy));
        when(tagService.getTagStats()).thenReturn(Arrays.asList(sy, sw));
        mockMvc.perform(get("/tags"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$", hasSize(2)));
        mockMvc.perform(get("/tags")
                .param("user_id", "1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$[0].messages", is(5)));
    }
}