encrypt_test.go 3.3 KB

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