capEndpointLogic_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package pub
  2. import (
  3. "context"
  4. "testing"
  5. "perms-system-server/internal/config"
  6. "perms-system-server/internal/svc"
  7. "perms-system-server/internal/testutil"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. // TC-1211: cap.js 已启用
  12. func TestCapEndpoint_Enabled(t *testing.T) {
  13. cfg := testutil.GetTestConfig()
  14. cfg.Capjs = config.CapjsConf{
  15. Enable: 1,
  16. EndpointURL: "https://cap.example.com",
  17. Key: "site-key-123",
  18. Secret: "secret",
  19. }
  20. svcCtx := svc.NewServiceContext(cfg)
  21. logic := NewCapEndpointLogic(context.Background(), svcCtx)
  22. resp, err := logic.CapEndpoint()
  23. require.NoError(t, err)
  24. require.NotNil(t, resp)
  25. assert.Equal(t, "https://cap.example.com/site-key-123/", resp.Data)
  26. }
  27. // TC-1212: cap.js 未启用(Enable=0)
  28. func TestCapEndpoint_Disabled(t *testing.T) {
  29. cfg := testutil.GetTestConfig()
  30. cfg.Capjs = config.CapjsConf{Enable: 0}
  31. svcCtx := svc.NewServiceContext(cfg)
  32. logic := NewCapEndpointLogic(context.Background(), svcCtx)
  33. resp, err := logic.CapEndpoint()
  34. require.NoError(t, err)
  35. require.NotNil(t, resp)
  36. assert.Equal(t, "", resp.Data)
  37. }
  38. // TC-1255: cap.js 启用但 EndpointURL 为空
  39. func TestCapEndpoint_EnabledButNoURL(t *testing.T) {
  40. cfg := testutil.GetTestConfig()
  41. cfg.Capjs = config.CapjsConf{Enable: 1, EndpointURL: "", Key: "k"}
  42. svcCtx := svc.NewServiceContext(cfg)
  43. logic := NewCapEndpointLogic(context.Background(), svcCtx)
  44. resp, err := logic.CapEndpoint()
  45. require.NoError(t, err)
  46. assert.Equal(t, "", resp.Data)
  47. }
  48. // TC-1256: cap.js 启用但 Key 为空
  49. func TestCapEndpoint_EnabledButNoKey(t *testing.T) {
  50. cfg := testutil.GetTestConfig()
  51. cfg.Capjs = config.CapjsConf{Enable: 1, EndpointURL: "https://cap.example.com", Key: ""}
  52. svcCtx := svc.NewServiceContext(cfg)
  53. logic := NewCapEndpointLogic(context.Background(), svcCtx)
  54. resp, err := logic.CapEndpoint()
  55. require.NoError(t, err)
  56. assert.Equal(t, "", resp.Data)
  57. }