[MM-29049] Fix remaining errcheck errors in app, api4 and web package (#31307)

Co-authored-by: Claude <noreply@anthropic.com>
Этот коммит содержится в:
Ben Schumacher
2025-06-06 07:44:43 +02:00
коммит произвёл GitHub
родитель 7e013f4c1a
Коммит e3452dce94
12 изменённых файлов: 188 добавлений и 115 удалений

Просмотреть файл

@@ -86,29 +86,7 @@ linters:
- errcheck - errcheck
path: "\ path: "\
channels/api4/apitestlib.go|\ channels/api4/apitestlib.go|\
channels/api4/bot_test.go|\
channels/api4/channel_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/file_test.go|\
channels/app/helper_test.go|\ channels/app/helper_test.go|\
channels/app/platform/helper_test.go|\ channels/app/platform/helper_test.go|\
@@ -157,7 +135,6 @@ linters:
channels/store/storetest/team_store.go|\ channels/store/storetest/team_store.go|\
channels/store/storetest/thread_store.go|\ channels/store/storetest/thread_store.go|\
channels/store/storetest/user_store.go|\ channels/store/storetest/user_store.go|\
channels/web/web_test.go|\
cmd/mattermost/commands/cmdtestlib.go|\ cmd/mattermost/commands/cmdtestlib.go|\
cmd/mattermost/commands/db.go|\ cmd/mattermost/commands/db.go|\
cmd/mattermost/commands/export.go|\ cmd/mattermost/commands/export.go|\

Просмотреть файл

@@ -80,7 +80,9 @@ func getPreviewSubscription(c *Context, w http.ResponseWriter, r *http.Request)
return 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) { 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 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) { 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 return
} }
w.Write(byteSanitizedProductsData) if _, err := w.Write(byteSanitizedProductsData); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
return 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) { 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 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) { 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 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) { 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 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) { 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 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) { 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 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) { func getSubscriptionInvoicePDF(c *Context, w http.ResponseWriter, r *http.Request) {

Просмотреть файл

@@ -16,7 +16,7 @@ import (
"github.com/mattermost/mattermost/server/v8/einterfaces/mocks" "github.com/mattermost/mattermost/server/v8/einterfaces/mocks"
) )
func Test_GetSubscription(t *testing.T) { func TestGetSubscription(t *testing.T) {
mainHelper.Parallel(t) mainHelper.Parallel(t)
deliquencySince := int64(2000000000) deliquencySince := int64(2000000000)
@@ -57,7 +57,8 @@ func Test_GetSubscription(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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")) th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
@@ -83,7 +84,8 @@ func Test_GetSubscription(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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")) 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) mainHelper.Parallel(t)
t.Run("Returns forbidden for invalid business email", func(t *testing.T) { t.Run("Returns forbidden for invalid business email", func(t *testing.T) {
mainHelper.Parallel(t) mainHelper.Parallel(t)
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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"} validBusinessEmail := model.ValidateBusinessEmailRequest{Email: "invalid@slacker.com"}
@@ -138,7 +141,8 @@ func Test_validateBusinessEmail(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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"} validBusinessEmail := model.ValidateBusinessEmailRequest{Email: "valid@mattermost.com"}
@@ -164,7 +168,8 @@ func Test_validateBusinessEmail(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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")) 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) mainHelper.Parallel(t)
t.Run("validate the Cloud Customer has used a valid email to create the workspace", func(t *testing.T) { t.Run("validate the Cloud Customer has used a valid email to create the workspace", func(t *testing.T) {
mainHelper.Parallel(t) mainHelper.Parallel(t)
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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")) th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
@@ -205,7 +211,7 @@ func Test_validateWorkspaceBusinessEmail(t *testing.T) {
}() }()
th.App.Srv().Cloud = &cloud th.App.Srv().Cloud = &cloud
_, err := th.SystemAdminClient.ValidateWorkspaceBusinessEmail(context.Background()) _, err = th.SystemAdminClient.ValidateWorkspaceBusinessEmail(context.Background())
require.NoError(t, err) require.NoError(t, err)
}) })
@@ -214,7 +220,8 @@ func Test_validateWorkspaceBusinessEmail(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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")) th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
@@ -242,7 +249,7 @@ func Test_validateWorkspaceBusinessEmail(t *testing.T) {
}() }()
th.App.Srv().Cloud = &cloud th.App.Srv().Cloud = &cloud
_, err := th.SystemAdminClient.ValidateWorkspaceBusinessEmail(context.Background()) _, err = th.SystemAdminClient.ValidateWorkspaceBusinessEmail(context.Background())
require.NoError(t, err) require.NoError(t, err)
}) })
@@ -251,7 +258,8 @@ func Test_validateWorkspaceBusinessEmail(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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")) th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
@@ -353,7 +361,8 @@ func TestGetCloudProducts(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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")) th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
@@ -376,7 +385,8 @@ func TestGetCloudProducts(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() 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")) th.App.Srv().SetLicense(model.NewTestLicense("cloud"))

Просмотреть файл

@@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
) )
func (api *API) InitCluster() { 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) c.Err = model.NewAppError("getClusterStatus", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return return
} }
w.Write(js) if _, err := w.Write(js); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
} }

Просмотреть файл

@@ -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) c.Err = model.NewAppError("listCommandAutocompleteSuggestions", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return 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) { 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 := make(map[string]string)
resp["token"] = rcmd.Token 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))
}
} }

