diff --git a/Graduating-Server/build.gradle.kts b/Graduating-Server/build.gradle.kts index 51ab2a1..7a86234 100644 --- a/Graduating-Server/build.gradle.kts +++ b/Graduating-Server/build.gradle.kts @@ -54,8 +54,12 @@ dependencies { // DB runtimeOnly("com.mysql:mysql-connector-j") + // Test testImplementation("org.springframework.boot:spring-boot-starter-test") + testImplementation("org.springframework.security:spring-security-test") testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") + testImplementation("com.h2database:h2:2.1.214") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") } diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthService.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthService.kt index fe69fb7..05648b7 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthService.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthService.kt @@ -24,29 +24,29 @@ class AuthService( private val googleOAuth2Client: GoogleOAuth2Client, private val appleOAuth2Client: AppleOAuth2Client, private val appleOAuth2Helper: AppleOAuth2Helper, - private val jwtUtils: JwtClient, + private val jwtClient: JwtClient, private val googleOAuth2Helper: GoogleOAuth2Helper ) { fun refresh(req: RefreshReq): TokenRes { - jwtUtils.parseToken(req.refreshToken) + jwtClient.parseToken(req.refreshToken) val user = run { - val username = jwtUtils.payload(JwtPayloadKey.USERNAME, req.refreshToken) + val username = jwtClient.payload(JwtPayloadKey.USERNAME, req.refreshToken) userRepository.getByUsername(username) } - return jwtUtils.generate(user) + return jwtClient.generate(user) } fun oAuth2SignIn(req: OAuth2SignInReq): TokenRes { - val token = when (req.platformType) { + val user = when (req.platformType) { PlatformType.GOOGLE -> googleSignIn(req) PlatformType.APPLE -> appleSignIn(req) else -> throw CustomException(HttpStatus.BAD_REQUEST, "Invalid platform type") } - return jwtUtils.generate(token) + return jwtClient.generate(user) } private fun googleSignIn(req: OAuth2SignInReq): User { diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/graduating/GraduatingEntity.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/graduating/GraduatingEntity.kt index da7c330..7dee4be 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/graduating/GraduatingEntity.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/graduating/GraduatingEntity.kt @@ -3,7 +3,7 @@ package com.bestswlkh0310.graduating.graduatingserver.core.graduating import jakarta.persistence.* @Entity -@Table(name = "graduating") +@Table(name = "tbl_graduating") class GraduatingEntity( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/meal/MealEntity.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/meal/MealEntity.kt index 42dfbf2..3f28a63 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/meal/MealEntity.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/meal/MealEntity.kt @@ -6,7 +6,7 @@ import java.time.LocalDate @Entity @Table( - name = "meal", + name = "tbl_meal", uniqueConstraints = [ UniqueConstraint( name = "UniqueMealDateAndMealTypeAndSchoolId", diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/school/SchoolEntity.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/school/SchoolEntity.kt index 9b8bc8a..df27648 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/school/SchoolEntity.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/school/SchoolEntity.kt @@ -4,7 +4,7 @@ import jakarta.persistence.* import java.time.LocalDate @Entity -@Table(name = "school") +@Table(name = "tbl_school") class SchoolEntity( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/user/User.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/user/User.kt index 7dc20ea..b8671c3 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/user/User.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/user/User.kt @@ -2,7 +2,7 @@ package com.bestswlkh0310.graduating.graduatingserver.core.user import jakarta.persistence.* -@Entity(name = "`user`") +@Entity(name = "tbl_user") class User( id: Long = 0, username: String, diff --git a/Graduating-Server/src/main/resources/application.yml b/Graduating-Server/src/main/resources/application.yml index 76c9220..62845b2 100644 --- a/Graduating-Server/src/main/resources/application.yml +++ b/Graduating-Server/src/main/resources/application.yml @@ -19,7 +19,7 @@ neis: apikey: ${NEIS_APIKEY} jwt: expired: - access: 3600_000 + access: 3_600_000 refresh: 300_000_000 secret-key: ${JWT_SECRET} oauth2: diff --git a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/AuthControllerTest.kt b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/AuthControllerTest.kt new file mode 100644 index 0000000..9e27a8a --- /dev/null +++ b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/AuthControllerTest.kt @@ -0,0 +1,46 @@ +package com.bestswlkh0310.graduating.graduatingserver + +import com.bestswlkh0310.graduating.graduatingserver.api.auth.res.TokenRes +import com.bestswlkh0310.graduating.graduatingserver.core.user.PlatformType +import com.bestswlkh0310.graduating.graduatingserver.core.user.User +import com.bestswlkh0310.graduating.graduatingserver.core.user.UserRepository +import com.bestswlkh0310.graduating.graduatingserver.infra.token.JwtClient +import com.bestswlkh0310.graduating.graduatingserver.util.TestAnnotation +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.web.servlet.MockMvc + +@TestAnnotation +class AuthControllerTest { + + @Autowired + lateinit var userRepository: UserRepository + @Autowired + lateinit var jwtClient: JwtClient + @Autowired + lateinit var mvc: MockMvc + + private var token: TokenRes? = null + + @BeforeEach + fun beforeEach() { + val user = userRepository.save( + User( + id = 0, + username = "hhhello0507@gmail.com", + nickname = "testuser", + platformType = PlatformType.GOOGLE + ) + ) + token = jwtClient.generate(user) + } + + @Test + fun `sign up`() { +// mvc.perform( +// MockMvcRequestBuilders.post("/api/auth/signup") +// ) + println(token) + } +} diff --git a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/GraduatingServerApplicationTests.kt b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/GraduatingServerApplicationTests.kt deleted file mode 100644 index e8bcbf1..0000000 --- a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/GraduatingServerApplicationTests.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.bestswlkh0310.graduating.graduatingserver - -import org.junit.jupiter.api.Test -import org.springframework.boot.test.context.SpringBootTest - -@SpringBootTest -class GraduatingServerApplicationTests { - - @Test - fun contextLoads() { - } - -} diff --git a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/util/TestAnnotation.kt b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/util/TestAnnotation.kt new file mode 100644 index 0000000..dcb42e6 --- /dev/null +++ b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/util/TestAnnotation.kt @@ -0,0 +1,20 @@ +package com.bestswlkh0310.graduating.graduatingserver.util + +import org.springframework.boot.jdbc.EmbeddedDatabaseConnection +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.annotation.DirtiesContext +import org.springframework.test.context.TestPropertySource + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@SpringBootTest +@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) // reset DB +@AutoConfigureTestDatabase( + connection = EmbeddedDatabaseConnection.H2, + replace = AutoConfigureTestDatabase.Replace.ANY +) +@TestPropertySource("classpath:application-test.yml") +@AutoConfigureMockMvc +annotation class TestAnnotation \ No newline at end of file diff --git a/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/util/toJson.kt b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/util/toJson.kt new file mode 100644 index 0000000..d08fa8a --- /dev/null +++ b/Graduating-Server/src/test/kotlin/com/bestswlkh0310/graduating/graduatingserver/util/toJson.kt @@ -0,0 +1,16 @@ +package com.bestswlkh0310.graduating.graduatingserver.util + +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue + +fun Any.toJson(): String = try { + jacksonObjectMapper().writeValueAsString(this) +} catch (e: Exception) { + throw RuntimeException(e) +} + +inline fun String.fromJson(): T = try { + jacksonObjectMapper().readValue(this) +} catch (e: Exception) { + throw RuntimeException(e) +} \ No newline at end of file diff --git a/Graduating-Server/src/test/resources/application-test.yml b/Graduating-Server/src/test/resources/application-test.yml new file mode 100644 index 0000000..a2311cb --- /dev/null +++ b/Graduating-Server/src/test/resources/application-test.yml @@ -0,0 +1,15 @@ +spring: + datasource: + url: jdbc:h2:mem:testdb + # username: + # password: + driver-class-name: org.h2.Driver + h2: + console: + enabled: true + jpa: + database-platform: org.hibernate.dialect.H2Dialect + hibernate: + ddl-auto: + update + show-sql: true \ No newline at end of file