gitea源码

user_form_test.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2018 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package forms
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/modules/glob"
  7. "code.gitea.io/gitea/modules/setting"
  8. "code.gitea.io/gitea/modules/test"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestRegisterForm_IsDomainAllowed_Empty(t *testing.T) {
  12. oldService := setting.Service
  13. defer func() {
  14. setting.Service = oldService
  15. }()
  16. setting.Service.EmailDomainAllowList = nil
  17. form := RegisterForm{}
  18. assert.True(t, form.IsEmailDomainAllowed())
  19. }
  20. func TestRegisterForm_IsDomainAllowed_InvalidEmail(t *testing.T) {
  21. defer test.MockVariableValue(&setting.Service.EmailDomainAllowList, []glob.Glob{glob.MustCompile("gitea.io")})()
  22. tt := []struct {
  23. email string
  24. }{
  25. {"invalid-email"},
  26. {"gitea.io"},
  27. }
  28. for _, v := range tt {
  29. form := RegisterForm{Email: v.email}
  30. assert.False(t, form.IsEmailDomainAllowed())
  31. }
  32. }
  33. func TestRegisterForm_IsDomainAllowed_AllowedEmail(t *testing.T) {
  34. defer test.MockVariableValue(&setting.Service.EmailDomainAllowList, []glob.Glob{glob.MustCompile("gitea.io"), glob.MustCompile("*.allow")})()
  35. tt := []struct {
  36. email string
  37. valid bool
  38. }{
  39. {"security@gitea.io", true},
  40. {"security@gITea.io", true},
  41. {"invalid", false},
  42. {"seee@example.com", false},
  43. {"user@my.allow", true},
  44. {"user@my.allow1", false},
  45. }
  46. for _, v := range tt {
  47. form := RegisterForm{Email: v.email}
  48. assert.Equal(t, v.valid, form.IsEmailDomainAllowed())
  49. }
  50. }
  51. func TestRegisterForm_IsDomainAllowed_BlockedEmail(t *testing.T) {
  52. defer test.MockVariableValue(&setting.Service.EmailDomainBlockList, []glob.Glob{glob.MustCompile("gitea.io"), glob.MustCompile("*.block")})()
  53. tt := []struct {
  54. email string
  55. valid bool
  56. }{
  57. {"security@gitea.io", false},
  58. {"security@gitea.example", true},
  59. {"invalid", true},
  60. {"user@my.block", false},
  61. {"user@my.block1", true},
  62. }
  63. for _, v := range tt {
  64. form := RegisterForm{Email: v.email}
  65. assert.Equal(t, v.valid, form.IsEmailDomainAllowed())
  66. }
  67. }