userutil_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. )
  15. func TestDashboardURLPath(t *testing.T) {
  16. t.Run("user", func(t *testing.T) {
  17. got := DashboardURLPath("alice", false)
  18. want := "/"
  19. assert.Equal(t, want, got)
  20. })
  21. t.Run("organization", func(t *testing.T) {
  22. got := DashboardURLPath("acme", true)
  23. want := "/org/acme/dashboard/"
  24. assert.Equal(t, want, got)
  25. })
  26. }
  27. func TestGenerateActivateCode(t *testing.T) {
  28. conf.SetMockAuth(t,
  29. conf.AuthOpts{
  30. ActivateCodeLives: 10,
  31. },
  32. )
  33. code := GenerateActivateCode(1, "alice@example.com", "Alice", "123456", "rands")
  34. got := tool.VerifyTimeLimitCode("1alice@example.comalice123456rands", conf.Auth.ActivateCodeLives, code[:tool.TIME_LIMIT_CODE_LENGTH])
  35. assert.True(t, got)
  36. }
  37. func TestCustomAvatarPath(t *testing.T) {
  38. if runtime.GOOS == "windows" {
  39. t.Skip("Skipping testing on Windows")
  40. return
  41. }
  42. conf.SetMockPicture(t,
  43. conf.PictureOpts{
  44. AvatarUploadPath: "data/avatars",
  45. },
  46. )
  47. got := CustomAvatarPath(1)
  48. want := "data/avatars/1"
  49. assert.Equal(t, want, got)
  50. }
  51. func TestGenerateRandomAvatar(t *testing.T) {
  52. if runtime.GOOS == "windows" {
  53. t.Skip("Skipping testing on Windows")
  54. return
  55. }
  56. conf.SetMockPicture(t,
  57. conf.PictureOpts{
  58. AvatarUploadPath: os.TempDir(),
  59. },
  60. )
  61. err := GenerateRandomAvatar(1, "alice", "alice@example.com")
  62. require.NoError(t, err)
  63. got := osutil.IsFile(CustomAvatarPath(1))
  64. assert.True(t, got)
  65. }
  66. func TestEncodePassword(t *testing.T) {
  67. want := EncodePassword("123456", "rands")
  68. tests := []struct {
  69. name string
  70. password string
  71. rands string
  72. wantEqual bool
  73. }{
  74. {
  75. name: "correct",
  76. password: "123456",
  77. rands: "rands",
  78. wantEqual: true,
  79. },
  80. {
  81. name: "wrong password",
  82. password: "111333",
  83. rands: "rands",
  84. wantEqual: false,
  85. },
  86. {
  87. name: "wrong salt",
  88. password: "111333",
  89. rands: "salt",
  90. wantEqual: false,
  91. },
  92. }
  93. for _, test := range tests {
  94. t.Run(test.name, func(t *testing.T) {
  95. got := EncodePassword(test.password, test.rands)
  96. if test.wantEqual {
  97. assert.Equal(t, want, got)
  98. } else {
  99. assert.NotEqual(t, want, got)
  100. }
  101. })
  102. }
  103. }
  104. func TestValidatePassword(t *testing.T) {
  105. want := EncodePassword("123456", "rands")
  106. tests := []struct {
  107. name string
  108. password string
  109. rands string
  110. wantEqual bool
  111. }{
  112. {
  113. name: "correct",
  114. password: "123456",
  115. rands: "rands",
  116. wantEqual: true,
  117. },
  118. {
  119. name: "wrong password",
  120. password: "111333",
  121. rands: "rands",
  122. wantEqual: false,
  123. },
  124. {
  125. name: "wrong salt",
  126. password: "111333",
  127. rands: "salt",
  128. wantEqual: false,
  129. },
  130. }
  131. for _, test := range tests {
  132. t.Run(test.name, func(t *testing.T) {
  133. got := ValidatePassword(want, test.rands, test.password)
  134. assert.Equal(t, test.wantEqual, got)
  135. })
  136. }
  137. }