Срок действия jwt принят в Spring Boot TestJAVA

Программисты JAVA общаются здесь
Anonymous
Срок действия jwt принят в Spring Boot Test

Сообщение Anonymous »

Разве весна не проверяйте дату истечения срока действия токена JWT, по крайней мере, в тестах?
org.springframework.boot
spring-boot-starter-parent
3.4.5






org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-oauth2-resource-server


org.springframework.boot
spring-boot-starter-test
test

< /code>
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.time.Instant;
import java.time.temporal.ChronoUnit;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class TransferControllerTest {

@Autowired
MockMvc mockMvc;

@Test
void testUnauthorized() throws Exception {
mockMvc
.perform(patch("/api/money-transfer")
.with(jwt().jwt(jwt -> jwt
.claim("userid", 15L)
.expiresAt(Instant.now().minus(1, ChronoUnit.DAYS))))
.andExpect(status().isUnauthorized());
}
}
< /code>
package com.example.pixel_money_transfer_api.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/money-transfer")
public class TransferController {

@PatchMapping
public ResponseEntity performTransfer() {
return ResponseEntity.ok().build(); // gutted every bit of logic
}
}
< /code>
java.lang.AssertionError: Status expected: but was:
Expected :401
Actual :200


Подробнее здесь: https://stackoverflow.com/questions/796 ... -boot-test

Вернуться в «JAVA»