MCPcopy Index your code
hub / github.com/coder/coder / TestAIProviderKeys

Function TestAIProviderKeys

enterprise/dbcrypt/dbcrypt_internal_test.go:1229–1307  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1227}
1228
1229func TestAIProviderKeys(t *testing.T) {
1230 t.Parallel()
1231 ctx := context.Background()
1232
1233 //nolint:gosec // test credentials
1234 const apiKey = "sk-test-api-key"
1235
1236 insertProviderAndKey := func(t *testing.T, crypt *dbCrypt, ciphers []Cipher) (database.AIProvider, database.AIProviderKey) {
1237 t.Helper()
1238 provider := dbgen.AIProvider(t, crypt, database.AIProvider{
1239 Name: "openai-test",
1240 Type: database.AiProviderTypeOpenai,
1241 BaseUrl: "https://api.openai.com/v1/",
1242 })
1243 key := dbgen.AIProviderKey(t, crypt, database.AIProviderKey{
1244 ProviderID: provider.ID,
1245 APIKey: apiKey,
1246 })
1247 requireAIProviderKeyDecrypted(t, key, ciphers, apiKey)
1248 return provider, key
1249 }
1250
1251 t.Run("InsertAIProviderKey", func(t *testing.T) {
1252 t.Parallel()
1253 db, crypt, ciphers := setup(t)
1254 _, key := insertProviderAndKey(t, crypt, ciphers)
1255 requireAIProviderKeyRawEncrypted(ctx, t, db, key.ID, ciphers, apiKey)
1256 })
1257
1258 t.Run("InsertAIProviderKeyEmpty", func(t *testing.T) {
1259 t.Parallel()
1260 db, crypt, _ := setup(t)
1261 provider := dbgen.AIProvider(t, crypt, database.AIProvider{
1262 Name: "openai-empty-key",
1263 })
1264 key := dbgen.AIProviderKey(t, crypt, database.AIProviderKey{
1265 ProviderID: provider.ID,
1266 }, func(p *database.InsertAIProviderKeyParams) {
1267 p.APIKey = ""
1268 })
1269 require.False(t, key.ApiKeyKeyID.Valid)
1270 raw, err := db.GetAIProviderKeyByID(ctx, key.ID)
1271 require.NoError(t, err)
1272 require.Empty(t, raw.APIKey)
1273 })
1274
1275 t.Run("GetAIProviderKeysByProviderID", func(t *testing.T) {
1276 t.Parallel()
1277 db, crypt, ciphers := setup(t)
1278 provider, key := insertProviderAndKey(t, crypt, ciphers)
1279 keys, err := crypt.GetAIProviderKeysByProviderID(ctx, provider.ID)
1280 require.NoError(t, err)
1281 require.Len(t, keys, 1)
1282 requireAIProviderKeyDecrypted(t, keys[0], ciphers, apiKey)
1283 requireAIProviderKeyRawEncrypted(ctx, t, db, key.ID, ciphers, apiKey)
1284 })
1285
1286 t.Run("GetAIProviderKeysByProviderIDs", func(t *testing.T) {

Callers

nothing calls this directly

Calls 13

AIProviderFunction · 0.92
AIProviderKeyFunction · 0.92
setupFunction · 0.70
HelperMethod · 0.65
RunMethod · 0.65
GetAIProviderKeyByIDMethod · 0.65
DeleteAIProviderKeyMethod · 0.65
EmptyMethod · 0.45

Tested by

no test coverage detected