userutil_test.go 3.9 KB

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