27.4.1. Modified endpoints related to profile image - backend (#27)
This commit is contained in:
@ -6,7 +6,7 @@ public class FileConstant {
|
|||||||
public static final String JPG_EXTENSION = "jpg";
|
public static final String JPG_EXTENSION = "jpg";
|
||||||
public static final String USER_FOLDER = System.getProperty("user.home") + "/supportportal/user/";
|
public static final String USER_FOLDER = System.getProperty("user.home") + "/supportportal/user/";
|
||||||
public static final String DIRECTORY_CREATED = "Created directory for: ";
|
public static final String DIRECTORY_CREATED = "Created directory for: ";
|
||||||
public static final String DEFAULT_USER_IMAGE_PATH = "/user/image/profile/";
|
public static final String DEFAULT_USER_IMAGE_URI_PATTERN = "/user/%s/profile-image";
|
||||||
public static final String USER_IMAGE_FILENAME = "avatar.jpg";
|
public static final String USER_IMAGE_FILENAME = "avatar.jpg";
|
||||||
public static final String FILE_SAVED_IN_FILE_SYSTEM = "Saved file in file system by name: ";
|
public static final String FILE_SAVED_IN_FILE_SYSTEM = "Saved file in file system by name: ";
|
||||||
public static final String DOT = ".";
|
public static final String DOT = ".";
|
||||||
|
|||||||
@ -104,22 +104,17 @@ public class UserResource {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("{username}/profileImage")
|
@PutMapping("{userId}/profile-image")
|
||||||
public User updateProfileImage(@PathVariable String username, MultipartFile profileImage) {
|
public User updateProfileImage(@PathVariable UUID userId, MultipartFile profileImage) {
|
||||||
return userService.updateProfileImage(username, profileImage);
|
return userService.updateProfileImage(userId, profileImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "{username}/image/profile", produces = MediaType.IMAGE_JPEG_VALUE)
|
@GetMapping(path = "{userId}/profile-image/{filename}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||||
public byte[] getProfileImage(@PathVariable String username) throws IOException {
|
|
||||||
return userService.getProfileImage(username);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping(path = "image/profile/{userId}/{filename}", produces = MediaType.IMAGE_JPEG_VALUE)
|
|
||||||
public byte[] getProfileImageByUserId(@PathVariable UUID userId, @PathVariable String filename) throws IOException {
|
public byte[] getProfileImageByUserId(@PathVariable UUID userId, @PathVariable String filename) throws IOException {
|
||||||
return userService.getImageByUserId(userId, filename);
|
return userService.getImageByUserId(userId, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "image/profile/{userId}", produces = MediaType.IMAGE_JPEG_VALUE)
|
@GetMapping(path = "{userId}/profile-image", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||||
public byte[] getDefaultProfileImage(@PathVariable UUID userId) {
|
public byte[] getDefaultProfileImage(@PathVariable UUID userId) {
|
||||||
return userService.getDefaultProfileImage(userId);
|
return userService.getDefaultProfileImage(userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,9 +30,7 @@ public interface UserService extends UserDetailsService {
|
|||||||
|
|
||||||
void resetPassword(String email);
|
void resetPassword(String email);
|
||||||
|
|
||||||
User updateProfileImage(String username, MultipartFile profileImage);
|
User updateProfileImage(UUID userId, MultipartFile profileImage);
|
||||||
|
|
||||||
byte[] getProfileImage(String username) throws IOException;
|
|
||||||
|
|
||||||
byte[] getImageByUserId(UUID userId, String filename) throws IOException;
|
byte[] getImageByUserId(UUID userId, String filename) throws IOException;
|
||||||
|
|
||||||
|
|||||||
@ -105,15 +105,13 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
private String generateDefaultProfileImageUrl(UUID userId) {
|
private String generateDefaultProfileImageUrl(UUID userId) {
|
||||||
return ServletUriComponentsBuilder.fromCurrentContextPath()
|
return ServletUriComponentsBuilder.fromCurrentContextPath()
|
||||||
.path(DEFAULT_USER_IMAGE_PATH)
|
.path(String.format(DEFAULT_USER_IMAGE_URI_PATTERN, userId))
|
||||||
.pathSegment(userId.toString())
|
|
||||||
.toUriString();
|
.toUriString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String generateProfileImageUrl(UUID userId) {
|
private String generateProfileImageUrl(UUID userId) {
|
||||||
return ServletUriComponentsBuilder.fromCurrentContextPath()
|
return ServletUriComponentsBuilder.fromCurrentContextPath()
|
||||||
.path(DEFAULT_USER_IMAGE_PATH)
|
.path(String.format(DEFAULT_USER_IMAGE_URI_PATTERN, userId))
|
||||||
.pathSegment(userId.toString())
|
|
||||||
.pathSegment(USER_IMAGE_FILENAME)
|
.pathSegment(USER_IMAGE_FILENAME)
|
||||||
.toUriString();
|
.toUriString();
|
||||||
}
|
}
|
||||||
@ -265,20 +263,19 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User updateProfileImage(String username, MultipartFile profileImage) {
|
public User updateProfileImage(UUID userId, MultipartFile profileImage) {
|
||||||
User user = findByUsername(username);
|
User user = findByUserId(userId);
|
||||||
saveProfileImage(user, profileImage);
|
saveProfileImage(user, profileImage);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] getProfileImage(String username) throws IOException {
|
public byte[] getImageByUserId(UUID userId, String filename) throws IOException {
|
||||||
User user = findByUsername(username);
|
|
||||||
return getImageByUserId(user.getUserId(), USER_IMAGE_FILENAME);
|
if (!userRepository.existsByUserId(userId)) {
|
||||||
|
throw new UserNotFoundException(USER_NOT_FOUND_MSG);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] getImageByUserId(UUID userId, String filename) throws IOException {
|
|
||||||
Path userProfileImagePath = Paths
|
Path userProfileImagePath = Paths
|
||||||
.get(USER_FOLDER, userId.toString(), filename);
|
.get(USER_FOLDER, userId.toString(), filename);
|
||||||
return Files.readAllBytes(userProfileImagePath);
|
return Files.readAllBytes(userProfileImagePath);
|
||||||
|
|||||||
@ -40,7 +40,7 @@ spring:
|
|||||||
resources:
|
resources:
|
||||||
add-mappings: false
|
add-mappings: false
|
||||||
app:
|
app:
|
||||||
public-urls: /user/login,/user/register,/user/*/image/**,/user/image/**
|
public-urls: /user/login,/user/register,/user/*/profile-image/**
|
||||||
cors:
|
cors:
|
||||||
allowed-origins: http://localhost:4200,https://localhost:4200,http://art-support-portal.s3-website.eu-north-1.amazonaws.com,http://portal.shyshkin.net
|
allowed-origins: http://localhost:4200,https://localhost:4200,http://art-support-portal.s3-website.eu-north-1.amazonaws.com,http://portal.shyshkin.net
|
||||||
jwt:
|
jwt:
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import org.springframework.web.util.UriComponentsBuilder;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import static net.shyshkin.study.fullstack.supportportal.backend.constant.FileConstant.DEFAULT_USER_IMAGE_PATH;
|
import static net.shyshkin.study.fullstack.supportportal.backend.constant.FileConstant.DEFAULT_USER_IMAGE_URI_PATTERN;
|
||||||
import static net.shyshkin.study.fullstack.supportportal.backend.domain.Role.ROLE_ADMIN;
|
import static net.shyshkin.study.fullstack.supportportal.backend.domain.Role.ROLE_ADMIN;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@ -61,8 +61,7 @@ public abstract class BaseUserTest {
|
|||||||
private String generateProfileImageUrl(UUID userId) {
|
private String generateProfileImageUrl(UUID userId) {
|
||||||
return UriComponentsBuilder
|
return UriComponentsBuilder
|
||||||
.fromUriString("http://localhost:8080")
|
.fromUriString("http://localhost:8080")
|
||||||
.path(DEFAULT_USER_IMAGE_PATH)
|
.path(String.format(DEFAULT_USER_IMAGE_URI_PATTERN, userId))
|
||||||
.pathSegment(userId.toString())
|
|
||||||
// .pathSegment(USER_IMAGE_FILENAME)
|
// .pathSegment(USER_IMAGE_FILENAME)
|
||||||
.toUriString();
|
.toUriString();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,6 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.common.BaseUserTest;
|
import net.shyshkin.study.fullstack.supportportal.backend.common.BaseUserTest;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.constant.FileConstant;
|
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.Role;
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.Role;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
|
||||||
@ -36,6 +35,7 @@ import java.time.temporal.ChronoUnit;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static net.shyshkin.study.fullstack.supportportal.backend.constant.FileConstant.*;
|
||||||
import static net.shyshkin.study.fullstack.supportportal.backend.constant.SecurityConstants.JWT_TOKEN_HEADER;
|
import static net.shyshkin.study.fullstack.supportportal.backend.constant.SecurityConstants.JWT_TOKEN_HEADER;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.within;
|
import static org.assertj.core.api.Assertions.within;
|
||||||
@ -216,7 +216,7 @@ class UserResourceTest extends BaseUserTest {
|
|||||||
.hasFieldOrPropertyWithValue("isActive", true)
|
.hasFieldOrPropertyWithValue("isActive", true)
|
||||||
.hasFieldOrPropertyWithValue("isNotLocked", true)
|
.hasFieldOrPropertyWithValue("isNotLocked", true)
|
||||||
.hasFieldOrPropertyWithValue("role", "ROLE_ADMIN")
|
.hasFieldOrPropertyWithValue("role", "ROLE_ADMIN")
|
||||||
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format("/user/image/profile/%s", u.getUserId())));
|
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format(DEFAULT_USER_IMAGE_URI_PATTERN, u.getUserId())));
|
||||||
|
|
||||||
String token = responseEntity.getHeaders().getFirst(JWT_TOKEN_HEADER);
|
String token = responseEntity.getHeaders().getFirst(JWT_TOKEN_HEADER);
|
||||||
log.debug("Token: {}", token);
|
log.debug("Token: {}", token);
|
||||||
@ -573,10 +573,10 @@ class UserResourceTest extends BaseUserTest {
|
|||||||
.hasFieldOrPropertyWithValue("isActive", true)
|
.hasFieldOrPropertyWithValue("isActive", true)
|
||||||
.hasFieldOrPropertyWithValue("isNotLocked", true)
|
.hasFieldOrPropertyWithValue("isNotLocked", true)
|
||||||
.hasFieldOrPropertyWithValue("role", "ROLE_ADMIN")
|
.hasFieldOrPropertyWithValue("role", "ROLE_ADMIN")
|
||||||
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format("/user/image/profile/%s/avatar.jpg", u.getUserId())));
|
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format(DEFAULT_USER_IMAGE_URI_PATTERN.concat("/avatar.jpg"), u.getUserId())));
|
||||||
|
|
||||||
User createdUser = responseEntity.getBody();
|
User createdUser = responseEntity.getBody();
|
||||||
Path path = Path.of(FileConstant.USER_FOLDER, createdUser.getUserId().toString(), FileConstant.USER_IMAGE_FILENAME);
|
Path path = Path.of(USER_FOLDER, createdUser.getUserId().toString(), USER_IMAGE_FILENAME);
|
||||||
log.debug("Path of created file: {}", path);
|
log.debug("Path of created file: {}", path);
|
||||||
assertThat(Files.exists(path)).isTrue();
|
assertThat(Files.exists(path)).isTrue();
|
||||||
assertThat(Files.getLastModifiedTime(path).toInstant()).isCloseTo(Instant.now(), within(1, ChronoUnit.SECONDS));
|
assertThat(Files.getLastModifiedTime(path).toInstant()).isCloseTo(Instant.now(), within(1, ChronoUnit.SECONDS));
|
||||||
@ -632,10 +632,10 @@ class UserResourceTest extends BaseUserTest {
|
|||||||
.hasFieldOrPropertyWithValue("isActive", true)
|
.hasFieldOrPropertyWithValue("isActive", true)
|
||||||
.hasFieldOrPropertyWithValue("isNotLocked", true)
|
.hasFieldOrPropertyWithValue("isNotLocked", true)
|
||||||
.hasFieldOrPropertyWithValue("role", "ROLE_ADMIN")
|
.hasFieldOrPropertyWithValue("role", "ROLE_ADMIN")
|
||||||
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format("/user/image/profile/%s/avatar.jpg", u.getUserId())));
|
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format(DEFAULT_USER_IMAGE_URI_PATTERN.concat("/avatar.jpg"), user.getUserId())));
|
||||||
|
|
||||||
User createdUser = responseEntity.getBody();
|
User createdUser = responseEntity.getBody();
|
||||||
Path path = Path.of(FileConstant.USER_FOLDER, createdUser.getUserId().toString(), FileConstant.USER_IMAGE_FILENAME);
|
Path path = Path.of(USER_FOLDER, createdUser.getUserId().toString(), USER_IMAGE_FILENAME);
|
||||||
log.debug("Path of created file: {}", path);
|
log.debug("Path of created file: {}", path);
|
||||||
assertThat(Files.exists(path)).isTrue();
|
assertThat(Files.exists(path)).isTrue();
|
||||||
assertThat(Files.getLastModifiedTime(path).toInstant()).isCloseTo(Instant.now(), within(1, ChronoUnit.SECONDS));
|
assertThat(Files.getLastModifiedTime(path).toInstant()).isCloseTo(Instant.now(), within(1, ChronoUnit.SECONDS));
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package net.shyshkin.study.fullstack.supportportal.backend.controller;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.common.BaseUserTest;
|
import net.shyshkin.study.fullstack.supportportal.backend.common.BaseUserTest;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.constant.FileConstant;
|
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.Role;
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.Role;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
|
||||||
@ -34,6 +33,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static net.shyshkin.study.fullstack.supportportal.backend.constant.FileConstant.*;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.within;
|
import static org.assertj.core.api.Assertions.within;
|
||||||
import static org.springframework.http.HttpStatus.*;
|
import static org.springframework.http.HttpStatus.*;
|
||||||
@ -52,6 +52,10 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
|
|
||||||
|
public static final String USER_IMAGE_ENDPOINT_TEMPLATE = "/user/{userId}/profile-image";
|
||||||
|
public static final String USER_DEFAULT_IMAGE_URI_TEMPLATE = USER_IMAGE_ENDPOINT_TEMPLATE;
|
||||||
|
public static final String USER_CUSTOM_IMAGE_URI_TEMPLATE = "/user/{userId}/profile-image/{filename}";
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
class AddNewUserTests {
|
class AddNewUserTests {
|
||||||
|
|
||||||
@ -575,7 +579,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
void updateProfileImage_correct() throws IOException {
|
void updateProfileImage_correct() throws IOException {
|
||||||
|
|
||||||
//given
|
//given
|
||||||
String username = user.getUsername();
|
UUID userId = user.getUserId();
|
||||||
|
|
||||||
MultipartFile profileImage = new MockMultipartFile("profileImage", "test.png",
|
MultipartFile profileImage = new MockMultipartFile("profileImage", "test.png",
|
||||||
"image/png", ("Spring Framework" + UUID.randomUUID()).getBytes());
|
"image/png", ("Spring Framework" + UUID.randomUUID()).getBytes());
|
||||||
@ -585,7 +589,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
body.add("profileImage", profileImage.getResource());
|
body.add("profileImage", profileImage.getResource());
|
||||||
|
|
||||||
//when
|
//when
|
||||||
var requestEntity = RequestEntity.put("/user/{username}/profileImage", username)
|
var requestEntity = RequestEntity.put(USER_IMAGE_ENDPOINT_TEMPLATE, userId)
|
||||||
.contentType(MULTIPART_FORM_DATA)
|
.contentType(MULTIPART_FORM_DATA)
|
||||||
.body(body);
|
.body(body);
|
||||||
var responseEntity = restTemplate
|
var responseEntity = restTemplate
|
||||||
@ -597,16 +601,16 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
assertThat(responseEntity.getBody())
|
assertThat(responseEntity.getBody())
|
||||||
.isNotNull()
|
.isNotNull()
|
||||||
.hasNoNullFieldsOrPropertiesExcept("lastLoginDate", "lastLoginDateDisplay", "password", "id")
|
.hasNoNullFieldsOrPropertiesExcept("lastLoginDate", "lastLoginDateDisplay", "password", "id")
|
||||||
.hasFieldOrPropertyWithValue("username", username)
|
.hasFieldOrPropertyWithValue("username", user.getUsername())
|
||||||
.hasFieldOrPropertyWithValue("email", user.getEmail())
|
.hasFieldOrPropertyWithValue("email", user.getEmail())
|
||||||
.hasFieldOrPropertyWithValue("firstName", user.getFirstName())
|
.hasFieldOrPropertyWithValue("firstName", user.getFirstName())
|
||||||
.hasFieldOrPropertyWithValue("lastName", user.getLastName())
|
.hasFieldOrPropertyWithValue("lastName", user.getLastName())
|
||||||
.hasFieldOrPropertyWithValue("isActive", user.isActive())
|
.hasFieldOrPropertyWithValue("isActive", user.isActive())
|
||||||
.hasFieldOrPropertyWithValue("isNotLocked", user.isNotLocked())
|
.hasFieldOrPropertyWithValue("isNotLocked", user.isNotLocked())
|
||||||
.hasFieldOrPropertyWithValue("role", user.getRole())
|
.hasFieldOrPropertyWithValue("role", user.getRole())
|
||||||
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format("/user/image/profile/%s/avatar.jpg", user.getUserId())));
|
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format(DEFAULT_USER_IMAGE_URI_PATTERN.concat("/avatar.jpg"), user.getUserId())));
|
||||||
|
|
||||||
Path path = Path.of(FileConstant.USER_FOLDER, user.getUserId().toString(), FileConstant.USER_IMAGE_FILENAME);
|
Path path = Path.of(USER_FOLDER, user.getUserId().toString(), USER_IMAGE_FILENAME);
|
||||||
log.debug("Path of created file: {}", path);
|
log.debug("Path of created file: {}", path);
|
||||||
assertThat(Files.exists(path)).isTrue();
|
assertThat(Files.exists(path)).isTrue();
|
||||||
assertThat(Files.getLastModifiedTime(path).toInstant()).isCloseTo(Instant.now(), within(100, ChronoUnit.MILLIS));
|
assertThat(Files.getLastModifiedTime(path).toInstant()).isCloseTo(Instant.now(), within(100, ChronoUnit.MILLIS));
|
||||||
@ -616,8 +620,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
void updateProfileImage_absentUser() {
|
void updateProfileImage_absentUser() {
|
||||||
|
|
||||||
//given
|
//given
|
||||||
String username = FAKER.name().username();
|
UUID userId = UUID.randomUUID();
|
||||||
|
|
||||||
|
|
||||||
MultipartFile profileImage = new MockMultipartFile("profileImage", "test.txt",
|
MultipartFile profileImage = new MockMultipartFile("profileImage", "test.txt",
|
||||||
"text/plain", ("Spring Framework" + UUID.randomUUID()).getBytes());
|
"text/plain", ("Spring Framework" + UUID.randomUUID()).getBytes());
|
||||||
@ -627,7 +630,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
body.add("profileImage", profileImage.getResource());
|
body.add("profileImage", profileImage.getResource());
|
||||||
|
|
||||||
//when
|
//when
|
||||||
var requestEntity = RequestEntity.put("/user/{username}/profileImage", username)
|
var requestEntity = RequestEntity.put(USER_IMAGE_ENDPOINT_TEMPLATE, userId)
|
||||||
.contentType(MULTIPART_FORM_DATA)
|
.contentType(MULTIPART_FORM_DATA)
|
||||||
.body(body);
|
.body(body);
|
||||||
var responseEntity = restTemplate
|
var responseEntity = restTemplate
|
||||||
@ -640,7 +643,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
.isNotNull()
|
.isNotNull()
|
||||||
.hasNoNullFieldsOrProperties()
|
.hasNoNullFieldsOrProperties()
|
||||||
.hasFieldOrPropertyWithValue("httpStatus", BAD_REQUEST)
|
.hasFieldOrPropertyWithValue("httpStatus", BAD_REQUEST)
|
||||||
.hasFieldOrPropertyWithValue("message", String.format("User with username `%s` not found", username));
|
.hasFieldOrPropertyWithValue("message", "User not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -660,11 +663,11 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
void getProfileImage_correct() throws IOException {
|
void getProfileImage_correct() throws IOException {
|
||||||
|
|
||||||
//given
|
//given
|
||||||
String username = user.getUsername();
|
UUID userId = user.getUserId();
|
||||||
uploadProfileImage(username);
|
uploadProfileImage(userId);
|
||||||
|
|
||||||
//when
|
//when
|
||||||
RequestEntity<Void> requestEntity = RequestEntity.get("/user/{username}/image/profile", username)
|
RequestEntity<Void> requestEntity = RequestEntity.get(USER_CUSTOM_IMAGE_URI_TEMPLATE, userId, USER_IMAGE_FILENAME)
|
||||||
.accept(IMAGE_JPEG)
|
.accept(IMAGE_JPEG)
|
||||||
.build();
|
.build();
|
||||||
var responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<byte[]>() {
|
var responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<byte[]>() {
|
||||||
@ -680,12 +683,12 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
void getProfileImage_absentUser() throws IOException {
|
void getProfileImage_absentUser() throws IOException {
|
||||||
|
|
||||||
//given
|
//given
|
||||||
String username = user.getUsername();
|
UUID userId = user.getUserId();
|
||||||
uploadProfileImage(username);
|
uploadProfileImage(userId);
|
||||||
String absentUsername = FAKER.name().username();
|
UUID absentUserId = UUID.randomUUID();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
RequestEntity<Void> requestEntity = RequestEntity.get("/user/{username}/image/profile", absentUsername)
|
RequestEntity<Void> requestEntity = RequestEntity.get(USER_DEFAULT_IMAGE_URI_TEMPLATE, absentUserId)
|
||||||
.accept(IMAGE_JPEG, APPLICATION_JSON)
|
.accept(IMAGE_JPEG, APPLICATION_JSON)
|
||||||
.build();
|
.build();
|
||||||
var responseEntity = restTemplate.exchange(requestEntity, HttpResponse.class);
|
var responseEntity = restTemplate.exchange(requestEntity, HttpResponse.class);
|
||||||
@ -697,7 +700,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
.isNotNull()
|
.isNotNull()
|
||||||
.hasNoNullFieldsOrProperties()
|
.hasNoNullFieldsOrProperties()
|
||||||
.hasFieldOrPropertyWithValue("httpStatus", BAD_REQUEST)
|
.hasFieldOrPropertyWithValue("httpStatus", BAD_REQUEST)
|
||||||
.hasFieldOrPropertyWithValue("message", String.format("User with username `%s` not found", absentUsername));
|
.hasFieldOrPropertyWithValue("message", "User not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -705,10 +708,10 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
|
|
||||||
//given
|
//given
|
||||||
user = userRepository.save(createRandomUser());
|
user = userRepository.save(createRandomUser());
|
||||||
String username = user.getUsername();
|
UUID userId = user.getUserId();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
RequestEntity<Void> requestEntity = RequestEntity.get("/user/{username}/image/profile", username)
|
RequestEntity<Void> requestEntity = RequestEntity.get(USER_CUSTOM_IMAGE_URI_TEMPLATE, userId, USER_IMAGE_FILENAME)
|
||||||
.accept(IMAGE_JPEG, APPLICATION_JSON)
|
.accept(IMAGE_JPEG, APPLICATION_JSON)
|
||||||
.build();
|
.build();
|
||||||
var responseEntity = restTemplate.exchange(requestEntity, HttpResponse.class);
|
var responseEntity = restTemplate.exchange(requestEntity, HttpResponse.class);
|
||||||
@ -727,10 +730,10 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
void getImageById_correct() throws IOException {
|
void getImageById_correct() throws IOException {
|
||||||
|
|
||||||
//given
|
//given
|
||||||
String username = user.getUsername();
|
UUID userId = user.getUserId();
|
||||||
uploadProfileImage(username);
|
uploadProfileImage(userId);
|
||||||
String profileImageUrlFull = user.getProfileImageUrl();
|
String profileImageUrlFull = user.getProfileImageUrl();
|
||||||
String profileImageUrl = profileImageUrlFull.substring(profileImageUrlFull.indexOf("/user/image/profile"));
|
String profileImageUrl = profileImageUrlFull.substring(profileImageUrlFull.indexOf("/user/"));
|
||||||
log.debug("Image URL: {}", profileImageUrl);
|
log.debug("Image URL: {}", profileImageUrl);
|
||||||
|
|
||||||
//when
|
//when
|
||||||
@ -754,7 +757,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
UUID userId = user.getUserId();
|
UUID userId = user.getUserId();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
RequestEntity<Void> requestEntity = RequestEntity.get("/user/image/profile/{userId}", userId)
|
RequestEntity<Void> requestEntity = RequestEntity.get(USER_DEFAULT_IMAGE_URI_TEMPLATE, userId)
|
||||||
.accept(IMAGE_JPEG)
|
.accept(IMAGE_JPEG)
|
||||||
.build();
|
.build();
|
||||||
var responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<byte[]>() {
|
var responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<byte[]>() {
|
||||||
@ -773,7 +776,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
UUID userId = UUID.randomUUID();
|
UUID userId = UUID.randomUUID();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
RequestEntity<Void> requestEntity = RequestEntity.get("/user/image/profile/{userId}", userId)
|
RequestEntity<Void> requestEntity = RequestEntity.get(USER_DEFAULT_IMAGE_URI_TEMPLATE, userId)
|
||||||
.accept(IMAGE_JPEG, APPLICATION_JSON)
|
.accept(IMAGE_JPEG, APPLICATION_JSON)
|
||||||
.build();
|
.build();
|
||||||
var responseEntity = restTemplate.exchange(requestEntity, HttpResponse.class);
|
var responseEntity = restTemplate.exchange(requestEntity, HttpResponse.class);
|
||||||
@ -795,7 +798,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
String userId = "not_a_UUID";
|
String userId = "not_a_UUID";
|
||||||
|
|
||||||
//when
|
//when
|
||||||
RequestEntity<Void> requestEntity = RequestEntity.get("/user/image/profile/{userId}", userId)
|
RequestEntity<Void> requestEntity = RequestEntity.get(USER_DEFAULT_IMAGE_URI_TEMPLATE, userId)
|
||||||
.accept(IMAGE_JPEG, APPLICATION_JSON)
|
.accept(IMAGE_JPEG, APPLICATION_JSON)
|
||||||
.build();
|
.build();
|
||||||
var responseEntity = restTemplate.exchange(requestEntity, HttpResponse.class);
|
var responseEntity = restTemplate.exchange(requestEntity, HttpResponse.class);
|
||||||
@ -810,7 +813,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
.hasFieldOrPropertyWithValue("message", "Invalid UUID string: " + userId);
|
.hasFieldOrPropertyWithValue("message", "Invalid UUID string: " + userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void uploadProfileImage(String username) throws IOException {
|
private void uploadProfileImage(UUID userId) throws IOException {
|
||||||
|
|
||||||
MultipartFile profileImage = new MockMultipartFile("profileImage", "test.jpg",
|
MultipartFile profileImage = new MockMultipartFile("profileImage", "test.jpg",
|
||||||
IMAGE_JPEG_VALUE, ("Spring Framework" + UUID.randomUUID()).getBytes());
|
IMAGE_JPEG_VALUE, ("Spring Framework" + UUID.randomUUID()).getBytes());
|
||||||
@ -820,7 +823,7 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
body.add("profileImage", profileImage.getResource());
|
body.add("profileImage", profileImage.getResource());
|
||||||
|
|
||||||
//when
|
//when
|
||||||
var requestEntity = RequestEntity.put("/user/{username}/profileImage", username)
|
var requestEntity = RequestEntity.put(USER_IMAGE_ENDPOINT_TEMPLATE, userId)
|
||||||
.contentType(MULTIPART_FORM_DATA)
|
.contentType(MULTIPART_FORM_DATA)
|
||||||
.body(body);
|
.body(body);
|
||||||
var responseEntity = restTemplate
|
var responseEntity = restTemplate
|
||||||
@ -832,16 +835,17 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
assertThat(responseEntity.getBody())
|
assertThat(responseEntity.getBody())
|
||||||
.isNotNull()
|
.isNotNull()
|
||||||
.hasNoNullFieldsOrPropertiesExcept("lastLoginDate", "lastLoginDateDisplay", "password", "id")
|
.hasNoNullFieldsOrPropertiesExcept("lastLoginDate", "lastLoginDateDisplay", "password", "id")
|
||||||
.hasFieldOrPropertyWithValue("username", username)
|
.hasFieldOrPropertyWithValue("userId", userId)
|
||||||
|
.hasFieldOrPropertyWithValue("username", user.getUsername())
|
||||||
.hasFieldOrPropertyWithValue("email", user.getEmail())
|
.hasFieldOrPropertyWithValue("email", user.getEmail())
|
||||||
.hasFieldOrPropertyWithValue("firstName", user.getFirstName())
|
.hasFieldOrPropertyWithValue("firstName", user.getFirstName())
|
||||||
.hasFieldOrPropertyWithValue("lastName", user.getLastName())
|
.hasFieldOrPropertyWithValue("lastName", user.getLastName())
|
||||||
.hasFieldOrPropertyWithValue("isActive", user.isActive())
|
.hasFieldOrPropertyWithValue("isActive", user.isActive())
|
||||||
.hasFieldOrPropertyWithValue("isNotLocked", user.isNotLocked())
|
.hasFieldOrPropertyWithValue("isNotLocked", user.isNotLocked())
|
||||||
.hasFieldOrPropertyWithValue("role", user.getRole())
|
.hasFieldOrPropertyWithValue("role", user.getRole())
|
||||||
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format("/user/image/profile/%s/avatar.jpg", user.getUserId())));
|
.satisfies(u -> assertThat(u.getProfileImageUrl()).endsWith(String.format(DEFAULT_USER_IMAGE_URI_PATTERN.concat("/avatar.jpg"), user.getUserId())));
|
||||||
|
|
||||||
Path path = Path.of(FileConstant.USER_FOLDER, user.getUserId().toString(), FileConstant.USER_IMAGE_FILENAME);
|
Path path = Path.of(USER_FOLDER, user.getUserId().toString(), USER_IMAGE_FILENAME);
|
||||||
log.debug("Path of created file: {}", path);
|
log.debug("Path of created file: {}", path);
|
||||||
assertThat(Files.exists(path)).isTrue();
|
assertThat(Files.exists(path)).isTrue();
|
||||||
assertThat(Files.getLastModifiedTime(path).toInstant()).isCloseTo(Instant.now(), within(200, ChronoUnit.MILLIS));
|
assertThat(Files.getLastModifiedTime(path).toInstant()).isCloseTo(Instant.now(), within(200, ChronoUnit.MILLIS));
|
||||||
|
|||||||
@ -117,12 +117,12 @@ class UserServiceTest extends BaseUserTest {
|
|||||||
//given
|
//given
|
||||||
User fakeUser = createRandomUser();
|
User fakeUser = createRandomUser();
|
||||||
user = userRepository.save(fakeUser);
|
user = userRepository.save(fakeUser);
|
||||||
String username = user.getUsername();
|
UUID userId = user.getUserId();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
MockMultipartFile multipartFile = new MockMultipartFile("file", "test.jpg",
|
MockMultipartFile multipartFile = new MockMultipartFile("file", "test.jpg",
|
||||||
"image/jpeg", ("Spring Framework" + UUID.randomUUID()).getBytes());
|
"image/jpeg", ("Spring Framework" + UUID.randomUUID()).getBytes());
|
||||||
userService.updateProfileImage(username, multipartFile);
|
userService.updateProfileImage(userId, multipartFile);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
Path path = Path.of(FileConstant.USER_FOLDER, user.getUserId().toString(), FileConstant.USER_IMAGE_FILENAME);
|
Path path = Path.of(FileConstant.USER_FOLDER, user.getUserId().toString(), FileConstant.USER_IMAGE_FILENAME);
|
||||||
|
|||||||
Reference in New Issue
Block a user