From b1b23079c6a49df29b6f27b85e98d6a9b1d3607c Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Wed, 21 Mar 2018 11:33:47 -0400 Subject: Fix paging for GET /teams and GET /teams/{id}/members endpoints (#8488) --- api4/team.go | 6 +++--- api4/team_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) (limited to 'api4') diff --git a/api4/team.go b/api4/team.go index 8e4c5c312..f8a1c556c 100644 --- a/api4/team.go +++ b/api4/team.go @@ -285,7 +285,7 @@ func getTeamMembers(c *Context, w http.ResponseWriter, r *http.Request) { return } - if members, err := c.App.GetTeamMembers(c.Params.TeamId, c.Params.Page, c.Params.PerPage); err != nil { + if members, err := c.App.GetTeamMembers(c.Params.TeamId, c.Params.Page*c.Params.PerPage, c.Params.PerPage); err != nil { c.Err = err return } else { @@ -543,9 +543,9 @@ func getAllTeams(c *Context, w http.ResponseWriter, r *http.Request) { var err *model.AppError if c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { - teams, err = c.App.GetAllTeamsPage(c.Params.Page, c.Params.PerPage) + teams, err = c.App.GetAllTeamsPage(c.Params.Page*c.Params.PerPage, c.Params.PerPage) } else { - teams, err = c.App.GetAllOpenTeamsPage(c.Params.Page, c.Params.PerPage) + teams, err = c.App.GetAllOpenTeamsPage(c.Params.Page*c.Params.PerPage, c.Params.PerPage) } if err != nil { diff --git a/api4/team_test.go b/api4/team_test.go index 04a0e9ae4..c2edcdaaa 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -567,6 +567,10 @@ func TestGetAllTeams(t *testing.T) { _, resp := Client.CreateTeam(team) CheckNoError(t, resp) + team2 := &model.Team{DisplayName: "Name2", Name: GenerateTestTeamName(), Email: th.GenerateTestEmail(), Type: model.TEAM_OPEN, AllowOpenInvite: true} + _, resp = Client.CreateTeam(team2) + CheckNoError(t, resp) + rrteams, resp := Client.GetAllTeams("", 0, 1) CheckNoError(t, resp) @@ -611,6 +615,17 @@ func TestGetAllTeams(t *testing.T) { t.Fatal("wrong number of teams - should be 0") } + rrteams, resp = Client.GetAllTeams("", 0, 2) + CheckNoError(t, resp) + rrteams2, resp = Client.GetAllTeams("", 1, 2) + CheckNoError(t, resp) + + for _, t1 := range rrteams { + for _, t2 := range rrteams2 { + assert.NotEqual(t, t1.Id, t2.Id, "different pages should not have the same teams") + } + } + Client.Logout() _, resp = Client.GetAllTeams("", 1, 10) CheckUnauthorizedStatus(t, resp) @@ -1140,6 +1155,17 @@ func TestGetTeamMembers(t *testing.T) { t.Fatal("should be no member") } + rmembers, resp = Client.GetTeamMembers(team.Id, 0, 2, "") + CheckNoError(t, resp) + rmembers2, resp := Client.GetTeamMembers(team.Id, 1, 2, "") + CheckNoError(t, resp) + + for _, tm1 := range rmembers { + for _, tm2 := range rmembers2 { + assert.NotEqual(t, tm1.UserId+tm1.TeamId, tm2.UserId+tm2.TeamId, "different pages should not have the same members") + } + } + _, resp = Client.GetTeamMembers("junk", 0, 100, "") CheckBadRequestStatus(t, resp) -- cgit v1.2.3-1-g7c22 From 9d701c704416a1d8648dd2818a8a15c4da99b424 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Wed, 21 Mar 2018 14:27:14 -0400 Subject: Fix various segfaults when running `go test` manually (#8448) * failing to find i18n shouldn't segfault The server was trying to handle the fact that it couldn't find the i18n directory, by emitting a translated log message... * fix utils.FindDir The attempts to find the directory in the parent or grandparent directory don't work if the current working directory was inside `enterprise`, with `enterprise` itself being a symlink as per the usual developer setup. Recurse to the root of the filesystem, cleaning the path along the way to work around this limitation (and allow tests to be run from an arbitrarily deep nesting level.) Fix corresponding usages to employ filepath.Join. * failing to find html templates shouldn't segfault * fail fast if the test user cannot be created * rework utils.FindDir to retain backwards compatibility --- api4/apitestlib.go | 9 +++++++-- api4/oauth.go | 3 ++- api4/plugin_test.go | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) (limited to 'api4') diff --git a/api4/apitestlib.go b/api4/apitestlib.go index 6edd37812..386afdadd 100644 --- a/api4/apitestlib.go +++ b/api4/apitestlib.go @@ -11,6 +11,7 @@ import ( "net" "net/http" "os" + "path/filepath" "reflect" "strconv" "strings" @@ -302,7 +303,11 @@ func (me *TestHelper) CreateUserWithClient(client *model.Client4) *model.User { } utils.DisableDebugLogForTest() - ruser, _ := client.CreateUser(user) + ruser, response := client.CreateUser(user) + if response.Error != nil { + panic(response.Error) + } + ruser.Password = "Password1" store.Must(me.App.Srv.Store.User().VerifyEmail(ruser.Id)) utils.EnableDebugLogForTest() @@ -675,7 +680,7 @@ func CheckInternalErrorStatus(t *testing.T, resp *model.Response) { func readTestFile(name string) ([]byte, error) { path, _ := utils.FindDir("tests") - file, err := os.Open(path + "/" + name) + file, err := os.Open(filepath.Join(path, name)) if err != nil { return nil, err } diff --git a/api4/oauth.go b/api4/oauth.go index d0f43256a..a173159b6 100644 --- a/api4/oauth.go +++ b/api4/oauth.go @@ -6,6 +6,7 @@ package api4 import ( "net/http" "net/url" + "path/filepath" "strings" l4g "github.com/alecthomas/log4go" @@ -375,7 +376,7 @@ func authorizeOAuthPage(c *Context, w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public") staticDir, _ := utils.FindDir(model.CLIENT_DIR) - http.ServeFile(w, r, staticDir+"root.html") + http.ServeFile(w, r, filepath.Join(staticDir, "root.html")) } func getAccessToken(c *Context, w http.ResponseWriter, r *http.Request) { diff --git a/api4/plugin_test.go b/api4/plugin_test.go index e385b5c8c..045ae9212 100644 --- a/api4/plugin_test.go +++ b/api4/plugin_test.go @@ -8,6 +8,7 @@ import ( "encoding/json" "io/ioutil" "os" + "path/filepath" "testing" "github.com/mattermost/mattermost-server/model" @@ -53,7 +54,7 @@ func TestPlugin(t *testing.T) { }() path, _ := utils.FindDir("tests") - file, err := os.Open(path + "/testplugin.tar.gz") + file, err := os.Open(filepath.Join(path, "testplugin.tar.gz")) if err != nil { t.Fatal(err) } -- cgit v1.2.3-1-g7c22 From 74e703f58daff2aded07a969410f988cff9ada9b Mon Sep 17 00:00:00 2001 From: Chris Duarte Date: Thu, 22 Mar 2018 06:53:43 -0700 Subject: Timezone feature (#8185) * Add supported timezones into config Remove Timezone list creation in favor of timezone from configs Add Timezone field to Users table Clean up format of SupportedTimezones in config * Remove unwanted change * Add test for updating user timezone * Add empty map[string]string if Timezone is null * Add EnableTimezoneSelection config * Revert back to map[string]string for ClientConfig * Refactor SupportedTimezones into timezones.json * Include timezones.json in TestConfigFlag * Add timezone api endpoint * Bump varchar size to 256 and setMaxSize in user_store * Refactor LoadConfig to LoadConfig and LoadTimezoneConfig * Remove unnecessary argument in LoadConfig, mail_test * Add test for timezone endpoint * Add license header * Refactor timezones endpoint to system.go * Add system base route to timezone endpoint * db timezone upgrade in db v4.9 * Avoid saving SupportedTimezones to config.json * Add timezonePath support in config * Remove EnableTimezoneSelection from config * Use return statement without return parameter * Refactor test for SupportedTimezones * Check for supportedTimezone != nil instead of using len * Decouple SupportedTimezones out of Config * Fix failing test * Add LastTeamIconUpdate back in upgrade.go * Write timezone config in config_flag_test * Add code fallback for default timezone support --- api4/system.go | 14 ++++++++++++++ api4/system_test.go | 12 ++++++++++++ api4/user_test.go | 13 +++++++++++++ 3 files changed, 39 insertions(+) (limited to 'api4') diff --git a/api4/system.go b/api4/system.go index 7b63afc0b..4ae8ee7b9 100644 --- a/api4/system.go +++ b/api4/system.go @@ -17,6 +17,8 @@ import ( func (api *API) InitSystem() { api.BaseRoutes.System.Handle("/ping", api.ApiHandler(getSystemPing)).Methods("GET") + api.BaseRoutes.System.Handle("/timezones", api.ApiSessionRequired(getSupportedTimezones)).Methods("GET") + api.BaseRoutes.ApiRoot.Handle("/config", api.ApiSessionRequired(getConfig)).Methods("GET") api.BaseRoutes.ApiRoot.Handle("/config", api.ApiSessionRequired(updateConfig)).Methods("PUT") api.BaseRoutes.ApiRoot.Handle("/config/reload", api.ApiSessionRequired(configReload)).Methods("POST") @@ -378,6 +380,18 @@ func getAnalytics(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(rows.ToJson())) } +func getSupportedTimezones(c *Context, w http.ResponseWriter, r *http.Request) { + supportedTimezones := c.App.Timezones() + + if supportedTimezones != nil { + w.Write([]byte(model.TimezonesToJson(supportedTimezones))) + return + } + + emptyTimezones := make([]string, 0) + w.Write([]byte(model.TimezonesToJson(emptyTimezones))) +} + func testS3(c *Context, w http.ResponseWriter, r *http.Request) { cfg := model.ConfigFromJson(r.Body) if cfg == nil { diff --git a/api4/system_test.go b/api4/system_test.go index 6ef02cbfe..bb3790d4b 100644 --- a/api4/system_test.go +++ b/api4/system_test.go @@ -527,3 +527,15 @@ func TestS3TestConnection(t *testing.T) { t.Fatal("should return error ") } } + +func TestSupportedTimezones(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + Client := th.Client + + supportedTimezonesFromConfig := th.App.Timezones() + supportedTimezones, resp := Client.GetSupportedTimezone() + + CheckNoError(t, resp) + assert.Equal(t, supportedTimezonesFromConfig, supportedTimezones) +} diff --git a/api4/user_test.go b/api4/user_test.go index f04cd6ab2..359756aeb 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -996,6 +996,10 @@ func TestPatchUser(t *testing.T) { patch.Position = new(string) patch.NotifyProps = model.StringMap{} patch.NotifyProps["comment"] = "somethingrandom" + patch.Timezone = model.StringMap{} + patch.Timezone["useAutomaticTimezone"] = "true" + patch.Timezone["automaticTimezone"] = "America/New_York" + patch.Timezone["manualTimezone"] = "" ruser, resp := Client.PatchUser(user.Id, patch) CheckNoError(t, resp) @@ -1019,6 +1023,15 @@ func TestPatchUser(t *testing.T) { if ruser.NotifyProps["comment"] != "somethingrandom" { t.Fatal("NotifyProps did not update properly") } + if ruser.Timezone["useAutomaticTimezone"] != "true" { + t.Fatal("useAutomaticTimezone did not update properly") + } + if ruser.Timezone["automaticTimezone"] != "America/New_York" { + t.Fatal("automaticTimezone did not update properly") + } + if ruser.Timezone["manualTimezone"] != "" { + t.Fatal("manualTimezone did not update properly") + } patch.Username = model.NewString(th.BasicUser2.Username) _, resp = Client.PatchUser(user.Id, patch) -- cgit v1.2.3-1-g7c22