github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/web/web_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package web
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/mattermost/mattermost-server/app"
    12  	"github.com/mattermost/mattermost-server/mlog"
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/mattermost/mattermost-server/store"
    15  	"github.com/mattermost/mattermost-server/store/sqlstore"
    16  	"github.com/mattermost/mattermost-server/store/storetest"
    17  	"github.com/mattermost/mattermost-server/utils"
    18  )
    19  
    20  var ApiClient *model.Client4
    21  var URL string
    22  
    23  type persistentTestStore struct {
    24  	store.Store
    25  }
    26  
    27  func (*persistentTestStore) Close() {}
    28  
    29  var testStoreContainer *storetest.RunningContainer
    30  var testStore *persistentTestStore
    31  
    32  func StopTestStore() {
    33  	if testStoreContainer != nil {
    34  		testStoreContainer.Stop()
    35  		testStoreContainer = nil
    36  	}
    37  }
    38  
    39  type TestHelper struct {
    40  	App          *app.App
    41  	BasicUser    *model.User
    42  	BasicChannel *model.Channel
    43  	BasicTeam    *model.Team
    44  }
    45  
    46  func Setup() *TestHelper {
    47  	a, err := app.New(app.StoreOverride(testStore), app.DisableConfigWatch)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	prevListenAddress := *a.Config().ServiceSettings.ListenAddress
    52  	a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
    53  	serverErr := a.StartServer()
    54  	if serverErr != nil {
    55  		panic(serverErr)
    56  	}
    57  	a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress })
    58  
    59  	NewWeb(a, a.Srv.Router)
    60  	URL = fmt.Sprintf("http://localhost:%v", a.Srv.ListenAddr.Port)
    61  	ApiClient = model.NewAPIv4Client(URL)
    62  
    63  	a.DoAdvancedPermissionsMigration()
    64  	a.DoEmojisPermissionsMigration()
    65  
    66  	a.Srv.Store.MarkSystemRanUnitTests()
    67  
    68  	a.UpdateConfig(func(cfg *model.Config) {
    69  		*cfg.TeamSettings.EnableOpenServer = true
    70  	})
    71  
    72  	th := &TestHelper{
    73  		App: a,
    74  	}
    75  
    76  	return th
    77  }
    78  
    79  func (th *TestHelper) InitBasic() *TestHelper {
    80  	user, _ := th.App.CreateUser(&model.User{Email: model.NewId() + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1", EmailVerified: true, Roles: model.SYSTEM_ADMIN_ROLE_ID})
    81  
    82  	team, _ := th.App.CreateTeam(&model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: user.Email, Type: model.TEAM_OPEN})
    83  
    84  	th.App.JoinUserToTeam(team, user, "")
    85  
    86  	channel, _ := th.App.CreateChannel(&model.Channel{DisplayName: "Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id, CreatorId: user.Id}, true)
    87  
    88  	th.BasicUser = user
    89  	th.BasicChannel = channel
    90  	th.BasicTeam = team
    91  
    92  	return th
    93  }
    94  
    95  func (th *TestHelper) TearDown() {
    96  	th.App.Shutdown()
    97  	if err := recover(); err != nil {
    98  		StopTestStore()
    99  		panic(err)
   100  	}
   101  }
   102  
   103  /* Test disabled for now so we don't requrie the client to build. Maybe re-enable after client gets moved out.
   104  func TestStatic(t *testing.T) {
   105  	Setup()
   106  
   107  	// add a short delay to make sure the server is ready to receive requests
   108  	time.Sleep(1 * time.Second)
   109  
   110  	resp, err := http.Get(URL + "/static/root.html")
   111  
   112  	if err != nil {
   113  		t.Fatalf("got error while trying to get static files %v", err)
   114  	} else if resp.StatusCode != http.StatusOK {
   115  		t.Fatalf("couldn't get static files %v", resp.StatusCode)
   116  	}
   117  }
   118  */
   119  
   120  func TestMain(m *testing.M) {
   121  	// Setup a global logger to catch tests logging outside of app context
   122  	// The global logger will be stomped by apps initalizing but that's fine for testing. Ideally this won't happen.
   123  	mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{
   124  		EnableConsole: true,
   125  		ConsoleJson:   true,
   126  		ConsoleLevel:  "error",
   127  		EnableFile:    false,
   128  	}))
   129  
   130  	utils.TranslationsPreInit()
   131  
   132  	status := 0
   133  
   134  	container, settings, err := storetest.NewPostgreSQLContainer()
   135  	if err != nil {
   136  		panic(err)
   137  	}
   138  
   139  	testStoreContainer = container
   140  	testStore = &persistentTestStore{store.NewLayeredStore(sqlstore.NewSqlSupplier(*settings, nil), nil, nil)}
   141  
   142  	defer func() {
   143  		StopTestStore()
   144  		os.Exit(status)
   145  	}()
   146  
   147  	status = m.Run()
   148  
   149  }
   150  
   151  func TestCheckClientCompatability(t *testing.T) {
   152  	//Browser Name, UA String, expected result (if the browser should fail the test false and if it should pass the true)
   153  	type uaTest struct {
   154  		Name      string // Name of Browser
   155  		UserAgent string // Useragent of Browser
   156  		Result    bool   // Expected result (true if browser should be compatible, false if browser shouldn't be compatible)
   157  	}
   158  	var uaTestParameters = []uaTest{
   159  		{"Mozilla 40.1", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1", true},
   160  		{"Chrome 60", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36", true},
   161  		{"Chrome Mobile", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Mobile Safari/537.36", true},
   162  		{"MM Classic App", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR6.170623.013; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/61.0.3163.81 Mobile Safari/537.36 Web-Atoms-Mobile-WebView", true},
   163  		{"MM App 3.7.1", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Mattermost/3.7.1 Chrome/56.0.2924.87 Electron/1.6.11 Safari/537.36", true},
   164  		{"Franz 4.0.4", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Franz/4.0.4 Chrome/52.0.2743.82 Electron/1.3.1 Safari/537.36", true},
   165  		{"Edge 14", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393", true},
   166  		{"Internet Explorer 9", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0", false},
   167  		{"Internet Explorer 11", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko", true},
   168  		{"Internet Explorer 11 2", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Zoom 3.6.0; rv:11.0) like Gecko", true},
   169  		{"Internet Explorer 11 (Compatibility Mode) 1", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; .NET CLR 1.1.4322; InfoPath.3; Zoom 3.6.0)", false},
   170  		{"Internet Explorer 11 (Compatibility Mode) 2", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Zoom 3.6.0)", false},
   171  		{"Safari 9", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Safari/604.1.38", true},
   172  		{"Safari 8", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12", false},
   173  		{"Safari Mobile", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B137 Safari/601.1", true},
   174  	}
   175  	for _, browser := range uaTestParameters {
   176  		t.Run(browser.Name, func(t *testing.T) {
   177  			if result := CheckClientCompatability(browser.UserAgent); result != browser.Result {
   178  				t.Fatalf("%s User Agent Test failed!", browser.Name)
   179  			}
   180  		})
   181  	}
   182  }