mirror of
https://github.com/mattermost/mattermost.git
synced 2026-02-03 20:40:00 -05:00
Fix: panic on nil token in DeleteToken
This commit is contained in:
parent
dbcfd0c90e
commit
4cde319cd4
2 changed files with 27 additions and 0 deletions
|
|
@ -1900,6 +1900,9 @@ func (a *App) ConsumeTokenOnce(tokenType, tokenStr string) (*model.Token, *model
|
|||
}
|
||||
|
||||
func (a *App) DeleteToken(token *model.Token) *model.AppError {
|
||||
if token == nil {
|
||||
return model.NewAppError("DeleteToken", "api.context.invalid_param.app_error", map[string]any{"Name": "token"}, "", http.StatusBadRequest)
|
||||
}
|
||||
err := a.Srv().Store().Token().Delete(token.Token)
|
||||
if err != nil {
|
||||
return model.NewAppError("DeleteToken", "app.recover.delete.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
|
|
|
|||
|
|
@ -2917,3 +2917,27 @@ func TestConsumeTokenOnce(t *testing.T) {
|
|||
assert.Equal(t, http.StatusNotFound, appErr.StatusCode)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteToken(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
th := Setup(t).InitBasic(t)
|
||||
defer th.TearDown()
|
||||
|
||||
t.Run("nil token returns error", func(t *testing.T) {
|
||||
appErr := th.App.DeleteToken(nil)
|
||||
require.NotNil(t, appErr, "Should return error for nil token")
|
||||
require.Equal(t, "api.context.invalid_param.app_error", appErr.Id)
|
||||
assert.Equal(t, http.StatusBadRequest, appErr.StatusCode)
|
||||
})
|
||||
|
||||
t.Run("valid token deletes successfully", func(t *testing.T) {
|
||||
token := model.NewToken(
|
||||
model.TokenTypeVerifyEmail,
|
||||
model.MapToJSON(map[string]string{"userId": th.BasicUser.Id}),
|
||||
)
|
||||
require.NoError(t, th.App.Srv().Store().Token().Save(token))
|
||||
|
||||
appErr := th.App.DeleteToken(token)
|
||||
require.Nil(t, appErr, "Should successfully delete valid token")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue