/* * 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 . */ package com.juick.api.tests; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.Message; import com.juick.Tag; import com.juick.User; import com.juick.api.configuration.ApiAppConfiguration; import com.juick.api.tests.configuration.MockStorageConfiguration; import com.juick.configuration.RepositoryConfiguration; import com.juick.service.ImagesService; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.util.DateFormattersHolder; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; 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.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import javax.inject.Inject; import java.util.Arrays; import java.util.Collections; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; /** * Created by vitalyster on 25.11.2016. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {RepositoryConfiguration.class, ApiAppConfiguration.class, MockStorageConfiguration.class}) @WebAppConfiguration public class MessagesTests extends AbstractJUnit4SpringContextTests { private MockMvc mockMvc; @Inject private WebApplicationContext webApplicationContext; @Inject private MessagesService messagesService; @Inject private UserService userService; @Inject private TagService tagService; @Inject private ObjectMapper jsonMapper; @Inject private ImagesService imagesService; private static User ugnich, freefd; static String ugnichName, ugnichPassword, freefdName, freefdPassword; static Message msg; private static boolean isSetUp = false; @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) .apply(SecurityMockMvcConfigurers.springSecurity()) .dispatchOptions(true) .build(); if (!isSetUp) { ugnichName = "ugnich"; ugnichPassword = "MyPassw0rd!"; freefdName = "freefd"; freefdPassword = "MyPassw0rd!"; 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); String msgText = "Привет, я - Угнич"; int mid = messagesService.createMessage(ugnich.getUid(), msgText, "png", null); msg = messagesService.getMessage(mid); isSetUp = true; } } @Test public void testAllUnAuthorized() throws Exception { mockMvc.perform(get("/")) .andExpect(status().is4xxClientError()); mockMvc.perform(get("/auth")) .andExpect(status().is4xxClientError()); mockMvc.perform(get("/home")) .andExpect(status().is4xxClientError()); mockMvc.perform(get("/messages/recommended")) .andExpect(status().is4xxClientError()); mockMvc.perform(get("/messages/set_privacy")) .andExpect(status().is4xxClientError()); } @Test public void homeTestWithMessages() throws Exception { mockMvc.perform( get("/home") .with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$[-1].mid", is(msg.getMid()))) .andExpect(jsonPath("$[-1].timestamp", is(DateFormattersHolder.getMessageFormatterInstance().format(msg.getTimestamp())))) .andExpect(jsonPath("$[-1].body", is(msg.getText()))) .andExpect(jsonPath("$[-1].attachment.url", is("https://i.juick.com/p/1.png"))) .andExpect(jsonPath("$[-1].attachment.small.url", is("https://i.juick.com/photos-512/1.png"))); } @Test public void homeTestWithMessagesAndRememberMe() throws Exception { String ugnichHash = userService.getHashByUID(ugnich.getUid()); mockMvc.perform( get("/home") .with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()) .andReturn(); mockMvc.perform(get("/home") .param("hash", ugnichHash)) .andExpect(status().isOk()); } @Test public void homeTestWithMessagesAndSimpleCors() throws Exception { mockMvc.perform( get("/home") .with(httpBasic(ugnichName, ugnichPassword)) .header("Origin", "http://api.example.net")) .andExpect(status().isOk()) .andExpect(header().string("Access-Control-Allow-Origin", "*")); } @Test public void homeTestWithPreflightCors() throws Exception { mockMvc.perform( options("/home") .with(httpBasic(ugnichName, ugnichPassword)) .header("Origin", "http://api.example.net") .header("Access-Control-Request-Method", "POST") .header("Access-Control-Request-Headers", "X-PINGOTHER, Content-Type")) .andExpect(status().isOk()) .andExpect(header().string("Access-Control-Allow-Origin", "*")) .andExpect(header().string("Access-Control-Allow-Methods", "POST,GET,PUT,OPTIONS,DELETE")) .andExpect(header().string("Access-Control-Allow-Headers", "X-PINGOTHER, Content-Type")); } @Test public void anonymousApis() throws Exception { mockMvc.perform(get("/messages")) .andExpect(status().isOk()); mockMvc.perform(get("/users") .param("uname", "ugnich") .param("uname", "freefd")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$", hasSize(2))); } @Test public void tags() throws Exception { Tag weather = tagService.getTag("weather", true); Tag yo = tagService.getTag("yo", true); messagesService.createMessage(ugnich.getUid(), "text", null, Arrays.asList(yo, weather)); messagesService.createMessage(freefd.getUid(), "text2", null, Collections.singletonList(yo)); mockMvc.perform(get("/tags")) .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(2))) .andExpect(jsonPath("$[0].messages", is(2))); mockMvc.perform(get("/tags") .param("user_id", String.valueOf(ugnich.getUid()))) .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(2))) .andExpect(jsonPath("$[0].messages", is(1))); } @Test public void postWithReferer() throws Exception { mockMvc.perform(post("/post") .param("body", "yo") .with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()); } @Test public void threadWithEphemeralNumberShouldReturn404() throws Exception { mockMvc.perform(get("/thread").param("mid", "999999999") .with(httpBasic(ugnichName, ugnichPassword))).andExpect(status().is4xxClientError()); } @Test public void performRequestsWithIssuedToken() throws Exception { String ugnichHash = userService.getHashByUID(ugnich.getUid()); mockMvc.perform(get("/home")).andExpect(status().isUnauthorized()); mockMvc.perform(get("/auth")) .andExpect(status().isUnauthorized()); mockMvc.perform(get("/auth").with(httpBasic(ugnichName, "wrongpassword"))) .andExpect(status().isUnauthorized()); MvcResult result = mockMvc.perform(get("/auth").with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()) .andReturn(); String authHash = jsonMapper.readValue(result.getResponse().getContentAsString(), String.class); assertThat(authHash, equalTo(ugnichHash)); mockMvc.perform(get("/home").param("hash", ugnichHash)).andExpect(status().isOk()); } }