2022-01-14 10:03:31 -05:00
// Copyright 2020 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2022-01-14 10:03:31 -05:00
2022-08-24 21:31:57 -05:00
package auth_test
2022-01-14 10:03:31 -05:00
import (
"testing"
2022-08-24 21:31:57 -05:00
auth_model "code.gitea.io/gitea/models/auth"
2023-09-16 09:39:12 -05:00
"code.gitea.io/gitea/models/db"
2022-01-14 10:03:31 -05:00
"code.gitea.io/gitea/models/unittest"
2023-01-11 21:51:00 -05:00
"github.com/go-webauthn/webauthn/webauthn"
2022-01-14 10:03:31 -05:00
"github.com/stretchr/testify/assert"
2024-07-30 14:41:10 -05:00
"github.com/stretchr/testify/require"
2022-01-14 10:03:31 -05:00
)
func TestGetWebAuthnCredentialByID ( t * testing . T ) {
2024-07-30 14:41:10 -05:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-01-14 10:03:31 -05:00
2023-09-16 09:39:12 -05:00
res , err := auth_model . GetWebAuthnCredentialByID ( db . DefaultContext , 1 )
2024-07-30 14:41:10 -05:00
require . NoError ( t , err )
2022-01-14 10:03:31 -05:00
assert . Equal ( t , "WebAuthn credential" , res . Name )
2023-09-16 09:39:12 -05:00
_ , err = auth_model . GetWebAuthnCredentialByID ( db . DefaultContext , 342432 )
2024-07-30 14:41:10 -05:00
require . Error ( t , err )
2022-08-24 21:31:57 -05:00
assert . True ( t , auth_model . IsErrWebAuthnCredentialNotExist ( err ) )
2022-01-14 10:03:31 -05:00
}
func TestGetWebAuthnCredentialsByUID ( t * testing . T ) {
2024-07-30 14:41:10 -05:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-01-14 10:03:31 -05:00
2023-09-16 09:39:12 -05:00
res , err := auth_model . GetWebAuthnCredentialsByUID ( db . DefaultContext , 32 )
2024-07-30 14:41:10 -05:00
require . NoError ( t , err )
2022-01-14 10:03:31 -05:00
assert . Len ( t , res , 1 )
assert . Equal ( t , "WebAuthn credential" , res [ 0 ] . Name )
}
func TestWebAuthnCredential_TableName ( t * testing . T ) {
2022-08-24 21:31:57 -05:00
assert . Equal ( t , "webauthn_credential" , auth_model . WebAuthnCredential { } . TableName ( ) )
2022-01-14 10:03:31 -05:00
}
func TestWebAuthnCredential_UpdateSignCount ( t * testing . T ) {
2024-07-30 14:41:10 -05:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-24 21:31:57 -05:00
cred := unittest . AssertExistsAndLoadBean ( t , & auth_model . WebAuthnCredential { ID : 1 } )
2022-01-14 10:03:31 -05:00
cred . SignCount = 1
2024-07-30 14:41:10 -05:00
require . NoError ( t , cred . UpdateSignCount ( db . DefaultContext ) )
2022-08-24 21:31:57 -05:00
unittest . AssertExistsIf ( t , true , & auth_model . WebAuthnCredential { ID : 1 , SignCount : 1 } )
2022-01-14 10:03:31 -05:00
}
func TestWebAuthnCredential_UpdateLargeCounter ( t * testing . T ) {
2024-07-30 14:41:10 -05:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-24 21:31:57 -05:00
cred := unittest . AssertExistsAndLoadBean ( t , & auth_model . WebAuthnCredential { ID : 1 } )
2022-01-14 10:03:31 -05:00
cred . SignCount = 0xffffffff
2024-07-30 14:41:10 -05:00
require . NoError ( t , cred . UpdateSignCount ( db . DefaultContext ) )
2022-08-24 21:31:57 -05:00
unittest . AssertExistsIf ( t , true , & auth_model . WebAuthnCredential { ID : 1 , SignCount : 0xffffffff } )
2022-01-14 10:03:31 -05:00
}
2024-08-28 00:40:40 -05:00
func TestWebAuthenCredential_UpdateFromLegacy ( t * testing . T ) {
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
cred := unittest . AssertExistsAndLoadBean ( t , & auth_model . WebAuthnCredential { ID : 1 , Legacy : true } )
cred . Legacy = false
cred . BackupEligible = true
cred . BackupState = true
require . NoError ( t , cred . UpdateFromLegacy ( db . DefaultContext ) )
unittest . AssertExistsIf ( t , true , & auth_model . WebAuthnCredential { ID : 1 , BackupEligible : true , BackupState : true } , "legacy = false" )
}
2022-01-14 10:03:31 -05:00
func TestCreateCredential ( t * testing . T ) {
2024-07-30 14:41:10 -05:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-01-14 10:03:31 -05:00
2024-08-28 00:40:40 -05:00
res , err := auth_model . CreateCredential ( db . DefaultContext , 1 , "WebAuthn Created Credential" , & webauthn . Credential { ID : [ ] byte ( "Test" ) , Flags : webauthn . CredentialFlags { BackupEligible : true , BackupState : true } } )
2024-07-30 14:41:10 -05:00
require . NoError ( t , err )
2022-01-14 10:03:31 -05:00
assert . Equal ( t , "WebAuthn Created Credential" , res . Name )
2022-07-30 08:25:26 -05:00
assert . Equal ( t , [ ] byte ( "Test" ) , res . CredentialID )
2022-01-14 10:03:31 -05:00
2024-08-28 00:40:40 -05:00
unittest . AssertExistsIf ( t , true , & auth_model . WebAuthnCredential { Name : "WebAuthn Created Credential" , UserID : 1 , BackupEligible : true , BackupState : true } , "legacy = false" )
2022-01-14 10:03:31 -05:00
}