spring-boot-realworld-examp.../src/test/java/io/spring/api/CurrentUserApiTest.java

153 lines
4.9 KiB
Java
Raw Normal View History

2017-08-08 20:14:14 +07:00
package io.spring.api;
import io.restassured.RestAssured;
2017-08-18 16:08:27 +07:00
import io.spring.application.data.UserData;
2017-08-18 11:09:07 +07:00
import io.spring.core.user.User;
2017-08-08 20:14:14 +07:00
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.test.context.junit4.SpringRunner;
2017-08-14 13:27:36 +07:00
import java.util.HashMap;
import java.util.Map;
2017-08-18 11:09:07 +07:00
import java.util.Optional;
2017-08-08 20:14:14 +07:00
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsEqual.equalTo;
2017-08-18 11:09:07 +07:00
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
2017-08-08 20:14:14 +07:00
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@SpringBootTest(webEnvironment = RANDOM_PORT)
@RunWith(SpringRunner.class)
2017-08-15 09:47:18 +07:00
public class CurrentUserApiTest extends TestWithCurrentUser {
2017-08-08 20:14:14 +07:00
@LocalServerPort
private int port;
@Before
public void setUp() throws Exception {
RestAssured.port = port;
2017-08-16 14:58:51 +07:00
userFixture();
2017-08-14 13:27:36 +07:00
}
@Test
public void should_get_current_user_with_token() throws Exception {
2017-08-08 20:14:14 +07:00
given()
.header("Authorization", "Token " + token)
.contentType("application/json")
.when()
.get("/user")
.then()
.statusCode(200)
.body("user.email", equalTo(email))
.body("user.username", equalTo(username))
.body("user.bio", equalTo(""))
2017-08-18 11:09:07 +07:00
.body("user.image", equalTo(defaultAvatar))
2017-08-08 20:14:14 +07:00
.body("user.token", equalTo(token));
}
2017-08-14 10:09:11 +07:00
@Test
public void should_get_401_without_token() throws Exception {
given()
.contentType("application/json")
.when()
.get("/user")
.then()
.statusCode(401);
}
@Test
public void should_get_401_with_invalid_token() throws Exception {
given()
.contentType("application/json")
.header("Authorization", "Token asdfasd")
.when()
.get("/user")
.then()
.statusCode(401);
}
2017-08-14 13:27:36 +07:00
@Test
public void should_update_current_user_profile() throws Exception {
String newEmail = "newemail@example.com";
String newBio = "updated";
2017-08-18 11:09:07 +07:00
String newUsername = "newusernamee";
2017-08-14 13:27:36 +07:00
Map<String, Object> param = new HashMap<String, Object>() {{
put("user", new HashMap<String, Object>() {{
put("email", newEmail);
put("bio", newBio);
2017-08-18 11:09:07 +07:00
put("username", newUsername);
2017-08-14 13:27:36 +07:00
}});
}};
2017-08-18 16:08:27 +07:00
when(userRepository.findByUsername(eq(newUsername))).thenReturn(Optional.empty());
when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.empty());
2017-08-18 11:09:07 +07:00
when(userReadService.findByUsername(eq(newUsername))).thenReturn(new UserData(user.getId(), newEmail, newUsername, newBio, user.getImage()));
given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
2017-08-18 16:08:27 +07:00
.statusCode(200);
2017-08-18 11:09:07 +07:00
}
@Test
public void should_get_error_if_email_exists_when_update_user_profile() throws Exception {
String newEmail = "newemail@example.com";
String newBio = "updated";
String newUsername = "newusernamee";
Map<String, Object> param = prepareUpdateParam(newEmail, newBio, newUsername);
when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.of(new User(newEmail, "username", "123", "", "")));
when(userRepository.findByUsername(eq(newUsername))).thenReturn(Optional.empty());
when(userReadService.findByUsername(eq(newUsername))).thenReturn(new UserData(user.getId(), newEmail, newUsername, newBio, user.getImage()));
2017-08-14 13:27:36 +07:00
given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
2017-08-18 11:09:07 +07:00
.prettyPeek()
2017-08-14 13:27:36 +07:00
.then()
2017-08-18 11:09:07 +07:00
.statusCode(422)
.body("errors.email[0]", equalTo("email already exist"));
2017-08-14 13:27:36 +07:00
2017-08-18 11:09:07 +07:00
}
private HashMap<String, Object> prepareUpdateParam(final String newEmail, final String newBio, final String newUsername) {
return new HashMap<String, Object>() {{
put("user", new HashMap<String, Object>() {{
put("email", newEmail);
put("bio", newBio);
put("username", newUsername);
}});
}};
2017-08-14 13:27:36 +07:00
}
@Test
public void should_get_401_if_not_login() throws Exception {
given()
.contentType("application/json")
.body(new HashMap<String, Object>() {{
put("user", new HashMap<String, Object>());
}})
.when()
.put("/user")
.then().statusCode(401);
}
2017-08-08 20:14:14 +07:00
}