diff options
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_audit_store_test.go | 15 | ||||
-rw-r--r-- | store/sql_channel_store.go | 16 | ||||
-rw-r--r-- | store/sql_channel_store_test.go | 74 | ||||
-rw-r--r-- | store/sql_post_store.go | 12 | ||||
-rw-r--r-- | store/sql_post_store_test.go | 6 | ||||
-rw-r--r-- | store/sql_session_store_test.go | 16 | ||||
-rw-r--r-- | store/sql_store.go | 10 | ||||
-rw-r--r-- | store/sql_team_store_test.go | 13 | ||||
-rw-r--r-- | store/sql_user_store.go | 2 | ||||
-rw-r--r-- | store/sql_user_store_test.go | 25 | ||||
-rw-r--r-- | store/store.go | 9 |
11 files changed, 123 insertions, 75 deletions
diff --git a/store/sql_audit_store_test.go b/store/sql_audit_store_test.go index 3e6f22730..4d804d150 100644 --- a/store/sql_audit_store_test.go +++ b/store/sql_audit_store_test.go @@ -6,17 +6,24 @@ package store import ( "github.com/mattermost/platform/model" "testing" + "time" ) func TestSqlAuditStore(t *testing.T) { Setup() audit := &model.Audit{UserId: model.NewId(), IpAddress: "ipaddress", Action: "Action"} - <-store.Audit().Save(audit) - <-store.Audit().Save(audit) - <-store.Audit().Save(audit) + Must(store.Audit().Save(audit)) + time.Sleep(100 * time.Millisecond) + Must(store.Audit().Save(audit)) + time.Sleep(100 * time.Millisecond) + Must(store.Audit().Save(audit)) + time.Sleep(100 * time.Millisecond) audit.ExtraInfo = "extra" - <-store.Audit().Save(audit) + time.Sleep(100 * time.Millisecond) + Must(store.Audit().Save(audit)) + + time.Sleep(100 * time.Millisecond) c := store.Audit().Get(audit.UserId, 100) result := <-c diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 463fce16f..d61fbcdd0 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -79,7 +79,13 @@ func (s SqlChannelStore) Save(channel *model.Channel) StoreChannel { if err := s.GetMaster().Insert(channel); err != nil { if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'Name'") { - result.Err = model.NewAppError("SqlChannelStore.Save", "A channel with that name already exists", "id="+channel.Id+", "+err.Error()) + dupChannel := model.Channel{} + s.GetReplica().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId=? AND Name=? AND DeleteAt > 0", channel.TeamId, channel.Name) + if (dupChannel.DeleteAt > 0) { + result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name was previously created", "id="+channel.Id+", "+err.Error()) + } else { + result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name already exists", "id="+channel.Id+", "+err.Error()) + } } else { result.Err = model.NewAppError("SqlChannelStore.Save", "We couldn't save the channel", "id="+channel.Id+", "+err.Error()) } @@ -111,7 +117,13 @@ func (s SqlChannelStore) Update(channel *model.Channel) StoreChannel { if count, err := s.GetMaster().Update(channel); err != nil { if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'Name'") { - result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name already exists", "id="+channel.Id+", "+err.Error()) + dupChannel := model.Channel{} + s.GetReplica().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId=? AND Name=? AND DeleteAt > 0", channel.TeamId, channel.Name) + if (dupChannel.DeleteAt > 0) { + result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name was previously created", "id="+channel.Id+", "+err.Error()) + } else { + result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name already exists", "id="+channel.Id+", "+err.Error()) + } } else { result.Err = model.NewAppError("SqlChannelStore.Update", "We encounted an error updating the channel", "id="+channel.Id+", "+err.Error()) } diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index ce4ff75f0..9821e9ad0 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -6,6 +6,7 @@ package store import ( "github.com/mattermost/platform/model" "testing" + "time" ) func TestChannelStoreSave(t *testing.T) { @@ -55,7 +56,12 @@ func TestChannelStoreUpdate(t *testing.T) { o1.DisplayName = "Name" o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN - <-store.Channel().Save(&o1) + + if err := (<-store.Channel().Save(&o1)).Err; err != nil { + t.Fatal(err) + } + + time.Sleep(100 * time.Millisecond) if err := (<-store.Channel().Update(&o1)).Err; err != nil { t.Fatal(err) @@ -80,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) @@ -103,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) @@ -173,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) @@ -195,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 { @@ -250,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 { @@ -297,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 { @@ -323,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) @@ -366,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) @@ -435,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 { @@ -463,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.go b/store/sql_post_store.go index 01900023f..7ada515d7 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -72,7 +72,13 @@ func (s SqlPostStore) Save(post *model.Post) StoreChannel { result.Err = model.NewAppError("SqlPostStore.Save", "We couldn't save the Post", "id="+post.Id+", "+err.Error()) } else { time := model.GetMillis() - s.GetMaster().Exec("UPDATE Channels SET LastPostAt = ?, TotalMsgCount = TotalMsgCount + 1 WHERE Id = ?", time, post.ChannelId) + + if post.Type != model.POST_JOIN_LEAVE { + s.GetMaster().Exec("UPDATE Channels SET LastPostAt = ?, TotalMsgCount = TotalMsgCount + 1 WHERE Id = ?", time, post.ChannelId) + } else { + // don't update TotalMsgCount for unimportant messages so that the channel isn't marked as unread + s.GetMaster().Exec("UPDATE Channels SET LastPostAt = ? WHERE Id = ?", time, post.ChannelId) + } if len(post.RootId) > 0 { s.GetMaster().Exec("UPDATE Posts SET UpdateAt = ? WHERE Id = ?", time, post.RootId) @@ -361,8 +367,8 @@ func (s SqlPostStore) Search(teamId string, userId string, terms string, isHasht searchType := "Message" if isHashtagSearch { searchType = "Hashtags" - for _,term := range strings.Split(terms, " ") { - termMap[term] = true; + for _, term := range strings.Split(terms, " ") { + termMap[term] = true } } 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 1b03b75fd..bd1a7de2e 100644 --- a/store/sql_team_store_test.go +++ b/store/sql_team_store_test.go @@ -6,6 +6,7 @@ package store import ( "github.com/mattermost/platform/model" "testing" + "time" ) func TestTeamStoreSave(t *testing.T) { @@ -39,7 +40,11 @@ func TestTeamStoreUpdate(t *testing.T) { o1.Domain = "a" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN - <-store.Team().Save(&o1) + if err := (<-store.Team().Save(&o1)).Err; err != nil { + t.Fatal(err) + } + + time.Sleep(100 * time.Millisecond) if err := (<-store.Team().Update(&o1)).Err; err != nil { t.Fatal(err) @@ -86,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) @@ -135,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.go b/store/sql_user_store.go index 776c4f8ac..fc3d125c4 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -137,7 +137,7 @@ func (us SqlUserStore) Update(user *model.User, allowActiveUpdate bool) StoreCha if count, err := us.GetMaster().Update(user); err != nil { result.Err = model.NewAppError("SqlUserStore.Update", "We encounted an error updating the account", "user_id="+user.Id+", "+err.Error()) } else if count != 1 { - result.Err = model.NewAppError("SqlUserStore.Update", "We couldn't update the account", "user_id="+user.Id) + result.Err = model.NewAppError("SqlUserStore.Update", "We couldn't update the account", fmt.Sprintf("user_id=%v, count=%v", user.Id, count)) } else { result.Data = [2]*model.User{user, oldUser} } diff --git a/store/sql_user_store_test.go b/store/sql_user_store_test.go index 4231920a5..12737caa8 100644 --- a/store/sql_user_store_test.go +++ b/store/sql_user_store_test.go @@ -7,6 +7,7 @@ import ( "github.com/mattermost/platform/model" "strings" "testing" + "time" ) func TestUserStoreSave(t *testing.T) { @@ -64,7 +65,9 @@ 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)) + + time.Sleep(100 * time.Millisecond) if err := (<-store.User().Update(&u1, false)).Err; err != nil { t.Fatal(err) @@ -87,7 +90,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 +112,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 +134,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 +169,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 +190,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 +225,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 +243,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 +260,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 |