You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

51 lines
1.6 KiB

  1. package controller
  2. import (
  3. "testing"
  4. "github.com/QuantumNous/new-api/constant"
  5. "github.com/QuantumNous/new-api/model"
  6. "github.com/glebarez/sqlite"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "gorm.io/gorm"
  10. "gorm.io/gorm/logger"
  11. )
  12. func setupChannelAssetCredentialControllerDB(t *testing.T) *gorm.DB {
  13. t.Helper()
  14. db, err := gorm.Open(sqlite.Open("file:controller_channel_asset_credentials?mode=memory&cache=shared"), &gorm.Config{
  15. Logger: logger.Default.LogMode(logger.Silent),
  16. })
  17. require.NoError(t, err)
  18. sqlDB, err := db.DB()
  19. require.NoError(t, err)
  20. sqlDB.SetMaxOpenConns(1)
  21. originalDB := model.DB
  22. model.DB = db
  23. require.NoError(t, db.AutoMigrate(&model.Channel{}, &model.ChannelAssetCredential{}))
  24. t.Cleanup(func() {
  25. model.DB = originalDB
  26. require.NoError(t, sqlDB.Close())
  27. })
  28. return db
  29. }
  30. func TestAttachChannelAssetCredentialSummariesDoesNotExposeSecrets(t *testing.T) {
  31. db := setupChannelAssetCredentialControllerDB(t)
  32. channel := &model.Channel{Id: 61, Type: constant.ChannelTypeChinaMobileSeedance, Key: "video-key", Name: "channel"}
  33. require.NoError(t, db.Create(channel).Error)
  34. require.NoError(t, model.UpsertChannelAssetCredential(&model.ChannelAssetCredential{
  35. ChannelId: 61,
  36. AccessKey: "ak-secret",
  37. SecretKey: "sk-secret",
  38. PoolID: "pool-61",
  39. }))
  40. require.NoError(t, attachChannelAssetCredentialSummaries([]*model.Channel{channel}))
  41. assert.True(t, channel.AssetCredentialConfigured)
  42. assert.Equal(t, "pool-61", channel.AssetCredentialPoolID)
  43. assert.NotContains(t, channel.Key, "ak-secret")
  44. assert.NotContains(t, channel.Key, "sk-secret")
  45. }