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