2022-03-28 08:26:30 +07:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-04-18 06:58:24 +07:00
|
|
|
func TestUserInfo_SetDefaultMethod(t *testing.T) {
|
2022-03-28 08:26:30 +07:00
|
|
|
none := "none"
|
|
|
|
|
2022-04-18 06:58:24 +07:00
|
|
|
testName := func(i int, have UserInfo, methods []string, fallback string) string {
|
2022-03-28 08:26:30 +07:00
|
|
|
method := have.Method
|
|
|
|
|
|
|
|
if method == "" {
|
|
|
|
method = none
|
|
|
|
}
|
|
|
|
|
|
|
|
has := ""
|
|
|
|
|
|
|
|
if have.HasTOTP || have.HasDuo || have.HasWebauthn {
|
|
|
|
has += " has"
|
|
|
|
|
|
|
|
if have.HasTOTP {
|
|
|
|
has += " " + SecondFactorMethodTOTP
|
|
|
|
}
|
|
|
|
|
|
|
|
if have.HasDuo {
|
|
|
|
has += " " + SecondFactorMethodDuo
|
|
|
|
}
|
|
|
|
|
|
|
|
if have.HasWebauthn {
|
|
|
|
has += " " + SecondFactorMethodWebauthn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
available := none
|
2022-04-18 06:58:24 +07:00
|
|
|
if len(methods) != 0 {
|
|
|
|
available = strings.Join(methods, " ")
|
2022-03-28 08:26:30 +07:00
|
|
|
}
|
|
|
|
|
2022-04-18 06:58:24 +07:00
|
|
|
if fallback != "" {
|
|
|
|
fallback = "/fallback " + fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%d/method %s%s/available methods %s%s", i+1, method, has, available, fallback)
|
2022-03-28 08:26:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2022-04-18 06:58:24 +07:00
|
|
|
have UserInfo
|
|
|
|
want UserInfo
|
|
|
|
|
|
|
|
methods []string
|
|
|
|
fallback string
|
|
|
|
|
|
|
|
changed bool
|
2022-03-28 08:26:30 +07:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodWebauthn,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: []string{SecondFactorMethodWebauthn, SecondFactorMethodDuo},
|
|
|
|
changed: true,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodWebauthn, SecondFactorMethodDuo},
|
|
|
|
changed: true,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodWebauthn,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: []string{SecondFactorMethodTOTP},
|
|
|
|
changed: true,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodWebauthn,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: []string{SecondFactorMethodTOTP},
|
|
|
|
changed: true,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodWebauthn,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: []string{SecondFactorMethodWebauthn},
|
|
|
|
changed: true,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: []string{SecondFactorMethodDuo},
|
|
|
|
changed: true,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodWebauthn,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodWebauthn,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodWebauthn, SecondFactorMethodDuo},
|
|
|
|
changed: false,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodWebauthn,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: []string{SecondFactorMethodWebauthn, SecondFactorMethodDuo},
|
|
|
|
changed: true,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: []string{SecondFactorMethodDuo},
|
|
|
|
changed: true,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
},
|
2022-04-18 06:58:24 +07:00
|
|
|
methods: nil,
|
|
|
|
changed: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodWebauthn, SecondFactorMethodDuo},
|
|
|
|
fallback: SecondFactorMethodDuo,
|
|
|
|
changed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodWebauthn},
|
|
|
|
fallback: SecondFactorMethodDuo,
|
|
|
|
changed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
methods: []string{SecondFactorMethodWebauthn, SecondFactorMethodDuo},
|
|
|
|
changed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodWebauthn,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
methods: []string{SecondFactorMethodWebauthn, SecondFactorMethodDuo},
|
|
|
|
fallback: SecondFactorMethodWebauthn,
|
|
|
|
changed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodWebauthn,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
},
|
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodDuo},
|
|
|
|
fallback: SecondFactorMethodDuo,
|
|
|
|
changed: true,
|
2022-03-28 08:26:30 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2022-04-18 06:58:24 +07:00
|
|
|
t.Run(testName(i, tc.have, tc.methods, tc.fallback), func(t *testing.T) {
|
|
|
|
changed := tc.have.SetDefaultPreferred2FAMethod(tc.methods, tc.fallback)
|
2022-03-28 08:26:30 +07:00
|
|
|
|
|
|
|
assert.Equal(t, tc.changed, changed)
|
|
|
|
assert.Equal(t, tc.want, tc.have)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|