register api
This commit is contained in:
16
src/test/java/io/spring/RealworldApplicationTests.java
Normal file
16
src/test/java/io/spring/RealworldApplicationTests.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package io.spring;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class RealworldApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
186
src/test/java/io/spring/api/UsersApiTest.java
Normal file
186
src/test/java/io/spring/api/UsersApiTest.java
Normal file
@@ -0,0 +1,186 @@
|
||||
package io.spring.api;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.spring.application.JwtService;
|
||||
import io.spring.application.user.UserData;
|
||||
import io.spring.application.user.UserReadService;
|
||||
import io.spring.core.user.User;
|
||||
import io.spring.core.user.UserRepository;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@RunWith(SpringRunner.class)
|
||||
public class UsersApiTest {
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@MockBean
|
||||
private UserRepository userRepository;
|
||||
|
||||
@MockBean
|
||||
private JwtService jwtService;
|
||||
|
||||
@MockBean
|
||||
private UserReadService userReadService;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
RestAssured.port = port;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_create_user_success() throws Exception {
|
||||
String email = "john@jacob.com";
|
||||
String username = "johnjacob";
|
||||
|
||||
when(jwtService.toToken(any())).thenReturn("123");
|
||||
UserData userData = new UserData(email, username, "", "https://static.productionready.io/images/smiley-cyrus.jpg");
|
||||
when(userReadService.findOne(eq(username))).thenReturn(userData);
|
||||
|
||||
Map<String, Object> param = new HashMap<String, Object>() {{
|
||||
put("user", new HashMap<String, Object>() {{
|
||||
put("email", email);
|
||||
put("password", "johnnyjacob");
|
||||
put("username", username);
|
||||
}});
|
||||
}};
|
||||
|
||||
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(""))
|
||||
.body("user.image", equalTo("https://static.productionready.io/images/smiley-cyrus.jpg"))
|
||||
.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 = "";
|
||||
|
||||
Map<String, Object> param = new HashMap<String, Object>() {{
|
||||
put("user", new HashMap<String, Object>() {{
|
||||
put("email", email);
|
||||
put("password", "johnnyjacob");
|
||||
put("username", username);
|
||||
}});
|
||||
}};
|
||||
|
||||
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";
|
||||
|
||||
Map<String, Object> param = new HashMap<String, Object>() {{
|
||||
put("user", new HashMap<String, Object>() {{
|
||||
put("email", email);
|
||||
put("password", "johnnyjacob");
|
||||
put("username", username);
|
||||
}});
|
||||
}};
|
||||
|
||||
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", ""
|
||||
)));
|
||||
|
||||
Map<String, Object> param = new HashMap<String, Object>() {{
|
||||
put("user", new HashMap<String, Object>() {{
|
||||
put("email", email);
|
||||
put("password", "johnnyjacob");
|
||||
put("username", username);
|
||||
}});
|
||||
}};
|
||||
|
||||
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());
|
||||
|
||||
Map<String, Object> param = new HashMap<String, Object>() {{
|
||||
put("user", new HashMap<String, Object>() {{
|
||||
put("email", email);
|
||||
put("password", "johnnyjacob");
|
||||
put("username", username);
|
||||
}});
|
||||
}};
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(param)
|
||||
.when()
|
||||
.post("/users")
|
||||
.then()
|
||||
.statusCode(422)
|
||||
.body("errors.email[0]", equalTo("duplicated email"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package io.spring.infrastructure.user;
|
||||
|
||||
import io.spring.core.user.User;
|
||||
import io.spring.core.user.UserRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@MybatisTest
|
||||
@Import(MyBatisUserRepository.class)
|
||||
public class MyBatisUserRepositoryTest {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
public void should_save_and_fetch_user_success() throws Exception {
|
||||
User user = new User("aisensiy@163.com", "aisensiy", "123", "", "default");
|
||||
userRepository.save(user);
|
||||
Optional<User> userOptional = userRepository.findByUsername("aisensiy");
|
||||
assertThat(userOptional.get(), is(user));
|
||||
Optional<User> userOptional2 = userRepository.findByEmail("aisensiy@163.com");
|
||||
assertThat(userOptional2.get(), is(user));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user