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.

99 lines
3.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package wikipedia
  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. searchText := "Czechoslovakian bananna"
  20. wikipediaAPIURL := "https://en.wikipedia.org/w/api.php"
  21. // Mock the response from Wikipedia
  22. wikipediaTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) {
  23. query := req.URL.Query()
  24. // Check the base API URL
  25. if !strings.HasPrefix(req.URL.String(), wikipediaAPIURL) {
  26. t.Fatalf("Bad URL: got %s want prefix %s", req.URL.String(), wikipediaAPIURL)
  27. }
  28. // Check the request method
  29. if req.Method != "GET" {
  30. t.Fatalf("Bad method: got %s want GET", req.Method)
  31. }
  32. // Check the search query
  33. // Example query - https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=RMS+Titanic
  34. var searchString = query.Get("titles")
  35. var searchStringLength = len(searchString)
  36. if searchStringLength > 0 && searchString != searchText {
  37. t.Fatalf("Bad search string: got \"%s\" (%d characters) ", searchString, searchStringLength)
  38. }
  39. page := wikipediaPage{
  40. PageID: 1,
  41. NS: 1,
  42. Title: "Test page",
  43. Touched: "2017-02-21 00:00:00",
  44. LastRevID: 1,
  45. Extract: "Some extract text",
  46. }
  47. pages := map[string]wikipediaPage{
  48. "1": page,
  49. }
  50. res := wikipediaSearchResults{
  51. Query: wikipediaQuery{
  52. Pages: pages,
  53. },
  54. }
  55. b, err := json.Marshal(res)
  56. if err != nil {
  57. t.Fatalf("Failed to marshal Wikipedia response - %s", err)
  58. }
  59. return &http.Response{
  60. StatusCode: 200,
  61. Body: ioutil.NopCloser(bytes.NewBuffer(b)),
  62. }, nil
  63. })
  64. // clobber the Wikipedia service http client instance
  65. httpClient = &http.Client{Transport: wikipediaTrans}
  66. // Create the Wikipedia service
  67. srv, err := types.CreateService("id", ServiceType, "@wikipediabot:hyrule", []byte(`{}`))
  68. if err != nil {
  69. t.Fatal("Failed to create Wikipedia service: ", err)
  70. }
  71. wikipedia := srv.(*Service)
  72. // Mock the response from Matrix
  73. matrixTrans := struct{ testutils.MockTransport }{}
  74. matrixTrans.RT = func(req *http.Request) (*http.Response, error) {
  75. return nil, fmt.Errorf("Unknown URL: %s", req.URL.String())
  76. }
  77. matrixCli, _ := mautrix.NewClient("https://hyrule", "@wikipediabot:hyrule", "its_a_secret")
  78. matrixCli.Client = &http.Client{Transport: matrixTrans}
  79. // Execute the matrix !command
  80. cmds := wikipedia.Commands(matrixCli)
  81. if len(cmds) != 1 {
  82. t.Fatalf("Unexpected number of commands: %d", len(cmds))
  83. }
  84. cmd := cmds[0]
  85. _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{searchText})
  86. if err != nil {
  87. t.Fatalf("Failed to process command: %s", err.Error())
  88. }
  89. }