Skip to content

Commit

Permalink
chore: Refactor test utility classes - Use kotlin top level functions…
Browse files Browse the repository at this point in the history
… and variables
  • Loading branch information
sang-w0o committed Oct 23, 2021
1 parent f94bd80 commit eab82dc
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import com.template.auth.exception.UserIdNotFoundException
import com.template.auth.tools.JwtTokenUtil
import com.template.user.domain.User
import com.template.user.domain.UserRepository
import com.template.util.TestUtils.EMAIL
import com.template.util.TestUtils.NAME
import com.template.util.TestUtils.PASSWORD
import com.template.util.EMAIL
import com.template.util.NAME
import com.template.util.PASSWORD
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.springframework.beans.factory.annotation.Autowired
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/com/template/integration/auth/LoginTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.template.integration.auth
import com.jayway.jsonpath.JsonPath
import com.template.auth.dto.LoginRequestDto
import com.template.integration.ApiIntegrationTest
import com.template.util.TestUtils.EMAIL
import com.template.util.TestUtils.PASSWORD
import com.template.util.EMAIL
import com.template.util.PASSWORD
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.template.integration.user

import com.template.integration.ApiIntegrationTest
import com.template.util.TestUtils.EMAIL
import com.template.util.TestUtils.NAME
import com.template.util.EMAIL
import com.template.util.NAME
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.test.web.servlet.ResultActionsDsl
Expand Down
9 changes: 1 addition & 8 deletions src/test/kotlin/com/template/unit/BaseUnitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ package com.template.unit
import com.template.auth.tools.JwtProperties
import com.template.user.domain.User
import com.template.user.domain.UserRepository
import com.template.util.TestUtils.EMAIL
import com.template.util.TestUtils.EXTRA_TIME
import com.template.util.TestUtils.JWT_ACCESS_TOKEN_EXP
import com.template.util.TestUtils.JWT_REFRESH_TOKEN_EXP
import com.template.util.TestUtils.JWT_SECRET
import com.template.util.TestUtils.NAME
import com.template.util.TestUtils.PASSWORD
import com.template.util.TestUtils.USER_ID
import com.template.util.*
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import org.junit.jupiter.api.extension.ExtendWith
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.template.auth.exception.AuthenticateException
import com.template.auth.service.AuthService
import com.template.auth.tools.JwtTokenUtil
import com.template.unit.BaseUnitTest
import com.template.util.TestUtils.TOKEN
import com.template.util.TestUtils.USER_ID
import com.template.util.TOKEN
import com.template.util.USER_ID
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.BeforeEach
Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/com/template/unit/auth/JwtTokenUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.template.auth.exception.AuthenticateException
import com.template.auth.tools.JwtTokenUtil
import com.template.unit.BaseUnitTest
import com.template.user.dto.UserDto
import com.template.util.TestUtils.EXTRA_TIME
import com.template.util.TestUtils.USER_ID
import com.template.util.TestUtils.generateExpiredToken
import com.template.util.TestUtils.generateOtherSignatureToken
import com.template.util.EXTRA_TIME
import com.template.util.USER_ID
import com.template.util.generateExpiredToken
import com.template.util.generateOtherSignatureToken
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import org.junit.jupiter.api.Assertions.assertFalse
Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/com/template/unit/auth/LoginUnitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import com.template.auth.service.AuthService
import com.template.auth.tools.JwtTokenUtil
import com.template.unit.BaseUnitTest
import com.template.user.domain.User
import com.template.util.TestUtils.EMAIL
import com.template.util.TestUtils.NAME
import com.template.util.TestUtils.PASSWORD
import com.template.util.TestUtils.USER_ID
import com.template.util.EMAIL
import com.template.util.NAME
import com.template.util.PASSWORD
import com.template.util.USER_ID
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.BeforeEach
Expand Down
61 changes: 29 additions & 32 deletions src/test/kotlin/com/template/util/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,35 @@ import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import java.util.*

object TestUtils {
const val EMAIL = "[email protected]"
const val NAME = "testUserName"
const val PASSWORD = "testPassword"
const val USER_ID = 1
const val TOKEN = "token"
const val JWT_SECRET = "TestJwtSecretKey"
const val JWT_ACCESS_TOKEN_EXP = 86400000
const val JWT_REFRESH_TOKEN_EXP = 604800000
const val EXTRA_TIME = 2000000

const val EMAIL = "[email protected]"
const val NAME = "testUserName"
const val PASSWORD = "testPassword"
const val USER_ID = 1
const val TOKEN = "token"
const val JWT_SECRET = "TestJwtSecretKey"
const val JWT_ACCESS_TOKEN_EXP = 86400000
const val JWT_REFRESH_TOKEN_EXP = 604800000
const val EXTRA_TIME = 2000000

fun generateExpiredToken(exp: Int, secret: String): String {
val realExp = EXTRA_TIME + exp
val claims: MutableMap<String, Any> = mutableMapOf()
claims["userId"] = USER_ID
return Jwts.builder()
.setClaims(claims)
.setIssuedAt(Date(System.currentTimeMillis() - realExp))
.setExpiration(Date(System.currentTimeMillis() - EXTRA_TIME))
.signWith(SignatureAlgorithm.HS256, secret)
.compact()
}
fun generateExpiredToken(exp: Int, secret: String): String {
val realExp = EXTRA_TIME + exp
val claims: MutableMap<String, Any> = mutableMapOf()
claims["userId"] = USER_ID
return Jwts.builder()
.setClaims(claims)
.setIssuedAt(Date(System.currentTimeMillis() - realExp))
.setExpiration(Date(System.currentTimeMillis() - EXTRA_TIME))
.signWith(SignatureAlgorithm.HS256, secret)
.compact()
}

fun generateOtherSignatureToken(exp: Int): String {
val claims: MutableMap<String, Any> = mutableMapOf()
claims["userId"] = USER_ID
return Jwts.builder()
.setClaims(claims)
.setIssuedAt(Date(System.currentTimeMillis()))
.setExpiration(Date(System.currentTimeMillis() + exp))
.signWith(SignatureAlgorithm.HS256, "Other Signature")
.compact()
}
fun generateOtherSignatureToken(exp: Int): String {
val claims: MutableMap<String, Any> = mutableMapOf()
claims["userId"] = USER_ID
return Jwts.builder()
.setClaims(claims)
.setIssuedAt(Date(System.currentTimeMillis()))
.setExpiration(Date(System.currentTimeMillis() + exp))
.signWith(SignatureAlgorithm.HS256, "Other Signature")
.compact()
}

0 comments on commit eab82dc

Please sign in to comment.