2022-01-14 16:03:31 +01:00
|
|
|
// 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 04:31:57 +02:00
|
|
|
package auth_test
|
2022-01-14 16:03:31 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-01-14 16:03:31 +01:00
|
|
|
"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 04:31:57 +02:00
|
|
|
res, err := auth_model.GetWebAuthnCredentialByID(1)
|
2022-01-14 16:03:31 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "WebAuthn credential", res.Name)
|
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
_, err = auth_model.GetWebAuthnCredentialByID(342432)
|
2022-01-14 16:03:31 +01:00
|
|
|
assert.Error(t, err)
|
2022-08-25 04:31:57 +02:00
|
|
|
assert.True(t, auth_model.IsErrWebAuthnCredentialNotExist(err))
|
2022-01-14 16:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWebAuthnCredentialsByUID(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
res, err := auth_model.GetWebAuthnCredentialsByUID(32)
|
2022-01-14 16:03:31 +01: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 04:31:57 +02:00
|
|
|
assert.Equal(t, "webauthn_credential", auth_model.WebAuthnCredential{}.TableName())
|
2022-01-14 16:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebAuthnCredential_UpdateSignCount(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-25 04:31:57 +02:00
|
|
|
cred := unittest.AssertExistsAndLoadBean(t, &auth_model.WebAuthnCredential{ID: 1})
|
2022-01-14 16:03:31 +01:00
|
|
|
cred.SignCount = 1
|
|
|
|
assert.NoError(t, cred.UpdateSignCount())
|
2022-08-25 04:31:57 +02:00
|
|
|
unittest.AssertExistsIf(t, true, &auth_model.WebAuthnCredential{ID: 1, SignCount: 1})
|
2022-01-14 16:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebAuthnCredential_UpdateLargeCounter(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-25 04:31:57 +02:00
|
|
|
cred := unittest.AssertExistsAndLoadBean(t, &auth_model.WebAuthnCredential{ID: 1})
|
2022-01-14 16:03:31 +01:00
|
|
|
cred.SignCount = 0xffffffff
|
|
|
|
assert.NoError(t, cred.UpdateSignCount())
|
2022-08-25 04:31:57 +02:00
|
|
|
unittest.AssertExistsIf(t, true, &auth_model.WebAuthnCredential{ID: 1, SignCount: 0xffffffff})
|
2022-01-14 16:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateCredential(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
res, err := auth_model.CreateCredential(1, "WebAuthn Created Credential", &webauthn.Credential{ID: []byte("Test")})
|
2022-01-14 16:03:31 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "WebAuthn Created Credential", res.Name)
|
2022-07-30 15:25:26 +02:00
|
|
|
assert.Equal(t, []byte("Test"), res.CredentialID)
|
2022-01-14 16:03:31 +01:00
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
unittest.AssertExistsIf(t, true, &auth_model.WebAuthnCredential{Name: "WebAuthn Created Credential", UserID: 1})
|
2022-01-14 16:03:31 +01:00
|
|
|
}
|