From 0bc3e46082d9018188262f9cb8fdbc206d0656a0 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 16 Mar 2017 14:59:44 -0400 Subject: Implement GET /logs endpoint for APIv4 (#5778) --- api4/system.go | 17 +++++++++++++++++ api4/system_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) (limited to 'api4') diff --git a/api4/system.go b/api4/system.go index d33be0c66..f12d802ef 100644 --- a/api4/system.go +++ b/api4/system.go @@ -21,6 +21,8 @@ func InitSystem() { BaseRoutes.ApiRoot.Handle("/email/test", ApiSessionRequired(testEmail)).Methods("POST") BaseRoutes.ApiRoot.Handle("/database/recycle", ApiSessionRequired(databaseRecycle)).Methods("POST") BaseRoutes.ApiRoot.Handle("/caches/invalidate", ApiSessionRequired(invalidateCaches)).Methods("POST") + + BaseRoutes.ApiRoot.Handle("/logs", ApiSessionRequired(getLogs)).Methods("GET") } func getSystemPing(c *Context, w http.ResponseWriter, r *http.Request) { @@ -94,3 +96,18 @@ func invalidateCaches(c *Context, w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") ReturnStatusOK(w) } + +func getLogs(c *Context, w http.ResponseWriter, r *http.Request) { + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + lines, err := app.GetLogs(c.Params.Page, c.Params.PerPage) + if err != nil { + c.Err = err + return + } + + w.Write([]byte(model.ArrayToJson(lines))) +} diff --git a/api4/system_test.go b/api4/system_test.go index 4da91f428..80b4996ca 100644 --- a/api4/system_test.go +++ b/api4/system_test.go @@ -4,6 +4,7 @@ import ( "strings" "testing" + l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" ) @@ -145,3 +146,43 @@ func TestInvalidateCaches(t *testing.T) { t.Fatal("should clean the cache") } } + +func TestGetLogs(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + for i := 0; i < 20; i++ { + l4g.Info(i) + } + + logs, resp := th.SystemAdminClient.GetLogs(0, 10) + CheckNoError(t, resp) + + if len(logs) != 10 { + t.Log(len(logs)) + t.Fatal("wrong length") + } + + logs, resp = th.SystemAdminClient.GetLogs(1, 10) + CheckNoError(t, resp) + + if len(logs) != 10 { + t.Log(len(logs)) + t.Fatal("wrong length") + } + + logs, resp = th.SystemAdminClient.GetLogs(-1, -1) + CheckNoError(t, resp) + + if len(logs) != 0 { + t.Fatal("should not be empty") + } + + _, resp = Client.GetLogs(0, 10) + CheckForbiddenStatus(t, resp) + + Client.Logout() + _, resp = Client.GetLogs(0, 10) + CheckUnauthorizedStatus(t, resp) +} -- cgit v1.2.3-1-g7c22