client_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package dingbot
  2. import (
  3. "bytes"
  4. "github.com/eryajf/chatgpt-dingtalk/config"
  5. "image"
  6. "image/color"
  7. "image/png"
  8. "os"
  9. "testing"
  10. )
  11. func TestUploadMedia_Pass_WithValidConfig(t *testing.T) {
  12. // 设置了钉钉 ClientID 和 ClientSecret 的环境变量才执行以下测试,用于快速验证钉钉图片上传能力
  13. clientId, clientSecret := os.Getenv("DINGTALK_CLIENT_ID_FOR_TEST"), os.Getenv("DINGTALK_CLIENT_SECRET_FOR_TEST")
  14. if len(clientId) <= 0 || len(clientSecret) <= 0 {
  15. return
  16. }
  17. credentials := []config.Credential{
  18. config.Credential{
  19. ClientID: clientId,
  20. ClientSecret: clientSecret,
  21. },
  22. }
  23. client := NewDingTalkClientManager(&config.Configuration{Credentials: credentials}).GetClientByOAuthClientID(clientId)
  24. var imageContent []byte
  25. {
  26. // 生成一张用于测试的图片
  27. img := image.NewRGBA(image.Rect(0, 0, 200, 100))
  28. blue := color.RGBA{0, 0, 255, 255}
  29. for x := 0; x < img.Bounds().Dx(); x++ {
  30. for y := 0; y < img.Bounds().Dy(); y++ {
  31. img.Set(x, y, blue)
  32. }
  33. }
  34. buf := new(bytes.Buffer)
  35. err := png.Encode(buf, img)
  36. if err != nil {
  37. return
  38. }
  39. // get the byte array from the buffer
  40. imageContent = buf.Bytes()
  41. }
  42. result, err := client.UploadMedia(imageContent, "filename.png", "image", "image/png")
  43. if err != nil {
  44. t.Errorf("upload media failed, err=%s", err.Error())
  45. return
  46. }
  47. if result.MediaID == "" {
  48. t.Errorf("upload media failed, empty media id")
  49. return
  50. }
  51. }