env: rename things, export type

This commit is contained in:
Daniel Krol 2022-07-25 17:46:41 -04:00
parent f792ba5846
commit 832778ffd1
2 changed files with 24 additions and 26 deletions

30
env/env.go vendored
View file

@ -11,19 +11,17 @@ import (
const whitelistKey = "ACCOUNT_WHITELIST" const whitelistKey = "ACCOUNT_WHITELIST"
const verificationModeKey = "ACCOUNT_VERIFICATION_MODE" const verificationModeKey = "ACCOUNT_VERIFICATION_MODE"
// Not exported, so that GetAccountVerificationMode is the only way to get one type AccountVerificationMode string
// of these values from outside of this package.
type accountVerificationMode string
// Everyone can make an account. Only use for dev purposes. // Everyone can make an account. Only use for dev purposes.
const ModeAllowAll = accountVerificationMode("AllowAll") const AccountVerificationModeAllowAll = AccountVerificationMode("AllowAll")
// Verify accounts via email. Good for big open servers. // Verify accounts via email. Good for big open servers.
const ModeEmailVerify = accountVerificationMode("EmailVerify") const AccountVerificationModeEmailVerify = AccountVerificationMode("EmailVerify")
// Specific email accounts are automatically verified. Good for small // Specific email accounts are automatically verified. Good for small
// self-hosting users. // self-hosting users.
const ModeWhitelist = accountVerificationMode("Whitelist") const AccountVerificationModeWhitelist = AccountVerificationMode("Whitelist")
// For test stubs // For test stubs
type EnvInterface interface { type EnvInterface interface {
@ -36,39 +34,39 @@ func (e *Env) Getenv(key string) string {
return os.Getenv(key) return os.Getenv(key)
} }
func GetAccountVerificationMode(e EnvInterface) (accountVerificationMode, error) { func GetAccountVerificationMode(e EnvInterface) (AccountVerificationMode, error) {
return getAccountVerificationMode(e.Getenv(verificationModeKey)) return getAccountVerificationMode(e.Getenv(verificationModeKey))
} }
func GetAccountWhitelist(e EnvInterface, mode accountVerificationMode) (emails []auth.Email, err error) { func GetAccountWhitelist(e EnvInterface, mode AccountVerificationMode) (emails []auth.Email, err error) {
return getAccountWhitelist(e.Getenv(whitelistKey), mode) return getAccountWhitelist(e.Getenv(whitelistKey), mode)
} }
// Factor out the guts of the functions so we can test them by just passing in // Factor out the guts of the functions so we can test them by just passing in
// the env vars // the env vars
func getAccountVerificationMode(modeStr string) (accountVerificationMode, error) { func getAccountVerificationMode(modeStr string) (AccountVerificationMode, error) {
mode := accountVerificationMode(modeStr) mode := AccountVerificationMode(modeStr)
switch mode { switch mode {
case "": case "":
// Whitelist is the least dangerous mode. If you forget to set any env // Whitelist is the least dangerous mode. If you forget to set any env
// vars, it effectively disables all account creation. // vars, it effectively disables all account creation.
return ModeWhitelist, nil return AccountVerificationModeWhitelist, nil
case ModeAllowAll: case AccountVerificationModeAllowAll:
case ModeEmailVerify: case AccountVerificationModeEmailVerify:
case ModeWhitelist: case AccountVerificationModeWhitelist:
default: default:
return "", fmt.Errorf("Invalid account verification mode in %s: %s", verificationModeKey, mode) return "", fmt.Errorf("Invalid account verification mode in %s: %s", verificationModeKey, mode)
} }
return mode, nil return mode, nil
} }
func getAccountWhitelist(whitelist string, mode accountVerificationMode) (emails []auth.Email, err error) { func getAccountWhitelist(whitelist string, mode AccountVerificationMode) (emails []auth.Email, err error) {
if whitelist == "" { if whitelist == "" {
return []auth.Email{}, nil return []auth.Email{}, nil
} }
if mode != ModeWhitelist { if mode != AccountVerificationModeWhitelist {
return nil, fmt.Errorf("Do not specify ACCOUNT_WHITELIST in env if ACCOUNT_VERIFICATION_MODE is not Whitelist") return nil, fmt.Errorf("Do not specify ACCOUNT_WHITELIST in env if ACCOUNT_VERIFICATION_MODE is not Whitelist")
} }

20
env/env_test.go vendored
View file

@ -13,32 +13,32 @@ func TestAccountVerificationMode(t *testing.T) {
name string name string
modeStr string modeStr string
expectedMode accountVerificationMode expectedMode AccountVerificationMode
expectErr bool expectErr bool
}{ }{
{ {
name: "allow all", name: "allow all",
modeStr: "AllowAll", modeStr: "AllowAll",
expectedMode: ModeAllowAll, expectedMode: AccountVerificationModeAllowAll,
}, },
{ {
name: "email verify", name: "email verify",
modeStr: "EmailVerify", modeStr: "EmailVerify",
expectedMode: ModeEmailVerify, expectedMode: AccountVerificationModeEmailVerify,
}, },
{ {
name: "whitelist", name: "whitelist",
modeStr: "Whitelist", modeStr: "Whitelist",
expectedMode: ModeWhitelist, expectedMode: AccountVerificationModeWhitelist,
}, },
{ {
name: "blank", name: "blank",
modeStr: "", modeStr: "",
expectedMode: ModeWhitelist, expectedMode: AccountVerificationModeWhitelist,
}, },
{ {
name: "invalid", name: "invalid",
@ -70,33 +70,33 @@ func TestAccountWhitelist(t *testing.T) {
whitelist string whitelist string
expectedEmails []auth.Email expectedEmails []auth.Email
expectedErr error expectedErr error
mode accountVerificationMode mode AccountVerificationMode
}{ }{
{ {
name: "empty", name: "empty",
mode: ModeWhitelist, mode: AccountVerificationModeWhitelist,
whitelist: "", whitelist: "",
expectedEmails: []auth.Email{}, expectedEmails: []auth.Email{},
}, },
{ {
name: "invalid mode", name: "invalid mode",
mode: ModeEmailVerify, mode: AccountVerificationModeEmailVerify,
whitelist: "test1@example.com,test2@example.com", whitelist: "test1@example.com,test2@example.com",
expectedErr: fmt.Errorf("Do not specify ACCOUNT_WHITELIST in env if ACCOUNT_VERIFICATION_MODE is not Whitelist"), expectedErr: fmt.Errorf("Do not specify ACCOUNT_WHITELIST in env if ACCOUNT_VERIFICATION_MODE is not Whitelist"),
}, },
{ {
name: "spaces in email", name: "spaces in email",
mode: ModeWhitelist, mode: AccountVerificationModeWhitelist,
whitelist: "test1@example.com ,test2@example.com", whitelist: "test1@example.com ,test2@example.com",
expectedErr: fmt.Errorf("Emails in ACCOUNT_WHITELIST should be comma separated with no spaces."), expectedErr: fmt.Errorf("Emails in ACCOUNT_WHITELIST should be comma separated with no spaces."),
}, },
{ {
name: "invalid email", name: "invalid email",
mode: ModeWhitelist, mode: AccountVerificationModeWhitelist,
whitelist: "test1@example.com,test2-example.com", whitelist: "test1@example.com,test2-example.com",
expectedErr: fmt.Errorf("Invalid email in ACCOUNT_WHITELIST: test2-example.com"), expectedErr: fmt.Errorf("Invalid email in ACCOUNT_WHITELIST: test2-example.com"),
}, },