Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 272 additions & 0 deletions src/test/java/io/spring/api/CurrentUserApiTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.spring.api;

import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -176,4 +177,275 @@ public void should_get_401_if_not_login() throws Exception {
.then()
.statusCode(401);
}

@Test
public void should_get_error_if_username_exists_when_update_user_profile() throws Exception {
String newEmail = "newemail@example.com";
String newBio = "updated";
String newUsername = "existinguser";

Map<String, Object> param = prepareUpdateParam(newEmail, newBio, newUsername);

when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.empty());
when(userRepository.findByUsername(eq(newUsername)))
.thenReturn(Optional.of(new User("other@example.com", newUsername, "123", "", "")));

when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));

given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
.statusCode(422)
.body("errors.username[0]", equalTo("username already exist"));
}

@Test
public void should_get_error_if_both_email_and_username_exist_when_update() throws Exception {
String newEmail = "taken@example.com";
String newBio = "updated";
String newUsername = "takenuser";

Map<String, Object> param = prepareUpdateParam(newEmail, newBio, newUsername);

when(userRepository.findByEmail(eq(newEmail)))
.thenReturn(Optional.of(new User(newEmail, "someone", "123", "", "")));
when(userRepository.findByUsername(eq(newUsername)))
.thenReturn(Optional.of(new User("other@example.com", newUsername, "123", "", "")));

when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));

given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
.statusCode(422)
.body("errors", hasKey("email"))
.body("errors", hasKey("username"));
}

@Test
public void should_update_user_profile_with_only_bio() throws Exception {
Map<String, Object> param =
new HashMap<String, Object>() {
{
put(
"user",
new HashMap<String, Object>() {
{
put("bio", "new bio content");
}
});
}
};

when(userRepository.findByUsername(eq(""))).thenReturn(Optional.empty());
when(userRepository.findByEmail(eq(""))).thenReturn(Optional.empty());
when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));

given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
.statusCode(200);
}

@Test
public void should_update_user_profile_with_only_image() throws Exception {
Map<String, Object> param =
new HashMap<String, Object>() {
{
put(
"user",
new HashMap<String, Object>() {
{
put("image", "https://example.com/newimage.jpg");
}
});
}
};

when(userRepository.findByUsername(eq(""))).thenReturn(Optional.empty());
when(userRepository.findByEmail(eq(""))).thenReturn(Optional.empty());
when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));

given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
.statusCode(200);
}

@Test
public void should_get_401_with_invalid_token_on_update() throws Exception {
String invalidToken = "invalidtoken";
when(jwtService.getSubFromToken(eq(invalidToken))).thenReturn(Optional.empty());

Map<String, Object> param =
new HashMap<String, Object>() {
{
put(
"user",
new HashMap<String, Object>() {
{
put("bio", "updated bio");
}
});
}
};

given()
.contentType("application/json")
.header("Authorization", "Token " + invalidToken)
.body(param)
.when()
.put("/user")
.then()
.statusCode(401);
}

@Test
public void should_get_error_for_invalid_email_format_on_update() throws Exception {
Map<String, Object> param =
new HashMap<String, Object>() {
{
put(
"user",
new HashMap<String, Object>() {
{
put("email", "not-an-email");
}
});
}
};

when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));

given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
.statusCode(422)
.body("errors", hasKey("email"));
}

@Test
public void should_allow_update_with_own_existing_email() throws Exception {
Map<String, Object> param =
new HashMap<String, Object>() {
{
put(
"user",
new HashMap<String, Object>() {
{
put("email", email);
put("bio", "updated bio");
}
});
}
};

when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
when(userRepository.findByUsername(eq(""))).thenReturn(Optional.empty());
when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));

given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
.statusCode(200);
}

@Test
public void should_allow_update_with_own_existing_username() throws Exception {
Map<String, Object> param =
new HashMap<String, Object>() {
{
put(
"user",
new HashMap<String, Object>() {
{
put("username", username);
put("bio", "updated bio");
}
});
}
};

when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));
when(userRepository.findByEmail(eq(""))).thenReturn(Optional.empty());
when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));

given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
.statusCode(200);
}

@Test
public void should_return_correct_response_structure_on_get_current_user() throws Exception {
when(userQueryService.findById(any())).thenReturn(Optional.of(userData));

given()
.header("Authorization", "Token " + token)
.contentType("application/json")
.when()
.get("/user")
.then()
.statusCode(200)
.body("user", hasKey("email"))
.body("user", hasKey("username"))
.body("user", hasKey("bio"))
.body("user", hasKey("image"))
.body("user", hasKey("token"));
}

@Test
public void should_return_correct_response_structure_on_update() throws Exception {
String newEmail = "newemail@example.com";
String newBio = "updated";
String newUsername = "newusernamee";

Map<String, Object> param = prepareUpdateParam(newEmail, newBio, newUsername);

when(userRepository.findByUsername(eq(newUsername))).thenReturn(Optional.empty());
when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.empty());
when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));

given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
.statusCode(200)
.body("user", hasKey("email"))
.body("user", hasKey("username"))
.body("user", hasKey("bio"))
.body("user", hasKey("image"))
.body("user", hasKey("token"));
}
}
Loading