Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: PUT /users/me #109

Merged
merged 10 commits into from
Jul 9, 2023
Merged

feature: PUT /users/me #109

merged 10 commits into from
Jul 9, 2023

Conversation

lohas1107
Copy link
Contributor

Why need this change? / Root cause:

  • As a user, I want update my nickname

Changes made:

  • User related

Test Scope / Change impact:

  • UserController

Issue

Comment on lines 30 to 43
private fun validateNicknameDuplicated(nickname: String) {
if (userRepository.existsUserByNickname(nickname)) {
throw PlatformException("invalid nickname: duplicated")
}
}

private fun validateNicknameLength(nickname: String) {
if (nickname.toByteArray().size < 4) {
throw PlatformException("invalid nickname: too short")
}
if (nickname.toByteArray().size > 16) {
throw PlatformException("invalid nickname: too long")
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q:為什麼需要先轉換成 ByteArray

Copy link
Contributor Author

@lohas1107 lohas1107 Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因為之前的結論是限制字元長度,所以這邊先轉成字元再判斷長度:
最少 4 英文字元 (= 2 中文字)
最長 16 英文字元 (= 8 中文字)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那可以把 nickname.toByteArray() 宣告成變數 🐣

Copy link
Collaborator

@Wally5077 Wally5077 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review done.

@lohas1107 lohas1107 requested a review from Wally5077 July 7, 2023 15:02
Comment on lines 14 to 15
private val nicknameByteSizeMinimum: Int = 4
private val nicknameByteSizeMaximum: Int = 16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

companion object {
    private const val NICK_NAME_MINIMUM_BYTE_SIZE = 4
    private const val NICK_NAME_MAXIMUM_BYTE_SIZE = 16
} 

private val nicknameByteSizeMaximum: Int = 16

fun updateNickname(nickname: String) {
if (nickname.toByteArray().size < nicknameByteSizeMinimum) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nickname.toByteArray() 用變數接

Comment on lines 47 to 51
var Jwt.email: String?
get() = claims["email"] as String?
set(value) {
claims["email"] = value
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private val Jwt.email: String
    get() = claims["email"]?.let { it as String }
        ?: throw PlatformException("JWT email should exist.")

Comment on lines 44 to 47
fun givenUserNickname_whenUpdateEnglishNickname_thenShouldUpdateNickname() {
givenUserNickname()
.whenUpdateUserSelf(UpdateUserRequest("my nick name"))
.thenShouldUpdateNickname("my nick name")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 這個測試案例可以改成這樣:
fun givenUserNamedNeverever_whenChangeUserNicknameToAbc_thenUserNickNameShouldBeAbc() {
    givenUserNickname("Neverever")
        .whenChangeUserNickName(UpdateUserRequest("abc"))
        .thenShouldUpdateNickname("abc")
}
  1. 下面的測試案例名稱可以依照上方範例檢視然後修改

private val nicknameByteSizeMinimum: Int = 4
private val nicknameByteSizeMaximum: Int = 16

fun updateNickname(nickname: String) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fun changeNickname(nickname: String)

Comment on lines 85 to 87
private fun givenUserNickname(): User {
val user = User(User.Id("1"), "userA@example.com", "Neverever", mockUser.identities)
return userRepository.createUser(user)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在存進資料庫前不會有任何 UserId 存在

}

private fun givenAnotherUserNickname(): User {
val user = User(User.Id("2"), "userB@example.com", "周杰倫", mockUser.identities)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto,在存進資料庫前不會有任何 UserId 存在

Comment on lines 134 to 135
.getBody(PlatformViewModel::class.java)
.also { assertThat(it.message).contains(message) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 應該可以用 jsonPath 比對
  2. exists() 改為 value() 就可以比對了

.also { assertThat(it!!.nickname).isEqualTo(nickname) }
}

private fun ResultActions.thenShouldUpdateNicknameFailed(message: String) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thenShouldChangeNicknameFailed

@@ -59,4 +119,20 @@ class UserControllerTest @Autowired constructor(
.andExpect(jsonPath("$.nickname").doesNotExist())
}

private fun ResultActions.thenShouldUpdateNickname(nickname: String) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thenUserNickNameSholdBeChanged

@lohas1107 lohas1107 requested a review from frankvicky July 7, 2023 23:35
Copy link
Collaborator

@Wally5077 Wally5077 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review done.

Copy link
Collaborator

@Wally5077 Wally5077 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sonarcloud
Copy link

sonarcloud bot commented Jul 8, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@lohas1107 lohas1107 merged commit b199716 into main Jul 9, 2023
5 checks passed
@lohas1107 lohas1107 deleted the feature/update-user-info branch July 9, 2023 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PUT /users/me 修改使用者暱稱
3 participants