encrypt_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package dify_invocation
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "testing"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/network"
  11. )
  12. func TestEncryptRequired(t *testing.T) {
  13. data := map[string]any{
  14. "key": "value",
  15. }
  16. payload := &InvokeEncryptRequest{
  17. BaseInvokeDifyRequest: BaseInvokeDifyRequest{
  18. TenantId: "123",
  19. UserId: "456",
  20. Type: INVOKE_TYPE_ENCRYPT,
  21. },
  22. InvokeEncryptSchema: InvokeEncryptSchema{
  23. Opt: ENCRYPT_OPT_ENCRYPT,
  24. Namespace: ENCRYPT_NAMESPACE_ENDPOINT,
  25. Identity: "test123",
  26. Data: data,
  27. Config: map[string]plugin_entities.ProviderConfig{
  28. "key": {
  29. Name: "key",
  30. Type: plugin_entities.CONFIG_TYPE_SECRET_INPUT,
  31. },
  32. },
  33. },
  34. }
  35. if !payload.EncryptRequired(data) {
  36. t.Errorf("EncryptRequired should return true")
  37. }
  38. payload.Config["key"] = plugin_entities.ProviderConfig{
  39. Name: "key",
  40. Type: plugin_entities.CONFIG_TYPE_TEXT_INPUT,
  41. }
  42. if payload.EncryptRequired(data) {
  43. t.Errorf("EncryptRequired should return false")
  44. }
  45. }
  46. func TestInvokeEncrypt(t *testing.T) {
  47. server := gin.Default()
  48. gin.SetMode(gin.ReleaseMode)
  49. port, err := network.GetRandomPort()
  50. if err != nil {
  51. t.Errorf("GetRandomPort failed: %v", err)
  52. }
  53. http_invoked := false
  54. server.POST("/inner/api/invoke/encrypt", func(ctx *gin.Context) {
  55. data := make(map[string]any)
  56. if err := ctx.BindJSON(&data); err != nil {
  57. t.Errorf("BindJSON failed: %v", err)
  58. }
  59. if data["data"].(map[string]any)["key"] != "value" {
  60. t.Errorf("data[key] should be `value`, but got %v", data["data"].(map[string]any)["key"])
  61. }
  62. http_invoked = true
  63. ctx.JSON(http.StatusOK, gin.H{
  64. "key": "encrypted",
  65. })
  66. })
  67. srv := &http.Server{
  68. Addr: fmt.Sprintf(":%d", port),
  69. Handler: server,
  70. }
  71. go func() {
  72. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  73. log.Fatalf("listen: %s\n", err)
  74. }
  75. }()
  76. defer srv.Close()
  77. time.Sleep(1 * time.Second)
  78. if err := InitDifyInvocationDaemon(fmt.Sprintf("http://localhost:%d", port), "test"); err != nil {
  79. t.Errorf("InitDifyInvocationDaemon failed: %v", err)
  80. }
  81. payload := &InvokeEncryptRequest{
  82. BaseInvokeDifyRequest: BaseInvokeDifyRequest{
  83. TenantId: "123",
  84. UserId: "456",
  85. Type: INVOKE_TYPE_ENCRYPT,
  86. },
  87. InvokeEncryptSchema: InvokeEncryptSchema{
  88. Opt: ENCRYPT_OPT_ENCRYPT,
  89. Namespace: ENCRYPT_NAMESPACE_ENDPOINT,
  90. Identity: "test123",
  91. Data: map[string]any{"key": "value"},
  92. Config: map[string]plugin_entities.ProviderConfig{
  93. "key": {
  94. Name: "key",
  95. Type: plugin_entities.CONFIG_TYPE_SECRET_INPUT,
  96. },
  97. },
  98. },
  99. }
  100. if encrypted, err := InvokeEncrypt(payload); err != nil {
  101. t.Errorf("InvokeEncrypt failed: %v", err)
  102. } else {
  103. if encrypted["key"] != "encrypted" {
  104. t.Errorf("encrypted[key] should be `encrypted`, but got %v", encrypted["key"])
  105. }
  106. }
  107. if !http_invoked {
  108. t.Errorf("http_invoked should be true")
  109. }
  110. }