-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feat/edit-chat
- Loading branch information
Showing
63 changed files
with
1,052 additions
and
252 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
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
28 changes: 28 additions & 0 deletions
28
chat/src/main/kotlin/kpring/chat/chatroom/repository/InvitationRepository.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,28 @@ | ||
package kpring.chat.chatroom.repository | ||
|
||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.data.redis.core.ValueOperations | ||
import org.springframework.stereotype.Component | ||
import java.time.Duration | ||
|
||
@Component | ||
class InvitationRepository( | ||
private val redisTemplate: RedisTemplate<String, String>, | ||
) { | ||
fun getValue(key: String): String? { | ||
return redisTemplate.opsForValue().get(key) | ||
} | ||
|
||
fun setValueAndExpiration( | ||
key: String, | ||
value: String, | ||
expiration: Duration, | ||
) { | ||
val ops: ValueOperations<String, String> = redisTemplate.opsForValue() | ||
ops.set(key, value, expiration) | ||
} | ||
|
||
fun getExpiration(key: String): Long { | ||
return redisTemplate.getExpire(key) | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
chat/src/main/kotlin/kpring/chat/chatroom/service/InvitationService.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,62 @@ | ||
package kpring.chat.chatroom.service | ||
|
||
import kpring.chat.chatroom.repository.InvitationRepository | ||
import kpring.chat.global.config.ChatRoomProperty | ||
import org.springframework.stereotype.Service | ||
import java.nio.charset.StandardCharsets | ||
import java.security.MessageDigest | ||
import java.util.* | ||
|
||
@Service | ||
class InvitationService( | ||
private val invitationRepository: InvitationRepository, | ||
private val chatRoomProperty: ChatRoomProperty, | ||
) { | ||
fun getInvitation( | ||
userId: String, | ||
chatRoomId: String, | ||
): String? { | ||
val key = generateKey(userId, chatRoomId) | ||
return invitationRepository.getValue(key) | ||
} | ||
|
||
fun setInvitation( | ||
userId: String, | ||
chatRoomId: String, | ||
): String { | ||
val key = generateKey(userId, chatRoomId) | ||
val value = generateValue() | ||
invitationRepository.setValueAndExpiration(key, value, chatRoomProperty.getExpiration()) | ||
return value | ||
} | ||
|
||
fun generateKeyAndCode( | ||
userId: String, | ||
chatRoomId: String, | ||
code: String, | ||
): String { | ||
val key = generateKey(userId, chatRoomId) | ||
return generateCode(key, code) | ||
} | ||
|
||
private fun generateCode( | ||
key: String, | ||
value: String, | ||
): String { | ||
val combinedString = "$key,$value" | ||
val digest = MessageDigest.getInstance("SHA-256") | ||
val hash = digest.digest(combinedString.toByteArray(StandardCharsets.UTF_8)) | ||
return Base64.getEncoder().encodeToString(hash) | ||
} | ||
|
||
private fun generateKey( | ||
userId: String, | ||
chatRoomId: String, | ||
): String { | ||
return "$userId:$chatRoomId" | ||
} | ||
|
||
private fun generateValue(): String { | ||
return UUID.randomUUID().toString() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
chat/src/main/kotlin/kpring/chat/global/config/ChatRoomProperty.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,19 @@ | ||
package kpring.chat.global.config | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.context.annotation.Configuration | ||
import java.time.Duration | ||
|
||
@Configuration | ||
@ConfigurationProperties(prefix = "chatroom") | ||
class ChatRoomProperty { | ||
private lateinit var expiration: Duration | ||
|
||
fun getExpiration(): Duration { | ||
return expiration | ||
} | ||
|
||
fun setExpiration(expiration: Duration) { | ||
this.expiration = expiration | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
chat/src/main/kotlin/kpring/chat/global/config/RedisConfig.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,16 @@ | ||
package kpring.chat.global.config | ||
|
||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.data.redis.connection.RedisConnectionFactory | ||
import org.springframework.data.redis.core.RedisTemplate | ||
|
||
@Configuration | ||
class RedisConfig { | ||
@Bean | ||
fun redisTemplate(connectionFactory: RedisConnectionFactory): RedisTemplate<*, *> { | ||
val template = RedisTemplate<ByteArray, ByteArray>() | ||
template.connectionFactory = connectionFactory | ||
return template | ||
} | ||
} |
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
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
102 changes: 102 additions & 0 deletions
102
chat/src/test/kotlin/kpring/chat/chatroom/api/v1/ChatRoomControllerTest.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,102 @@ | ||
package kpring.chat.chat.api.v1 | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.ninjasquad.springmockk.MockkBean | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.mockk.every | ||
import io.mockk.junit5.MockKExtension | ||
import kpring.chat.chatroom.api.v1.ChatRoomController | ||
import kpring.chat.chatroom.service.ChatRoomService | ||
import kpring.chat.global.ChatRoomTest | ||
import kpring.chat.global.CommonTest | ||
import kpring.core.auth.client.AuthClient | ||
import kpring.core.auth.dto.response.TokenInfo | ||
import kpring.core.auth.enums.TokenType | ||
import kpring.core.chat.chat.dto.response.InvitationResponse | ||
import kpring.core.global.dto.response.ApiResponse | ||
import kpring.test.restdoc.dsl.restDoc | ||
import kpring.test.restdoc.json.JsonDataType | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | ||
import org.springframework.restdocs.ManualRestDocumentation | ||
import org.springframework.restdocs.RestDocumentationExtension | ||
import org.springframework.restdocs.operation.preprocess.Preprocessors | ||
import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation | ||
import org.springframework.test.context.junit.jupiter.SpringExtension | ||
import org.springframework.test.web.reactive.server.WebTestClient | ||
import org.springframework.test.web.servlet.client.MockMvcWebTestClient | ||
import org.springframework.web.context.WebApplicationContext | ||
|
||
@WebMvcTest(controllers = [ChatRoomController::class]) | ||
@ExtendWith(RestDocumentationExtension::class) | ||
@ExtendWith(SpringExtension::class) | ||
@ExtendWith(MockKExtension::class) | ||
class ChatRoomControllerTest( | ||
private val om: ObjectMapper, | ||
webContext: WebApplicationContext, | ||
@MockkBean val chatRoomService: ChatRoomService, | ||
@MockkBean val authClient: AuthClient, | ||
) : DescribeSpec({ | ||
|
||
val restDocument = ManualRestDocumentation() | ||
val webTestClient: WebTestClient = | ||
MockMvcWebTestClient.bindToApplicationContext(webContext).configureClient().baseUrl("http://localhost:8081").filter( | ||
WebTestClientRestDocumentation.documentationConfiguration(restDocument).operationPreprocessors() | ||
.withRequestDefaults(Preprocessors.prettyPrint()).withResponseDefaults(Preprocessors.prettyPrint()), | ||
).build() | ||
|
||
beforeSpec { restDocument.beforeTest(this.javaClass, "chat controller") } | ||
|
||
afterSpec { restDocument.afterTest() } | ||
|
||
describe("GET /api/v1/chatroom/{chatRoomId}/invite : getChatRoomInvitation api test") { | ||
|
||
val url = "/api/v1/chatroom/{chatRoomId}/invite" | ||
it("getChatRoomInvitation api test") { | ||
|
||
// Given | ||
val chatRoomId = ChatRoomTest.TEST_ROOM_ID | ||
val userId = CommonTest.TEST_USER_ID | ||
val key = "62e9df6b-13cb-4673-a6fe-8566451b7f15" | ||
val data = InvitationResponse(key) | ||
|
||
every { authClient.getTokenInfo(any()) } returns | ||
ApiResponse( | ||
data = | ||
TokenInfo( | ||
type = TokenType.ACCESS, userId = CommonTest.TEST_USER_ID, | ||
), | ||
) | ||
|
||
every { | ||
chatRoomService.getChatRoomInvitation( | ||
chatRoomId, | ||
userId, | ||
) | ||
} returns data | ||
|
||
// When | ||
val result = webTestClient.get().uri(url, chatRoomId).header("Authorization", "Bearer mock_token").exchange() | ||
|
||
val docs = result.expectStatus().isOk.expectBody().json(om.writeValueAsString(ApiResponse(data = data))) | ||
|
||
// Then | ||
docs.restDoc( | ||
identifier = "getChatRoomInvitation_200", | ||
description = "채팅방 참여코드를 위한 key값을 반환하는 api", | ||
) { | ||
request { | ||
path { | ||
"chatRoomId" mean "채팅방 참여코드를 발급할 채팅방 Id" | ||
} | ||
} | ||
|
||
response { | ||
body { | ||
"data.code" type JsonDataType.Strings mean "참여 코드" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) |
5 changes: 5 additions & 0 deletions
5
core/src/main/kotlin/kpring/core/chat/chat/dto/response/InvitationResponse.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,5 @@ | ||
package kpring.core.chat.chat.dto.response | ||
|
||
data class InvitationResponse( | ||
val code: String, | ||
) |
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 |
---|---|---|
|
@@ -21,3 +21,4 @@ | |
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
Chart.lock |
Oops, something went wrong.