github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/store/storetest/command_store.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  	"github.com/mattermost/mattermost-server/store"
    11  )
    12  
    13  func TestCommandStore(t *testing.T, ss store.Store) {
    14  	t.Run("Save", func(t *testing.T) { testCommandStoreSave(t, ss) })
    15  	t.Run("Get", func(t *testing.T) { testCommandStoreGet(t, ss) })
    16  	t.Run("GetByTeam", func(t *testing.T) { testCommandStoreGetByTeam(t, ss) })
    17  	t.Run("GetByTrigger", func(t *testing.T) { testCommandStoreGetByTrigger(t, ss) })
    18  	t.Run("Delete", func(t *testing.T) { testCommandStoreDelete(t, ss) })
    19  	t.Run("DeleteByTeam", func(t *testing.T) { testCommandStoreDeleteByTeam(t, ss) })
    20  	t.Run("DeleteByUser", func(t *testing.T) { testCommandStoreDeleteByUser(t, ss) })
    21  	t.Run("Update", func(t *testing.T) { testCommandStoreUpdate(t, ss) })
    22  	t.Run("CommandCount", func(t *testing.T) { testCommandCount(t, ss) })
    23  }
    24  
    25  func testCommandStoreSave(t *testing.T, ss store.Store) {
    26  	o1 := model.Command{}
    27  	o1.CreatorId = model.NewId()
    28  	o1.Method = model.COMMAND_METHOD_POST
    29  	o1.TeamId = model.NewId()
    30  	o1.URL = "http://nowhere.com/"
    31  	o1.Trigger = "trigger"
    32  
    33  	if err := (<-ss.Command().Save(&o1)).Err; err != nil {
    34  		t.Fatal("couldn't save item", err)
    35  	}
    36  
    37  	if err := (<-ss.Command().Save(&o1)).Err; err == nil {
    38  		t.Fatal("shouldn't be able to update from save")
    39  	}
    40  }
    41  
    42  func testCommandStoreGet(t *testing.T, ss store.Store) {
    43  	o1 := &model.Command{}
    44  	o1.CreatorId = model.NewId()
    45  	o1.Method = model.COMMAND_METHOD_POST
    46  	o1.TeamId = model.NewId()
    47  	o1.URL = "http://nowhere.com/"
    48  	o1.Trigger = "trigger"
    49  
    50  	o1 = (<-ss.Command().Save(o1)).Data.(*model.Command)
    51  
    52  	if r1 := <-ss.Command().Get(o1.Id); r1.Err != nil {
    53  		t.Fatal(r1.Err)
    54  	} else {
    55  		if r1.Data.(*model.Command).CreateAt != o1.CreateAt {
    56  			t.Fatal("invalid returned command")
    57  		}
    58  	}
    59  
    60  	if err := (<-ss.Command().Get("123")).Err; err == nil {
    61  		t.Fatal("Missing id should have failed")
    62  	}
    63  }
    64  
    65  func testCommandStoreGetByTeam(t *testing.T, ss store.Store) {
    66  	o1 := &model.Command{}
    67  	o1.CreatorId = model.NewId()
    68  	o1.Method = model.COMMAND_METHOD_POST
    69  	o1.TeamId = model.NewId()
    70  	o1.URL = "http://nowhere.com/"
    71  	o1.Trigger = "trigger"
    72  
    73  	o1 = (<-ss.Command().Save(o1)).Data.(*model.Command)
    74  
    75  	if r1 := <-ss.Command().GetByTeam(o1.TeamId); r1.Err != nil {
    76  		t.Fatal(r1.Err)
    77  	} else {
    78  		if r1.Data.([]*model.Command)[0].CreateAt != o1.CreateAt {
    79  			t.Fatal("invalid returned command")
    80  		}
    81  	}
    82  
    83  	if result := <-ss.Command().GetByTeam("123"); result.Err != nil {
    84  		t.Fatal(result.Err)
    85  	} else {
    86  		if len(result.Data.([]*model.Command)) != 0 {
    87  			t.Fatal("no commands should have returned")
    88  		}
    89  	}
    90  }
    91  
    92  func testCommandStoreGetByTrigger(t *testing.T, ss store.Store) {
    93  	o1 := &model.Command{}
    94  	o1.CreatorId = model.NewId()
    95  	o1.Method = model.COMMAND_METHOD_POST
    96  	o1.TeamId = model.NewId()
    97  	o1.URL = "http://nowhere.com/"
    98  	o1.Trigger = "trigger1"
    99  
   100  	o2 := &model.Command{}
   101  	o2.CreatorId = model.NewId()
   102  	o2.Method = model.COMMAND_METHOD_POST
   103  	o2.TeamId = model.NewId()
   104  	o2.URL = "http://nowhere.com/"
   105  	o2.Trigger = "trigger1"
   106  
   107  	o1 = (<-ss.Command().Save(o1)).Data.(*model.Command)
   108  	o2 = (<-ss.Command().Save(o2)).Data.(*model.Command)
   109  
   110  	if r1 := <-ss.Command().GetByTrigger(o1.TeamId, o1.Trigger); r1.Err != nil {
   111  		t.Fatal(r1.Err)
   112  	} else {
   113  		if r1.Data.(*model.Command).Id != o1.Id {
   114  			t.Fatal("invalid returned command")
   115  		}
   116  	}
   117  
   118  	store.Must(ss.Command().Delete(o1.Id, model.GetMillis()))
   119  
   120  	if result := <-ss.Command().GetByTrigger(o1.TeamId, o1.Trigger); result.Err == nil {
   121  		t.Fatal("no commands should have returned")
   122  	}
   123  }
   124  
   125  func testCommandStoreDelete(t *testing.T, ss store.Store) {
   126  	o1 := &model.Command{}
   127  	o1.CreatorId = model.NewId()
   128  	o1.Method = model.COMMAND_METHOD_POST
   129  	o1.TeamId = model.NewId()
   130  	o1.URL = "http://nowhere.com/"
   131  	o1.Trigger = "trigger"
   132  
   133  	o1 = (<-ss.Command().Save(o1)).Data.(*model.Command)
   134  
   135  	if r1 := <-ss.Command().Get(o1.Id); r1.Err != nil {
   136  		t.Fatal(r1.Err)
   137  	} else {
   138  		if r1.Data.(*model.Command).CreateAt != o1.CreateAt {
   139  			t.Fatal("invalid returned command")
   140  		}
   141  	}
   142  
   143  	if r2 := <-ss.Command().Delete(o1.Id, model.GetMillis()); r2.Err != nil {
   144  		t.Fatal(r2.Err)
   145  	}
   146  
   147  	if r3 := (<-ss.Command().Get(o1.Id)); r3.Err == nil {
   148  		t.Log(r3.Data)
   149  		t.Fatal("Missing id should have failed")
   150  	}
   151  }
   152  
   153  func testCommandStoreDeleteByTeam(t *testing.T, ss store.Store) {
   154  	o1 := &model.Command{}
   155  	o1.CreatorId = model.NewId()
   156  	o1.Method = model.COMMAND_METHOD_POST
   157  	o1.TeamId = model.NewId()
   158  	o1.URL = "http://nowhere.com/"
   159  	o1.Trigger = "trigger"
   160  
   161  	o1 = (<-ss.Command().Save(o1)).Data.(*model.Command)
   162  
   163  	if r1 := <-ss.Command().Get(o1.Id); r1.Err != nil {
   164  		t.Fatal(r1.Err)
   165  	} else {
   166  		if r1.Data.(*model.Command).CreateAt != o1.CreateAt {
   167  			t.Fatal("invalid returned command")
   168  		}
   169  	}
   170  
   171  	if r2 := <-ss.Command().PermanentDeleteByTeam(o1.TeamId); r2.Err != nil {
   172  		t.Fatal(r2.Err)
   173  	}
   174  
   175  	if r3 := (<-ss.Command().Get(o1.Id)); r3.Err == nil {
   176  		t.Log(r3.Data)
   177  		t.Fatal("Missing id should have failed")
   178  	}
   179  }
   180  
   181  func testCommandStoreDeleteByUser(t *testing.T, ss store.Store) {
   182  	o1 := &model.Command{}
   183  	o1.CreatorId = model.NewId()
   184  	o1.Method = model.COMMAND_METHOD_POST
   185  	o1.TeamId = model.NewId()
   186  	o1.URL = "http://nowhere.com/"
   187  	o1.Trigger = "trigger"
   188  
   189  	o1 = (<-ss.Command().Save(o1)).Data.(*model.Command)
   190  
   191  	if r1 := <-ss.Command().Get(o1.Id); r1.Err != nil {
   192  		t.Fatal(r1.Err)
   193  	} else {
   194  		if r1.Data.(*model.Command).CreateAt != o1.CreateAt {
   195  			t.Fatal("invalid returned command")
   196  		}
   197  	}
   198  
   199  	if r2 := <-ss.Command().PermanentDeleteByUser(o1.CreatorId); r2.Err != nil {
   200  		t.Fatal(r2.Err)
   201  	}
   202  
   203  	if r3 := (<-ss.Command().Get(o1.Id)); r3.Err == nil {
   204  		t.Log(r3.Data)
   205  		t.Fatal("Missing id should have failed")
   206  	}
   207  }
   208  
   209  func testCommandStoreUpdate(t *testing.T, ss store.Store) {
   210  	o1 := &model.Command{}
   211  	o1.CreatorId = model.NewId()
   212  	o1.Method = model.COMMAND_METHOD_POST
   213  	o1.TeamId = model.NewId()
   214  	o1.URL = "http://nowhere.com/"
   215  	o1.Trigger = "trigger"
   216  
   217  	o1 = (<-ss.Command().Save(o1)).Data.(*model.Command)
   218  
   219  	o1.Token = model.NewId()
   220  
   221  	if r2 := <-ss.Command().Update(o1); r2.Err != nil {
   222  		t.Fatal(r2.Err)
   223  	}
   224  
   225  	o1.URL = "junk"
   226  
   227  	if r2 := <-ss.Command().Update(o1); r2.Err == nil {
   228  		t.Fatal("should have failed - bad URL")
   229  	}
   230  }
   231  
   232  func testCommandCount(t *testing.T, ss store.Store) {
   233  	o1 := &model.Command{}
   234  	o1.CreatorId = model.NewId()
   235  	o1.Method = model.COMMAND_METHOD_POST
   236  	o1.TeamId = model.NewId()
   237  	o1.URL = "http://nowhere.com/"
   238  	o1.Trigger = "trigger"
   239  
   240  	o1 = (<-ss.Command().Save(o1)).Data.(*model.Command)
   241  
   242  	if r1 := <-ss.Command().AnalyticsCommandCount(""); r1.Err != nil {
   243  		t.Fatal(r1.Err)
   244  	} else {
   245  		if r1.Data.(int64) == 0 {
   246  			t.Fatal("should be at least 1 command")
   247  		}
   248  	}
   249  
   250  	if r2 := <-ss.Command().AnalyticsCommandCount(o1.TeamId); r2.Err != nil {
   251  		t.Fatal(r2.Err)
   252  	} else {
   253  		if r2.Data.(int64) != 1 {
   254  			t.Fatal("should be 1 command")
   255  		}
   256  	}
   257  }