1
0

userutil_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2022 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE and LICENSE.gogs file.
  4. // Copyright 2025 Huan-Gogs Authors. All rights reserved.
  5. // Use of this source code is governed by a MIT-style
  6. // license that can be found in the LICENSE file.
  7. package userutil
  8. import (
  9. "os"
  10. "runtime"
  11. "testing"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. "github.com/SongZihuan/huan-gogs/internal/conf"
  15. "github.com/SongZihuan/huan-gogs/internal/osutil"
  16. "github.com/SongZihuan/huan-gogs/public"
  17. )
  18. func TestDashboardURLPath(t *testing.T) {
  19. t.Run("user", func(t *testing.T) {
  20. got := DashboardURLPath("alice", false)
  21. want := "/"
  22. assert.Equal(t, want, got)
  23. })
  24. t.Run("organization", func(t *testing.T) {
  25. got := DashboardURLPath("acme", true)
  26. want := "/org/acme/dashboard/"
  27. assert.Equal(t, want, got)
  28. })
  29. }
  30. func TestCustomAvatarPath(t *testing.T) {
  31. if runtime.GOOS == "windows" {
  32. t.Skip("Skipping testing on Windows")
  33. return
  34. }
  35. conf.SetMockPicture(t,
  36. conf.PictureOpts{
  37. AvatarUploadPath: "data/avatars",
  38. },
  39. )
  40. got := CustomAvatarPath(1)
  41. want := "data/avatars/1"
  42. assert.Equal(t, want, got)
  43. }
  44. func TestGenerateRandomAvatar(t *testing.T) {
  45. if runtime.GOOS == "windows" {
  46. t.Skip("Skipping testing on Windows")
  47. return
  48. }
  49. conf.SetMockPicture(t,
  50. conf.PictureOpts{
  51. AvatarUploadPath: os.TempDir(),
  52. },
  53. )
  54. avatarPath := CustomAvatarPath(1)
  55. defer func() { _ = os.Remove(avatarPath) }()
  56. err := GenerateRandomAvatar(1, "alice", "alice@example.com")
  57. require.NoError(t, err)
  58. got := osutil.IsFile(avatarPath)
  59. assert.True(t, got)
  60. }
  61. func TestSaveAvatar(t *testing.T) {
  62. if runtime.GOOS == "windows" {
  63. t.Skip("Skipping testing on Windows")
  64. return
  65. }
  66. conf.SetMockPicture(t,
  67. conf.PictureOpts{
  68. AvatarUploadPath: os.TempDir(),
  69. },
  70. )
  71. avatar, err := public.Files.ReadFile("img/avatar_default.png")
  72. require.NoError(t, err)
  73. avatarPath := CustomAvatarPath(1)
  74. defer func() { _ = os.Remove(avatarPath) }()
  75. err = SaveAvatar(1, avatar)
  76. require.NoError(t, err)
  77. got := osutil.IsFile(avatarPath)
  78. assert.True(t, got)
  79. }
  80. func TestEncodePassword(t *testing.T) {
  81. want := EncodePassword("123456", "rands")
  82. tests := []struct {
  83. name string
  84. password string
  85. rands string
  86. wantEqual bool
  87. }{
  88. {
  89. name: "correct",
  90. password: "123456",
  91. rands: "rands",
  92. wantEqual: true,
  93. },
  94. {
  95. name: "wrong password",
  96. password: "111333",
  97. rands: "rands",
  98. wantEqual: false,
  99. },
  100. {
  101. name: "wrong salt",
  102. password: "111333",
  103. rands: "salt",
  104. wantEqual: false,
  105. },
  106. }
  107. for _, test := range tests {
  108. t.Run(test.name, func(t *testing.T) {
  109. got := EncodePassword(test.password, test.rands)
  110. if test.wantEqual {
  111. assert.Equal(t, want, got)
  112. } else {
  113. assert.NotEqual(t, want, got)
  114. }
  115. })
  116. }
  117. }
  118. func TestValidatePassword(t *testing.T) {
  119. want := EncodePassword("123456", "rands")
  120. tests := []struct {
  121. name string
  122. password string
  123. rands string
  124. wantEqual bool
  125. }{
  126. {
  127. name: "correct",
  128. password: "123456",
  129. rands: "rands",
  130. wantEqual: true,
  131. },
  132. {
  133. name: "wrong password",
  134. password: "111333",
  135. rands: "rands",
  136. wantEqual: false,
  137. },
  138. {
  139. name: "wrong salt",
  140. password: "111333",
  141. rands: "salt",
  142. wantEqual: false,
  143. },
  144. }
  145. for _, test := range tests {
  146. t.Run(test.name, func(t *testing.T) {
  147. got := ValidatePassword(want, test.rands, test.password)
  148. assert.Equal(t, test.wantEqual, got)
  149. })
  150. }
  151. }
  152. func TestMailResendCacheKey(t *testing.T) {
  153. got := MailResendCacheKey(1)
  154. assert.Equal(t, "mailResend::1", got)
  155. }
  156. func TestTwoFactorCacheKey(t *testing.T) {
  157. got := TwoFactorCacheKey(1, "113654")
  158. assert.Equal(t, "twoFactor::1::113654", got)
  159. }
  160. func TestRandomSalt(t *testing.T) {
  161. salt1, err := RandomSalt()
  162. require.NoError(t, err)
  163. salt2, err := RandomSalt()
  164. require.NoError(t, err)
  165. assert.NotEqual(t, salt1, salt2)
  166. }