github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/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/mattermost/mattermost-server/v5/model"
    11  	"github.com/mattermost/mattermost-server/v5/store"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestCommandWebhookStore(t *testing.T, ss store.Store) {
    17  	t.Run("", func(t *testing.T) { testCommandWebhookStore(t, ss) })
    18  }
    19  
    20  func testCommandWebhookStore(t *testing.T, ss store.Store) {
    21  	cws := ss.CommandWebhook()
    22  
    23  	h1 := &model.CommandWebhook{}
    24  	h1.CommandId = model.NewId()
    25  	h1.UserId = model.NewId()
    26  	h1.ChannelId = model.NewId()
    27  	h1, err := cws.Save(h1)
    28  	require.Nil(t, err)
    29  
    30  	var r1 *model.CommandWebhook
    31  	r1, nErr := cws.Get(h1.Id)
    32  	require.Nil(t, nErr)
    33  	assert.Equal(t, *r1, *h1, "invalid returned webhook")
    34  
    35  	_, nErr = cws.Get("123")
    36  	var nfErr *store.ErrNotFound
    37  	require.True(t, errors.As(nErr, &nfErr), "Should have set the status as not found for missing id")
    38  
    39  	h2 := &model.CommandWebhook{}
    40  	h2.CreateAt = model.GetMillis() - 2*model.COMMAND_WEBHOOK_LIFETIME
    41  	h2.CommandId = model.NewId()
    42  	h2.UserId = model.NewId()
    43  	h2.ChannelId = model.NewId()
    44  	h2, err = cws.Save(h2)
    45  	require.Nil(t, err)
    46  
    47  	_, nErr = cws.Get(h2.Id)
    48  	require.NotNil(t, nErr, "Should have set the status as not found for expired webhook")
    49  	require.True(t, errors.As(nErr, &nfErr), "Should have set the status as not found for expired webhook")
    50  
    51  	cws.Cleanup()
    52  
    53  	_, nErr = cws.Get(h1.Id)
    54  	require.Nil(t, nErr, "Should have no error getting unexpired webhook")
    55  
    56  	_, nErr = cws.Get(h2.Id)
    57  	require.True(t, errors.As(nErr, &nfErr), "Should have set the status as not found for expired webhook")
    58  
    59  	nErr = cws.TryUse(h1.Id, 1)
    60  	require.Nil(t, nErr, "Should be able to use webhook once")
    61  
    62  	nErr = cws.TryUse(h1.Id, 1)
    63  	require.NotNil(t, nErr, "Should be able to use webhook once")
    64  	var invErr *store.ErrInvalidInput
    65  	require.True(t, errors.As(nErr, &invErr), "Should be able to use webhook once")
    66  }