Skip to content

Commit

Permalink
Merge 21fabfe into 28ad645
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilander committed Mar 24, 2017
2 parents 28ad645 + 21fabfe commit 043c17d
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
44 changes: 43 additions & 1 deletion api4/system.go
Expand Up @@ -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")
Expand Down Expand Up @@ -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())))
}
53 changes: 53 additions & 0 deletions api4/system_test.go
@@ -1,6 +1,7 @@
package api4

import (
"net/http"
"strings"
"testing"

Expand Down Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions i18n/en.json
Expand Up @@ -47,6 +47,14 @@
"id": "September",
"translation": "September"
},
{
"id": "api.config.client.old_format.app_error",
"translation": "New format for the client configuration is not supported yet. Please specify format=old in the query string."
},
{
"id": "api.license.client.old_format.app_error",
"translation": "New format for the client license is not supported yet. Please specify format=old in the query string."
},
{
"id": "api.admin.add_certificate.no_file.app_error",
"translation": "No file under 'certificate' in request"
Expand Down
27 changes: 27 additions & 0 deletions model/client4.go
Expand Up @@ -130,6 +130,10 @@ func (c *Client4) GetConfigRoute() string {
return fmt.Sprintf("/config")
}

func (c *Client4) GetLicenseRoute() string {
return fmt.Sprintf("/license")
}

func (c *Client4) GetPostRoute(postId string) string {
return fmt.Sprintf(c.GetPostsRoute()+"/%v", postId)
}
Expand Down Expand Up @@ -1223,6 +1227,7 @@ func (c *Client4) GetPing() (bool, *Response) {
}
}

// TestEmail will attempt to connect to the configured SMTP server.
func (c *Client4) TestEmail() (bool, *Response) {
if r, err := c.DoApiPost(c.GetTestEmailRoute(), ""); err != nil {
return false, &Response{StatusCode: r.StatusCode, Error: err}
Expand Down Expand Up @@ -1252,6 +1257,28 @@ func (c *Client4) ReloadConfig() (bool, *Response) {
}
}

// GetOldClientConfig will retrieve the parts of the server configuration needed by the
// client, formatted in the old format.
func (c *Client4) GetOldClientConfig(etag string) (map[string]string, *Response) {
if r, err := c.DoApiGet(c.GetConfigRoute()+"/client?format=old", etag); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return MapFromJson(r.Body), BuildResponse(r)
}
}

// GetOldClientLicense will retrieve the parts of the server license needed by the
// client, formatted in the old format.
func (c *Client4) GetOldClientLicense(etag string) (map[string]string, *Response) {
if r, err := c.DoApiGet(c.GetLicenseRoute()+"/client?format=old", etag); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return MapFromJson(r.Body), BuildResponse(r)
}
}

// DatabaseRecycle will recycle the connections. Discard current connection and get new one.
func (c *Client4) DatabaseRecycle() (bool, *Response) {
if r, err := c.DoApiPost(c.GetDatabaseRoute()+"/recycle", ""); err != nil {
Expand Down

0 comments on commit 043c17d

Please sign in to comment.