github.com/spreadshirt/mattermost-server@v5.3.2-0.20180927191755-a257d501df3d+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  
    42  	BasicUser    *model.User
    43  	BasicChannel *model.Channel
    44  	BasicTeam    *model.Team
    45  
    46  	SystemAdminUser *model.User
    47  }
    48  
    49  func Setup() *TestHelper {
    50  	a, err := app.New(app.StoreOverride(testStore), app.DisableConfigWatch)
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  	prevListenAddress := *a.Config().ServiceSettings.ListenAddress
    55  	a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
    56  	serverErr := a.StartServer()
    57  	if serverErr != nil {
    58  		panic(serverErr)
    59  	}
    60  	a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress })
    61  
    62  	NewWeb(a, a.Srv.Router)
    63  	URL = fmt.Sprintf("http://localhost:%v", a.Srv.ListenAddr.Port)
    64  	ApiClient = model.NewAPIv4Client(URL)
    65  
    66  	a.DoAdvancedPermissionsMigration()
    67  	a.DoEmojisPermissionsMigration()
    68  
    69  	a.Srv.Store.MarkSystemRanUnitTests()
    70  
    71  	a.UpdateConfig(func(cfg *model.Config) {
    72  		*cfg.TeamSettings.EnableOpenServer = true
    73  	})
    74  
    75  	th := &TestHelper{
    76  		App: a,
    77  	}
    78  
    79  	return th
    80  }
    81  
    82  func (th *TestHelper) InitBasic() *TestHelper {
    83  	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})
    84  
    85  	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})
    86  
    87  	team, _ := th.App.CreateTeam(&model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: user.Email, Type: model.TEAM_OPEN})
    88  
    89  	th.App.JoinUserToTeam(team, user, "")
    90  
    91  	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)
    92  
    93  	th.BasicUser = user
    94  	th.BasicChannel = channel
    95  	th.BasicTeam = team
    96  
    97  	return th
    98  }
    99  
   100  func (th *TestHelper) TearDown() {
   101  	th.App.Shutdown()
   102  	if err := recover(); err != nil {
   103  		StopTestStore()
   104  		panic(err)
   105  	}
   106  }
   107  
   108  /* Test disabled for now so we don't requrie the client to build. Maybe re-enable after client gets moved out.
   109  func TestStatic(t *testing.T) {
   110  	Setup()
   111  
   112  	// add a short delay to make sure the server is ready to receive requests
   113  	time.Sleep(1 * time.Second)
   114  
   115  	resp, err := http.Get(URL + "/static/root.html")
   116  
   117  	if err != nil {
   118  		t.Fatalf("got error while trying to get static files %v", err)
   119  	} else if resp.StatusCode != http.StatusOK {
   120  		t.Fatalf("couldn't get static files %v", resp.StatusCode)
   121  	}
   122  }
   123  */
   124  
   125  func TestMain(m *testing.M) {
   126  	// Setup a global logger to catch tests logging outside of app context
   127  	// The global logger will be stomped by apps initalizing but that's fine for testing. Ideally this won't happen.
   128  	mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{
   129  		EnableConsole: true,
   130  		ConsoleJson:   true,
   131  		ConsoleLevel:  "error",
   132  		EnableFile:    false,
   133  	}))
   134  
   135  	utils.TranslationsPreInit()
   136  
   137  	status := 0
   138  
   139  	container, settings, err := storetest.NewPostgreSQLContainer()
   140  	if err != nil {
   141  		panic(err)
   142  	}
   143  
   144  	testStoreContainer = container
   145  	testStore = &persistentTestStore{store.NewLayeredStore(sqlstore.NewSqlSupplier(*settings, nil), nil, nil)}
   146  
   147  	defer func() {
   148  		StopTestStore()
   149  		os.Exit(status)
   150  	}()
   151  
   152  	status = m.Run()
   153  
   154  }
   155  
   156  func TestCheckClientCompatability(t *testing.T) {
   157  	//Browser Name, UA String, expected result (if the browser should fail the test false and if it should pass the true)
   158  	type uaTest struct {
   159  		Name      string // Name of Browser
   160  		UserAgent string // Useragent of Browser
   161  		Result    bool   // Expected result (true if browser should be compatible, false if browser shouldn't be compatible)
   162  	}
   163  	var uaTestParameters = []uaTest{
   164  		{"Mozilla 40.1", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1", true},
   165  		{"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},
   166  		{"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},
   167  		{"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},
   168  		{"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},
   169  		{"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},
   170  		{"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},
   171  		{"Internet Explorer 9", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0", false},
   172  		{"Internet Explorer 11", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko", true},
   173  		{"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},
   174  		{"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},
   175  		{"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},
   176  		{"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},
   177  		{"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},
   178  		{"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},
   179  	}
   180  	for _, browser := range uaTestParameters {
   181  		t.Run(browser.Name, func(t *testing.T) {
   182  			if result := CheckClientCompatability(browser.UserAgent); result != browser.Result {
   183  				t.Fatalf("%s User Agent Test failed!", browser.Name)
   184  			}
   185  		})
   186  	}
   187  }