github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/command_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/require"
    11  
    12  	"github.com/mattermost/mattermost-server/v5/model"
    13  	"github.com/mattermost/mattermost-server/v5/store"
    14  )
    15  
    16  func TestCommandStore(t *testing.T, ss store.Store) {
    17  	t.Run("Save", func(t *testing.T) { testCommandStoreSave(t, ss) })
    18  	t.Run("Get", func(t *testing.T) { testCommandStoreGet(t, ss) })
    19  	t.Run("GetByTeam", func(t *testing.T) { testCommandStoreGetByTeam(t, ss) })
    20  	t.Run("GetByTrigger", func(t *testing.T) { testCommandStoreGetByTrigger(t, ss) })
    21  	t.Run("Delete", func(t *testing.T) { testCommandStoreDelete(t, ss) })
    22  	t.Run("DeleteByTeam", func(t *testing.T) { testCommandStoreDeleteByTeam(t, ss) })
    23  	t.Run("DeleteByUser", func(t *testing.T) { testCommandStoreDeleteByUser(t, ss) })
    24  	t.Run("Update", func(t *testing.T) { testCommandStoreUpdate(t, ss) })
    25  	t.Run("CommandCount", func(t *testing.T) { testCommandCount(t, ss) })
    26  }
    27  
    28  func testCommandStoreSave(t *testing.T, ss store.Store) {
    29  	o1 := model.Command{}
    30  	o1.CreatorId = model.NewId()
    31  	o1.Method = model.COMMAND_METHOD_POST
    32  	o1.TeamId = model.NewId()
    33  	o1.URL = "http://nowhere.com/"
    34  	o1.Trigger = "trigger"
    35  
    36  	_, nErr := ss.Command().Save(&o1)
    37  	require.NoError(t, nErr)
    38  
    39  	_, err := ss.Command().Save(&o1)
    40  	require.Error(t, err, "shouldn't be able to update from save")
    41  }
    42  
    43  func testCommandStoreGet(t *testing.T, ss store.Store) {
    44  	o1 := &model.Command{}
    45  	o1.CreatorId = model.NewId()
    46  	o1.Method = model.COMMAND_METHOD_POST
    47  	o1.TeamId = model.NewId()
    48  	o1.URL = "http://nowhere.com/"
    49  	o1.Trigger = "trigger"
    50  
    51  	o1, nErr := ss.Command().Save(o1)
    52  	require.NoError(t, nErr)
    53  
    54  	r1, nErr := ss.Command().Get(o1.Id)
    55  	require.NoError(t, nErr)
    56  	require.Equal(t, r1.CreateAt, o1.CreateAt, "invalid returned command")
    57  
    58  	_, err := ss.Command().Get("123")
    59  	require.Error(t, err)
    60  	var nfErr *store.ErrNotFound
    61  	require.True(t, errors.As(err, &nfErr))
    62  }
    63  
    64  func testCommandStoreGetByTeam(t *testing.T, ss store.Store) {
    65  	o1 := &model.Command{}
    66  	o1.CreatorId = model.NewId()
    67  	o1.Method = model.COMMAND_METHOD_POST
    68  	o1.TeamId = model.NewId()
    69  	o1.URL = "http://nowhere.com/"
    70  	o1.Trigger = "trigger"
    71  
    72  	o1, nErr := ss.Command().Save(o1)
    73  	require.NoError(t, nErr)
    74  
    75  	r1, nErr := ss.Command().GetByTeam(o1.TeamId)
    76  	require.NoError(t, nErr)
    77  	require.NotEmpty(t, r1, "no command returned")
    78  	require.Equal(t, r1[0].CreateAt, o1.CreateAt, "invalid returned command")
    79  
    80  	result, nErr := ss.Command().GetByTeam("123")
    81  	require.NoError(t, nErr)
    82  	require.Empty(t, result, "no commands should have returned")
    83  }
    84  
    85  func testCommandStoreGetByTrigger(t *testing.T, ss store.Store) {
    86  	o1 := &model.Command{}
    87  	o1.CreatorId = model.NewId()
    88  	o1.Method = model.COMMAND_METHOD_POST
    89  	o1.TeamId = model.NewId()
    90  	o1.URL = "http://nowhere.com/"
    91  	o1.Trigger = "trigger1"
    92  
    93  	o2 := &model.Command{}
    94  	o2.CreatorId = model.NewId()
    95  	o2.Method = model.COMMAND_METHOD_POST
    96  	o2.TeamId = model.NewId()
    97  	o2.URL = "http://nowhere.com/"
    98  	o2.Trigger = "trigger1"
    99  
   100  	o1, nErr := ss.Command().Save(o1)
   101  	require.NoError(t, nErr)
   102  
   103  	_, nErr = ss.Command().Save(o2)
   104  	require.NoError(t, nErr)
   105  
   106  	var r1 *model.Command
   107  	r1, nErr = ss.Command().GetByTrigger(o1.TeamId, o1.Trigger)
   108  	require.NoError(t, nErr)
   109  	require.Equal(t, r1.Id, o1.Id, "invalid returned command")
   110  
   111  	nErr = ss.Command().Delete(o1.Id, model.GetMillis())
   112  	require.NoError(t, nErr)
   113  
   114  	_, err := ss.Command().GetByTrigger(o1.TeamId, o1.Trigger)
   115  	require.Error(t, err)
   116  	var nfErr *store.ErrNotFound
   117  	require.True(t, errors.As(err, &nfErr))
   118  }
   119  
   120  func testCommandStoreDelete(t *testing.T, ss store.Store) {
   121  	o1 := &model.Command{}
   122  	o1.CreatorId = model.NewId()
   123  	o1.Method = model.COMMAND_METHOD_POST
   124  	o1.TeamId = model.NewId()
   125  	o1.URL = "http://nowhere.com/"
   126  	o1.Trigger = "trigger"
   127  
   128  	o1, nErr := ss.Command().Save(o1)
   129  	require.NoError(t, nErr)
   130  
   131  	r1, nErr := ss.Command().Get(o1.Id)
   132  	require.NoError(t, nErr)
   133  	require.Equal(t, r1.CreateAt, o1.CreateAt, "invalid returned command")
   134  
   135  	nErr = ss.Command().Delete(o1.Id, model.GetMillis())
   136  	require.NoError(t, nErr)
   137  
   138  	_, err := ss.Command().Get(o1.Id)
   139  	require.Error(t, err)
   140  	var nfErr *store.ErrNotFound
   141  	require.True(t, errors.As(err, &nfErr))
   142  }
   143  
   144  func testCommandStoreDeleteByTeam(t *testing.T, ss store.Store) {
   145  	o1 := &model.Command{}
   146  	o1.CreatorId = model.NewId()
   147  	o1.Method = model.COMMAND_METHOD_POST
   148  	o1.TeamId = model.NewId()
   149  	o1.URL = "http://nowhere.com/"
   150  	o1.Trigger = "trigger"
   151  
   152  	o1, nErr := ss.Command().Save(o1)
   153  	require.NoError(t, nErr)
   154  
   155  	r1, nErr := ss.Command().Get(o1.Id)
   156  	require.NoError(t, nErr)
   157  	require.Equal(t, r1.CreateAt, o1.CreateAt, "invalid returned command")
   158  
   159  	nErr = ss.Command().PermanentDeleteByTeam(o1.TeamId)
   160  	require.NoError(t, nErr)
   161  
   162  	_, err := ss.Command().Get(o1.Id)
   163  	require.Error(t, err)
   164  	var nfErr *store.ErrNotFound
   165  	require.True(t, errors.As(err, &nfErr))
   166  }
   167  
   168  func testCommandStoreDeleteByUser(t *testing.T, ss store.Store) {
   169  	o1 := &model.Command{}
   170  	o1.CreatorId = model.NewId()
   171  	o1.Method = model.COMMAND_METHOD_POST
   172  	o1.TeamId = model.NewId()
   173  	o1.URL = "http://nowhere.com/"
   174  	o1.Trigger = "trigger"
   175  
   176  	o1, nErr := ss.Command().Save(o1)
   177  	require.NoError(t, nErr)
   178  
   179  	r1, nErr := ss.Command().Get(o1.Id)
   180  	require.NoError(t, nErr)
   181  	require.Equal(t, r1.CreateAt, o1.CreateAt, "invalid returned command")
   182  
   183  	nErr = ss.Command().PermanentDeleteByUser(o1.CreatorId)
   184  	require.NoError(t, nErr)
   185  
   186  	_, err := ss.Command().Get(o1.Id)
   187  	require.Error(t, err)
   188  	var nfErr *store.ErrNotFound
   189  	require.True(t, errors.As(err, &nfErr))
   190  }
   191  
   192  func testCommandStoreUpdate(t *testing.T, ss store.Store) {
   193  	o1 := &model.Command{}
   194  	o1.CreatorId = model.NewId()
   195  	o1.Method = model.COMMAND_METHOD_POST
   196  	o1.TeamId = model.NewId()
   197  	o1.URL = "http://nowhere.com/"
   198  	o1.Trigger = "trigger"
   199  
   200  	o1, nErr := ss.Command().Save(o1)
   201  	require.NoError(t, nErr)
   202  
   203  	o1.Token = model.NewId()
   204  
   205  	_, nErr = ss.Command().Update(o1)
   206  	require.NoError(t, nErr)
   207  
   208  	o1.URL = "junk"
   209  
   210  	_, err := ss.Command().Update(o1)
   211  	require.Error(t, err)
   212  }
   213  
   214  func testCommandCount(t *testing.T, ss store.Store) {
   215  	o1 := &model.Command{}
   216  	o1.CreatorId = model.NewId()
   217  	o1.Method = model.COMMAND_METHOD_POST
   218  	o1.TeamId = model.NewId()
   219  	o1.URL = "http://nowhere.com/"
   220  	o1.Trigger = "trigger"
   221  
   222  	o1, nErr := ss.Command().Save(o1)
   223  	require.NoError(t, nErr)
   224  
   225  	r1, nErr := ss.Command().AnalyticsCommandCount("")
   226  	require.NoError(t, nErr)
   227  	require.NotZero(t, r1, "should be at least 1 command")
   228  
   229  	r2, nErr := ss.Command().AnalyticsCommandCount(o1.TeamId)
   230  	require.NoError(t, nErr)
   231  	require.Equal(t, r2, int64(1), "should be 1 command")
   232  }