[MM-48031] Work template read API (#21661)
Этот коммит содержится в:
@@ -140,6 +140,8 @@ type Routes struct {
|
||||
|
||||
Usage *mux.Router // 'api/v4/usage'
|
||||
|
||||
WorkTemplates *mux.Router // 'api/v4/worktemplates'
|
||||
|
||||
HostedCustomer *mux.Router // 'api/v4/hosted_customer'
|
||||
|
||||
Drafts *mux.Router // 'api/v4/drafts'
|
||||
@@ -269,6 +271,8 @@ func Init(srv *app.Server) (*API, error) {
|
||||
|
||||
api.BaseRoutes.Usage = api.BaseRoutes.APIRoot.PathPrefix("/usage").Subrouter()
|
||||
|
||||
api.BaseRoutes.WorkTemplates = api.BaseRoutes.APIRoot.PathPrefix("/worktemplates").Subrouter()
|
||||
|
||||
api.BaseRoutes.HostedCustomer = api.BaseRoutes.APIRoot.PathPrefix("/hosted_customer").Subrouter()
|
||||
|
||||
api.BaseRoutes.Drafts = api.BaseRoutes.APIRoot.PathPrefix("/drafts").Subrouter()
|
||||
@@ -316,6 +320,7 @@ func Init(srv *app.Server) (*API, error) {
|
||||
api.InitExport()
|
||||
api.InitInsights()
|
||||
api.InitUsage()
|
||||
api.InitWorkTemplate()
|
||||
api.InitHostedCustomer()
|
||||
api.InitDrafts()
|
||||
if err := api.InitGraphQL(); err != nil {
|
||||
|
||||
67
api4/worktemplates.go
Обычный файл
67
api4/worktemplates.go
Обычный файл
@@ -0,0 +1,67 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package api4
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
)
|
||||
|
||||
func (api *API) InitWorkTemplate() {
|
||||
api.BaseRoutes.WorkTemplates.Handle("/categories", api.APISessionRequired(needsWorkTemplateFeatureFlag(getWorkTemplateCategories))).Methods("GET")
|
||||
api.BaseRoutes.WorkTemplates.Handle("/categories/{category}/templates", api.APISessionRequired(needsWorkTemplateFeatureFlag(getWorkTemplates))).Methods("GET")
|
||||
}
|
||||
|
||||
func needsWorkTemplateFeatureFlag(h handlerFunc) handlerFunc {
|
||||
return func(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
if !c.App.Config().FeatureFlags.WorkTemplate {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
h(c, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func getWorkTemplateCategories(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
t := c.AppContext.GetT()
|
||||
|
||||
categories, appErr := c.App.GetWorkTemplateCategories(t)
|
||||
if appErr != nil {
|
||||
c.Err = appErr
|
||||
return
|
||||
}
|
||||
|
||||
b, err := json.Marshal(categories)
|
||||
if err != nil {
|
||||
c.Err = model.NewAppError("getWorkTemplateCategories", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(b)
|
||||
}
|
||||
|
||||
func getWorkTemplates(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireCategory()
|
||||
if c.Err != nil {
|
||||
return
|
||||
}
|
||||
t := c.AppContext.GetT()
|
||||
|
||||
workTemplates, appErr := c.App.GetWorkTemplates(c.Params.Category, c.App.Config().FeatureFlags.ToMap(), t)
|
||||
if appErr != nil {
|
||||
c.Err = appErr
|
||||
return
|
||||
}
|
||||
|
||||
b, err := json.Marshal(workTemplates)
|
||||
if err != nil {
|
||||
c.Err = model.NewAppError("getWorkTemplates", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(b)
|
||||
}
|
||||
110
api4/worktemplates_test.go
Обычный файл
110
api4/worktemplates_test.go
Обычный файл
@@ -0,0 +1,110 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package api4
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/app/worktemplates"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWorkTemplateCategories(t *testing.T) {
|
||||
// Setup
|
||||
cleanup := setupWorktemplateFeatureFlag(t)
|
||||
defer cleanup()
|
||||
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
assert := require.New(t)
|
||||
|
||||
worktemplates.OrderedWorkTemplateCategories = []*worktemplates.WorkTemplateCategory{
|
||||
{
|
||||
ID: "test-category",
|
||||
Name: "Test Category",
|
||||
},
|
||||
{
|
||||
ID: "test-category-2",
|
||||
Name: "Test Category 2",
|
||||
},
|
||||
}
|
||||
|
||||
// Act
|
||||
categories, _, clientErr := th.Client.GetWorktemplateCategories()
|
||||
|
||||
// Assert
|
||||
require.NoError(t, clientErr)
|
||||
require.Len(t, categories, 2)
|
||||
assert.Equal("test-category", categories[0].ID)
|
||||
assert.Equal("test-category-2", categories[1].ID)
|
||||
}
|
||||
|
||||
func TestGetWorkTemplatesByCategory(t *testing.T) {
|
||||
// Setup
|
||||
cleanup := setupWorktemplateFeatureFlag(t)
|
||||
defer cleanup()
|
||||
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
assert := require.New(t)
|
||||
|
||||
worktemplates.OrderedWorkTemplateCategories = []*worktemplates.WorkTemplateCategory{
|
||||
{
|
||||
ID: "test-category",
|
||||
Name: "Test Category",
|
||||
},
|
||||
{
|
||||
ID: "test-category-2",
|
||||
Name: "Test Category 2",
|
||||
},
|
||||
}
|
||||
|
||||
worktemplates.OrderedWorkTemplates = []*worktemplates.WorkTemplate{
|
||||
{
|
||||
ID: "test-template",
|
||||
Category: "test-category",
|
||||
UseCase: "Test Template",
|
||||
},
|
||||
{
|
||||
ID: "test-template-2",
|
||||
Category: "test-category",
|
||||
UseCase: "Test Template 2",
|
||||
},
|
||||
{ // This one should not be returned because of the feature flag
|
||||
ID: "test-template-3",
|
||||
Category: "test-category",
|
||||
UseCase: "Test Template 3",
|
||||
FeatureFlag: &worktemplates.FeatureFlag{
|
||||
Name: "random-nonexistant-feature-flag",
|
||||
Value: "true",
|
||||
},
|
||||
},
|
||||
{ // this one should not be returned because of the category
|
||||
ID: "test-template-4",
|
||||
Category: "test-category-2",
|
||||
UseCase: "Test Template 4",
|
||||
},
|
||||
}
|
||||
|
||||
// Act
|
||||
workTemplates, _, clientErr := th.Client.GetWorkTemplatesByCategory("test-category")
|
||||
|
||||
// Assert
|
||||
assert.NoError(clientErr, "error while retrieve worktemplates list")
|
||||
assert.Len(workTemplates, 2)
|
||||
assert.Equal("test-template", workTemplates[0].ID)
|
||||
assert.Equal("test-template-2", workTemplates[1].ID)
|
||||
}
|
||||
|
||||
func setupWorktemplateFeatureFlag(t *testing.T) func() {
|
||||
t.Helper()
|
||||
|
||||
oldFFValue := os.Getenv("MM_FEATUREFLAGS_WORKTEMPLATE")
|
||||
os.Setenv("MM_FEATUREFLAGS_WORKTEMPLATE", "true")
|
||||
|
||||
return func() {
|
||||
os.Setenv("MM_FEATUREFLAGS_WORKTEMPLATE", oldFFValue)
|
||||
}
|
||||
}
|
||||
Ссылка в новой задаче
Block a user