Просмотреть файл

@@ -157,7 +157,8 @@ func TestUpdateCommand(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckNotFoundStatus(t, resp) 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) _, resp, err := th.SystemAdminClient.UpdateCommand(context.Background(), cmd2)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -216,7 +217,8 @@ func TestMoveCommand(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckNotFoundStatus(t, resp) 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) resp, err = th.SystemAdminClient.MoveCommand(context.Background(), newTeam.Id, rcmd2.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -275,7 +277,8 @@ func TestDeleteCommand(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckNotFoundStatus(t, resp) 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) resp, err = th.SystemAdminClient.DeleteCommand(context.Background(), rcmd2.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -300,10 +303,8 @@ func TestListCommands(t *testing.T) {
Method: model.CommandMethodPost, Method: model.CommandMethodPost,
Trigger: "custom_command", Trigger: "custom_command",
} }
_, _, rootErr := th.SystemAdminClient.CreateCommand(context.Background(), newCmd)
_, _, err := th.SystemAdminClient.CreateCommand(context.Background(), newCmd) require.NoError(t, rootErr)
require.NoError(t, err)
th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) {
listCommands, _, err := c.ListCommands(context.Background(), th.BasicTeam.Id, false) listCommands, _, err := c.ListCommands(context.Background(), th.BasicTeam.Id, false)
require.NoError(t, err) require.NoError(t, err)
@@ -355,10 +356,13 @@ func TestListCommands(t *testing.T) {
}) })
t.Run("NoMember", func(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() user := th.CreateUser()
th.SystemAdminClient.RemoveTeamMember(context.Background(), th.BasicTeam.Id, user.Id) _, _, err = client.Login(context.Background(), user.Email, user.Password)
client.Login(context.Background(), user.Email, user.Password) require.NoError(t, err)
_, resp, err := client.ListCommands(context.Background(), th.BasicTeam.Id, false) _, resp, err := client.ListCommands(context.Background(), th.BasicTeam.Id, false)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
@@ -368,7 +372,8 @@ func TestListCommands(t *testing.T) {
}) })
t.Run("NotLoggedIn", func(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) _, resp, err := client.ListCommands(context.Background(), th.BasicTeam.Id, false)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -432,17 +437,21 @@ func TestListAutocompleteCommands(t *testing.T) {
}) })
t.Run("NoMember", func(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() user := th.CreateUser()
th.SystemAdminClient.RemoveTeamMember(context.Background(), th.BasicTeam.Id, user.Id) _, _, err = client.Login(context.Background(), user.Email, user.Password)
client.Login(context.Background(), user.Email, user.Password) require.NoError(t, err)
_, resp, err := client.ListAutocompleteCommands(context.Background(), th.BasicTeam.Id) _, resp, err := client.ListAutocompleteCommands(context.Background(), th.BasicTeam.Id)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
}) })
t.Run("NotLoggedIn", func(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.ListAutocompleteCommands(context.Background(), th.BasicTeam.Id) _, resp, err := client.ListAutocompleteCommands(context.Background(), th.BasicTeam.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -526,17 +535,21 @@ func TestListCommandAutocompleteSuggestions(t *testing.T) {
}) })
t.Run("NoMember", func(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() user := th.CreateUser()
th.SystemAdminClient.RemoveTeamMember(context.Background(), th.BasicTeam.Id, user.Id) _, _, err = client.Login(context.Background(), user.Email, user.Password)
client.Login(context.Background(), user.Email, user.Password) require.NoError(t, err)
_, resp, err := client.ListCommandAutocompleteSuggestions(context.Background(), "/", th.BasicTeam.Id) _, resp, err := client.ListCommandAutocompleteSuggestions(context.Background(), "/", th.BasicTeam.Id)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
}) })
t.Run("NotLoggedIn", func(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.ListCommandAutocompleteSuggestions(context.Background(), "/", th.BasicTeam.Id) _, resp, err := client.ListCommandAutocompleteSuggestions(context.Background(), "/", th.BasicTeam.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -561,9 +574,8 @@ func TestGetCommand(t *testing.T) {
Method: model.CommandMethodPost, Method: model.CommandMethodPost,
Trigger: "roger", Trigger: "roger",
} }
newCmd, _, rootErr := th.SystemAdminClient.CreateCommand(context.Background(), newCmd)
newCmd, _, err := th.SystemAdminClient.CreateCommand(context.Background(), newCmd) require.NoError(t, rootErr)
require.NoError(t, err)
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
t.Run("ValidId", func(t *testing.T) { t.Run("ValidId", func(t *testing.T) {
cmd, _, err := client.GetCommandById(context.Background(), newCmd.Id) cmd, _, err := client.GetCommandById(context.Background(), newCmd.Id)
@@ -589,17 +601,21 @@ func TestGetCommand(t *testing.T) {
}) })
t.Run("NoMember", func(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() user := th.CreateUser()
th.SystemAdminClient.RemoveTeamMember(context.Background(), th.BasicTeam.Id, user.Id) _, _, err = th.Client.Login(context.Background(), user.Email, user.Password)
th.Client.Login(context.Background(), user.Email, user.Password) require.NoError(t, err)
_, resp, err := th.Client.GetCommandById(context.Background(), newCmd.Id) _, resp, err := th.Client.GetCommandById(context.Background(), newCmd.Id)
require.Error(t, err) require.Error(t, err)
CheckNotFoundStatus(t, resp) CheckNotFoundStatus(t, resp)
}) })
t.Run("NotLoggedIn", func(t *testing.T) { 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) _, resp, err := th.Client.GetCommandById(context.Background(), newCmd.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -695,13 +711,15 @@ func TestExecuteInvalidCommand(t *testing.T) {
CheckNotFoundStatus(t, resp) CheckNotFoundStatus(t, resp)
otherUser := th.CreateUser() 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") _, resp, err = client.ExecuteCommand(context.Background(), channel.Id, "/getcommand")
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) 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") _, resp, err = client.ExecuteCommand(context.Background(), channel.Id, "/getcommand")
require.Error(t, err) 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) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPost, r.Method) 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, token, r.FormValue("token"))
require.Equal(t, th.BasicTeam.Name, r.FormValue("team_domain")) 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) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPost, r.Method) 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")) require.Equal(t, team2.Name, r.FormValue("team_domain"))
w.Header().Set("Content-Type", "application/json") 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) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPost, r.Method) 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")) require.Equal(t, th.BasicTeam.Name, r.FormValue("team_domain"))
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
@@ -1145,7 +1166,8 @@ func TestExecuteCommandReadOnly(t *testing.T) {
// Enable Enterprise features // Enable Enterprise features
th.App.Srv().SetLicense(model.NewTestLicense()) th.App.Srv().SetLicense(model.NewTestLicense())
th.App.SetPhase2PermissionsMigrationStatus(true) err = th.App.SetPhase2PermissionsMigrationStatus(true)
require.NoError(t, err)
_, appErr = th.App.PatchChannelModerationsForChannel( _, appErr = th.App.PatchChannelModerationsForChannel(
th.Context, th.Context,

Просмотреть файл

@@ -158,5 +158,7 @@ func downloadComplianceReport(c *Context, w http.ResponseWriter, r *http.Request
auditRec.Success() auditRec.Success()
w.Write(reportBytes) if _, err := w.Write(reportBytes); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
} }

Просмотреть файл

@@ -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) c.Err = model.NewAppError("updateConfig", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return return
} }
w.Write(js) if _, err := w.Write(js); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
return 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.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) { 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) c.Err = model.NewAppError("patchConfig", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return return
} }
w.Write(js) if _, err := w.Write(js); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
return return
} }

