diff --git a/server/.golangci.yml b/server/.golangci.yml index 296278763a6..e7b25ff6041 100644 --- a/server/.golangci.yml +++ b/server/.golangci.yml @@ -86,29 +86,7 @@ linters: - errcheck path: "\ channels/api4/apitestlib.go|\ - channels/api4/bot_test.go|\ channels/api4/channel_test.go|\ - channels/api4/cloud.go|\ - channels/api4/cloud_test.go|\ - channels/api4/cluster.go|\ - channels/api4/command.go|\ - channels/api4/command_test.go|\ - channels/api4/compliance.go|\ - channels/api4/config.go|\ - channels/api4/config_local.go|\ - channels/api4/config_test.go|\ - channels/api4/data_retention.go|\ - channels/api4/preference_test.go|\ - channels/api4/reaction_test.go|\ - channels/api4/role.go|\ - channels/api4/saml.go|\ - channels/api4/scheme.go|\ - channels/api4/shared_channel.go|\ - channels/api4/system.go|\ - channels/api4/system_local.go|\ - channels/api4/team_local.go|\ - channels/api4/websocket_test.go|\ - channels/app/bot_test.go|\ channels/app/file_test.go|\ channels/app/helper_test.go|\ channels/app/platform/helper_test.go|\ @@ -157,7 +135,6 @@ linters: channels/store/storetest/team_store.go|\ channels/store/storetest/thread_store.go|\ channels/store/storetest/user_store.go|\ - channels/web/web_test.go|\ cmd/mattermost/commands/cmdtestlib.go|\ cmd/mattermost/commands/db.go|\ cmd/mattermost/commands/export.go|\ diff --git a/server/channels/api4/cloud.go b/server/channels/api4/cloud.go index d5afca0dda9..19c8a1700ed 100644 --- a/server/channels/api4/cloud.go +++ b/server/channels/api4/cloud.go @@ -80,7 +80,9 @@ func getPreviewSubscription(c *Context, w http.ResponseWriter, r *http.Request) return } - w.Write(json) + if _, err := w.Write(json); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func getSubscription(c *Context, w http.ResponseWriter, r *http.Request) { @@ -143,7 +145,9 @@ func getSubscription(c *Context, w http.ResponseWriter, r *http.Request) { return } - w.Write(json) + if _, err := w.Write(json); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func validateBusinessEmail(c *Context, w http.ResponseWriter, r *http.Request) { @@ -276,11 +280,15 @@ func getCloudProducts(c *Context, w http.ResponseWriter, r *http.Request) { return } - w.Write(byteSanitizedProductsData) + if _, err := w.Write(byteSanitizedProductsData); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } return } - w.Write(byteProductsData) + if _, err := w.Write(byteProductsData); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func getCloudLimits(c *Context, w http.ResponseWriter, r *http.Request) { @@ -306,7 +314,9 @@ func getCloudLimits(c *Context, w http.ResponseWriter, r *http.Request) { return } - w.Write(json) + if _, err := w.Write(json); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func getCloudCustomer(c *Context, w http.ResponseWriter, r *http.Request) { @@ -337,7 +347,9 @@ func getCloudCustomer(c *Context, w http.ResponseWriter, r *http.Request) { return } - w.Write(json) + if _, err := w.Write(json); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func getInstallation(c *Context, w http.ResponseWriter, r *http.Request) { @@ -403,7 +415,9 @@ func updateCloudCustomer(c *Context, w http.ResponseWriter, r *http.Request) { return } - w.Write(json) + if _, err := w.Write(json); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func updateCloudCustomerAddress(c *Context, w http.ResponseWriter, r *http.Request) { @@ -446,7 +460,9 @@ func updateCloudCustomerAddress(c *Context, w http.ResponseWriter, r *http.Reque return } - w.Write(json) + if _, err := w.Write(json); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func getInvoicesForSubscription(c *Context, w http.ResponseWriter, r *http.Request) { @@ -477,7 +493,9 @@ func getInvoicesForSubscription(c *Context, w http.ResponseWriter, r *http.Reque return } - w.Write(json) + if _, err := w.Write(json); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func getSubscriptionInvoicePDF(c *Context, w http.ResponseWriter, r *http.Request) { diff --git a/server/channels/api4/cloud_test.go b/server/channels/api4/cloud_test.go index 1c436a94ee2..e3f9efd86d2 100644 --- a/server/channels/api4/cloud_test.go +++ b/server/channels/api4/cloud_test.go @@ -16,7 +16,7 @@ import ( "github.com/mattermost/mattermost/server/v8/einterfaces/mocks" ) -func Test_GetSubscription(t *testing.T) { +func TestGetSubscription(t *testing.T) { mainHelper.Parallel(t) deliquencySince := int64(2000000000) @@ -57,7 +57,8 @@ func Test_GetSubscription(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + require.NoError(t, err) th.App.Srv().SetLicense(model.NewTestLicense("cloud")) @@ -83,7 +84,8 @@ func Test_GetSubscription(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + require.NoError(t, err) th.App.Srv().SetLicense(model.NewTestLicense("cloud")) @@ -105,14 +107,15 @@ func Test_GetSubscription(t *testing.T) { }) } -func Test_validateBusinessEmail(t *testing.T) { +func TestValidateBusinessEmail(t *testing.T) { mainHelper.Parallel(t) t.Run("Returns forbidden for invalid business email", func(t *testing.T) { mainHelper.Parallel(t) th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + require.NoError(t, err) validBusinessEmail := model.ValidateBusinessEmailRequest{Email: "invalid@slacker.com"} @@ -138,7 +141,8 @@ func Test_validateBusinessEmail(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + require.NoError(t, err) validBusinessEmail := model.ValidateBusinessEmailRequest{Email: "valid@mattermost.com"} @@ -164,7 +168,8 @@ func Test_validateBusinessEmail(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + require.NoError(t, err) th.App.Srv().SetLicense(model.NewTestLicense("cloud")) @@ -175,14 +180,15 @@ func Test_validateBusinessEmail(t *testing.T) { }) } -func Test_validateWorkspaceBusinessEmail(t *testing.T) { +func TestValidateWorkspaceBusinessEmail(t *testing.T) { mainHelper.Parallel(t) t.Run("validate the Cloud Customer has used a valid email to create the workspace", func(t *testing.T) { mainHelper.Parallel(t) th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + require.NoError(t, err) th.App.Srv().SetLicense(model.NewTestLicense("cloud")) @@ -205,7 +211,7 @@ func Test_validateWorkspaceBusinessEmail(t *testing.T) { }() th.App.Srv().Cloud = &cloud - _, err := th.SystemAdminClient.ValidateWorkspaceBusinessEmail(context.Background()) + _, err = th.SystemAdminClient.ValidateWorkspaceBusinessEmail(context.Background()) require.NoError(t, err) }) @@ -214,7 +220,8 @@ func Test_validateWorkspaceBusinessEmail(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + require.NoError(t, err) th.App.Srv().SetLicense(model.NewTestLicense("cloud")) @@ -242,7 +249,7 @@ func Test_validateWorkspaceBusinessEmail(t *testing.T) { }() th.App.Srv().Cloud = &cloud - _, err := th.SystemAdminClient.ValidateWorkspaceBusinessEmail(context.Background()) + _, err = th.SystemAdminClient.ValidateWorkspaceBusinessEmail(context.Background()) require.NoError(t, err) }) @@ -251,7 +258,8 @@ func Test_validateWorkspaceBusinessEmail(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + require.NoError(t, err) th.App.Srv().SetLicense(model.NewTestLicense("cloud")) @@ -353,7 +361,8 @@ func TestGetCloudProducts(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.SystemAdminUser.Email, th.SystemAdminUser.Password) + _, _, err := th.Client.Login(context.Background(), th.SystemAdminUser.Email, th.SystemAdminUser.Password) + require.NoError(t, err) th.App.Srv().SetLicense(model.NewTestLicense("cloud")) @@ -376,7 +385,8 @@ func TestGetCloudProducts(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) + require.NoError(t, err) th.App.Srv().SetLicense(model.NewTestLicense("cloud")) diff --git a/server/channels/api4/cluster.go b/server/channels/api4/cluster.go index 102ee25485a..e57edf948ef 100644 --- a/server/channels/api4/cluster.go +++ b/server/channels/api4/cluster.go @@ -8,6 +8,7 @@ import ( "net/http" "github.com/mattermost/mattermost/server/public/model" + "github.com/mattermost/mattermost/server/public/shared/mlog" ) func (api *API) InitCluster() { @@ -31,5 +32,7 @@ func getClusterStatus(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewAppError("getClusterStatus", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } diff --git a/server/channels/api4/command.go b/server/channels/api4/command.go index 6f08f3922f7..b115d666966 100644 --- a/server/channels/api4/command.go +++ b/server/channels/api4/command.go @@ -439,7 +439,9 @@ func listCommandAutocompleteSuggestions(c *Context, w http.ResponseWriter, r *ht c.Err = model.NewAppError("listCommandAutocompleteSuggestions", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func regenCommandToken(c *Context, w http.ResponseWriter, r *http.Request) { @@ -487,5 +489,7 @@ func regenCommandToken(c *Context, w http.ResponseWriter, r *http.Request) { resp := make(map[string]string) resp["token"] = rcmd.Token - w.Write([]byte(model.MapToJSON(resp))) + if _, err := w.Write([]byte(model.MapToJSON(resp))); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } diff --git a/server/channels/api4/command_test.go b/server/channels/api4/command_test.go index b10d9b4883c..6065a03a56d 100644 --- a/server/channels/api4/command_test.go +++ b/server/channels/api4/command_test.go @@ -157,7 +157,8 @@ func TestUpdateCommand(t *testing.T) { require.Error(t, err) CheckNotFoundStatus(t, resp) }) - th.SystemAdminClient.Logout(context.Background()) + _, err := th.SystemAdminClient.Logout(context.Background()) + require.NoError(t, err) _, resp, err := th.SystemAdminClient.UpdateCommand(context.Background(), cmd2) require.Error(t, err) CheckUnauthorizedStatus(t, resp) @@ -216,7 +217,8 @@ func TestMoveCommand(t *testing.T) { require.Error(t, err) CheckNotFoundStatus(t, resp) - th.SystemAdminClient.Logout(context.Background()) + _, err = th.SystemAdminClient.Logout(context.Background()) + require.NoError(t, err) resp, err = th.SystemAdminClient.MoveCommand(context.Background(), newTeam.Id, rcmd2.Id) require.Error(t, err) CheckUnauthorizedStatus(t, resp) @@ -275,7 +277,8 @@ func TestDeleteCommand(t *testing.T) { require.Error(t, err) CheckNotFoundStatus(t, resp) - th.SystemAdminClient.Logout(context.Background()) + _, err = th.SystemAdminClient.Logout(context.Background()) + require.NoError(t, err) resp, err = th.SystemAdminClient.DeleteCommand(context.Background(), rcmd2.Id) require.Error(t, err) CheckUnauthorizedStatus(t, resp) @@ -300,10 +303,8 @@ func TestListCommands(t *testing.T) { Method: model.CommandMethodPost, Trigger: "custom_command", } - - _, _, err := th.SystemAdminClient.CreateCommand(context.Background(), newCmd) - require.NoError(t, err) - + _, _, rootErr := th.SystemAdminClient.CreateCommand(context.Background(), newCmd) + require.NoError(t, rootErr) th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { listCommands, _, err := c.ListCommands(context.Background(), th.BasicTeam.Id, false) require.NoError(t, err) @@ -355,10 +356,13 @@ func TestListCommands(t *testing.T) { }) t.Run("NoMember", func(t *testing.T) { - client.Logout(context.Background()) + _, err := client.Logout(context.Background()) + require.NoError(t, err) + user := th.CreateUser() - th.SystemAdminClient.RemoveTeamMember(context.Background(), th.BasicTeam.Id, user.Id) - client.Login(context.Background(), user.Email, user.Password) + _, _, err = client.Login(context.Background(), user.Email, user.Password) + require.NoError(t, err) + _, resp, err := client.ListCommands(context.Background(), th.BasicTeam.Id, false) require.Error(t, err) CheckForbiddenStatus(t, resp) @@ -368,7 +372,8 @@ func TestListCommands(t *testing.T) { }) t.Run("NotLoggedIn", func(t *testing.T) { - client.Logout(context.Background()) + _, err := client.Logout(context.Background()) + require.NoError(t, err) _, resp, err := client.ListCommands(context.Background(), th.BasicTeam.Id, false) require.Error(t, err) CheckUnauthorizedStatus(t, resp) @@ -432,17 +437,21 @@ func TestListAutocompleteCommands(t *testing.T) { }) t.Run("NoMember", func(t *testing.T) { - client.Logout(context.Background()) + _, err := client.Logout(context.Background()) + require.NoError(t, err) + user := th.CreateUser() - th.SystemAdminClient.RemoveTeamMember(context.Background(), th.BasicTeam.Id, user.Id) - client.Login(context.Background(), user.Email, user.Password) + _, _, err = client.Login(context.Background(), user.Email, user.Password) + require.NoError(t, err) + _, resp, err := client.ListAutocompleteCommands(context.Background(), th.BasicTeam.Id) require.Error(t, err) CheckForbiddenStatus(t, resp) }) t.Run("NotLoggedIn", func(t *testing.T) { - client.Logout(context.Background()) + _, err := client.Logout(context.Background()) + require.NoError(t, err) _, resp, err := client.ListAutocompleteCommands(context.Background(), th.BasicTeam.Id) require.Error(t, err) CheckUnauthorizedStatus(t, resp) @@ -526,17 +535,21 @@ func TestListCommandAutocompleteSuggestions(t *testing.T) { }) t.Run("NoMember", func(t *testing.T) { - client.Logout(context.Background()) + _, err := client.Logout(context.Background()) + require.NoError(t, err) + user := th.CreateUser() - th.SystemAdminClient.RemoveTeamMember(context.Background(), th.BasicTeam.Id, user.Id) - client.Login(context.Background(), user.Email, user.Password) + _, _, err = client.Login(context.Background(), user.Email, user.Password) + require.NoError(t, err) + _, resp, err := client.ListCommandAutocompleteSuggestions(context.Background(), "/", th.BasicTeam.Id) require.Error(t, err) CheckForbiddenStatus(t, resp) }) t.Run("NotLoggedIn", func(t *testing.T) { - client.Logout(context.Background()) + _, err := client.Logout(context.Background()) + require.NoError(t, err) _, resp, err := client.ListCommandAutocompleteSuggestions(context.Background(), "/", th.BasicTeam.Id) require.Error(t, err) CheckUnauthorizedStatus(t, resp) @@ -561,9 +574,8 @@ func TestGetCommand(t *testing.T) { Method: model.CommandMethodPost, Trigger: "roger", } - - newCmd, _, err := th.SystemAdminClient.CreateCommand(context.Background(), newCmd) - require.NoError(t, err) + newCmd, _, rootErr := th.SystemAdminClient.CreateCommand(context.Background(), newCmd) + require.NoError(t, rootErr) th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { t.Run("ValidId", func(t *testing.T) { cmd, _, err := client.GetCommandById(context.Background(), newCmd.Id) @@ -589,17 +601,21 @@ func TestGetCommand(t *testing.T) { }) t.Run("NoMember", func(t *testing.T) { - th.Client.Logout(context.Background()) + _, err := th.Client.Logout(context.Background()) + require.NoError(t, err) + user := th.CreateUser() - th.SystemAdminClient.RemoveTeamMember(context.Background(), th.BasicTeam.Id, user.Id) - th.Client.Login(context.Background(), user.Email, user.Password) + _, _, err = th.Client.Login(context.Background(), user.Email, user.Password) + require.NoError(t, err) + _, resp, err := th.Client.GetCommandById(context.Background(), newCmd.Id) require.Error(t, err) CheckNotFoundStatus(t, resp) }) t.Run("NotLoggedIn", func(t *testing.T) { - th.Client.Logout(context.Background()) + _, err := th.Client.Logout(context.Background()) + require.NoError(t, err) _, resp, err := th.Client.GetCommandById(context.Background(), newCmd.Id) require.Error(t, err) CheckUnauthorizedStatus(t, resp) @@ -695,13 +711,15 @@ func TestExecuteInvalidCommand(t *testing.T) { CheckNotFoundStatus(t, resp) otherUser := th.CreateUser() - client.Login(context.Background(), otherUser.Email, otherUser.Password) + _, _, err = client.Login(context.Background(), otherUser.Email, otherUser.Password) + require.NoError(t, err) _, resp, err = client.ExecuteCommand(context.Background(), channel.Id, "/getcommand") require.Error(t, err) CheckForbiddenStatus(t, resp) - client.Logout(context.Background()) + _, err = client.Logout(context.Background()) + require.NoError(t, err) _, resp, err = client.ExecuteCommand(context.Background(), channel.Id, "/getcommand") require.Error(t, err) @@ -803,7 +821,8 @@ func TestExecutePostCommand(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { require.Equal(t, http.MethodPost, r.Method) - r.ParseForm() + err := r.ParseForm() + require.NoError(t, err) require.Equal(t, token, r.FormValue("token")) require.Equal(t, th.BasicTeam.Name, r.FormValue("team_domain")) @@ -1042,7 +1061,8 @@ func TestExecuteCommandInTeamUserIsNotOn(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { require.Equal(t, http.MethodPost, r.Method) - r.ParseForm() + err := r.ParseForm() + require.NoError(t, err) require.Equal(t, team2.Name, r.FormValue("team_domain")) w.Header().Set("Content-Type", "application/json") @@ -1116,7 +1136,8 @@ func TestExecuteCommandReadOnly(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { require.Equal(t, http.MethodPost, r.Method) - r.ParseForm() + err := r.ParseForm() + require.NoError(t, err) require.Equal(t, th.BasicTeam.Name, r.FormValue("team_domain")) w.Header().Set("Content-Type", "application/json") @@ -1145,7 +1166,8 @@ func TestExecuteCommandReadOnly(t *testing.T) { // Enable Enterprise features th.App.Srv().SetLicense(model.NewTestLicense()) - th.App.SetPhase2PermissionsMigrationStatus(true) + err = th.App.SetPhase2PermissionsMigrationStatus(true) + require.NoError(t, err) _, appErr = th.App.PatchChannelModerationsForChannel( th.Context, diff --git a/server/channels/api4/compliance.go b/server/channels/api4/compliance.go index 795d8c7518e..f16e8100270 100644 --- a/server/channels/api4/compliance.go +++ b/server/channels/api4/compliance.go @@ -158,5 +158,7 @@ func downloadComplianceReport(c *Context, w http.ResponseWriter, r *http.Request auditRec.Success() - w.Write(reportBytes) + if _, err := w.Write(reportBytes); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } diff --git a/server/channels/api4/config.go b/server/channels/api4/config.go index f88768e6059..e697176a4c4 100644 --- a/server/channels/api4/config.go +++ b/server/channels/api4/config.go @@ -239,7 +239,9 @@ func updateConfig(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewAppError("updateConfig", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } return } @@ -281,7 +283,9 @@ func getEnvironmentConfig(c *Context, w http.ResponseWriter, r *http.Request) { }) w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") - w.Write([]byte(model.StringInterfaceToJSON(envConfig))) + if _, err := w.Write([]byte(model.StringInterfaceToJSON(envConfig))); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func patchConfig(c *Context, w http.ResponseWriter, r *http.Request) { @@ -386,7 +390,9 @@ func patchConfig(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewAppError("patchConfig", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } return } diff --git a/server/channels/api4/config_local.go b/server/channels/api4/config_local.go index 39b704ab693..79059905bd0 100644 --- a/server/channels/api4/config_local.go +++ b/server/channels/api4/config_local.go @@ -208,5 +208,8 @@ func localGetClientConfig(c *Context, w http.ResponseWriter, r *http.Request) { auditRec.Success() - w.Write([]byte(model.MapToJSON(c.App.Srv().Platform().ClientConfigWithComputed()))) + _, err := w.Write([]byte(model.MapToJSON(c.App.Srv().Platform().ClientConfigWithComputed()))) + if err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } diff --git a/server/channels/api4/config_test.go b/server/channels/api4/config_test.go index 55e482d2c49..2389c5feb4b 100644 --- a/server/channels/api4/config_test.go +++ b/server/channels/api4/config_test.go @@ -81,7 +81,8 @@ func TestGetConfigWithAccessTag(t *testing.T) { cfg.SupportSettings.SupportEmail = &mockSupportEmail }) - th.Client.Login(context.Background(), th.BasicUser.Username, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Username, th.BasicUser.Password) + require.NoError(t, err) // add read sysconsole environment config th.AddPermissionToRole(model.PermissionSysconsoleReadEnvironmentRateLimiting.Id, model.SystemUserRoleId) @@ -107,7 +108,8 @@ func TestGetConfigAnyFlagsAccess(t *testing.T) { th := Setup(t) defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Username, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Username, th.BasicUser.Password) + require.NoError(t, err) _, resp, _ := th.Client.GetConfig(context.Background()) t.Run("Check permissions error with no sysconsole read permission", func(t *testing.T) { @@ -259,7 +261,10 @@ func TestUpdateConfig(t *testing.T) { t.Run("Should not be able to modify ComplianceSettings.Directory in cloud", func(t *testing.T) { th.App.Srv().SetLicense(model.NewTestLicense("cloud")) - defer th.App.Srv().RemoveLicense() + defer func() { + appErr := th.App.Srv().RemoveLicense() + require.Nil(t, appErr) + }() cfg2 := th.App.Config().Clone() *cfg2.ComplianceSettings.Directory = "hellodir" @@ -297,7 +302,8 @@ func TestUpdateConfig(t *testing.T) { func TestGetConfigWithoutManageSystemPermission(t *testing.T) { th := Setup(t) defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Username, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Username, th.BasicUser.Password) + require.NoError(t, err) t.Run("any sysconsole read permission provides config read access", func(t *testing.T) { // forbidden by default @@ -316,7 +322,8 @@ func TestGetConfigWithoutManageSystemPermission(t *testing.T) { func TestUpdateConfigWithoutManageSystemPermission(t *testing.T) { th := Setup(t) defer th.TearDown() - th.Client.Login(context.Background(), th.BasicUser.Username, th.BasicUser.Password) + _, _, err := th.Client.Login(context.Background(), th.BasicUser.Username, th.BasicUser.Password) + require.NoError(t, err) // add read sysconsole integrations config th.AddPermissionToRole(model.PermissionSysconsoleReadIntegrationsIntegrationManagement.Id, model.SystemUserRoleId) @@ -866,11 +873,17 @@ func TestMigrateConfig(t *testing.T) { f, err := config.NewStoreFromDSN("from.json", false, nil, false) require.NoError(t, err) - defer f.RemoveFile("from.json") + defer func() { + err = f.RemoveFile("from.json") + require.NoError(t, err) + }() _, err = config.NewStoreFromDSN("to.json", false, nil, true) require.NoError(t, err) - defer f.RemoveFile("to.json") + defer func() { + err = f.RemoveFile("to.json") + require.NoError(t, err) + }() _, err = th.LocalClient.MigrateConfig(context.Background(), "from.json", "to.json") require.NoError(t, err) diff --git a/server/channels/api4/data_retention.go b/server/channels/api4/data_retention.go index 43c89361901..7452685a3cf 100644 --- a/server/channels/api4/data_retention.go +++ b/server/channels/api4/data_retention.go @@ -46,7 +46,9 @@ func getGlobalPolicy(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewAppError("getGlobalPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func getPolicies(c *Context, w http.ResponseWriter, r *http.Request) { @@ -69,7 +71,9 @@ func getPolicies(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewAppError("getPolicies", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func getPoliciesCount(c *Context, w http.ResponseWriter, r *http.Request) { @@ -111,7 +115,9 @@ func getPolicy(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewAppError("getPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func createPolicy(c *Context, w http.ResponseWriter, r *http.Request) { @@ -144,7 +150,9 @@ func createPolicy(c *Context, w http.ResponseWriter, r *http.Request) { } auditRec.Success() w.WriteHeader(http.StatusCreated) - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func patchPolicy(c *Context, w http.ResponseWriter, r *http.Request) { @@ -180,7 +188,9 @@ func patchPolicy(c *Context, w http.ResponseWriter, r *http.Request) { return } auditRec.Success() - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func deletePolicy(c *Context, w http.ResponseWriter, r *http.Request) { @@ -226,7 +236,9 @@ func getTeamsForPolicy(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewAppError("Api4.getTeamsForPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(b) + if _, err := w.Write(b); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func searchTeamsInPolicy(c *Context, w http.ResponseWriter, r *http.Request) { @@ -258,7 +270,9 @@ func searchTeamsInPolicy(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewAppError("searchTeamsInPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func addTeamsToPolicy(c *Context, w http.ResponseWriter, r *http.Request) { @@ -338,7 +352,9 @@ func getChannelsForPolicy(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = model.NewAppError("Api4.getChannelsForPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err) return } - w.Write(b) + if _, err := w.Write(b); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func searchChannelsInPolicy(c *Context, w http.ResponseWriter, r *http.Request) { @@ -377,7 +393,9 @@ func searchChannelsInPolicy(c *Context, w http.ResponseWriter, r *http.Request) return } - w.Write(channelsJSON) + if _, err := w.Write(channelsJSON); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func addChannelsToPolicy(c *Context, w http.ResponseWriter, r *http.Request) { @@ -461,7 +479,9 @@ func getTeamPoliciesForUser(c *Context, w http.ResponseWriter, r *http.Request) c.Err = model.NewAppError("getTeamPoliciesForUser", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(jsonErr) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } func getChannelPoliciesForUser(c *Context, w http.ResponseWriter, r *http.Request) { @@ -489,5 +509,7 @@ func getChannelPoliciesForUser(c *Context, w http.ResponseWriter, r *http.Reques c.Err = model.NewAppError("getChannelPoliciesForUser", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(jsonErr) return } - w.Write(js) + if _, err := w.Write(js); err != nil { + c.Logger.Warn("Error while writing response", mlog.Err(err)) + } } diff --git a/server/channels/web/web_test.go b/server/channels/web/web_test.go index 410dd15b59c..8f161f0cb79 100644 --- a/server/channels/web/web_test.go +++ b/server/channels/web/web_test.go @@ -8,8 +8,6 @@ import ( "net/http" "net/http/httptest" "os" - "os/exec" - "path" "path/filepath" "testing" "time" @@ -80,7 +78,8 @@ func setupTestHelper(tb testing.TB, includeCacheLayer bool, options []app.Option *newConfig.LogSettings.EnableSentry = false // disable error reporting during tests *newConfig.LogSettings.ConsoleJson = false *newConfig.LogSettings.ConsoleLevel = mlog.LvlStdLog.Name - memoryStore.Set(newConfig) + _, _, err := memoryStore.Set(newConfig) + require.NoError(tb, err) options = append(options, app.ConfigStore(memoryStore)) if includeCacheLayer { // Adds the cache layer to the test store @@ -371,12 +370,6 @@ func TestStatic(t *testing.T) { func TestStaticFilesCaching(t *testing.T) { th := Setup(t).InitPlugins() - wd, err := os.Getwd() - require.NoError(t, err) - cmd := exec.Command("ls", path.Join(wd, "client", "plugins")) - cmd.Stdout = os.Stdout - cmd.Run() - fakeMainBundleName := "main.1234ab.js" fakeRootHTML := ` @@ -386,7 +379,7 @@ func TestStaticFilesCaching(t *testing.T) { fakeMainBundle := `module.exports = 'main';` fakeRemoteEntry := `module.exports = 'remote';` - err = os.WriteFile("./client/root.html", []byte(fakeRootHTML), 0600) + err := os.WriteFile("./client/root.html", []byte(fakeRootHTML), 0600) require.NoError(t, err) err = os.WriteFile("./client/"+fakeMainBundleName, []byte(fakeMainBundle), 0600) require.NoError(t, err)