encrypt_test.go 3.3 KB

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