diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-05-22 08:05:44 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-22 00:05:44 +0000 |
commit | 2cb66fff60c95efbd58b797f1197f2421f4687ce (patch) | |
tree | 31f92c081c0bea16089a834c80c5797d8c78c1ac /modules/setting/service_test.go | |
parent | 19993d8814e227ac0a52b73d36fdb03fbb143c3f (diff) | |
download | gitea-2cb66fff60c95efbd58b797f1197f2421f4687ce.tar.gz gitea-2cb66fff60c95efbd58b797f1197f2421f4687ce.zip |
Support wildcard in email domain allow/block list (#24831)
Replace #20257 (which is stale and incomplete)
Close #20255
Major changes:
* Deprecate the "WHITELIST", use "ALLOWLIST"
* Add wildcard support for EMAIL_DOMAIN_ALLOWLIST/EMAIL_DOMAIN_BLOCKLIST
* Update example config file and document
* Improve tests
Diffstat (limited to 'modules/setting/service_test.go')
-rw-r--r-- | modules/setting/service_test.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/modules/setting/service_test.go b/modules/setting/service_test.go new file mode 100644 index 0000000000..656e759f42 --- /dev/null +++ b/modules/setting/service_test.go @@ -0,0 +1,46 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package setting + +import ( + "testing" + + "github.com/gobwas/glob" + "github.com/stretchr/testify/assert" +) + +func TestLoadServices(t *testing.T) { + oldService := Service + defer func() { + Service = oldService + }() + + cfg, err := NewConfigProviderFromData(` +[service] +EMAIL_DOMAIN_WHITELIST = d1, *.w +EMAIL_DOMAIN_ALLOWLIST = d2, *.a +EMAIL_DOMAIN_BLOCKLIST = d3, *.b +`) + assert.NoError(t, err) + loadServiceFrom(cfg) + + match := func(globs []glob.Glob, s string) bool { + for _, g := range globs { + if g.Match(s) { + return true + } + } + return false + } + + assert.True(t, match(Service.EmailDomainAllowList, "d1")) + assert.True(t, match(Service.EmailDomainAllowList, "foo.w")) + assert.True(t, match(Service.EmailDomainAllowList, "d2")) + assert.True(t, match(Service.EmailDomainAllowList, "foo.a")) + assert.False(t, match(Service.EmailDomainAllowList, "d3")) + + assert.True(t, match(Service.EmailDomainBlockList, "d3")) + assert.True(t, match(Service.EmailDomainBlockList, "foo.b")) + assert.False(t, match(Service.EmailDomainBlockList, "d1")) +} |