userutil_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. }