[MM-60280] api4/license.go: add err check to JSON unmarshal (#28063)

Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2024-08-29 15:24:55 +02:00
коммит произвёл GitHub
родитель d5cc2eb2f6
Коммит 6235f6cb77
2 изменённых файлов: 18 добавлений и 1 удалений

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

@@ -212,7 +212,12 @@ func requestTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) {
c.Err = model.NewAppError("requestTrialLicense", "api.license.request-trial.bad-request", nil, "", http.StatusBadRequest)
return
}
json.Unmarshal(b, &trialRequest)
err = json.Unmarshal(b, &trialRequest)
if err != nil {
c.Err = model.NewAppError("requestTrialLicense", "api.license.request-trial.bad-request", nil, "", http.StatusBadRequest).Wrap(err)
return
}
var appErr *model.AppError
// If any of the newly supported trial request fields are set (ie, not a legacy request), process this as a new trial request (requiring the new fields) otherwise fall back on the old method.

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

@@ -386,6 +386,18 @@ func TestRequestTrialLicense(t *testing.T) {
CheckForbiddenStatus(t, resp)
})
t.Run("trial license invalid JSON", func(t *testing.T) {
// the JSON is invalid because it is missing a closing brace
licenseManagerMock := &mocks.LicenseInterface{}
licenseManagerMock.On("CanStartTrial").Return(true, nil).Once()
th.App.Srv().Platform().SetLicenseManager(licenseManagerMock)
resp, err := th.SystemAdminClient.DoAPIPost(context.Background(), "/trial-license", `{"users": 5`)
CheckErrorID(t, err, "api.license.request-trial.bad-request")
CheckBadRequestStatus(t, model.BuildResponse(resp))
})
t.Run("trial license user count less than current users", func(t *testing.T) {
nUsers := 1
license := model.NewTestLicense()