github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/command_webhook_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestCommandWebhookPreSave(t *testing.T) { 14 h := CommandWebhook{} 15 h.PreSave() 16 17 require.Len(t, h.Id, 26, "Id should be generated") 18 require.NotEqual(t, 0, h.CreateAt, "CreateAt should be set") 19 } 20 21 func TestCommandWebhookIsValid(t *testing.T) { 22 h := CommandWebhook{} 23 h.Id = NewId() 24 h.CreateAt = GetMillis() 25 h.CommandId = NewId() 26 h.UserId = NewId() 27 h.ChannelId = NewId() 28 29 for _, test := range []struct { 30 Transform func() 31 ExpectedError string 32 }{ 33 {func() {}, ""}, 34 {func() { h.Id = "asd" }, "model.command_hook.id.app_error"}, 35 {func() { h.CreateAt = 0 }, "model.command_hook.create_at.app_error"}, 36 {func() { h.CommandId = "asd" }, "model.command_hook.command_id.app_error"}, 37 {func() { h.UserId = "asd" }, "model.command_hook.user_id.app_error"}, 38 {func() { h.ChannelId = "asd" }, "model.command_hook.channel_id.app_error"}, 39 {func() { h.RootId = "asd" }, "model.command_hook.root_id.app_error"}, 40 {func() { h.RootId = NewId() }, ""}, 41 {func() { h.ParentId = "asd" }, "model.command_hook.parent_id.app_error"}, 42 {func() { h.ParentId = NewId() }, ""}, 43 } { 44 tmp := h 45 test.Transform() 46 err := h.IsValid() 47 48 if test.ExpectedError == "" { 49 assert.Error(t, err, "hook should be valid") 50 } else { 51 require.NotNil(t, err) 52 assert.Equal(t, test.ExpectedError, err.Id, "expected "+test.ExpectedError+" error") 53 } 54 55 h = tmp 56 } 57 }