77.1. Add new user (#9 Section 11: User Resource)
This commit is contained in:
@ -1,9 +1,11 @@
|
|||||||
package net.shyshkin.study.fullstack.supportportal.backend.controller;
|
package net.shyshkin.study.fullstack.supportportal.backend.controller;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.constant.SecurityConstants;
|
import net.shyshkin.study.fullstack.supportportal.backend.constant.SecurityConstants;
|
||||||
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.User;
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.dto.UserDto;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.service.UserService;
|
import net.shyshkin.study.fullstack.supportportal.backend.service.UserService;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.utility.JwtTokenProvider;
|
import net.shyshkin.study.fullstack.supportportal.backend.utility.JwtTokenProvider;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@ -15,7 +17,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
|
|
||||||
import static org.springframework.http.HttpStatus.OK;
|
import static org.springframework.http.HttpStatus.OK;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("user")
|
@RequestMapping("user")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@ -53,6 +55,12 @@ public class UserResource {
|
|||||||
.body(httpResponse);
|
.body(httpResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("add")
|
||||||
|
public User addNewUser(UserDto userDto) {
|
||||||
|
log.debug("User DTO: {}", userDto);
|
||||||
|
return userService.addNewUser(userDto);
|
||||||
|
}
|
||||||
|
|
||||||
private void authenticate(String username, String password) {
|
private void authenticate(String username, String password) {
|
||||||
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
|
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
|
||||||
authenticationManager.authenticate(auth);
|
authenticationManager.authenticate(auth);
|
||||||
|
|||||||
@ -0,0 +1,69 @@
|
|||||||
|
package net.shyshkin.study.fullstack.supportportal.backend.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.common.BaseUserTest;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.dto.UserDto;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.springframework.http.HttpStatus.OK;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
@TestPropertySource(properties = {
|
||||||
|
"app.public-urls=/**"
|
||||||
|
})
|
||||||
|
class UserResourceUnSecureTest extends BaseUserTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addNewUser() {
|
||||||
|
|
||||||
|
//given
|
||||||
|
UserDto userDto = createRandomUserDto();
|
||||||
|
Map<String, ?> paramMap = Map.of(
|
||||||
|
"firstName", userDto.getFirstName(),
|
||||||
|
"lastName", userDto.getLastName(),
|
||||||
|
"username", userDto.getUsername(),
|
||||||
|
"email", userDto.getEmail(),
|
||||||
|
"role", userDto.getRole().name(),
|
||||||
|
"isActive", String.valueOf(userDto.isActive()),
|
||||||
|
"isNonLocked", String.valueOf(userDto.isNonLocked())
|
||||||
|
);
|
||||||
|
|
||||||
|
//when
|
||||||
|
ResponseEntity<User> responseEntity = restTemplate
|
||||||
|
.postForEntity(
|
||||||
|
"/user/add?username={username}&email={email}" +
|
||||||
|
"&firstName={firstName}&lastName={lastName}" +
|
||||||
|
"&role={role}&active={isActive}&nonLocked={isNonLocked}",
|
||||||
|
null,
|
||||||
|
User.class,
|
||||||
|
paramMap
|
||||||
|
);
|
||||||
|
|
||||||
|
//then
|
||||||
|
log.debug("Response Entity: {}", responseEntity);
|
||||||
|
assertThat(responseEntity.getStatusCode()).isEqualTo(OK);
|
||||||
|
assertThat(responseEntity.getBody())
|
||||||
|
.isNotNull()
|
||||||
|
.hasNoNullFieldsOrPropertiesExcept("lastLoginDate", "lastLoginDateDisplay")
|
||||||
|
.hasFieldOrPropertyWithValue("username", userDto.getUsername())
|
||||||
|
.hasFieldOrPropertyWithValue("email", userDto.getEmail())
|
||||||
|
.hasFieldOrPropertyWithValue("firstName", userDto.getFirstName())
|
||||||
|
.hasFieldOrPropertyWithValue("lastName", userDto.getLastName())
|
||||||
|
.hasFieldOrPropertyWithValue("isActive", true)
|
||||||
|
.hasFieldOrPropertyWithValue("isNotLocked", true)
|
||||||
|
.hasFieldOrPropertyWithValue("role", "ROLE_ADMIN");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user