github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/store/storetest/command_store.go (about)

     1  // Copyright (c) 2016-present Xenia, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/xzl8028/xenia-server/model"
    10  	"github.com/xzl8028/xenia-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 != nil {
    34  		t.Fatal("couldn't save item", err)
    35  	}
    36  
    37  	if _, err := ss.Command().Save(&o1); 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, err := ss.Command().Save(o1)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	if r1, err := ss.Command().Get(o1.Id); err != nil {
    56  		t.Fatal(err)
    57  	} else {
    58  		if r1.CreateAt != o1.CreateAt {
    59  			t.Fatal("invalid returned command")
    60  		}
    61  	}
    62  
    63  	if _, err := ss.Command().Get("123"); err == nil {
    64  		t.Fatal("Missing id should have failed")
    65  	}
    66  }
    67  
    68  func testCommandStoreGetByTeam(t *testing.T, ss store.Store) {
    69  	o1 := &model.Command{}
    70  	o1.CreatorId = model.NewId()
    71  	o1.Method = model.COMMAND_METHOD_POST
    72  	o1.TeamId = model.NewId()
    73  	o1.URL = "http://nowhere.com/"
    74  	o1.Trigger = "trigger"
    75  
    76  	o1, err := ss.Command().Save(o1)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	if r1, err := ss.Command().GetByTeam(o1.TeamId); err != nil {
    82  		t.Fatal(err)
    83  	} else {
    84  		if r1[0].CreateAt != o1.CreateAt {
    85  			t.Fatal("invalid returned command")
    86  		}
    87  	}
    88  
    89  	if result, err := ss.Command().GetByTeam("123"); err != nil {
    90  		t.Fatal(err)
    91  	} else {
    92  		if len(result) != 0 {
    93  			t.Fatal("no commands should have returned")
    94  		}
    95  	}
    96  }
    97  
    98  func testCommandStoreGetByTrigger(t *testing.T, ss store.Store) {
    99  	o1 := &model.Command{}
   100  	o1.CreatorId = model.NewId()
   101  	o1.Method = model.COMMAND_METHOD_POST
   102  	o1.TeamId = model.NewId()
   103  	o1.URL = "http://nowhere.com/"
   104  	o1.Trigger = "trigger1"
   105  
   106  	o2 := &model.Command{}
   107  	o2.CreatorId = model.NewId()
   108  	o2.Method = model.COMMAND_METHOD_POST
   109  	o2.TeamId = model.NewId()
   110  	o2.URL = "http://nowhere.com/"
   111  	o2.Trigger = "trigger1"
   112  
   113  	o1, err := ss.Command().Save(o1)
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	_, err = ss.Command().Save(o2)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	var r1 *model.Command
   122  	if r1, err = ss.Command().GetByTrigger(o1.TeamId, o1.Trigger); err != nil {
   123  		t.Fatal(err)
   124  	} else {
   125  		if r1.Id != o1.Id {
   126  			t.Fatal("invalid returned command")
   127  		}
   128  	}
   129  
   130  	err = ss.Command().Delete(o1.Id, model.GetMillis())
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  
   135  	if _, err := ss.Command().GetByTrigger(o1.TeamId, o1.Trigger); err == nil {
   136  		t.Fatal("no commands should have returned")
   137  	}
   138  }
   139  
   140  func testCommandStoreDelete(t *testing.T, ss store.Store) {
   141  	o1 := &model.Command{}
   142  	o1.CreatorId = model.NewId()
   143  	o1.Method = model.COMMAND_METHOD_POST
   144  	o1.TeamId = model.NewId()
   145  	o1.URL = "http://nowhere.com/"
   146  	o1.Trigger = "trigger"
   147  
   148  	o1, err := ss.Command().Save(o1)
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  
   153  	if r1, err := ss.Command().Get(o1.Id); err != nil {
   154  		t.Fatal(err)
   155  	} else {
   156  		if r1.CreateAt != o1.CreateAt {
   157  			t.Fatal("invalid returned command")
   158  		}
   159  	}
   160  
   161  	if err := ss.Command().Delete(o1.Id, model.GetMillis()); err != nil {
   162  		t.Fatal(err)
   163  	}
   164  
   165  	if r3, err := ss.Command().Get(o1.Id); err == nil {
   166  		t.Log(r3)
   167  		t.Fatal("Missing id should have failed")
   168  	}
   169  }
   170  
   171  func testCommandStoreDeleteByTeam(t *testing.T, ss store.Store) {
   172  	o1 := &model.Command{}
   173  	o1.CreatorId = model.NewId()
   174  	o1.Method = model.COMMAND_METHOD_POST
   175  	o1.TeamId = model.NewId()
   176  	o1.URL = "http://nowhere.com/"
   177  	o1.Trigger = "trigger"
   178  
   179  	o1, err := ss.Command().Save(o1)
   180  	if err != nil {
   181  		t.Fatal(err)
   182  	}
   183  
   184  	if r1, err := ss.Command().Get(o1.Id); err != nil {
   185  		t.Fatal(err)
   186  	} else {
   187  		if r1.CreateAt != o1.CreateAt {
   188  			t.Fatal("invalid returned command")
   189  		}
   190  	}
   191  
   192  	if err := ss.Command().PermanentDeleteByTeam(o1.TeamId); err != nil {
   193  		t.Fatal(err)
   194  	}
   195  
   196  	if r3, err := ss.Command().Get(o1.Id); err == nil {
   197  		t.Log(r3)
   198  		t.Fatal("Missing id should have failed")
   199  	}
   200  }
   201  
   202  func testCommandStoreDeleteByUser(t *testing.T, ss store.Store) {
   203  	o1 := &model.Command{}
   204  	o1.CreatorId = model.NewId()
   205  	o1.Method = model.COMMAND_METHOD_POST
   206  	o1.TeamId = model.NewId()
   207  	o1.URL = "http://nowhere.com/"
   208  	o1.Trigger = "trigger"
   209  
   210  	o1, err := ss.Command().Save(o1)
   211  	if err != nil {
   212  		t.Fatal(err)
   213  	}
   214  
   215  	if r1, err := ss.Command().Get(o1.Id); err != nil {
   216  		t.Fatal(err)
   217  	} else {
   218  		if r1.CreateAt != o1.CreateAt {
   219  			t.Fatal("invalid returned command")
   220  		}
   221  	}
   222  
   223  	if err := ss.Command().PermanentDeleteByUser(o1.CreatorId); err != nil {
   224  		t.Fatal(err)
   225  	}
   226  
   227  	if r3, err := ss.Command().Get(o1.Id); err == nil {
   228  		t.Log(r3)
   229  		t.Fatal("Missing id should have failed")
   230  	}
   231  }
   232  
   233  func testCommandStoreUpdate(t *testing.T, ss store.Store) {
   234  	o1 := &model.Command{}
   235  	o1.CreatorId = model.NewId()
   236  	o1.Method = model.COMMAND_METHOD_POST
   237  	o1.TeamId = model.NewId()
   238  	o1.URL = "http://nowhere.com/"
   239  	o1.Trigger = "trigger"
   240  
   241  	o1, err := ss.Command().Save(o1)
   242  	if err != nil {
   243  		t.Fatal(err)
   244  	}
   245  
   246  	o1.Token = model.NewId()
   247  
   248  	if _, err := ss.Command().Update(o1); err != nil {
   249  		t.Fatal(err)
   250  	}
   251  
   252  	o1.URL = "junk"
   253  
   254  	if _, err := ss.Command().Update(o1); err == nil {
   255  		t.Fatal("should have failed - bad URL")
   256  	}
   257  }
   258  
   259  func testCommandCount(t *testing.T, ss store.Store) {
   260  	o1 := &model.Command{}
   261  	o1.CreatorId = model.NewId()
   262  	o1.Method = model.COMMAND_METHOD_POST
   263  	o1.TeamId = model.NewId()
   264  	o1.URL = "http://nowhere.com/"
   265  	o1.Trigger = "trigger"
   266  
   267  	o1, err := ss.Command().Save(o1)
   268  	if err != nil {
   269  		t.Fatal(err)
   270  	}
   271  
   272  	if r1, err := ss.Command().AnalyticsCommandCount(""); err != nil {
   273  		t.Fatal(err)
   274  	} else {
   275  		if r1 == 0 {
   276  			t.Fatal("should be at least 1 command")
   277  		}
   278  	}
   279  
   280  	if r2, err := ss.Command().AnalyticsCommandCount(o1.TeamId); err != nil {
   281  		t.Fatal(err)
   282  	} else {
   283  		if r2 != 1 {
   284  			t.Fatal("should be 1 command")
   285  		}
   286  	}
   287  }