Skip to content

Commit

Permalink
fix-api-auth-token
Browse files Browse the repository at this point in the history
  • Loading branch information
LarissaASLeite committed May 13, 2024
1 parent 7810c61 commit 25dc837
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@RequestMapping("/payhub")
interface AuthController {

@PostMapping("/auth")
@GetMapping("/auth")
@ResponseStatus(code = HttpStatus.OK)
void authToken(@RequestParam String token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(AuthControllerImpl.class)
Expand All @@ -35,23 +33,23 @@ class AuthControllerTest {
AuthService authServiceMock;

@Test
void authToken() throws Exception {
void authTokenOk() throws Exception {
doNothing().when(authServiceMock).authToken("token");

MvcResult result = mockMvc.perform(
post("/payhub/auth")
get("/payhub/auth")
.param("token", "token")
).andExpect(status().is2xxSuccessful()).andReturn();

Assertions.assertNotNull(result);
}

@Test
void authToken1() throws Exception {
void authTokenThrowMissingServletRequestParameterException() throws Exception {
doNothing().when(authServiceMock).authToken("token");

MvcResult result = mockMvc.perform(
post("/payhub/auth")
get("/payhub/auth")
).andExpect(status().isBadRequest()).andReturn();

ErrorDTO actual = objectMapper.readValue(result.getResponse().getContentAsString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void clean(){
wireMockServer.stop();
}
@Test
void authToken() throws Exception {
void authTokenOk() throws Exception {
String token = utils.generateJWK(EXPIRES_AT);
Map<String, String> claimsMap = createJWKClaims(ISS, AUD);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void clean(){
}

@Test
void validateToken() throws Exception {
void givenValidTokenThenSuccess() throws Exception {
String token = utils.generateJWK(new Date(System.currentTimeMillis() + 3600000));

String urlJwkProvider = utils.getUrlJwkProvider();
Expand All @@ -48,15 +48,15 @@ void validateToken() throws Exception {
}

@Test
void validate_ExpiredToken_ThrowsTokenExpiredException() throws Exception {
void givenTokenThenThrowTokenExpiredException() throws Exception {
String expiredToken = utils.generateJWK(new Date(System.currentTimeMillis() - 3600000));
String urlJwkProvider = utils.getUrlJwkProvider();

assertThrows(TokenExpiredException.class, () -> jwtValidator.validate(expiredToken, urlJwkProvider));
}

@Test
void validate_InvalidToken_ThrowsInvalidTokenException() {
void givenTokenThenThrowInvalidTokenException() {
String invalidToken = "your_invalid_token_here";
String urlJwkProvider = "your_jwk_provider_url_here";

Expand Down

0 comments on commit 25dc837

Please sign in to comment.