36.1 Test endpoint for security - Forbidden (#2)

This commit is contained in:
Art
2021-09-06 08:07:10 +03:00
parent be012f222e
commit 0656dc9969
4 changed files with 62 additions and 1 deletions

View File

@ -0,0 +1,15 @@
package net.shyshkin.study.fullstack.supportportal.backend.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class UserResource {
@GetMapping("home")
public String showUser() {
return "Application works";
}
}

View File

@ -6,11 +6,15 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import java.time.LocalDateTime;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Data @Data
@Builder @Builder
public class HttpResponse { public class HttpResponse {
// @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'hh:mm:ss.SSS")
private final LocalDateTime timestamp = LocalDateTime.now();
private int httpStatusCode; // 200, 201, 400, 500 private int httpStatusCode; // 200, 201, 400, 500
private HttpStatus httpStatus; private HttpStatus httpStatus;
private String reason; private String reason;

View File

@ -27,7 +27,7 @@ public abstract class BaseUserTest {
.firstName(FAKER.name().firstName()) .firstName(FAKER.name().firstName())
.lastName(FAKER.name().lastName()) .lastName(FAKER.name().lastName())
.username(FAKER.name().username()) .username(FAKER.name().username())
.password("bad_password") .password("{noop}bad_password")
.userId(UUID.randomUUID().toString()) .userId(UUID.randomUUID().toString())
.isActive(true) .isActive(true)
.isNotLocked(true) .isNotLocked(true)

View File

@ -0,0 +1,42 @@
package net.shyshkin.study.fullstack.supportportal.backend.controller;
import lombok.extern.slf4j.Slf4j;
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
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.test.context.ActiveProfiles;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.springframework.http.HttpStatus.FORBIDDEN;
@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("local")
class UserResourceTest {
@Autowired
TestRestTemplate restTemplate;
@Test
void showUser_forbidden() {
//when
var responseEntity = restTemplate.getForEntity("/user/home", HttpResponse.class);
//then
log.debug("Response Entity: {}", responseEntity);
assertThat(responseEntity.getStatusCode()).isEqualTo(FORBIDDEN);
assertThat(responseEntity.getBody())
.isNotNull()
.hasNoNullFieldsOrProperties()
.satisfies(httpResponse -> assertAll(
() -> assertThat(httpResponse.getHttpStatusCode()).isEqualTo(403),
() -> assertThat(httpResponse.getHttpStatus()).isEqualTo(FORBIDDEN),
() -> assertThat(httpResponse.getReason()).isEqualTo("FORBIDDEN"),
() -> assertThat(httpResponse.getMessage()).isEqualTo("You need to log in to access this page")
));
}
}