Просмотреть файл

@@ -208,5 +208,8 @@ func localGetClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
auditRec.Success() 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))
}
} }

Просмотреть файл

@@ -81,7 +81,8 @@ func TestGetConfigWithAccessTag(t *testing.T) {
cfg.SupportSettings.SupportEmail = &mockSupportEmail 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 // add read sysconsole environment config
th.AddPermissionToRole(model.PermissionSysconsoleReadEnvironmentRateLimiting.Id, model.SystemUserRoleId) th.AddPermissionToRole(model.PermissionSysconsoleReadEnvironmentRateLimiting.Id, model.SystemUserRoleId)
@@ -107,7 +108,8 @@ func TestGetConfigAnyFlagsAccess(t *testing.T) {
th := Setup(t) th := Setup(t)
defer th.TearDown() 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()) _, resp, _ := th.Client.GetConfig(context.Background())
t.Run("Check permissions error with no sysconsole read permission", func(t *testing.T) { 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) { t.Run("Should not be able to modify ComplianceSettings.Directory in cloud", func(t *testing.T) {
th.App.Srv().SetLicense(model.NewTestLicense("cloud")) 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 := th.App.Config().Clone()
*cfg2.ComplianceSettings.Directory = "hellodir" *cfg2.ComplianceSettings.Directory = "hellodir"
@@ -297,7 +302,8 @@ func TestUpdateConfig(t *testing.T) {
func TestGetConfigWithoutManageSystemPermission(t *testing.T) { func TestGetConfigWithoutManageSystemPermission(t *testing.T) {
th := Setup(t) th := Setup(t)
defer th.TearDown() 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) { t.Run("any sysconsole read permission provides config read access", func(t *testing.T) {
// forbidden by default // forbidden by default
@@ -316,7 +322,8 @@ func TestGetConfigWithoutManageSystemPermission(t *testing.T) {
func TestUpdateConfigWithoutManageSystemPermission(t *testing.T) { func TestUpdateConfigWithoutManageSystemPermission(t *testing.T) {
th := Setup(t) th := Setup(t)
defer th.TearDown() 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 // add read sysconsole integrations config
th.AddPermissionToRole(model.PermissionSysconsoleReadIntegrationsIntegrationManagement.Id, model.SystemUserRoleId) 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) f, err := config.NewStoreFromDSN("from.json", false, nil, false)
require.NoError(t, err) 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) _, err = config.NewStoreFromDSN("to.json", false, nil, true)
require.NoError(t, err) 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") _, err = th.LocalClient.MigrateConfig(context.Background(), "from.json", "to.json")
require.NoError(t, err) require.NoError(t, err)

Просмотреть файл

@@ -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) c.Err = model.NewAppError("getGlobalPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return 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) { 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) c.Err = model.NewAppError("getPolicies", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return 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) { 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) c.Err = model.NewAppError("getPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return 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) { 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() auditRec.Success()
w.WriteHeader(http.StatusCreated) 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) { 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 return
} }
auditRec.Success() 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) { 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) c.Err = model.NewAppError("Api4.getTeamsForPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return 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) { 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) c.Err = model.NewAppError("searchTeamsInPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return 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) { 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) c.Err = model.NewAppError("Api4.getChannelsForPolicy", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return 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) { 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 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) { 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) c.Err = model.NewAppError("getTeamPoliciesForUser", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(jsonErr)
return 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) { 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) c.Err = model.NewAppError("getChannelPoliciesForUser", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(jsonErr)
return return
} }
w.Write(js) if _, err := w.Write(js); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
} }

Просмотреть файл

@@ -8,8 +8,6 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"os/exec"
"path"
"path/filepath" "path/filepath"
"testing" "testing"
"time" "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.EnableSentry = false // disable error reporting during tests
*newConfig.LogSettings.ConsoleJson = false *newConfig.LogSettings.ConsoleJson = false
*newConfig.LogSettings.ConsoleLevel = mlog.LvlStdLog.Name *newConfig.LogSettings.ConsoleLevel = mlog.LvlStdLog.Name
memoryStore.Set(newConfig) _, _, err := memoryStore.Set(newConfig)
require.NoError(tb, err)
options = append(options, app.ConfigStore(memoryStore)) options = append(options, app.ConfigStore(memoryStore))
if includeCacheLayer { if includeCacheLayer {
// Adds the cache layer to the test store // Adds the cache layer to the test store
@@ -371,12 +370,6 @@ func TestStatic(t *testing.T) {
func TestStaticFilesCaching(t *testing.T) { func TestStaticFilesCaching(t *testing.T) {
th := Setup(t).InitPlugins() 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" fakeMainBundleName := "main.1234ab.js"
fakeRootHTML := `<html> fakeRootHTML := `<html>
<head> <head>
@@ -386,7 +379,7 @@ func TestStaticFilesCaching(t *testing.T) {
fakeMainBundle := `module.exports = 'main';` fakeMainBundle := `module.exports = 'main';`
fakeRemoteEntry := `module.exports = 'remote';` 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) require.NoError(t, err)
err = os.WriteFile("./client/"+fakeMainBundleName, []byte(fakeMainBundle), 0600) err = os.WriteFile("./client/"+fakeMainBundleName, []byte(fakeMainBundle), 0600)
require.NoError(t, err) require.NoError(t, err)