static_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package conf
  2. import (
  3. "sort"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "gopkg.in/ini.v1"
  7. )
  8. func Test_i18n_DateLang(t *testing.T) {
  9. c := &i18nConf{
  10. dateLangs: map[string]string{
  11. "en-US": "en",
  12. "zh-CN": "zh",
  13. },
  14. }
  15. tests := []struct {
  16. lang string
  17. want string
  18. }{
  19. {lang: "en-US", want: "en"},
  20. {lang: "zh-CN", want: "zh"},
  21. {lang: "jp-JP", want: "en"},
  22. }
  23. for _, test := range tests {
  24. t.Run("", func(t *testing.T) {
  25. assert.Equal(t, test.want, c.DateLang(test.lang))
  26. })
  27. }
  28. }
  29. func TestCheckInvalidOptions(t *testing.T) {
  30. cfg := ini.Empty()
  31. _, _ = cfg.Section("mailer").NewKey("ENABLED", "true")
  32. _, _ = cfg.Section("service").NewKey("START_TYPE", "true")
  33. _, _ = cfg.Section("security").NewKey("REVERSE_PROXY_AUTHENTICATION_USER", "true")
  34. _, _ = cfg.Section("auth").NewKey("ACTIVE_CODE_LIVE_MINUTES", "10")
  35. _, _ = cfg.Section("auth").NewKey("RESET_PASSWD_CODE_LIVE_MINUTES", "10")
  36. _, _ = cfg.Section("auth").NewKey("ENABLE_CAPTCHA", "true")
  37. _, _ = cfg.Section("auth").NewKey("ENABLE_NOTIFY_MAIL", "true")
  38. _, _ = cfg.Section("auth").NewKey("REGISTER_EMAIL_CONFIRM", "true")
  39. _, _ = cfg.Section("session").NewKey("GC_INTERVAL_TIME", "10")
  40. _, _ = cfg.Section("session").NewKey("SESSION_LIFE_TIME", "10")
  41. _, _ = cfg.Section("server").NewKey("ROOT_URL", "true")
  42. _, _ = cfg.Section("server").NewKey("LANDING_PAGE", "true")
  43. _, _ = cfg.Section("database").NewKey("DB_TYPE", "true")
  44. _, _ = cfg.Section("database").NewKey("PASSWD", "true")
  45. _, _ = cfg.Section("other").NewKey("SHOW_FOOTER_BRANDING", "true")
  46. _, _ = cfg.Section("other").NewKey("SHOW_FOOTER_TEMPLATE_LOAD_TIME", "true")
  47. _, _ = cfg.Section("email").NewKey("ENABLED", "true")
  48. _, _ = cfg.Section("server").NewKey("NONEXISTENT_OPTION", "true")
  49. wantWarnings := []string{
  50. "option [auth] ACTIVE_CODE_LIVE_MINUTES is invalid",
  51. "option [auth] ENABLE_CAPTCHA is invalid",
  52. "option [auth] ENABLE_NOTIFY_MAIL is invalid",
  53. "option [auth] REGISTER_EMAIL_CONFIRM is invalid",
  54. "option [auth] RESET_PASSWD_CODE_LIVE_MINUTES is invalid",
  55. "option [database] DB_TYPE is invalid",
  56. "option [database] PASSWD is invalid",
  57. "option [security] REVERSE_PROXY_AUTHENTICATION_USER is invalid",
  58. "option [session] GC_INTERVAL_TIME is invalid",
  59. "option [session] SESSION_LIFE_TIME is invalid",
  60. "section [mailer] is invalid, use [email] instead",
  61. "section [service] is invalid, use [auth] instead",
  62. "option [server] ROOT_URL is invalid",
  63. "option [server] LANDING_PAGE is invalid",
  64. "option [server] NONEXISTENT_OPTION is invalid",
  65. }
  66. gotWarnings := checkInvalidOptions(cfg)
  67. sort.Strings(wantWarnings)
  68. sort.Strings(gotWarnings)
  69. assert.Equal(t, wantWarnings, gotWarnings)
  70. }