From fa47132b8f1d6d889450021f6afbdc903208ac41 Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Tue, 14 Mar 2017 17:06:07 +0100 Subject: [APIV4] POST /caches/invalidate - endpoint for apiV4 (#5756) --- api4/system.go | 17 +++++++++++++++++ api4/system_test.go | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'api4') diff --git a/api4/system.go b/api4/system.go index 32c010d82..d33be0c66 100644 --- a/api4/system.go +++ b/api4/system.go @@ -20,6 +20,7 @@ func InitSystem() { BaseRoutes.ApiRoot.Handle("/config/reload", ApiSessionRequired(configReload)).Methods("POST") 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") } func getSystemPing(c *Context, w http.ResponseWriter, r *http.Request) { @@ -77,3 +78,19 @@ func databaseRecycle(c *Context, w http.ResponseWriter, r *http.Request) { ReturnStatusOK(w) } + +func invalidateCaches(c *Context, w http.ResponseWriter, r *http.Request) { + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + err := app.InvalidateAllCaches() + if err != nil { + c.Err = err + return + } + + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + ReturnStatusOK(w) +} diff --git a/api4/system_test.go b/api4/system_test.go index cb6ca7b81..4da91f428 100644 --- a/api4/system_test.go +++ b/api4/system_test.go @@ -126,5 +126,22 @@ func TestDatabaseRecycle(t *testing.T) { _, resp = th.SystemAdminClient.DatabaseRecycle() CheckNoError(t, resp) +} + +func TestInvalidateCaches(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + flag, resp := Client.InvalidateCaches() + CheckForbiddenStatus(t, resp) + if flag == true { + t.Fatal("should not clean the cache due no permission.") + } + flag, resp = th.SystemAdminClient.InvalidateCaches() + CheckNoError(t, resp) + if flag == false { + t.Fatal("should clean the cache") + } } -- cgit v1.2.3-1-g7c22