36.2 Test endpoint for security - correct JWT (#2)
This commit is contained in:
@ -15,7 +15,7 @@ public class JwtConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
public JWTVerifier jwtVerifier(@Value("${app.jwt.secret}") String secret) {
|
public JWTVerifier jwtVerifier(@Value("${app.jwt.secret}") String secret) {
|
||||||
|
|
||||||
Algorithm algorithm = Algorithm.HMAC256(secret);
|
Algorithm algorithm = Algorithm.HMAC512(secret);
|
||||||
return JWT.require(algorithm)
|
return JWT.require(algorithm)
|
||||||
.withIssuer(GET_ARRAYS_LLC)
|
.withIssuer(GET_ARRAYS_LLC)
|
||||||
.build(); //Reusable verifier instance
|
.build(); //Reusable verifier instance
|
||||||
|
|||||||
@ -12,7 +12,8 @@ spring:
|
|||||||
dialect: org.hibernate.dialect.MySQL8Dialect
|
dialect: org.hibernate.dialect.MySQL8Dialect
|
||||||
app:
|
app:
|
||||||
jwt:
|
jwt:
|
||||||
secret: ${random.value}
|
secret: VeRy_5ecretP@55W0rd!
|
||||||
|
# secret: ${random.value} #Does not work - every time generates new value
|
||||||
---
|
---
|
||||||
spring:
|
spring:
|
||||||
config:
|
config:
|
||||||
|
|||||||
@ -1,27 +1,35 @@
|
|||||||
package net.shyshkin.study.fullstack.supportportal.backend.controller;
|
package net.shyshkin.study.fullstack.supportportal.backend.controller;
|
||||||
|
|
||||||
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.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.UserPrincipal;
|
||||||
|
import net.shyshkin.study.fullstack.supportportal.backend.utility.JwtTokenProvider;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.RequestEntity;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||||
import static org.springframework.http.HttpStatus.FORBIDDEN;
|
import static org.springframework.http.HttpStatus.FORBIDDEN;
|
||||||
|
import static org.springframework.http.HttpStatus.OK;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
@ActiveProfiles("local")
|
class UserResourceTest extends BaseUserTest {
|
||||||
class UserResourceTest {
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
TestRestTemplate restTemplate;
|
TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
JwtTokenProvider jwtTokenProvider;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void showUser_forbidden() {
|
void showUserHome_forbidden() {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
var responseEntity = restTemplate.getForEntity("/user/home", HttpResponse.class);
|
var responseEntity = restTemplate.getForEntity("/user/home", HttpResponse.class);
|
||||||
@ -39,4 +47,28 @@ class UserResourceTest {
|
|||||||
() -> assertThat(httpResponse.getMessage()).isEqualTo("You need to log in to access this page")
|
() -> assertThat(httpResponse.getMessage()).isEqualTo("You need to log in to access this page")
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void showUserHome_correctToken() {
|
||||||
|
|
||||||
|
//given
|
||||||
|
User fakeUser = createRandomUser();
|
||||||
|
user = userRepository.save(fakeUser);
|
||||||
|
String validToken = jwtTokenProvider.generateJwtToken(new UserPrincipal(user));
|
||||||
|
log.debug("JWT Token: `{}`", validToken);
|
||||||
|
|
||||||
|
//when
|
||||||
|
RequestEntity<?> requestEntity = RequestEntity
|
||||||
|
.get("/user/home")
|
||||||
|
.header(HttpHeaders.AUTHORIZATION, "Bearer " + validToken)
|
||||||
|
.build();
|
||||||
|
var responseEntity = restTemplate.exchange(requestEntity, String.class);
|
||||||
|
|
||||||
|
//then
|
||||||
|
log.debug("Response Entity: {}", responseEntity);
|
||||||
|
assertThat(responseEntity.getStatusCode()).isEqualTo(OK);
|
||||||
|
assertThat(responseEntity.getBody())
|
||||||
|
.isNotNull()
|
||||||
|
.isEqualTo("Application works");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user