This commit is contained in:
@@ -1,105 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
modelUser "github.com/perfect-panel/server/internal/model/user"
|
||||
"github.com/perfect-panel/server/internal/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAppendFamilyOwnerEmailIfNeeded(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
methods []types.UserAuthMethod
|
||||
familyJoined bool
|
||||
ownerEmailMethod *modelUser.AuthMethods
|
||||
wantMethodCount int
|
||||
wantEmailCount int
|
||||
wantFirstAuthType string
|
||||
wantFirstAuthValue string
|
||||
}{
|
||||
{
|
||||
name: "inject owner email when member has no email",
|
||||
methods: []types.UserAuthMethod{
|
||||
{AuthType: "device", AuthIdentifier: "dev-1", Verified: true},
|
||||
},
|
||||
familyJoined: true,
|
||||
ownerEmailMethod: &modelUser.AuthMethods{AuthType: "email", AuthIdentifier: "owner@example.com", Verified: true},
|
||||
wantMethodCount: 2,
|
||||
wantEmailCount: 1,
|
||||
wantFirstAuthType: "email",
|
||||
wantFirstAuthValue: "owner@example.com",
|
||||
},
|
||||
{
|
||||
name: "do not inject when member already has email",
|
||||
methods: []types.UserAuthMethod{
|
||||
{AuthType: "email", AuthIdentifier: "member@example.com", Verified: true},
|
||||
{AuthType: "device", AuthIdentifier: "dev-1", Verified: true},
|
||||
},
|
||||
familyJoined: true,
|
||||
ownerEmailMethod: &modelUser.AuthMethods{AuthType: "email", AuthIdentifier: "owner@example.com", Verified: true},
|
||||
wantMethodCount: 2,
|
||||
wantEmailCount: 1,
|
||||
wantFirstAuthType: "email",
|
||||
wantFirstAuthValue: "member@example.com",
|
||||
},
|
||||
{
|
||||
name: "do not inject when owner has no email",
|
||||
methods: []types.UserAuthMethod{
|
||||
{AuthType: "device", AuthIdentifier: "dev-1", Verified: true},
|
||||
},
|
||||
familyJoined: true,
|
||||
ownerEmailMethod: &modelUser.AuthMethods{AuthType: "email", AuthIdentifier: "", Verified: true},
|
||||
wantMethodCount: 1,
|
||||
wantEmailCount: 0,
|
||||
wantFirstAuthType: "device",
|
||||
},
|
||||
{
|
||||
name: "do not inject for non active family relationship",
|
||||
methods: []types.UserAuthMethod{
|
||||
{AuthType: "device", AuthIdentifier: "dev-1", Verified: true},
|
||||
},
|
||||
familyJoined: false,
|
||||
ownerEmailMethod: &modelUser.AuthMethods{AuthType: "email", AuthIdentifier: "owner@example.com", Verified: true},
|
||||
wantMethodCount: 1,
|
||||
wantEmailCount: 0,
|
||||
wantFirstAuthType: "device",
|
||||
},
|
||||
{
|
||||
name: "sort keeps injected email at first position",
|
||||
methods: []types.UserAuthMethod{
|
||||
{AuthType: "mobile", AuthIdentifier: "+1234567890", Verified: true},
|
||||
{AuthType: "device", AuthIdentifier: "dev-1", Verified: true},
|
||||
},
|
||||
familyJoined: true,
|
||||
ownerEmailMethod: &modelUser.AuthMethods{AuthType: "email", AuthIdentifier: "owner@example.com", Verified: true},
|
||||
wantMethodCount: 3,
|
||||
wantEmailCount: 1,
|
||||
wantFirstAuthType: "email",
|
||||
wantFirstAuthValue: "owner@example.com",
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
finalMethods := appendFamilyOwnerEmailIfNeeded(testCase.methods, testCase.familyJoined, testCase.ownerEmailMethod)
|
||||
sortUserAuthMethodsByPriority(finalMethods)
|
||||
|
||||
require.Len(t, finalMethods, testCase.wantMethodCount)
|
||||
|
||||
emailCount := 0
|
||||
for _, method := range finalMethods {
|
||||
if method.AuthType == "email" {
|
||||
emailCount++
|
||||
}
|
||||
}
|
||||
require.Equal(t, testCase.wantEmailCount, emailCount)
|
||||
|
||||
require.Equal(t, testCase.wantFirstAuthType, finalMethods[0].AuthType)
|
||||
if testCase.wantFirstAuthValue != "" {
|
||||
require.Equal(t, testCase.wantFirstAuthValue, finalMethods[0].AuthIdentifier)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user