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