From a0d5c01dfd97b45478353ccff777de677f088d0f Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Mon, 27 Mar 2017 09:19:53 -0400 Subject: Implement client config/license endpoints for APIv4 (#5867) --- api4/system.go | 44 +++++++++++++++++++++++++++++++++++++++++++- api4/system_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) (limited to 'api4') diff --git a/api4/system.go b/api4/system.go index 972d526da..098f7e4b7 100644 --- a/api4/system.go +++ b/api4/system.go @@ -17,8 +17,12 @@ func InitSystem() { BaseRoutes.System.Handle("/ping", ApiHandler(getSystemPing)).Methods("GET") BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(getConfig)).Methods("GET") - BaseRoutes.ApiRoot.Handle("/config/reload", ApiSessionRequired(configReload)).Methods("POST") BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(updateConfig)).Methods("PUT") + BaseRoutes.ApiRoot.Handle("/config/reload", ApiSessionRequired(configReload)).Methods("POST") + BaseRoutes.ApiRoot.Handle("/config/client", ApiHandler(getClientConfig)).Methods("GET") + + BaseRoutes.ApiRoot.Handle("/license/client", ApiHandler(getClientLicense)).Methods("GET") + BaseRoutes.ApiRoot.Handle("/audits", ApiSessionRequired(getAudits)).Methods("GET") BaseRoutes.ApiRoot.Handle("/email/test", ApiSessionRequired(testEmail)).Methods("POST") BaseRoutes.ApiRoot.Handle("/database/recycle", ApiSessionRequired(databaseRecycle)).Methods("POST") @@ -155,3 +159,41 @@ func getLogs(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(model.ArrayToJson(lines))) } + +func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) { + format := r.URL.Query().Get("format") + + if format == "" { + c.Err = model.NewAppError("getClientConfig", "api.config.client.old_format.app_error", nil, "", http.StatusNotImplemented) + return + } + + if format != "old" { + c.SetInvalidParam("format") + return + } + + w.Write([]byte(model.MapToJson(utils.ClientCfg))) +} + +func getClientLicense(c *Context, w http.ResponseWriter, r *http.Request) { + format := r.URL.Query().Get("format") + + if format == "" { + c.Err = model.NewAppError("getClientLicense", "api.license.client.old_format.app_error", nil, "", http.StatusNotImplemented) + return + } + + if format != "old" { + c.SetInvalidParam("format") + return + } + + etag := utils.GetClientLicenseEtag(true) + if HandleEtag(etag, "Get Client License", w, r) { + return + } + + w.Header().Set(model.HEADER_ETAG_SERVER, etag) + w.Write([]byte(model.MapToJson(utils.GetSanitizedClientLicense()))) +} diff --git a/api4/system_test.go b/api4/system_test.go index 289a41907..b2b5d3f43 100644 --- a/api4/system_test.go +++ b/api4/system_test.go @@ -1,6 +1,7 @@ package api4 import ( + "net/http" "strings" "testing" @@ -129,6 +130,58 @@ func TestUpdateConfig(t *testing.T) { } } +func TestGetOldClientConfig(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + config, resp := Client.GetOldClientConfig("") + CheckNoError(t, resp) + + if len(config["Version"]) == 0 { + t.Fatal("config not returned correctly") + } + + Client.Logout() + + _, resp = Client.GetOldClientConfig("") + CheckNoError(t, resp) + + if _, err := Client.DoApiGet("/config/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented { + t.Fatal("should have errored with 501") + } + + if _, err := Client.DoApiGet("/config/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest { + t.Fatal("should have errored with 400") + } +} + +func TestGetOldClientLicense(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + license, resp := Client.GetOldClientLicense("") + CheckNoError(t, resp) + + if len(license["IsLicensed"]) == 0 { + t.Fatal("license not returned correctly") + } + + Client.Logout() + + _, resp = Client.GetOldClientLicense("") + CheckNoError(t, resp) + + if _, err := Client.DoApiGet("/license/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented { + t.Fatal("should have errored with 501") + } + + if _, err := Client.DoApiGet("/license/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest { + t.Fatal("should have errored with 400") + } +} + func TestGetAudits(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() -- cgit v1.2.3-1-g7c22