2022-07-24 22:02:55 +02:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-22 18:05:53 +02:00
|
|
|
"lbryio/wallet-sync-server/auth"
|
2022-07-24 22:02:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 01:18:04 +02:00
|
|
|
|
|
|
|
func TestMailgunConfigs(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
|
2022-08-01 17:50:16 +02:00
|
|
|
sendingDomain string
|
|
|
|
serverDomain string
|
|
|
|
privateAPIKey string
|
|
|
|
isDomainEUStr string
|
|
|
|
expectDomainEU bool
|
|
|
|
mode AccountVerificationMode
|
2022-08-01 01:18:04 +02:00
|
|
|
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2022-08-01 17:50:16 +02:00
|
|
|
name: "success with domain eu set",
|
|
|
|
mode: AccountVerificationModeEmailVerify,
|
|
|
|
sendingDomain: "sending.example.com",
|
|
|
|
serverDomain: "server.example.com",
|
|
|
|
privateAPIKey: "my-private-api-key",
|
|
|
|
isDomainEUStr: "true",
|
|
|
|
expectDomainEU: true,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success without domain eu set",
|
2022-08-05 02:26:01 +02:00
|
|
|
mode: AccountVerificationModeEmailVerify,
|
2022-08-01 17:50:16 +02:00
|
|
|
sendingDomain: "sending.example.com",
|
|
|
|
serverDomain: "server.example.com",
|
2022-08-01 01:18:04 +02:00
|
|
|
privateAPIKey: "my-private-api-key",
|
2022-08-05 02:26:01 +02:00
|
|
|
expectErr: false,
|
2022-08-01 01:18:04 +02:00
|
|
|
},
|
|
|
|
{
|
2022-08-01 17:50:16 +02:00
|
|
|
name: "invalid is domain eu",
|
|
|
|
mode: AccountVerificationModeEmailVerify,
|
|
|
|
sendingDomain: "sending.example.com",
|
|
|
|
serverDomain: "server.example.com",
|
|
|
|
privateAPIKey: "my-private-api-key",
|
|
|
|
isDomainEUStr: "invalid",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong mode with domain keys set",
|
|
|
|
mode: AccountVerificationModeWhitelist,
|
|
|
|
sendingDomain: "sending.example.com",
|
|
|
|
serverDomain: "server.example.com",
|
|
|
|
expectErr: true,
|
2022-08-01 01:18:04 +02:00
|
|
|
},
|
|
|
|
{
|
2022-08-01 17:50:16 +02:00
|
|
|
name: "wrong mode with private api key key set",
|
2022-08-05 02:26:01 +02:00
|
|
|
mode: AccountVerificationModeWhitelist,
|
2022-08-01 01:18:04 +02:00
|
|
|
privateAPIKey: "my-private-api-key",
|
2022-08-05 02:26:01 +02:00
|
|
|
expectErr: true,
|
2022-08-01 01:18:04 +02:00
|
|
|
},
|
|
|
|
{
|
2022-08-01 17:50:16 +02:00
|
|
|
name: "wrong mode with is domain eu key set",
|
|
|
|
mode: AccountVerificationModeWhitelist,
|
|
|
|
isDomainEUStr: "true",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing domains",
|
2022-08-05 02:26:01 +02:00
|
|
|
mode: AccountVerificationModeEmailVerify,
|
2022-08-01 01:18:04 +02:00
|
|
|
privateAPIKey: "my-private-api-key",
|
2022-08-05 02:26:01 +02:00
|
|
|
expectErr: true,
|
2022-08-01 01:18:04 +02:00
|
|
|
},
|
|
|
|
{
|
2022-08-01 17:50:16 +02:00
|
|
|
name: "missing private api key",
|
|
|
|
mode: AccountVerificationModeEmailVerify,
|
|
|
|
sendingDomain: "sending.example.com",
|
|
|
|
serverDomain: "server.example.com",
|
|
|
|
expectErr: true,
|
2022-08-01 01:18:04 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-08-01 17:50:16 +02:00
|
|
|
sendingDomain, serverDomain, isDomainEu, privateAPIKey, err := getMailgunConfigs(tc.sendingDomain, tc.serverDomain, tc.isDomainEUStr, tc.privateAPIKey, tc.mode)
|
2022-08-01 01:18:04 +02:00
|
|
|
if tc.expectErr && err == nil {
|
|
|
|
t.Errorf("Expected err")
|
|
|
|
}
|
|
|
|
if !tc.expectErr && err != nil {
|
|
|
|
t.Errorf("Unexpected err: %s", err.Error())
|
|
|
|
}
|
2022-08-01 17:50:16 +02:00
|
|
|
if !tc.expectErr && tc.sendingDomain != sendingDomain {
|
|
|
|
t.Errorf("Expected sendingDomain to be set")
|
|
|
|
}
|
|
|
|
if !tc.expectErr && tc.serverDomain != serverDomain {
|
|
|
|
t.Errorf("Expected serverDomain to be set")
|
2022-08-01 01:18:04 +02:00
|
|
|
}
|
|
|
|
if !tc.expectErr && tc.privateAPIKey != privateAPIKey {
|
|
|
|
t.Errorf("Expected privateAPIKey to be set")
|
|
|
|
}
|
2022-08-01 17:50:16 +02:00
|
|
|
if !tc.expectErr && tc.expectDomainEU != isDomainEu {
|
|
|
|
t.Errorf("Expected isDomainEu to be %v", tc.expectDomainEU)
|
|
|
|
}
|
2022-08-01 01:18:04 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|