-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: create Custom handler || handle custom exception * feat: format error response * feat: change error field to errorCode at ErrorResponse * chore: change some test codes * feat: apply pr * feat: handle 404 , add test code * docs: update open-api.yaml * feat: apply pr * feat: handle spring security exception * style: apply lintformat * feat: handle jwt error and add some tests * docs: update open-api.yaml --------- Co-authored-by: Git Actions <no-reply@github.com> Co-authored-by: Park Jeongseop <parkjeongseop@parkjeongseop.com>
- Loading branch information
1 parent
8eee37c
commit ff28981
Showing
36 changed files
with
591 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/integrationTest/kotlin/com/group4/ticketingservice/GlobalExceptionHandlerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.group4.ticketingservice | ||
|
||
import com.group4.ticketingservice.utils.exception.ErrorCodes | ||
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.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers | ||
|
||
@AutoConfigureMockMvc | ||
class GlobalExceptionHandlerTest : AbstractIntegrationTest() { | ||
@Autowired | ||
private lateinit var mockMvc: MockMvc | ||
|
||
@Test | ||
fun `GET_api_users_access_token_info should return HTTPStatus 403 Forbidden when jwt is expired`() { | ||
// expied jwt | ||
val jwt = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiaXNzIjoia21qIiwiaWF0IjoxNjk1ODg1ODk5LCJleHAiOjE2OTU4ODU4OTl9.ihz4uE9xP_TUU_GOe2pG8JkpyVofST4qqbIILnBeA20" | ||
val endpoint = "/users/access_token_info" | ||
val mvcResult = mockMvc.perform( | ||
MockMvcRequestBuilders.get(endpoint) | ||
.header("Authorization", jwt) | ||
) | ||
|
||
mvcResult.andExpect(MockMvcResultMatchers.status().isForbidden) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.timestamp").exists()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.errorCode").value(ErrorCodes.JWT_EXPIRED.errorCode)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").exists()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.path").value(endpoint)) | ||
} | ||
|
||
@Test | ||
fun `GET_api_users_access_token_info should return HTTPStatus 401 Unauthorized when jwt signature does not match`() { | ||
// jwt that signature does not match | ||
val jwt = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiaXNzIjoia21qIiwiaWF0IjoxNjk1ODg1ODk5LCJleHAiOjE2OTU4ODU4OTl9.Fer9Q0h5RpY9CHuSRWhqBfjILVEFZ0w-49j5jAg46hY" | ||
val endpoint = "/users/access_token_info" | ||
val mvcResult = mockMvc.perform( | ||
MockMvcRequestBuilders.get(endpoint) | ||
.header("Authorization", jwt) | ||
) | ||
|
||
mvcResult.andExpect(MockMvcResultMatchers.status().isUnauthorized) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.timestamp").exists()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.errorCode").value(ErrorCodes.JWT_AUTHENTICATION_FAILED.errorCode)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.message").exists()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.path").value(endpoint)) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/integrationTest/kotlin/com/group4/ticketingservice/ResponseFormatTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.group4.ticketingservice | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.group4.ticketingservice.dto.ErrorResponseDTO | ||
import com.group4.ticketingservice.utils.exception.ErrorCodes | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
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.http.HttpStatus | ||
import org.springframework.test.context.TestPropertySource | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get | ||
|
||
@AutoConfigureMockMvc | ||
@TestPropertySource(properties = ["spring.mvc.throw-exception-if-no-handler-found=true", "spring.web.resources.add-mappings=false"]) | ||
class ResponseFormatTest : AbstractIntegrationTest() { | ||
@Autowired | ||
private lateinit var mockMvc: MockMvc | ||
|
||
@Autowired | ||
lateinit var objectMapper: ObjectMapper | ||
|
||
@Test | ||
fun `check if default 404 response and ErrorResponseDTO have the same format`() { | ||
val mvcResult = mockMvc.perform(get("/non-existing-endpoint")) | ||
.andReturn() | ||
|
||
val response = mvcResult.response | ||
|
||
assertEquals(HttpStatus.NOT_FOUND.value(), response.status) | ||
|
||
val errorResponse = objectMapper.readValue(response.contentAsString, ErrorResponseDTO::class.java) | ||
|
||
assertNotNull(errorResponse.timestamp) | ||
assertEquals(ErrorCodes.END_POINT_NOT_FOUND.errorCode, errorResponse.errorCode) | ||
assertNotNull(errorResponse.message) | ||
assertEquals("/non-existing-endpoint", errorResponse.path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
src/main/kotlin/com/group4/ticketingservice/GlobalExceptionHandler.kt
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
src/main/kotlin/com/group4/ticketingservice/JwtAuthorizationEntryPoint.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/com/group4/ticketingservice/controller/TestController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.group4.ticketingservice.controller | ||
|
||
import com.group4.ticketingservice.utils.exception.CustomException | ||
import com.group4.ticketingservice.utils.exception.ErrorCodes | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
class TestController { | ||
@GetMapping("/test500") | ||
fun throwError(): String { | ||
throw RuntimeException("This is a test error.") | ||
} | ||
|
||
@PostMapping("/test") | ||
fun test(@RequestBody request: SampleDTO) { | ||
throw CustomException(ErrorCodes.TEST_ERROR) | ||
} | ||
} | ||
|
||
data class SampleDTO( | ||
val text: String | ||
) |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/group4/ticketingservice/dto/ResponseDTO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.group4.ticketingservice.dto | ||
|
||
import java.time.OffsetDateTime | ||
|
||
data class SuccessResponseDTO( | ||
val timestamp: OffsetDateTime = OffsetDateTime.now(), | ||
val message: String, | ||
val data: Any, | ||
val path: String | ||
|
||
) | ||
|
||
data class ErrorResponseDTO( | ||
val timestamp: OffsetDateTime = OffsetDateTime.now(), | ||
val errorCode: Int, | ||
val message: String, | ||
val path: String | ||
) |
Oops, something went wrong.