選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

69 行
2.4 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. accessKey := "ak-" + t.Name()
  33. secretKey := "sk-" + t.Name()
  34. channel := &model.Channel{Id: 61, Type: constant.ChannelTypeChinaMobileSeedance, Key: "video-key", Name: "channel"}
  35. require.NoError(t, db.Create(channel).Error)
  36. require.NoError(t, model.UpsertChannelAssetCredential(&model.ChannelAssetCredential{
  37. ChannelId: 61,
  38. AccessKey: accessKey,
  39. SecretKey: secretKey,
  40. PoolID: "pool-61",
  41. }))
  42. require.NoError(t, attachChannelAssetCredentialSummaries([]*model.Channel{channel}))
  43. assert.True(t, channel.AssetCredentialConfigured)
  44. assert.Equal(t, "pool-61", channel.AssetCredentialPoolID)
  45. assert.NotContains(t, channel.Key, accessKey)
  46. assert.NotContains(t, channel.Key, secretKey)
  47. }
  48. func TestAttachChannelAssetCredentialSummariesIncludesDoubaoVideo(t *testing.T) {
  49. db := setupChannelAssetCredentialControllerDB(t)
  50. channel := &model.Channel{Id: 54, Type: constant.ChannelTypeDoubaoVideo, Key: "video-key", Name: "doubao"}
  51. require.NoError(t, db.Create(channel).Error)
  52. require.NoError(t, model.UpsertChannelAssetCredential(&model.ChannelAssetCredential{
  53. ChannelId: 54,
  54. AccessKey: "ak-" + t.Name(),
  55. SecretKey: "sk-" + t.Name(),
  56. BaseURL: "http://example.com/openApi/portrait",
  57. }))
  58. require.NoError(t, attachChannelAssetCredentialSummaries([]*model.Channel{channel}))
  59. assert.True(t, channel.AssetCredentialConfigured)
  60. assert.Equal(t, "http://example.com/openApi/portrait", channel.AssetCredentialBaseURL)
  61. }