2022-01-14 20:33:31 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
package auth_test
|
2022-01-14 20:33:31 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-01-14 20:33:31 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
|
|
|
|
"github.com/duo-labs/webauthn/webauthn"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetWebAuthnCredentialByID(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
res, err := auth_model.GetWebAuthnCredentialByID(1)
|
2022-01-14 20:33:31 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "WebAuthn credential", res.Name)
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
_, err = auth_model.GetWebAuthnCredentialByID(342432)
|
2022-01-14 20:33:31 +05:30
|
|
|
assert.Error(t, err)
|
2022-08-25 08:01:57 +05:30
|
|
|
assert.True(t, auth_model.IsErrWebAuthnCredentialNotExist(err))
|
2022-01-14 20:33:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWebAuthnCredentialsByUID(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
res, err := auth_model.GetWebAuthnCredentialsByUID(32)
|
2022-01-14 20:33:31 +05:30
|
|
|
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 08:01:57 +05:30
|
|
|
assert.Equal(t, "webauthn_credential", auth_model.WebAuthnCredential{}.TableName())
|
2022-01-14 20:33:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebAuthnCredential_UpdateSignCount(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-25 08:01:57 +05:30
|
|
|
cred := unittest.AssertExistsAndLoadBean(t, &auth_model.WebAuthnCredential{ID: 1})
|
2022-01-14 20:33:31 +05:30
|
|
|
cred.SignCount = 1
|
|
|
|
assert.NoError(t, cred.UpdateSignCount())
|
2022-08-25 08:01:57 +05:30
|
|
|
unittest.AssertExistsIf(t, true, &auth_model.WebAuthnCredential{ID: 1, SignCount: 1})
|
2022-01-14 20:33:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebAuthnCredential_UpdateLargeCounter(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-25 08:01:57 +05:30
|
|
|
cred := unittest.AssertExistsAndLoadBean(t, &auth_model.WebAuthnCredential{ID: 1})
|
2022-01-14 20:33:31 +05:30
|
|
|
cred.SignCount = 0xffffffff
|
|
|
|
assert.NoError(t, cred.UpdateSignCount())
|
2022-08-25 08:01:57 +05:30
|
|
|
unittest.AssertExistsIf(t, true, &auth_model.WebAuthnCredential{ID: 1, SignCount: 0xffffffff})
|
2022-01-14 20:33:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateCredential(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
res, err := auth_model.CreateCredential(1, "WebAuthn Created Credential", &webauthn.Credential{ID: []byte("Test")})
|
2022-01-14 20:33:31 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "WebAuthn Created Credential", res.Name)
|
2022-07-30 18:55:26 +05:30
|
|
|
assert.Equal(t, []byte("Test"), res.CredentialID)
|
2022-01-14 20:33:31 +05:30
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
unittest.AssertExistsIf(t, true, &auth_model.WebAuthnCredential{Name: "WebAuthn Created Credential", UserID: 1})
|
2022-01-14 20:33:31 +05:30
|
|
|
}
|