2019-11-29 06:59:40 -05:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
2017-08-16 08:17:57 -04:00
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
2019-10-11 08:09:21 -04:00
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-08-16 08:17:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCommandWebhookPreSave(t *testing.T) {
|
|
|
|
|
h := CommandWebhook{}
|
|
|
|
|
h.PreSave()
|
2019-10-11 08:09:21 -04:00
|
|
|
|
|
|
|
|
require.Len(t, h.Id, 26, "Id should be generated")
|
|
|
|
|
require.NotEqual(t, 0, h.CreateAt, "CreateAt should be set")
|
2017-08-16 08:17:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCommandWebhookIsValid(t *testing.T) {
|
|
|
|
|
h := CommandWebhook{}
|
|
|
|
|
h.Id = NewId()
|
|
|
|
|
h.CreateAt = GetMillis()
|
|
|
|
|
h.CommandId = NewId()
|
|
|
|
|
h.UserId = NewId()
|
|
|
|
|
h.ChannelId = NewId()
|
|
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
|
Transform func()
|
|
|
|
|
ExpectedError string
|
|
|
|
|
}{
|
|
|
|
|
{func() {}, ""},
|
|
|
|
|
{func() { h.Id = "asd" }, "model.command_hook.id.app_error"},
|
2021-02-24 05:09:52 -05:00
|
|
|
{func() { h.Id = NewId() }, ""},
|
2017-08-16 08:17:57 -04:00
|
|
|
{func() { h.CreateAt = 0 }, "model.command_hook.create_at.app_error"},
|
2021-02-24 05:09:52 -05:00
|
|
|
{func() { h.CreateAt = GetMillis() }, ""},
|
2017-08-16 08:17:57 -04:00
|
|
|
{func() { h.CommandId = "asd" }, "model.command_hook.command_id.app_error"},
|
2021-02-24 05:09:52 -05:00
|
|
|
{func() { h.CommandId = NewId() }, ""},
|
2017-08-16 08:17:57 -04:00
|
|
|
{func() { h.UserId = "asd" }, "model.command_hook.user_id.app_error"},
|
2021-02-24 05:09:52 -05:00
|
|
|
{func() { h.UserId = NewId() }, ""},
|
2017-08-16 08:17:57 -04:00
|
|
|
{func() { h.ChannelId = "asd" }, "model.command_hook.channel_id.app_error"},
|
2021-02-24 05:09:52 -05:00
|
|
|
{func() { h.ChannelId = NewId() }, ""},
|
2017-08-16 08:17:57 -04:00
|
|
|
{func() { h.RootId = "asd" }, "model.command_hook.root_id.app_error"},
|
|
|
|
|
{func() { h.RootId = NewId() }, ""},
|
|
|
|
|
} {
|
|
|
|
|
tmp := h
|
|
|
|
|
test.Transform()
|
2022-08-18 08:53:11 -04:00
|
|
|
appErr := h.IsValid()
|
2019-10-11 08:09:21 -04:00
|
|
|
|
|
|
|
|
if test.ExpectedError == "" {
|
2022-08-18 08:53:11 -04:00
|
|
|
assert.Nil(t, appErr, "hook should be valid")
|
2019-10-11 08:09:21 -04:00
|
|
|
} else {
|
2022-08-18 08:53:11 -04:00
|
|
|
require.NotNil(t, appErr)
|
|
|
|
|
assert.Equal(t, test.ExpectedError, appErr.Id, "expected "+test.ExpectedError+" error")
|
2017-08-16 08:17:57 -04:00
|
|
|
}
|
2019-10-11 08:09:21 -04:00
|
|
|
|
2017-08-16 08:17:57 -04:00
|
|
|
h = tmp
|
|
|
|
|
}
|
|
|
|
|
}
|