You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package guggy
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/matrix-org/go-neb/database"
  11. "github.com/matrix-org/go-neb/testutils"
  12. "github.com/matrix-org/go-neb/types"
  13. "maunium.net/go/mautrix"
  14. )
  15. // TODO: It would be nice to tabularise this test so we can try failing different combinations of responses to make
  16. // sure all cases are handled, rather than just the general case as is here.
  17. func TestCommand(t *testing.T) {
  18. database.SetServiceDB(&database.NopStorage{})
  19. apiKey := "secret"
  20. guggyImageURL := "https://guggy.com/gifs/23ryf872fg"
  21. // Mock the response from Guggy
  22. guggyTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) {
  23. guggyURL := "https://text2gif.guggy.com/guggify"
  24. if req.URL.String() != guggyURL {
  25. t.Fatalf("Bad URL: got %s want %s", req.URL.String(), guggyURL)
  26. }
  27. if req.Method != "POST" {
  28. t.Fatalf("Bad method: got %s want POST", req.Method)
  29. }
  30. if req.Header.Get("apiKey") != apiKey {
  31. t.Fatalf("Bad apiKey: got %s want %s", req.Header.Get("apiKey"), apiKey)
  32. }
  33. // check the query is in the request body
  34. var reqBody guggyQuery
  35. if err := json.NewDecoder(req.Body).Decode(&reqBody); err != nil {
  36. t.Fatalf("Failed to read request body: %s", err)
  37. }
  38. if reqBody.Sentence != "hey listen!" {
  39. t.Fatalf("Bad query: got '%s' want '%s'", reqBody.Sentence, "hey listen!")
  40. }
  41. res := guggyGifResult{
  42. Width: 64,
  43. Height: 64,
  44. ReqID: "12345",
  45. GIF: guggyImageURL,
  46. }
  47. b, err := json.Marshal(res)
  48. if err != nil {
  49. t.Fatalf("Failed to marshal guggy response: %v", err)
  50. }
  51. return &http.Response{
  52. StatusCode: 200,
  53. Body: ioutil.NopCloser(bytes.NewBuffer(b)),
  54. }, nil
  55. })
  56. // clobber the guggy service http client instance
  57. httpClient = &http.Client{Transport: guggyTrans}
  58. // Create the Guggy service
  59. srv, err := types.CreateService("id", ServiceType, "@guggybot:hyrule", []byte(
  60. `{"api_key":"`+apiKey+`"}`,
  61. ))
  62. if err != nil {
  63. t.Fatal("Failed to create Guggy service: ", err)
  64. }
  65. guggy := srv.(*Service)
  66. // Mock the response from Matrix
  67. matrixTrans := struct{ testutils.MockTransport }{}
  68. matrixTrans.RT = func(req *http.Request) (*http.Response, error) {
  69. if req.URL.String() == guggyImageURL { // getting the guggy image
  70. return &http.Response{
  71. StatusCode: 200,
  72. Body: ioutil.NopCloser(bytes.NewBufferString("some image data")),
  73. }, nil
  74. } else if strings.Contains(req.URL.String(), "_matrix/media/r0/upload") { // uploading the image to matrix
  75. return &http.Response{
  76. StatusCode: 200,
  77. Body: ioutil.NopCloser(bytes.NewBufferString(`{"content_uri":"mxc://foo/bar"}`)),
  78. }, nil
  79. }
  80. return nil, fmt.Errorf("Unknown URL: %s", req.URL.String())
  81. }
  82. matrixCli, _ := mautrix.NewClient("https://hyrule", "@guggybot:hyrule", "its_a_secret")
  83. matrixCli.Client = &http.Client{Transport: matrixTrans}
  84. // Execute the matrix !command
  85. cmds := guggy.Commands(matrixCli)
  86. if len(cmds) != 1 {
  87. t.Fatalf("Unexpected number of commands: %d", len(cmds))
  88. }
  89. cmd := cmds[0]
  90. _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{"hey", "listen!"})
  91. if err != nil {
  92. t.Fatalf("Failed to process command: %s", err.Error())
  93. }
  94. }