github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/store/storetest/command_webhook_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"errors"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/mattermost/mattermost-server/v5/model"
    14  	"github.com/mattermost/mattermost-server/v5/store"
    15  )
    16  
    17  func TestCommandWebhookStore(t *testing.T, ss store.Store) {
    18  	t.Run("", func(t *testing.T) { testCommandWebhookStore(t, ss) })
    19  }
    20  
    21  func testCommandWebhookStore(t *testing.T, ss store.Store) {
    22  	cws := ss.CommandWebhook()
    23  
    24  	h1 := &model.CommandWebhook{}
    25  	h1.CommandId = model.NewId()
    26  	h1.UserId = model.NewId()
    27  	h1.ChannelId = model.NewId()
    28  	h1, err := cws.Save(h1)
    29  	require.NoError(t, err)
    30  
    31  	var r1 *model.CommandWebhook
    32  	r1, nErr := cws.Get(h1.Id)
    33  	require.NoError(t, nErr)
    34  	assert.Equal(t, *r1, *h1, "invalid returned webhook")
    35  
    36  	_, nErr = cws.Get("123")
    37  	var nfErr *store.ErrNotFound
    38  	require.True(t, errors.As(nErr, &nfErr), "Should have set the status as not found for missing id")
    39  
    40  	h2 := &model.CommandWebhook{}
    41  	h2.CreateAt = model.GetMillis() - 2*model.COMMAND_WEBHOOK_LIFETIME
    42  	h2.CommandId = model.NewId()
    43  	h2.UserId = model.NewId()
    44  	h2.ChannelId = model.NewId()
    45  	h2, err = cws.Save(h2)
    46  	require.NoError(t, err)
    47  
    48  	_, nErr = cws.Get(h2.Id)
    49  	require.Error(t, nErr, "Should have set the status as not found for expired webhook")
    50  	require.True(t, errors.As(nErr, &nfErr), "Should have set the status as not found for expired webhook")
    51  
    52  	cws.Cleanup()
    53  
    54  	_, nErr = cws.Get(h1.Id)
    55  	require.NoError(t, nErr, "Should have no error getting unexpired webhook")
    56  
    57  	_, nErr = cws.Get(h2.Id)
    58  	require.True(t, errors.As(nErr, &nfErr), "Should have set the status as not found for expired webhook")
    59  
    60  	nErr = cws.TryUse(h1.Id, 1)
    61  	require.NoError(t, nErr, "Should be able to use webhook once")
    62  
    63  	nErr = cws.TryUse(h1.Id, 1)
    64  	require.Error(t, nErr, "Should be able to use webhook once")
    65  	var invErr *store.ErrInvalidInput
    66  	require.True(t, errors.As(nErr, &invErr), "Should be able to use webhook once")
    67  }