Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Oct 4, 2024
1 parent 0ca151f commit 7a1d45a
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 24 deletions.
4 changes: 4 additions & 0 deletions Graduating-Server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.time.LocalDate

@Entity
@Table(
name = "meal",
name = "tbl_meal",
uniqueConstraints = [
UniqueConstraint(
name = "UniqueMealDateAndMealTypeAndSchoolId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Graduating-Server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 <reified T> String.fromJson(): T = try {
jacksonObjectMapper().readValue<T>(this)
} catch (e: Exception) {
throw RuntimeException(e)
}
15 changes: 15 additions & 0 deletions Graduating-Server/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7a1d45a

Please sign in to comment.