From 34fb817a4a4cd2761ba35a2c94f9d0eb79d06244 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Mon, 6 Jul 2015 00:50:42 -0800 Subject: Fixing unit tests --- store/sql_audit_store_test.go | 8 ++--- store/sql_channel_store_test.go | 66 ++++++++++++++++++++--------------------- store/sql_post_store_test.go | 6 ++-- store/sql_session_store_test.go | 16 +++++----- store/sql_store.go | 10 +++---- store/sql_team_store_test.go | 6 ++-- store/sql_user_store_test.go | 22 +++++++------- store/store.go | 9 ++++++ 8 files changed, 76 insertions(+), 67 deletions(-) (limited to 'store') diff --git a/store/sql_audit_store_test.go b/store/sql_audit_store_test.go index 9074f57c7..4d804d150 100644 --- a/store/sql_audit_store_test.go +++ b/store/sql_audit_store_test.go @@ -13,15 +13,15 @@ func TestSqlAuditStore(t *testing.T) { Setup() audit := &model.Audit{UserId: model.NewId(), IpAddress: "ipaddress", Action: "Action"} - <-store.Audit().Save(audit) + Must(store.Audit().Save(audit)) time.Sleep(100 * time.Millisecond) - <-store.Audit().Save(audit) + Must(store.Audit().Save(audit)) time.Sleep(100 * time.Millisecond) - <-store.Audit().Save(audit) + Must(store.Audit().Save(audit)) time.Sleep(100 * time.Millisecond) audit.ExtraInfo = "extra" time.Sleep(100 * time.Millisecond) - <-store.Audit().Save(audit) + Must(store.Audit().Save(audit)) time.Sleep(100 * time.Millisecond) diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index e9d0d1652..9821e9ad0 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -86,7 +86,7 @@ func TestChannelStoreGet(t *testing.T) { o1.DisplayName = "Name" o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o1) + Must(store.Channel().Save(&o1)) if r1 := <-store.Channel().Get(o1.Id); r1.Err != nil { t.Fatal(r1.Err) @@ -109,40 +109,40 @@ func TestChannelStoreDelete(t *testing.T) { o1.DisplayName = "Channel1" o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o1) + Must(store.Channel().Save(&o1)) o2 := model.Channel{} o2.TeamId = o1.TeamId o2.DisplayName = "Channel2" o2.Name = "a" + model.NewId() + "b" o2.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o2) + Must(store.Channel().Save(&o2)) o3 := model.Channel{} o3.TeamId = o1.TeamId o3.DisplayName = "Channel3" o3.Name = "a" + model.NewId() + "b" o3.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o3) + Must(store.Channel().Save(&o3)) o4 := model.Channel{} o4.TeamId = o1.TeamId o4.DisplayName = "Channel4" o4.Name = "a" + model.NewId() + "b" o4.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o4) + Must(store.Channel().Save(&o4)) m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m1) + Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o2.Id m2.UserId = m1.UserId m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m2) + Must(store.Channel().SaveMember(&m2)) if r := <-store.Channel().Delete(o1.Id, model.GetMillis()); r.Err != nil { t.Fatal(r.Err) @@ -179,7 +179,7 @@ func TestChannelStoreGetByName(t *testing.T) { o1.DisplayName = "Name" o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o1) + Must(store.Channel().Save(&o1)) if r1 := <-store.Channel().GetByName(o1.TeamId, o1.Name); r1.Err != nil { t.Fatal(r1.Err) @@ -201,32 +201,32 @@ func TestChannelMemberStore(t *testing.T) { u1.TeamId = model.NewId() u1.Email = model.NewId() u1.FullName = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) u2 := model.User{} u2.TeamId = model.NewId() u2.Email = model.NewId() u2.FullName = model.NewId() - <-store.User().Save(&u2) + Must(store.User().Save(&u2)) o1 := model.ChannelMember{} o1.ChannelId = model.NewId() o1.UserId = u1.Id o1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&o1) + Must(store.Channel().SaveMember(&o1)) o2 := model.ChannelMember{} o2.ChannelId = o1.ChannelId o2.UserId = u2.Id o2.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&o2) + Must(store.Channel().SaveMember(&o2)) members := (<-store.Channel().GetMembers(o1.ChannelId)).Data.([]model.ChannelMember) if len(members) != 2 { t.Fatal("should have saved 2 members") } - <-store.Channel().RemoveMember(o2.ChannelId, o2.UserId) + Must(store.Channel().RemoveMember(o2.ChannelId, o2.UserId)) members = (<-store.Channel().GetMembers(o1.ChannelId)).Data.([]model.ChannelMember) if len(members) != 1 { @@ -256,13 +256,13 @@ func TestChannelStorePermissionsTo(t *testing.T) { o1.DisplayName = "Channel1" o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o1) + Must(store.Channel().Save(&o1)) m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m1) + Must(store.Channel().SaveMember(&m1)) count := (<-store.Channel().CheckPermissionsTo(o1.TeamId, o1.Id, m1.UserId)).Data.(int64) if count != 1 { @@ -303,7 +303,7 @@ func TestChannelStoreOpenChannelPermissionsTo(t *testing.T) { o1.DisplayName = "Channel1" o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o1) + Must(store.Channel().Save(&o1)) count := (<-store.Channel().CheckOpenChannelPermissions(o1.TeamId, o1.Id)).Data.(int64) if count != 1 { @@ -329,32 +329,32 @@ func TestChannelStoreGetChannels(t *testing.T) { o2.DisplayName = "Channel2" o2.Name = "a" + model.NewId() + "b" o2.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o2) + Must(store.Channel().Save(&o2)) o1 := model.Channel{} o1.TeamId = model.NewId() o1.DisplayName = "Channel1" o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o1) + Must(store.Channel().Save(&o1)) m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m1) + Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o1.Id m2.UserId = model.NewId() m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m2) + Must(store.Channel().SaveMember(&m2)) m3 := model.ChannelMember{} m3.ChannelId = o2.Id m3.UserId = model.NewId() m3.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m3) + Must(store.Channel().SaveMember(&m3)) cresult := <-store.Channel().GetChannels(o1.TeamId, m1.UserId) list := cresult.Data.(*model.ChannelList) @@ -372,53 +372,53 @@ func TestChannelStoreGetMoreChannels(t *testing.T) { o1.DisplayName = "Channel1" o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o1) + Must(store.Channel().Save(&o1)) o2 := model.Channel{} o2.TeamId = model.NewId() o2.DisplayName = "Channel2" o2.Name = "a" + model.NewId() + "b" o2.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o2) + Must(store.Channel().Save(&o2)) m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m1) + Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o1.Id m2.UserId = model.NewId() m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m2) + Must(store.Channel().SaveMember(&m2)) m3 := model.ChannelMember{} m3.ChannelId = o2.Id m3.UserId = model.NewId() m3.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m3) + Must(store.Channel().SaveMember(&m3)) o3 := model.Channel{} o3.TeamId = o1.TeamId o3.DisplayName = "ChannelA" o3.Name = "a" + model.NewId() + "b" o3.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o3) + Must(store.Channel().Save(&o3)) o4 := model.Channel{} o4.TeamId = o1.TeamId o4.DisplayName = "ChannelB" o4.Name = "a" + model.NewId() + "b" o4.Type = model.CHANNEL_PRIVATE - <-store.Channel().Save(&o4) + Must(store.Channel().Save(&o4)) o5 := model.Channel{} o5.TeamId = o1.TeamId o5.DisplayName = "ChannelC" o5.Name = "a" + model.NewId() + "b" o5.Type = model.CHANNEL_PRIVATE - <-store.Channel().Save(&o5) + Must(store.Channel().Save(&o5)) cresult := <-store.Channel().GetMoreChannels(o1.TeamId, m1.UserId) list := cresult.Data.(*model.ChannelList) @@ -441,13 +441,13 @@ func TestChannelStoreUpdateLastViewedAt(t *testing.T) { o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN o1.TotalMsgCount = 25 - <-store.Channel().Save(&o1) + Must(store.Channel().Save(&o1)) m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m1) + Must(store.Channel().SaveMember(&m1)) err := (<-store.Channel().UpdateLastViewedAt(m1.ChannelId, m1.UserId)).Err if err != nil { @@ -469,13 +469,13 @@ func TestChannelStoreIncrementMentionCount(t *testing.T) { o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN o1.TotalMsgCount = 25 - <-store.Channel().Save(&o1) + Must(store.Channel().Save(&o1)) m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m1) + Must(store.Channel().SaveMember(&m1)) err := (<-store.Channel().IncrementMentionCount(m1.ChannelId, m1.UserId)).Err if err != nil { diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index 13dd6e5ed..d9805eb02 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -280,7 +280,7 @@ func TestPostStoreGetWithChildren(t *testing.T) { } } - <-store.Post().Delete(o3.Id, model.GetMillis()) + Must(store.Post().Delete(o3.Id, model.GetMillis())) if r2 := <-store.Post().Get(o1.Id); r2.Err != nil { t.Fatal(r2.Err) @@ -291,7 +291,7 @@ func TestPostStoreGetWithChildren(t *testing.T) { } } - <-store.Post().Delete(o2.Id, model.GetMillis()) + Must(store.Post().Delete(o2.Id, model.GetMillis())) if r3 := <-store.Post().Get(o1.Id); r3.Err != nil { t.Fatal(r3.Err) @@ -399,7 +399,7 @@ func TestPostStoreSearch(t *testing.T) { m1.ChannelId = c1.Id m1.UserId = userId m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - <-store.Channel().SaveMember(&m1) + Must(store.Channel().SaveMember(&m1)) c2 := &model.Channel{} c2.TeamId = teamId diff --git a/store/sql_session_store_test.go b/store/sql_session_store_test.go index edb1d4c14..581aff971 100644 --- a/store/sql_session_store_test.go +++ b/store/sql_session_store_test.go @@ -26,18 +26,18 @@ func TestSessionGet(t *testing.T) { s1 := model.Session{} s1.UserId = model.NewId() s1.TeamId = model.NewId() - <-store.Session().Save(&s1) + Must(store.Session().Save(&s1)) s2 := model.Session{} s2.UserId = s1.UserId s2.TeamId = s1.TeamId - <-store.Session().Save(&s2) + Must(store.Session().Save(&s2)) s3 := model.Session{} s3.UserId = s1.UserId s3.TeamId = s1.TeamId s3.ExpiresAt = 1 - <-store.Session().Save(&s3) + Must(store.Session().Save(&s3)) if rs1 := (<-store.Session().Get(s1.Id)); rs1.Err != nil { t.Fatal(rs1.Err) @@ -63,7 +63,7 @@ func TestSessionRemove(t *testing.T) { s1 := model.Session{} s1.UserId = model.NewId() s1.TeamId = model.NewId() - <-store.Session().Save(&s1) + Must(store.Session().Save(&s1)) if rs1 := (<-store.Session().Get(s1.Id)); rs1.Err != nil { t.Fatal(rs1.Err) @@ -73,7 +73,7 @@ func TestSessionRemove(t *testing.T) { } } - <-store.Session().Remove(s1.Id) + Must(store.Session().Remove(s1.Id)) if rs2 := (<-store.Session().Get(s1.Id)); rs2.Err == nil { t.Fatal("should have been removed") @@ -86,7 +86,7 @@ func TestSessionRemoveAlt(t *testing.T) { s1 := model.Session{} s1.UserId = model.NewId() s1.TeamId = model.NewId() - <-store.Session().Save(&s1) + Must(store.Session().Save(&s1)) if rs1 := (<-store.Session().Get(s1.Id)); rs1.Err != nil { t.Fatal(rs1.Err) @@ -96,7 +96,7 @@ func TestSessionRemoveAlt(t *testing.T) { } } - <-store.Session().Remove(s1.AltId) + Must(store.Session().Remove(s1.AltId)) if rs2 := (<-store.Session().Get(s1.Id)); rs2.Err == nil { t.Fatal("should have been removed") @@ -117,7 +117,7 @@ func TestSessionStoreUpdateLastActivityAt(t *testing.T) { s1 := model.Session{} s1.UserId = model.NewId() s1.TeamId = model.NewId() - <-store.Session().Save(&s1) + Must(store.Session().Save(&s1)) if err := (<-store.Session().UpdateLastActivityAt(s1.Id, 1234567890)).Err; err != nil { t.Fatal(err) diff --git a/store/sql_store.go b/store/sql_store.go index bef8b4867..a0a1a9f23 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -8,9 +8,9 @@ import ( "crypto/aes" "crypto/cipher" "crypto/hmac" + crand "crypto/rand" "crypto/sha256" "crypto/sha512" - crand "crypto/rand" dbsql "database/sql" "encoding/base64" "encoding/json" @@ -362,21 +362,21 @@ func decrypt(key []byte, cryptoText string) (string, error) { ciphertext, err := base64.URLEncoding.DecodeString(cryptoText) if err != nil { - return "", err + return "", err } skey := sha512.Sum512(key) ekey, akey := skey[:32], skey[32:] macfn := hmac.New(sha256.New, akey) if len(ciphertext) < aes.BlockSize+macfn.Size() { - return "", errors.New("short ciphertext") + return "", errors.New("short ciphertext") } macfn.Write(ciphertext[aes.BlockSize+macfn.Size():]) expectedMac := macfn.Sum(nil) - mac := ciphertext[aes.BlockSize:aes.BlockSize+macfn.Size()] + mac := ciphertext[aes.BlockSize : aes.BlockSize+macfn.Size()] if hmac.Equal(expectedMac, mac) != true { - return "", errors.New("Incorrect MAC for the given ciphertext") + return "", errors.New("Incorrect MAC for the given ciphertext") } block, err := aes.NewCipher(ekey) diff --git a/store/sql_team_store_test.go b/store/sql_team_store_test.go index 4722017e8..bd1a7de2e 100644 --- a/store/sql_team_store_test.go +++ b/store/sql_team_store_test.go @@ -91,7 +91,7 @@ func TestTeamStoreGet(t *testing.T) { o1.Domain = "a" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN - <-store.Team().Save(&o1) + Must(store.Team().Save(&o1)) if r1 := <-store.Team().Get(o1.Id); r1.Err != nil { t.Fatal(r1.Err) @@ -140,12 +140,12 @@ func TestTeamStoreGetForEmail(t *testing.T) { o1.Domain = "a" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN - <-store.Team().Save(&o1) + Must(store.Team().Save(&o1)) u1 := model.User{} u1.TeamId = o1.Id u1.Email = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) if r1 := <-store.Team().GetTeamsForEmail(u1.Email); r1.Err != nil { t.Fatal(r1.Err) diff --git a/store/sql_user_store_test.go b/store/sql_user_store_test.go index 4231920a5..f8dae0021 100644 --- a/store/sql_user_store_test.go +++ b/store/sql_user_store_test.go @@ -64,7 +64,7 @@ func TestUserStoreUpdate(t *testing.T) { u1 := model.User{} u1.TeamId = model.NewId() u1.Email = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) if err := (<-store.User().Update(&u1, false)).Err; err != nil { t.Fatal(err) @@ -87,7 +87,7 @@ func TestUserStoreUpdateLastPingAt(t *testing.T) { u1 := model.User{} u1.TeamId = model.NewId() u1.Email = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) if err := (<-store.User().UpdateLastPingAt(u1.Id, 1234567890)).Err; err != nil { t.Fatal(err) @@ -109,7 +109,7 @@ func TestUserStoreUpdateLastActivityAt(t *testing.T) { u1 := model.User{} u1.TeamId = model.NewId() u1.Email = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) if err := (<-store.User().UpdateLastActivityAt(u1.Id, 1234567890)).Err; err != nil { t.Fatal(err) @@ -131,12 +131,12 @@ func TestUserStoreUpdateUserAndSessionActivity(t *testing.T) { u1 := model.User{} u1.TeamId = model.NewId() u1.Email = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) s1 := model.Session{} s1.UserId = u1.Id s1.TeamId = u1.TeamId - <-store.Session().Save(&s1) + Must(store.Session().Save(&s1)) if err := (<-store.User().UpdateUserAndSessionActivity(u1.Id, s1.Id, 1234567890)).Err; err != nil { t.Fatal(err) @@ -166,7 +166,7 @@ func TestUserStoreGet(t *testing.T) { u1 := model.User{} u1.TeamId = model.NewId() u1.Email = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) if r1 := <-store.User().Get(u1.Id); r1.Err != nil { t.Fatal(r1.Err) @@ -187,12 +187,12 @@ func TestUserStoreGetProfiles(t *testing.T) { u1 := model.User{} u1.TeamId = model.NewId() u1.Email = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) u2 := model.User{} u2.TeamId = u1.TeamId u2.Email = model.NewId() - <-store.User().Save(&u2) + Must(store.User().Save(&u2)) if r1 := <-store.User().GetProfiles(u1.TeamId); r1.Err != nil { t.Fatal(r1.Err) @@ -222,7 +222,7 @@ func TestUserStoreGetByEmail(t *testing.T) { u1 := model.User{} u1.TeamId = model.NewId() u1.Email = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) if err := (<-store.User().GetByEmail(u1.TeamId, u1.Email)).Err; err != nil { t.Fatal(err) @@ -240,7 +240,7 @@ func TestUserStoreGetByUsername(t *testing.T) { u1.TeamId = model.NewId() u1.Email = model.NewId() u1.Username = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) if err := (<-store.User().GetByUsername(u1.TeamId, u1.Username)).Err; err != nil { t.Fatal(err) @@ -257,7 +257,7 @@ func TestUserStoreUpdatePassword(t *testing.T) { u1 := model.User{} u1.TeamId = model.NewId() u1.Email = model.NewId() - <-store.User().Save(&u1) + Must(store.User().Save(&u1)) hashedPassword := model.HashPassword("newpwd") diff --git a/store/store.go b/store/store.go index 8d4b49b6e..070ee0562 100644 --- a/store/store.go +++ b/store/store.go @@ -14,6 +14,15 @@ type StoreResult struct { type StoreChannel chan StoreResult +func Must(sc StoreChannel) interface{} { + r := <-sc + if r.Err != nil { + panic(r.Err) + } + + return r.Data +} + type Store interface { Team() TeamStore Channel() ChannelStore -- cgit v1.2.3-1-g7c22