manual_testing.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  2. // See LICENSE.txt for license information.
  3. package manualtesting
  4. import (
  5. "hash/fnv"
  6. "math/rand"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "time"
  11. "github.com/mattermost/mattermost-server/v5/api4"
  12. "github.com/mattermost/mattermost-server/v5/app"
  13. "github.com/mattermost/mattermost-server/v5/mlog"
  14. "github.com/mattermost/mattermost-server/v5/model"
  15. "github.com/mattermost/mattermost-server/v5/utils"
  16. "github.com/mattermost/mattermost-server/v5/web"
  17. )
  18. // TestEnvironment is a helper struct used for tests in manualtesting.
  19. type TestEnvironment struct {
  20. Params map[string][]string
  21. Client *model.Client4
  22. CreatedTeamID string
  23. CreatedUserID string
  24. Context *web.Context
  25. Writer http.ResponseWriter
  26. Request *http.Request
  27. }
  28. // Init adds manualtest endpoint to the API.
  29. func Init(api4 *api4.API) {
  30. api4.BaseRoutes.Root.Handle("/manualtest", api4.ApiHandler(manualTest)).Methods("GET")
  31. }
  32. func manualTest(c *web.Context, w http.ResponseWriter, r *http.Request) {
  33. // Let the world know
  34. mlog.Info("Setting up for manual test...")
  35. // URL Parameters
  36. params, err := url.ParseQuery(r.URL.RawQuery)
  37. if err != nil {
  38. c.Err = model.NewAppError("/manual", "manaultesting.manual_test.parse.app_error", nil, "", http.StatusBadRequest)
  39. return
  40. }
  41. // Grab a uuid (if available) to seed the random number generator so we don't get conflicts.
  42. uid, ok := params["uid"]
  43. if ok {
  44. hasher := fnv.New32a()
  45. hasher.Write([]byte(uid[0] + strconv.Itoa(int(time.Now().UTC().UnixNano()))))
  46. hash := hasher.Sum32()
  47. rand.Seed(int64(hash))
  48. } else {
  49. mlog.Debug("No uid in URL")
  50. }
  51. // Create a client for tests to use
  52. client := model.NewAPIv4Client("http://localhost" + *c.App.Config().ServiceSettings.ListenAddress)
  53. // Check for username parameter and create a user if present
  54. username, ok1 := params["username"]
  55. teamDisplayName, ok2 := params["teamname"]
  56. var teamID string
  57. var userID string
  58. if ok1 && ok2 {
  59. mlog.Info("Creating user and team")
  60. // Create team for testing
  61. team := &model.Team{
  62. DisplayName: teamDisplayName[0],
  63. Name: "zz" + utils.RandomName(utils.Range{Begin: 20, End: 20}, utils.LOWERCASE),
  64. Email: "success+" + model.NewId() + "simulator.amazonses.com",
  65. Type: model.TEAM_OPEN,
  66. }
  67. createdTeam, err := c.App.Srv().Store.Team().Save(team)
  68. if err != nil {
  69. c.Err = err
  70. return
  71. }
  72. channel := &model.Channel{DisplayName: "Town Square", Name: "town-square", Type: model.CHANNEL_OPEN, TeamId: createdTeam.Id}
  73. if _, err := c.App.CreateChannel(channel, false); err != nil {
  74. c.Err = err
  75. return
  76. }
  77. teamID = createdTeam.Id
  78. // Create user for testing
  79. user := &model.User{
  80. Email: "success+" + model.NewId() + "simulator.amazonses.com",
  81. Nickname: username[0],
  82. Password: app.USER_PASSWORD}
  83. user, resp := client.CreateUser(user)
  84. if resp.Error != nil {
  85. c.Err = resp.Error
  86. return
  87. }
  88. c.App.Srv().Store.User().VerifyEmail(user.Id, user.Email)
  89. c.App.Srv().Store.Team().SaveMember(&model.TeamMember{TeamId: teamID, UserId: user.Id}, *c.App.Config().TeamSettings.MaxUsersPerTeam)
  90. userID = user.Id
  91. // Login as user to generate auth token
  92. _, resp = client.LoginById(user.Id, app.USER_PASSWORD)
  93. if resp.Error != nil {
  94. c.Err = resp.Error
  95. return
  96. }
  97. // Respond with an auth token this can be overridden by a specific test as required
  98. sessionCookie := &http.Cookie{
  99. Name: model.SESSION_COOKIE_TOKEN,
  100. Value: client.AuthToken,
  101. Path: "/",
  102. MaxAge: *c.App.Config().ServiceSettings.SessionLengthWebInDays * 60 * 60 * 24,
  103. HttpOnly: true,
  104. }
  105. http.SetCookie(w, sessionCookie)
  106. http.Redirect(w, r, "/channels/town-square", http.StatusTemporaryRedirect)
  107. }
  108. // Setup test environment
  109. env := TestEnvironment{
  110. Params: params,
  111. Client: client,
  112. CreatedTeamID: teamID,
  113. CreatedUserID: userID,
  114. Context: c,
  115. Writer: w,
  116. Request: r,
  117. }
  118. // Grab the test ID and pick the test
  119. testname, ok := params["test"]
  120. if !ok {
  121. c.Err = model.NewAppError("/manual", "manaultesting.manual_test.parse.app_error", nil, "", http.StatusBadRequest)
  122. return
  123. }
  124. switch testname[0] {
  125. case "autolink":
  126. c.Err = testAutoLink(env)
  127. // ADD YOUR NEW TEST HERE!
  128. case "general":
  129. }
  130. }
  131. func getChannelID(a app.AppIface, channelname string, teamid string, userid string) (string, bool) {
  132. // Grab all the channels
  133. channels, err := a.Srv().Store.Channel().GetChannels(teamid, userid, false, 0)
  134. if err != nil {
  135. mlog.Debug("Unable to get channels")
  136. return "", false
  137. }
  138. for _, channel := range *channels {
  139. if channel.Name == channelname {
  140. return channel.Id, true
  141. }
  142. }
  143. mlog.Debug("Could not find channel", mlog.String("Channel name", channelname), mlog.Int("Possibilities searched", len(*channels)))
  144. return "", false
  145. }