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