2022-07-24 22:02:55 +02:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"lbryio/lbry-id/auth"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccountVerificationMode(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
modeStr string
|
2022-07-25 23:46:41 +02:00
|
|
|
expectedMode AccountVerificationMode
|
2022-07-24 22:02:55 +02:00
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "allow all",
|
|
|
|
|
|
|
|
modeStr: "AllowAll",
|
2022-07-25 23:46:41 +02:00
|
|
|
expectedMode: AccountVerificationModeAllowAll,
|
2022-07-24 22:02:55 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email verify",
|
|
|
|
|
|
|
|
modeStr: "EmailVerify",
|
2022-07-25 23:46:41 +02:00
|
|
|
expectedMode: AccountVerificationModeEmailVerify,
|
2022-07-24 22:02:55 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "whitelist",
|
|
|
|
|
|
|
|
modeStr: "Whitelist",
|
2022-07-25 23:46:41 +02:00
|
|
|
expectedMode: AccountVerificationModeWhitelist,
|
2022-07-24 22:02:55 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "blank",
|
|
|
|
|
|
|
|
modeStr: "",
|
2022-07-25 23:46:41 +02:00
|
|
|
expectedMode: AccountVerificationModeWhitelist,
|
2022-07-24 22:02:55 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid",
|
|
|
|
|
|
|
|
modeStr: "Banana",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
mode, err := getAccountVerificationMode(tc.modeStr)
|
|
|
|
if mode != tc.expectedMode {
|
|
|
|
t.Errorf("Expected mode %s got %s", tc.expectedMode, mode)
|
|
|
|
}
|
|
|
|
if tc.expectErr && err == nil {
|
|
|
|
t.Errorf("Expected err")
|
|
|
|
}
|
|
|
|
if !tc.expectErr && err != nil {
|
|
|
|
t.Errorf("Unexpected err: %s", err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountWhitelist(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
whitelist string
|
|
|
|
expectedEmails []auth.Email
|
|
|
|
expectedErr error
|
2022-07-25 23:46:41 +02:00
|
|
|
mode AccountVerificationMode
|
2022-07-24 22:02:55 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
mode: AccountVerificationModeWhitelist,
|
2022-07-24 22:02:55 +02:00
|
|
|
whitelist: "",
|
|
|
|
expectedEmails: []auth.Email{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid mode",
|
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
mode: AccountVerificationModeEmailVerify,
|
2022-07-24 22:02:55 +02:00
|
|
|
whitelist: "test1@example.com,test2@example.com",
|
|
|
|
expectedErr: fmt.Errorf("Do not specify ACCOUNT_WHITELIST in env if ACCOUNT_VERIFICATION_MODE is not Whitelist"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "spaces in email",
|
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
mode: AccountVerificationModeWhitelist,
|
2022-07-24 22:02:55 +02:00
|
|
|
whitelist: "test1@example.com ,test2@example.com",
|
|
|
|
expectedErr: fmt.Errorf("Emails in ACCOUNT_WHITELIST should be comma separated with no spaces."),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid email",
|
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
mode: AccountVerificationModeWhitelist,
|
2022-07-24 22:02:55 +02:00
|
|
|
whitelist: "test1@example.com,test2-example.com",
|
|
|
|
expectedErr: fmt.Errorf("Invalid email in ACCOUNT_WHITELIST: test2-example.com"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
emails, err := getAccountWhitelist(tc.whitelist, tc.mode)
|
|
|
|
if !reflect.DeepEqual(emails, tc.expectedEmails) {
|
|
|
|
t.Errorf("Expected emails %+v got %+v", tc.expectedEmails, emails)
|
|
|
|
}
|
|
|
|
if fmt.Sprint(err) != fmt.Sprint(tc.expectedErr) {
|
|
|
|
t.Errorf("Expected error `%s` got `%s`", tc.expectedErr, err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|