github.com/nhannv/mattermost-server@v5.11.1+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  	"testing"
     9  
    10  	"github.com/mattermost/mattermost-server/app"
    11  	"github.com/mattermost/mattermost-server/config"
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  var ApiClient *model.Client4
    16  var URL string
    17  
    18  type TestHelper struct {
    19  	App    *app.App
    20  	Server *app.Server
    21  
    22  	BasicUser    *model.User
    23  	BasicChannel *model.Channel
    24  	BasicTeam    *model.Team
    25  
    26  	SystemAdminUser *model.User
    27  }
    28  
    29  func Setup() *TestHelper {
    30  	store := mainHelper.GetStore()
    31  	store.DropAllTables()
    32  
    33  	memoryStore, err := config.NewMemoryStore()
    34  	if err != nil {
    35  		panic("failed to initialize memory store: " + err.Error())
    36  	}
    37  
    38  	var options []app.Option
    39  	options = append(options, app.ConfigStore(memoryStore))
    40  	options = append(options, app.StoreOverride(mainHelper.Store))
    41  
    42  	s, err := app.NewServer(options...)
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  	a := s.FakeApp()
    47  	prevListenAddress := *a.Config().ServiceSettings.ListenAddress
    48  	a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
    49  	serverErr := s.Start()
    50  	if serverErr != nil {
    51  		panic(serverErr)
    52  	}
    53  	a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress })
    54  
    55  	New(s, s.AppOptions, s.Router)
    56  	URL = fmt.Sprintf("http://localhost:%v", a.Srv.ListenAddr.Port)
    57  	ApiClient = model.NewAPIv4Client(URL)
    58  
    59  	a.DoAdvancedPermissionsMigration()
    60  	a.DoEmojisPermissionsMigration()
    61  
    62  	a.Srv.Store.MarkSystemRanUnitTests()
    63  
    64  	a.UpdateConfig(func(cfg *model.Config) {
    65  		*cfg.TeamSettings.EnableOpenServer = true
    66  	})
    67  
    68  	th := &TestHelper{
    69  		App:    a,
    70  		Server: s,
    71  	}
    72  
    73  	return th
    74  }
    75  
    76  func (th *TestHelper) InitBasic() *TestHelper {
    77  	th.SystemAdminUser, _ = 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})
    78  
    79  	user, _ := th.App.CreateUser(&model.User{Email: model.NewId() + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1", EmailVerified: true, Roles: model.SYSTEM_USER_ROLE_ID})
    80  
    81  	team, _ := th.App.CreateTeam(&model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: user.Email, Type: model.TEAM_OPEN})
    82  
    83  	th.App.JoinUserToTeam(team, user, "")
    84  
    85  	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)
    86  
    87  	th.BasicUser = user
    88  	th.BasicChannel = channel
    89  	th.BasicTeam = team
    90  
    91  	return th
    92  }
    93  
    94  func (th *TestHelper) TearDown() {
    95  	th.Server.Shutdown()
    96  	if err := recover(); err != nil {
    97  		panic(err)
    98  	}
    99  }
   100  
   101  /* Test disabled for now so we don't requrie the client to build. Maybe re-enable after client gets moved out.
   102  func TestStatic(t *testing.T) {
   103  	Setup()
   104  
   105  	// add a short delay to make sure the server is ready to receive requests
   106  	time.Sleep(1 * time.Second)
   107  
   108  	resp, err := http.Get(URL + "/static/root.html")
   109  
   110  	if err != nil {
   111  		t.Fatalf("got error while trying to get static files %v", err)
   112  	} else if resp.StatusCode != http.StatusOK {
   113  		t.Fatalf("couldn't get static files %v", resp.StatusCode)
   114  	}
   115  }
   116  */
   117  
   118  func TestCheckClientCompatability(t *testing.T) {
   119  	//Browser Name, UA String, expected result (if the browser should fail the test false and if it should pass the true)
   120  	type uaTest struct {
   121  		Name      string // Name of Browser
   122  		UserAgent string // Useragent of Browser
   123  		Result    bool   // Expected result (true if browser should be compatible, false if browser shouldn't be compatible)
   124  	}
   125  	var uaTestParameters = []uaTest{
   126  		{"Mozilla 40.1", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1", true},
   127  		{"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},
   128  		{"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},
   129  		{"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},
   130  		{"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},
   131  		{"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},
   132  		{"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},
   133  		{"Internet Explorer 9", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0", false},
   134  		{"Internet Explorer 11", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko", true},
   135  		{"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},
   136  		{"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},
   137  		{"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},
   138  		{"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},
   139  		{"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},
   140  		{"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},
   141  	}
   142  	for _, browser := range uaTestParameters {
   143  		t.Run(browser.Name, func(t *testing.T) {
   144  			if result := CheckClientCompatability(browser.UserAgent); result != browser.Result {
   145  				t.Fatalf("%s User Agent Test failed!", browser.Name)
   146  			}
   147  		})
   148  	}
   149  }