2020-11-09 13:19:03 +01:00
|
|
|
package integrationtests
|
|
|
|
|
|
|
|
import (
|
2021-12-10 16:46:37 +01:00
|
|
|
"errors"
|
2020-11-09 13:19:03 +01:00
|
|
|
"net/http"
|
2021-04-22 22:53:01 +02:00
|
|
|
"os"
|
2021-01-06 21:55:14 +01:00
|
|
|
"time"
|
2020-11-09 13:19:03 +01:00
|
|
|
|
2021-12-10 16:46:37 +01:00
|
|
|
"github.com/mattermost/focalboard/server/api"
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/client"
|
|
|
|
"github.com/mattermost/focalboard/server/server"
|
|
|
|
"github.com/mattermost/focalboard/server/services/config"
|
2021-12-17 18:30:47 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/store/sqlstore"
|
2021-12-10 16:46:37 +01:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-11-09 13:19:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestHelper struct {
|
2021-12-10 16:46:37 +01:00
|
|
|
Server *server.Server
|
|
|
|
Client *client.Client
|
|
|
|
Client2 *client.Client
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 18:30:47 +01:00
|
|
|
func getTestConfig() (*config.Configuration, error) {
|
|
|
|
dbType, connectionString, err := sqlstore.PrepareNewTestDatabase()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-22 22:53:01 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
logging := `
|
2021-05-29 08:23:10 +02:00
|
|
|
{
|
|
|
|
"testing": {
|
|
|
|
"type": "console",
|
|
|
|
"options": {
|
|
|
|
"out": "stdout"
|
|
|
|
},
|
|
|
|
"format": "plain",
|
|
|
|
"format_options": {
|
|
|
|
"delim": " "
|
|
|
|
},
|
|
|
|
"levels": [
|
|
|
|
{"id": 5, "name": "debug"},
|
|
|
|
{"id": 4, "name": "info"},
|
|
|
|
{"id": 3, "name": "warn"},
|
|
|
|
{"id": 2, "name": "error", "stacktrace": true},
|
|
|
|
{"id": 1, "name": "fatal", "stacktrace": true},
|
|
|
|
{"id": 0, "name": "panic", "stacktrace": true}
|
|
|
|
]
|
|
|
|
}
|
2021-06-04 16:38:49 +02:00
|
|
|
}`
|
2021-05-29 08:23:10 +02:00
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
return &config.Configuration{
|
2021-08-10 04:57:45 +02:00
|
|
|
ServerRoot: "http://localhost:8888",
|
|
|
|
Port: 8888,
|
|
|
|
DBType: dbType,
|
|
|
|
DBConfigString: connectionString,
|
|
|
|
DBTablePrefix: "test_",
|
|
|
|
WebPath: "./pack",
|
|
|
|
FilesDriver: "local",
|
|
|
|
FilesPath: "./files",
|
|
|
|
LoggingCfgJSON: logging,
|
|
|
|
SessionExpireTime: int64(30 * time.Second),
|
|
|
|
AuthMode: "native",
|
2021-12-17 18:30:47 +01:00
|
|
|
}, nil
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 04:57:45 +02:00
|
|
|
func newTestServer(singleUserToken string) *server.Server {
|
2021-12-17 18:30:47 +01:00
|
|
|
cfg, err := getTestConfig()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:08:01 +02:00
|
|
|
logger, _ := mlog.NewLogger()
|
2021-12-17 18:30:47 +01:00
|
|
|
if err = logger.Configure("", cfg.LoggingCfgJSON, nil); err != nil {
|
2021-06-21 11:21:42 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-25 16:49:06 +02:00
|
|
|
db, err := server.NewStore(cfg, logger)
|
|
|
|
if err != nil {
|
2021-07-09 03:09:02 +02:00
|
|
|
panic(err)
|
2021-06-25 16:49:06 +02:00
|
|
|
}
|
2021-09-13 21:36:36 +02:00
|
|
|
|
|
|
|
params := server.Params{
|
|
|
|
Cfg: cfg,
|
|
|
|
SingleUserToken: singleUserToken,
|
|
|
|
DBStore: db,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
srv, err := server.New(params)
|
2020-11-09 13:19:03 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-08-10 04:57:45 +02:00
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupTestHelper() *TestHelper {
|
|
|
|
sessionToken := "TESTTOKEN"
|
|
|
|
th := &TestHelper{}
|
|
|
|
th.Server = newTestServer(sessionToken)
|
|
|
|
th.Client = client.NewClient(th.Server.Config().ServerRoot, sessionToken)
|
|
|
|
return th
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupTestHelperWithoutToken() *TestHelper {
|
|
|
|
th := &TestHelper{}
|
|
|
|
th.Server = newTestServer("")
|
|
|
|
th.Client = client.NewClient(th.Server.Config().ServerRoot, "")
|
2021-12-10 16:46:37 +01:00
|
|
|
th.Client2 = client.NewClient(th.Server.Config().ServerRoot, "")
|
2020-11-09 13:19:03 +01:00
|
|
|
return th
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *TestHelper) InitBasic() *TestHelper {
|
|
|
|
go func() {
|
2021-01-18 21:59:32 +01:00
|
|
|
if err := th.Server.Start(); err != nil {
|
2020-11-09 13:19:03 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-06 21:55:14 +01:00
|
|
|
for {
|
|
|
|
URL := th.Server.Config().ServerRoot
|
2021-05-29 08:23:10 +02:00
|
|
|
th.Server.Logger().Info("Polling server", mlog.String("url", URL))
|
2021-07-09 03:09:02 +02:00
|
|
|
resp, err := http.Get(URL) //nolint:gosec
|
2021-01-06 21:55:14 +01:00
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
th.Server.Logger().Error("Polling failed", mlog.Err(err))
|
2021-01-06 21:55:14 +01:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
// Currently returns 404
|
|
|
|
// if resp.StatusCode != http.StatusOK {
|
2021-05-29 08:23:10 +02:00
|
|
|
// th.Server.Logger().Error("Not OK", mlog.Int("statusCode", resp.StatusCode))
|
2021-01-06 21:55:14 +01:00
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Reached this point: server is up and running!
|
2021-05-29 08:23:10 +02:00
|
|
|
th.Server.Logger().Info("Server ping OK", mlog.Int("statusCode", resp.StatusCode))
|
2021-01-06 21:55:14 +01:00
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
return th
|
|
|
|
}
|
|
|
|
|
2021-12-10 16:46:37 +01:00
|
|
|
var ErrRegisterFail = errors.New("register failed")
|
|
|
|
|
|
|
|
func (th *TestHelper) InitUsers(username1 string, username2 string) error {
|
|
|
|
workspace, err := th.Server.App().GetRootWorkspace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clients := []*client.Client{th.Client, th.Client2}
|
|
|
|
usernames := []string{username1, username2}
|
|
|
|
|
|
|
|
for i, client := range clients {
|
|
|
|
// register a new user
|
|
|
|
password := utils.NewID(utils.IDTypeNone)
|
|
|
|
registerRequest := &api.RegisterRequest{
|
|
|
|
Username: usernames[i],
|
|
|
|
Email: usernames[i] + "@example.com",
|
|
|
|
Password: password,
|
|
|
|
Token: workspace.SignupToken,
|
|
|
|
}
|
|
|
|
success, resp := client.Register(registerRequest)
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
if !success {
|
|
|
|
return ErrRegisterFail
|
|
|
|
}
|
|
|
|
|
|
|
|
// login
|
|
|
|
loginRequest := &api.LoginRequest{
|
|
|
|
Type: "normal",
|
|
|
|
Username: registerRequest.Username,
|
|
|
|
Email: registerRequest.Email,
|
|
|
|
Password: registerRequest.Password,
|
|
|
|
}
|
|
|
|
data, resp := client.Login(loginRequest)
|
|
|
|
if resp.Error != nil {
|
|
|
|
return resp.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Token = data.Token
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
func (th *TestHelper) TearDown() {
|
2021-06-21 11:21:42 +02:00
|
|
|
defer func() { _ = th.Server.Logger().Shutdown() }()
|
2021-05-29 08:23:10 +02:00
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
err := th.Server.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-08-10 04:57:45 +02:00
|
|
|
|
|
|
|
os.RemoveAll(th.Server.Config().FilesPath)
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|