123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package real
- import (
- "fmt"
- "log"
- "net/http"
- "testing"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
- "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
- "github.com/langgenius/dify-plugin-daemon/internal/utils/network"
- )
- func TestEncryptRequired(t *testing.T) {
- data := map[string]any{
- "key": "value",
- }
- payload := &dify_invocation.InvokeEncryptRequest{
- BaseInvokeDifyRequest: dify_invocation.BaseInvokeDifyRequest{
- TenantId: "123",
- UserId: "456",
- Type: dify_invocation.INVOKE_TYPE_ENCRYPT,
- },
- InvokeEncryptSchema: dify_invocation.InvokeEncryptSchema{
- Opt: dify_invocation.ENCRYPT_OPT_ENCRYPT,
- Namespace: dify_invocation.ENCRYPT_NAMESPACE_ENDPOINT,
- Identity: "test123",
- Data: data,
- Config: []plugin_entities.ProviderConfig{
- {
- Name: "key",
- Type: plugin_entities.CONFIG_TYPE_SECRET_INPUT,
- },
- },
- },
- }
- if !payload.EncryptRequired(data) {
- t.Errorf("EncryptRequired should return true")
- }
- payload.Config = []plugin_entities.ProviderConfig{
- {
- Name: "key",
- Type: plugin_entities.CONFIG_TYPE_TEXT_INPUT,
- },
- }
- if payload.EncryptRequired(data) {
- t.Errorf("EncryptRequired should return false")
- }
- }
- func TestInvokeEncrypt(t *testing.T) {
- server := gin.Default()
- gin.SetMode(gin.ReleaseMode)
- port, err := network.GetRandomPort()
- if err != nil {
- t.Errorf("GetRandomPort failed: %v", err)
- }
- http_invoked := false
- server.POST("/inner/api/invoke/encrypt", func(ctx *gin.Context) {
- data := make(map[string]any)
- if err := ctx.BindJSON(&data); err != nil {
- t.Errorf("BindJSON failed: %v", err)
- }
- if data["data"].(map[string]any)["key"] != "value" {
- t.Errorf("data[key] should be `value`, but got %v", data["data"].(map[string]any)["key"])
- }
- http_invoked = true
- ctx.JSON(http.StatusOK, gin.H{
- "data": map[string]any{
- "data": map[string]any{
- "key": "encrypted",
- },
- },
- })
- })
- srv := &http.Server{
- Addr: fmt.Sprintf(":%d", port),
- Handler: server,
- }
- go func() {
- if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
- log.Fatalf("listen: %s\n", err)
- }
- }()
- defer srv.Close()
- time.Sleep(1 * time.Second)
- i, err := NewDifyInvocationDaemon(fmt.Sprintf("http://localhost:%d", port), "test")
- if err != nil {
- t.Errorf("InitDifyInvocationDaemon failed: %v", err)
- return
- }
- payload := &dify_invocation.InvokeEncryptRequest{
- BaseInvokeDifyRequest: dify_invocation.BaseInvokeDifyRequest{
- TenantId: "123",
- UserId: "456",
- Type: dify_invocation.INVOKE_TYPE_ENCRYPT,
- },
- InvokeEncryptSchema: dify_invocation.InvokeEncryptSchema{
- Opt: dify_invocation.ENCRYPT_OPT_ENCRYPT,
- Namespace: dify_invocation.ENCRYPT_NAMESPACE_ENDPOINT,
- Identity: "test123",
- Data: map[string]any{"key": "value"},
- Config: []plugin_entities.ProviderConfig{
- {
- Name: "key",
- Type: plugin_entities.CONFIG_TYPE_SECRET_INPUT,
- },
- },
- },
- }
- if encrypted, err := i.InvokeEncrypt(payload); err != nil {
- t.Errorf("InvokeEncrypt failed: %v", err)
- } else {
- if encrypted["key"] != "encrypted" {
- t.Errorf("encrypted[key] should be `encrypted`, but got %v", encrypted["key"])
- }
- }
- if !http_invoked {
- t.Errorf("http_invoked should be true")
- }
- }
|