From 0c3c29825362e145aef86e9f050a75a6e408b757 Mon Sep 17 00:00:00 2001 From: Kunal Acharya Date: Mon, 9 Sep 2024 17:42:22 +0530 Subject: [PATCH] MM-56819: Introduce --local mode in MMCTL to handle user preferences. (#26972) --- server/channels/api4/api.go | 2 + server/channels/api4/preference_local.go | 12 +++++ server/channels/api4/preference_test.go | 58 ++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 server/channels/api4/preference_local.go diff --git a/server/channels/api4/api.go b/server/channels/api4/api.go index bb28fd6ce3..5bc47c3791 100644 --- a/server/channels/api4/api.go +++ b/server/channels/api4/api.go @@ -395,6 +395,7 @@ func InitLocal(srv *app.Server) *API { api.BaseRoutes.LDAP = api.BaseRoutes.APIRoot.PathPrefix("/ldap").Subrouter() api.BaseRoutes.System = api.BaseRoutes.APIRoot.PathPrefix("/system").Subrouter() + api.BaseRoutes.Preferences = api.BaseRoutes.User.PathPrefix("/preferences").Subrouter() api.BaseRoutes.Posts = api.BaseRoutes.APIRoot.PathPrefix("/posts").Subrouter() api.BaseRoutes.Post = api.BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter() api.BaseRoutes.PostsForChannel = api.BaseRoutes.Channel.PathPrefix("/posts").Subrouter() @@ -425,6 +426,7 @@ func InitLocal(srv *app.Server) *API { api.InitLdapLocal() api.InitSystemLocal() api.InitPostLocal() + api.InitPreferenceLocal() api.InitRoleLocal() api.InitUploadLocal() api.InitImportLocal() diff --git a/server/channels/api4/preference_local.go b/server/channels/api4/preference_local.go new file mode 100644 index 0000000000..36ac09cc9a --- /dev/null +++ b/server/channels/api4/preference_local.go @@ -0,0 +1,12 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package api4 + +func (api *API) InitPreferenceLocal() { + api.BaseRoutes.Preferences.Handle("", api.APILocal(getPreferences)).Methods("GET") + api.BaseRoutes.Preferences.Handle("", api.APILocal(updatePreferences)).Methods("PUT") + api.BaseRoutes.Preferences.Handle("/delete", api.APILocal(deletePreferences)).Methods("POST") + api.BaseRoutes.Preferences.Handle("/{category:[A-Za-z0-9_]+}", api.APILocal(getPreferencesByCategory)).Methods("GET") + api.BaseRoutes.Preferences.Handle("/{category:[A-Za-z0-9_]+}/name/{preference_name:[A-Za-z0-9_]+}", api.APILocal(getPreferenceByCategoryAndName)).Methods("GET") +} diff --git a/server/channels/api4/preference_test.go b/server/channels/api4/preference_test.go index 63cdbfc08c..078e129d1b 100644 --- a/server/channels/api4/preference_test.go +++ b/server/channels/api4/preference_test.go @@ -74,6 +74,14 @@ func TestGetPreferences(t *testing.T) { _, resp, err = client.GetPreferences(context.Background(), th.BasicUser2.Id) require.Error(t, err) CheckUnauthorizedStatus(t, resp) + + // GetPreferences API from System Admin and Local Client + th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { + prefs, resp, err := c.GetPreferences(context.Background(), user1.Id) + require.NotNil(t, prefs) + require.NoError(t, err) + CheckOKStatus(t, resp) + }) } func TestGetPreferencesByCategory(t *testing.T) { @@ -134,6 +142,14 @@ func TestGetPreferencesByCategory(t *testing.T) { _, resp, err = client.GetPreferencesByCategory(context.Background(), th.BasicUser2.Id, category) require.Error(t, err) CheckUnauthorizedStatus(t, resp) + + // GetPreferencesByCategory API from System Admin and Local Client + th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { + prefs, resp, err := c.GetPreferencesByCategory(context.Background(), user1.Id, category) + require.NotNil(t, prefs) + require.NoError(t, err) + CheckOKStatus(t, resp) + }) } func TestGetPreferenceByCategoryAndName(t *testing.T) { @@ -192,6 +208,14 @@ func TestGetPreferenceByCategoryAndName(t *testing.T) { _, resp, err = client.GetPreferenceByCategoryAndName(context.Background(), user.Id, preferences[0].Category, preferences[0].Name) require.Error(t, err) CheckUnauthorizedStatus(t, resp) + + // GetPreferenceByCategoryAndName API from System Admin and Local Client + th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { + pref, resp, err := c.GetPreferenceByCategoryAndName(context.Background(), user.Id, preferences[0].Category, preferences[0].Name) + require.NotNil(t, pref) + require.NoError(t, err) + CheckOKStatus(t, resp) + }) } func TestUpdatePreferences(t *testing.T) { @@ -255,6 +279,21 @@ func TestUpdatePreferences(t *testing.T) { resp, err = client.UpdatePreferences(context.Background(), user1.Id, preferences1) require.Error(t, err) CheckUnauthorizedStatus(t, resp) + + // UpdatePreferences API from System Admin and Local Client + th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { + preferences := model.Preferences{ + { + UserId: user1.Id, + Category: category, + Name: model.NewId(), + Value: "true", + }, + } + resp, err := c.UpdatePreferences(context.Background(), user1.Id, preferences) + require.NoError(t, err) + CheckOKStatus(t, resp) + }) } func TestUpdatePreferencesOverload(t *testing.T) { @@ -623,6 +662,25 @@ func TestDeletePreferences(t *testing.T) { resp, err = client.DeletePreferences(context.Background(), th.BasicUser.Id, preferences) require.Error(t, err) CheckUnauthorizedStatus(t, resp) + + // DeletePreferences API from System Admin and Local Client + th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { + // Creating Test Data + var preferences model.Preferences + preference := model.Preference{ + UserId: th.BasicUser.Id, + Category: model.PreferenceCategoryCustomStatus, + Name: model.NewId(), + Value: "true", + } + preferences = append(preferences, preference) + c.UpdatePreferences(context.Background(), th.BasicUser.Id, preferences) + + // Delete Prefrerences Operation + resp, err = c.DeletePreferences(context.Background(), th.BasicUser.Id, preferences) + require.NoError(t, err) + CheckOKStatus(t, resp) + }) } func TestDeletePreferencesOverload(t *testing.T) {