Add mailgun env vars

This commit is contained in:
Daniel Krol 2022-07-31 19:18:04 -04:00
parent 510dfe2b96
commit 1e37b0e7b1
3 changed files with 100 additions and 1 deletions

33
env/env.go vendored
View file

@ -15,6 +15,8 @@ import (
// We'll replace this with a config file later.
const whitelistKey = "ACCOUNT_WHITELIST"
const verificationModeKey = "ACCOUNT_VERIFICATION_MODE"
const mailgunDomainKey = "MAILGUN_DOMAIN"
const mailgunPrivateAPIKeyKey = "MAILGUN_PRIVATE_API_KEY"
type AccountVerificationMode string
@ -47,6 +49,10 @@ func GetAccountWhitelist(e EnvInterface, mode AccountVerificationMode) (emails [
return getAccountWhitelist(e.Getenv(whitelistKey), mode)
}
func GetMailgunConfigs(e EnvInterface, mode AccountVerificationMode) (domain string, privateAPIKey string, err error) {
return getMailgunConfigs(e.Getenv(mailgunDomainKey), e.Getenv(mailgunPrivateAPIKeyKey), mode)
}
// Factor out the guts of the functions so we can test them by just passing in
// the env vars
@ -72,7 +78,11 @@ func getAccountWhitelist(whitelist string, mode AccountVerificationMode) (emails
}
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 %s in env if %s is not %s",
whitelistKey,
verificationModeKey,
AccountVerificationModeWhitelist,
)
}
rawEmails := strings.Split(whitelist, ",")
@ -90,3 +100,24 @@ func getAccountWhitelist(whitelist string, mode AccountVerificationMode) (emails
}
return emails, nil
}
func getMailgunConfigs(domain string, privateAPIKey string, mode AccountVerificationMode) (string, string, error) {
if mode != AccountVerificationModeEmailVerify && (domain != "" || privateAPIKey != "") {
return "", "", fmt.Errorf("Do not specify %s or %s in env if %s is not %s",
mailgunDomainKey,
mailgunPrivateAPIKeyKey,
verificationModeKey,
AccountVerificationModeEmailVerify,
)
}
if mode == AccountVerificationModeEmailVerify && (domain == "" || privateAPIKey == "") {
return "", "", fmt.Errorf("Specify %s and %s in env if %s is %s",
mailgunDomainKey,
mailgunPrivateAPIKeyKey,
verificationModeKey,
AccountVerificationModeEmailVerify,
)
}
return domain, privateAPIKey, nil
}

62
env/env_test.go vendored
View file

@ -113,3 +113,65 @@ func TestAccountWhitelist(t *testing.T) {
})
}
}
func TestMailgunConfigs(t *testing.T) {
tt := []struct {
name string
domain string
privateAPIKey string
mode AccountVerificationMode
expectErr bool
}{
{
name: "success",
mode: AccountVerificationModeEmailVerify,
domain: "www.example.com",
privateAPIKey: "my-private-api-key",
expectErr: false,
},
{
name: "wrong mode with domain",
mode: AccountVerificationModeWhitelist,
domain: "www.example.com",
expectErr: true,
},
{
name: "wrong mode with private api key",
mode: AccountVerificationModeWhitelist,
privateAPIKey: "my-private-api-key",
expectErr: true,
},
{
name: "missing domain",
mode: AccountVerificationModeEmailVerify,
privateAPIKey: "my-private-api-key",
expectErr: true,
},
{
name: "missing private api key",
mode: AccountVerificationModeEmailVerify,
domain: "www.example.com",
expectErr: true,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
domain, privateAPIKey, err := getMailgunConfigs(tc.domain, tc.privateAPIKey, tc.mode)
if tc.expectErr && err == nil {
t.Errorf("Expected err")
}
if !tc.expectErr && err != nil {
t.Errorf("Unexpected err: %s", err.Error())
}
if !tc.expectErr && tc.domain != domain {
t.Errorf("Expected domain to be set")
}
if !tc.expectErr && tc.privateAPIKey != privateAPIKey {
t.Errorf("Expected privateAPIKey to be set")
}
})
}
}

View file

@ -36,6 +36,12 @@ func logEmailVerificationMode(e *env.Env) (err error) {
return
}
// just to report config errors to the user on startup
_, _, err = env.GetMailgunConfigs(e, verificationMode)
if err != nil {
return
}
if verificationMode == env.AccountVerificationModeWhitelist {
log.Printf("Account verification mode: %s - Whitelist has %d email(s).\n", verificationMode, len(accountWhitelist))
} else {