2022-03-28 01:26:30 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-04-17 23:58:24 +00:00
|
|
|
func TestUserInfo_SetDefaultMethod(t *testing.T) {
|
2022-03-28 01:26:30 +00:00
|
|
|
none := "none"
|
|
|
|
|
2022-04-17 23:58:24 +00:00
|
|
|
testName := func(i int, have UserInfo, methods []string, fallback string) string {
|
2022-03-28 01:26:30 +00:00
|
|
|
method := have.Method
|
|
|
|
|
|
|
|
if method == "" {
|
|
|
|
method = none
|
|
|
|
}
|
|
|
|
|
|
|
|
has := ""
|
|
|
|
|
2023-04-10 07:01:23 +00:00
|
|
|
if have.HasTOTP || have.HasDuo || have.HasWebAuthn {
|
2022-03-28 01:26:30 +00:00
|
|
|
has += " has"
|
|
|
|
|
|
|
|
if have.HasTOTP {
|
|
|
|
has += " " + SecondFactorMethodTOTP
|
|
|
|
}
|
|
|
|
|
|
|
|
if have.HasDuo {
|
|
|
|
has += " " + SecondFactorMethodDuo
|
|
|
|
}
|
|
|
|
|
2023-04-10 07:01:23 +00:00
|
|
|
if have.HasWebAuthn {
|
|
|
|
has += " " + SecondFactorMethodWebAuthn
|
2022-03-28 01:26:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
available := none
|
2022-04-17 23:58:24 +00:00
|
|
|
if len(methods) != 0 {
|
|
|
|
available = strings.Join(methods, " ")
|
2022-03-28 01:26:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 23:58:24 +00: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 01:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2022-04-17 23:58:24 +00:00
|
|
|
have UserInfo
|
|
|
|
want UserInfo
|
|
|
|
|
|
|
|
methods []string
|
|
|
|
fallback string
|
|
|
|
|
|
|
|
changed bool
|
2022-03-28 01:26:30 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
2023-04-10 07:01:23 +00:00
|
|
|
Method: SecondFactorMethodWebAuthn,
|
2022-03-28 01:26:30 +00:00
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2023-04-10 07:01:23 +00:00
|
|
|
methods: []string{SecondFactorMethodWebAuthn, SecondFactorMethodDuo},
|
2022-04-17 23:58:24 +00:00
|
|
|
changed: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2023-04-10 07:01:23 +00:00
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodWebAuthn, SecondFactorMethodDuo},
|
2022-04-17 23:58:24 +00:00
|
|
|
changed: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
2023-04-10 07:01:23 +00:00
|
|
|
Method: SecondFactorMethodWebAuthn,
|
2022-03-28 01:26:30 +00:00
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2022-04-17 23:58:24 +00:00
|
|
|
methods: []string{SecondFactorMethodTOTP},
|
|
|
|
changed: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
2023-04-10 07:01:23 +00:00
|
|
|
Method: SecondFactorMethodWebAuthn,
|
2022-03-28 01:26:30 +00:00
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2022-04-17 23:58:24 +00:00
|
|
|
methods: []string{SecondFactorMethodTOTP},
|
|
|
|
changed: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
2023-04-10 07:01:23 +00:00
|
|
|
Method: SecondFactorMethodWebAuthn,
|
2022-03-28 01:26:30 +00:00
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2023-04-10 07:01:23 +00:00
|
|
|
methods: []string{SecondFactorMethodWebAuthn},
|
2022-04-17 23:58:24 +00:00
|
|
|
changed: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2022-04-17 23:58:24 +00:00
|
|
|
methods: []string{SecondFactorMethodDuo},
|
|
|
|
changed: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
2023-04-10 07:01:23 +00:00
|
|
|
Method: SecondFactorMethodWebAuthn,
|
2022-03-28 01:26:30 +00:00
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
2023-04-10 07:01:23 +00:00
|
|
|
Method: SecondFactorMethodWebAuthn,
|
2022-03-28 01:26:30 +00:00
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2023-04-10 07:01:23 +00:00
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodWebAuthn, SecondFactorMethodDuo},
|
2022-04-17 23:58:24 +00:00
|
|
|
changed: false,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
2023-04-10 07:01:23 +00:00
|
|
|
Method: SecondFactorMethodWebAuthn,
|
2022-03-28 01:26:30 +00:00
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2023-04-10 07:01:23 +00:00
|
|
|
methods: []string{SecondFactorMethodWebAuthn, SecondFactorMethodDuo},
|
2022-04-17 23:58:24 +00:00
|
|
|
changed: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2022-04-17 23:58:24 +00:00
|
|
|
methods: []string{SecondFactorMethodDuo},
|
|
|
|
changed: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: true,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
2022-04-17 23:58:24 +00:00
|
|
|
methods: nil,
|
|
|
|
changed: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
2023-04-10 07:01:23 +00:00
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodWebAuthn, SecondFactorMethodDuo},
|
2022-04-17 23:58:24 +00:00
|
|
|
fallback: SecondFactorMethodDuo,
|
|
|
|
changed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
2023-04-10 07:01:23 +00:00
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodWebAuthn},
|
2022-04-17 23:58:24 +00:00
|
|
|
fallback: SecondFactorMethodDuo,
|
|
|
|
changed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: true,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
2023-04-10 07:01:23 +00:00
|
|
|
methods: []string{SecondFactorMethodWebAuthn, SecondFactorMethodDuo},
|
2022-04-17 23:58:24 +00:00
|
|
|
changed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
|
|
|
Method: SecondFactorMethodTOTP,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
2023-04-10 07:01:23 +00:00
|
|
|
Method: SecondFactorMethodWebAuthn,
|
2022-04-17 23:58:24 +00:00
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
2023-04-10 07:01:23 +00:00
|
|
|
methods: []string{SecondFactorMethodWebAuthn, SecondFactorMethodDuo},
|
|
|
|
fallback: SecondFactorMethodWebAuthn,
|
2022-04-17 23:58:24 +00:00
|
|
|
changed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
have: UserInfo{
|
2023-04-10 07:01:23 +00:00
|
|
|
Method: SecondFactorMethodWebAuthn,
|
2022-04-17 23:58:24 +00:00
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
|
|
|
want: UserInfo{
|
|
|
|
Method: SecondFactorMethodDuo,
|
|
|
|
HasDuo: false,
|
|
|
|
HasTOTP: false,
|
2023-04-10 07:01:23 +00:00
|
|
|
HasWebAuthn: false,
|
2022-04-17 23:58:24 +00:00
|
|
|
},
|
|
|
|
methods: []string{SecondFactorMethodTOTP, SecondFactorMethodDuo},
|
|
|
|
fallback: SecondFactorMethodDuo,
|
|
|
|
changed: true,
|
2022-03-28 01:26:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2022-04-17 23:58:24 +00: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 01:26:30 +00:00
|
|
|
|
|
|
|
assert.Equal(t, tc.changed, changed)
|
|
|
|
assert.Equal(t, tc.want, tc.have)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|