2017-08-08 10:01:06 +07:00
|
|
|
package io.spring.api;
|
|
|
|
|
2017-08-24 09:39:09 +07:00
|
|
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
|
|
|
import io.spring.JacksonCustomizations;
|
|
|
|
import io.spring.api.security.WebSecurityConfig;
|
|
|
|
import io.spring.application.UserQueryService;
|
2017-08-18 16:08:27 +07:00
|
|
|
import io.spring.application.data.UserData;
|
2017-08-24 09:39:09 +07:00
|
|
|
import io.spring.core.service.JwtService;
|
2017-08-08 10:01:06 +07:00
|
|
|
import io.spring.core.user.User;
|
|
|
|
import io.spring.core.user.UserRepository;
|
2017-08-24 09:39:09 +07:00
|
|
|
import io.spring.infrastructure.mybatis.readservice.UserReadService;
|
2017-08-24 12:35:28 +07:00
|
|
|
import io.spring.infrastructure.service.NaiveEncryptService;
|
2017-08-08 10:01:06 +07:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2017-10-27 12:30:26 +07:00
|
|
|
import org.junit.runner.RunWith;
|
2017-08-24 09:39:09 +07:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
2017-08-08 10:01:06 +07:00
|
|
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
2017-08-24 09:39:09 +07:00
|
|
|
import org.springframework.context.annotation.Import;
|
2017-10-27 12:30:26 +07:00
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
2017-08-24 09:39:09 +07:00
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
2017-08-08 10:01:06 +07:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
2017-08-24 09:39:09 +07:00
|
|
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
2017-08-08 10:01:06 +07:00
|
|
|
import static org.hamcrest.core.IsEqual.equalTo;
|
2018-05-10 13:50:44 +07:00
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
2017-08-08 10:01:06 +07:00
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
2017-10-27 12:30:26 +07:00
|
|
|
@RunWith(SpringRunner.class)
|
2017-08-24 09:39:09 +07:00
|
|
|
@WebMvcTest(UsersApi.class)
|
|
|
|
@Import({WebSecurityConfig.class, UserQueryService.class, NaiveEncryptService.class, JacksonCustomizations.class})
|
2017-08-25 10:34:41 +07:00
|
|
|
public class UsersApiTest {
|
2017-08-24 09:39:09 +07:00
|
|
|
@Autowired
|
|
|
|
private MockMvc mvc;
|
2017-08-08 10:01:06 +07:00
|
|
|
|
|
|
|
@MockBean
|
|
|
|
private UserRepository userRepository;
|
|
|
|
|
|
|
|
@MockBean
|
|
|
|
private JwtService jwtService;
|
|
|
|
|
|
|
|
@MockBean
|
|
|
|
private UserReadService userReadService;
|
2017-08-14 10:09:11 +07:00
|
|
|
private String defaultAvatar;
|
2017-08-08 10:01:06 +07:00
|
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
2017-08-24 09:39:09 +07:00
|
|
|
RestAssuredMockMvc.mockMvc(mvc);
|
2017-08-14 10:09:11 +07:00
|
|
|
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
2017-08-08 10:01:06 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_create_user_success() throws Exception {
|
|
|
|
String email = "john@jacob.com";
|
|
|
|
String username = "johnjacob";
|
|
|
|
|
|
|
|
when(jwtService.toToken(any())).thenReturn("123");
|
2017-08-18 16:08:27 +07:00
|
|
|
User user = new User(email, username, "123", "", defaultAvatar);
|
|
|
|
UserData userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
|
|
|
when(userReadService.findById(any())).thenReturn(userData);
|
2017-08-08 10:01:06 +07:00
|
|
|
|
2017-08-08 20:14:14 +07:00
|
|
|
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.empty());
|
|
|
|
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.empty());
|
|
|
|
|
|
|
|
Map<String, Object> param = prepareRegisterParameter(email, username);
|
2017-08-08 10:01:06 +07:00
|
|
|
|
|
|
|
given()
|
|
|
|
.contentType("application/json")
|
|
|
|
.body(param)
|
|
|
|
.when()
|
|
|
|
.post("/users")
|
|
|
|
.then()
|
|
|
|
.statusCode(201)
|
|
|
|
.body("user.email", equalTo(email))
|
|
|
|
.body("user.username", equalTo(username))
|
|
|
|
.body("user.bio", equalTo(""))
|
2017-08-14 10:09:11 +07:00
|
|
|
.body("user.image", equalTo(defaultAvatar))
|
2017-08-08 10:01:06 +07:00
|
|
|
.body("user.token", equalTo("123"));
|
|
|
|
|
|
|
|
verify(userRepository).save(any());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_show_error_message_for_blank_username() throws Exception {
|
|
|
|
|
|
|
|
String email = "john@jacob.com";
|
|
|
|
String username = "";
|
|
|
|
|
2017-08-08 20:14:14 +07:00
|
|
|
Map<String, Object> param = prepareRegisterParameter(email, username);
|
2017-08-08 10:01:06 +07:00
|
|
|
|
|
|
|
given()
|
|
|
|
.contentType("application/json")
|
|
|
|
.body(param)
|
|
|
|
.when()
|
|
|
|
.post("/users")
|
|
|
|
.then()
|
|
|
|
.statusCode(422)
|
|
|
|
.body("errors.username[0]", equalTo("can't be empty"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_show_error_message_for_invalid_email() throws Exception {
|
|
|
|
String email = "johnxjacob.com";
|
|
|
|
String username = "johnjacob";
|
|
|
|
|
2017-08-08 20:14:14 +07:00
|
|
|
Map<String, Object> param = prepareRegisterParameter(email, username);
|
2017-08-08 10:01:06 +07:00
|
|
|
|
|
|
|
given()
|
|
|
|
.contentType("application/json")
|
|
|
|
.body(param)
|
|
|
|
.when()
|
|
|
|
.post("/users")
|
|
|
|
.then()
|
|
|
|
.statusCode(422)
|
|
|
|
.body("errors.email[0]", equalTo("should be an email"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_show_error_for_duplicated_username() throws Exception {
|
|
|
|
String email = "john@jacob.com";
|
|
|
|
String username = "johnjacob";
|
|
|
|
|
|
|
|
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(new User(
|
|
|
|
email, username, "123", "bio", ""
|
|
|
|
)));
|
2017-08-18 16:08:27 +07:00
|
|
|
when(userRepository.findByEmail(any())).thenReturn(Optional.empty());
|
2017-08-08 10:01:06 +07:00
|
|
|
|
2017-08-08 20:14:14 +07:00
|
|
|
Map<String, Object> param = prepareRegisterParameter(email, username);
|
2017-08-08 10:01:06 +07:00
|
|
|
|
|
|
|
given()
|
|
|
|
.contentType("application/json")
|
|
|
|
.body(param)
|
|
|
|
.when()
|
|
|
|
.post("/users")
|
|
|
|
.then()
|
|
|
|
.statusCode(422)
|
|
|
|
.body("errors.username[0]", equalTo("duplicated username"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_show_error_for_duplicated_email() throws Exception {
|
|
|
|
String email = "john@jacob.com";
|
|
|
|
String username = "johnjacob2";
|
|
|
|
|
|
|
|
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(new User(
|
|
|
|
email, username, "123", "bio", ""
|
|
|
|
)));
|
|
|
|
|
|
|
|
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.empty());
|
|
|
|
|
2017-08-08 20:14:14 +07:00
|
|
|
Map<String, Object> param = prepareRegisterParameter(email, username);
|
2017-08-08 10:01:06 +07:00
|
|
|
|
|
|
|
given()
|
|
|
|
.contentType("application/json")
|
|
|
|
.body(param)
|
|
|
|
.when()
|
|
|
|
.post("/users")
|
|
|
|
.then()
|
|
|
|
.statusCode(422)
|
|
|
|
.body("errors.email[0]", equalTo("duplicated email"));
|
|
|
|
}
|
2017-08-08 20:14:14 +07:00
|
|
|
|
|
|
|
private HashMap<String, Object> prepareRegisterParameter(final String email, final String username) {
|
|
|
|
return new HashMap<String, Object>() {{
|
|
|
|
put("user", new HashMap<String, Object>() {{
|
|
|
|
put("email", email);
|
|
|
|
put("password", "johnnyjacob");
|
|
|
|
put("username", username);
|
|
|
|
}});
|
|
|
|
}};
|
|
|
|
}
|
2017-08-14 10:09:11 +07:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_login_success() throws Exception {
|
|
|
|
String email = "john@jacob.com";
|
|
|
|
String username = "johnjacob2";
|
|
|
|
String password = "123";
|
|
|
|
|
|
|
|
User user = new User(email, username, password, "", defaultAvatar);
|
2017-08-14 13:27:36 +07:00
|
|
|
UserData userData = new UserData("123", email, username, "", defaultAvatar);
|
2017-08-14 10:09:11 +07:00
|
|
|
|
|
|
|
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
|
2017-08-15 09:47:18 +07:00
|
|
|
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
|
2017-08-18 16:08:27 +07:00
|
|
|
when(userReadService.findById(eq(user.getId()))).thenReturn(userData);
|
2017-08-14 10:09:11 +07:00
|
|
|
when(jwtService.toToken(any())).thenReturn("123");
|
|
|
|
|
|
|
|
Map<String, Object> param = new HashMap<String, Object>() {{
|
|
|
|
put("user", new HashMap<String, Object>() {{
|
|
|
|
put("email", email);
|
|
|
|
put("password", password);
|
|
|
|
}});
|
|
|
|
}};
|
|
|
|
|
|
|
|
given()
|
|
|
|
.contentType("application/json")
|
|
|
|
.body(param)
|
|
|
|
.when()
|
|
|
|
.post("/users/login")
|
|
|
|
.then()
|
|
|
|
.statusCode(200)
|
|
|
|
.body("user.email", equalTo(email))
|
|
|
|
.body("user.username", equalTo(username))
|
|
|
|
.body("user.bio", equalTo(""))
|
|
|
|
.body("user.image", equalTo(defaultAvatar))
|
|
|
|
.body("user.token", equalTo("123"));;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_fail_login_with_wrong_password() throws Exception {
|
|
|
|
String email = "john@jacob.com";
|
|
|
|
String username = "johnjacob2";
|
|
|
|
String password = "123";
|
|
|
|
|
|
|
|
User user = new User(email, username, password, "", defaultAvatar);
|
2017-08-14 13:27:36 +07:00
|
|
|
UserData userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
2017-08-14 10:09:11 +07:00
|
|
|
|
|
|
|
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
|
2017-08-15 09:47:18 +07:00
|
|
|
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
|
2017-08-14 10:09:11 +07:00
|
|
|
|
|
|
|
Map<String, Object> param = new HashMap<String, Object>() {{
|
|
|
|
put("user", new HashMap<String, Object>() {{
|
|
|
|
put("email", email);
|
|
|
|
put("password", "123123");
|
|
|
|
}});
|
|
|
|
}};
|
|
|
|
|
|
|
|
given()
|
|
|
|
.contentType("application/json")
|
|
|
|
.body(param)
|
|
|
|
.when()
|
|
|
|
.post("/users/login")
|
|
|
|
.then()
|
|
|
|
.statusCode(422)
|
|
|
|
.body("errors.password[0]", equalTo("invalid email or password"));
|
|
|
|
}
|
2017-10-27 12:30:26 +07:00
|
|
|
}
|