userutil_test.go 4.1 KB

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