Files
headscale/hscontrol/db/api_key_byid_test.go
Kristoffer Dalby 10696fa634 db: look up API keys by explicit primary key, not struct condition
GetAPIKeyByID(0) returned the first key instead of not-found.
2026-06-08 10:04:49 +02:00

29 lines
780 B
Go

package db
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestGetAPIKeyByIDZeroReturnsError ensures GetAPIKeyByID(0) reports not-found
// rather than returning the lowest-ID key. GORM drops a zero-valued primary key
// from a struct condition, which would otherwise make the lookup unconditional.
func TestGetAPIKeyByIDZeroReturnsError(t *testing.T) {
db, err := newSQLiteTestDB()
require.NoError(t, err)
_, key1, err := db.CreateAPIKey(nil)
require.NoError(t, err)
require.NotNil(t, key1)
_, key2, err := db.CreateAPIKey(nil)
require.NoError(t, err)
require.NotNil(t, key2)
key, err := db.GetAPIKeyByID(0)
require.Error(t, err, "GetAPIKeyByID(0) should be not-found, got key=%+v", key)
assert.Nil(t, key)
}