mirror of
https://github.com/juanfont/headscale.git
synced 2026-04-23 17:18:50 +02:00
grpc: support expire/delete API keys by ID
Update ExpireApiKey and DeleteApiKey handlers to accept either ID or prefix for identifying the API key. Returns InvalidArgument error if neither or both are provided. Add tests for: - Expire by ID - Expire by prefix (backwards compatibility) - Delete by ID - Delete by prefix (backwards compatibility) - Error when neither ID nor prefix provided - Error when both ID and prefix provided Updates #2986
This commit is contained in:
@@ -246,3 +246,30 @@ func TestAPIKeyWithPrefix(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAPIKeyByID(t *testing.T) {
|
||||
db, err := newSQLiteTestDB()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create an API key
|
||||
_, apiKey, err := db.CreateAPIKey(nil)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, apiKey)
|
||||
|
||||
// Retrieve by ID
|
||||
retrievedKey, err := db.GetAPIKeyByID(apiKey.ID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, retrievedKey)
|
||||
assert.Equal(t, apiKey.ID, retrievedKey.ID)
|
||||
assert.Equal(t, apiKey.Prefix, retrievedKey.Prefix)
|
||||
}
|
||||
|
||||
func TestGetAPIKeyByIDNotFound(t *testing.T) {
|
||||
db, err := newSQLiteTestDB()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Try to get a non-existent key by ID
|
||||
key, err := db.GetAPIKeyByID(99999)
|
||||
require.Error(t, err)
|
||||
assert.Nil(t, key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user