From 365514174ef00dcf426b2b5704c3d7adebe926e1 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 2 Feb 2017 11:46:42 -0500 Subject: Add tear down to APIv4 tests (#5250) * Add tear down to APIv4 tests * Defer tear downs --- api4/apitestlib.go | 28 ++++++++++++++++++++++++++++ api4/team_test.go | 1 + api4/user_test.go | 6 +++++- app/channel.go | 16 ++++++++++++++++ app/team.go | 7 ++++++- i18n/en.json | 8 ++++++++ store/sql_channel_store.go | 35 +++++++++++++++++++++++++++++++++++ store/sql_channel_store_test.go | 17 +++++++++++++++++ store/sql_post_store.go | 17 +++++++++++++++++ store/sql_post_store_test.go | 14 ++++++++++++++ store/sql_team_store.go | 21 +++++++++++++++++++++ store/sql_team_store_test.go | 22 ++++++++++++++++++++++ store/store.go | 4 ++++ 13 files changed, 194 insertions(+), 2 deletions(-) diff --git a/api4/apitestlib.go b/api4/apitestlib.go index d5706bf2b..6229c8a08 100644 --- a/api4/apitestlib.go +++ b/api4/apitestlib.go @@ -60,6 +60,34 @@ func Setup() *TestHelper { return th } +func TearDown() { + options := map[string]bool{} + options[store.USER_SEARCH_OPTION_NAMES_ONLY_NO_FULL_NAME] = true + if result := <-app.Srv.Store.User().Search("", "fakeuser", options); result.Err != nil { + l4g.Error("Error tearing down test users") + } else { + users := result.Data.([]*model.User) + + for _, u := range users { + if err := app.PermanentDeleteUser(u); err != nil { + l4g.Error(err.Error()) + } + } + } + + if result := <-app.Srv.Store.Team().SearchByName("faketeam"); result.Err != nil { + l4g.Error("Error tearing down test teams") + } else { + teams := result.Data.([]*model.Team) + + for _, t := range teams { + if err := app.PermanentDeleteTeam(t); err != nil { + l4g.Error(err.Error()) + } + } + } +} + func (me *TestHelper) InitBasic() *TestHelper { me.TeamAdminUser = me.CreateUser() me.LoginTeamAdmin() diff --git a/api4/team_test.go b/api4/team_test.go index ba7ad094e..90f237151 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -14,6 +14,7 @@ import ( func TestCreateTeam(t *testing.T) { th := Setup().InitBasic() + defer TearDown() Client := th.Client team := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TEAM_OPEN} diff --git a/api4/user_test.go b/api4/user_test.go index 713e0268b..501bb38e3 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -13,7 +13,8 @@ import ( ) func TestCreateUser(t *testing.T) { - th := Setup() + th := Setup().InitBasic() + defer TearDown() Client := th.Client user := model.User{Email: GenerateTestEmail(), Nickname: "Corey Hulen", Password: "hello1", Username: GenerateTestUsername(), Roles: model.ROLE_SYSTEM_ADMIN.Id + " " + model.ROLE_SYSTEM_USER.Id} @@ -28,6 +29,7 @@ func TestCreateUser(t *testing.T) { } if ruser.Roles != model.ROLE_SYSTEM_USER.Id { + t.Log(ruser.Roles) t.Fatal("did not clear roles") } @@ -67,6 +69,7 @@ func TestCreateUser(t *testing.T) { func TestGetUser(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() Client := th.Client user := th.CreateUser() @@ -130,6 +133,7 @@ func TestGetUser(t *testing.T) { func TestUpdateUser(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() Client := th.Client user := th.CreateUser() diff --git a/app/channel.go b/app/channel.go index 1f5d308bf..025eccfcd 100644 --- a/app/channel.go +++ b/app/channel.go @@ -866,3 +866,19 @@ func ViewChannel(view *model.ChannelView, teamId string, userId string, clearPus return nil } + +func PermanentDeleteChannel(channel *model.Channel) *model.AppError { + if result := <-Srv.Store.Post().PermanentDeleteByChannel(channel.Id); result.Err != nil { + return result.Err + } + + if result := <-Srv.Store.Channel().PermanentDeleteMembersByChannel(channel.Id); result.Err != nil { + return result.Err + } + + if result := <-Srv.Store.Channel().PermanentDelete(channel.Id); result.Err != nil { + return result.Err + } + + return nil +} diff --git a/app/team.go b/app/team.go index 6225f3ee0..c1560d45a 100644 --- a/app/team.go +++ b/app/team.go @@ -539,8 +539,13 @@ func PermanentDeleteTeam(team *model.Team) *model.AppError { return result.Err } - if result := <-Srv.Store.Channel().PermanentDeleteByTeam(team.Id); result.Err != nil { + if result := <-Srv.Store.Channel().GetTeamChannels(team.Id); result.Err != nil { return result.Err + } else { + channels := result.Data.(*model.ChannelList) + for _, c := range *channels { + PermanentDeleteChannel(c) + } } if result := <-Srv.Store.Team().RemoveAllMembersByTeam(team.Id); result.Err != nil { diff --git a/i18n/en.json b/i18n/en.json index c58e113d2..6a7bb05f9 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -4559,6 +4559,10 @@ "id": "store.sql_channel.permanent_delete_by_team.app_error", "translation": "We couldn't delete the channels" }, + { + "id": "store.sql_channel.permanent_delete.app_error", + "translation": "We couldn't delete the channel" + }, { "id": "store.sql_channel.permanent_delete_members_by_user.app_error", "translation": "We couldn't remove the channel member" @@ -4915,6 +4919,10 @@ "id": "store.sql_post.permanent_delete.app_error", "translation": "We couldn't delete the post" }, + { + "id": "store.sql_post.permanent_delete_by_channel.app_error", + "translation": "We couldn't delete the posts by channel" + }, { "id": "store.sql_post.permanent_delete_all_comments_by_user.app_error", "translation": "We couldn't delete the comments for user" diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index a8474be80..7c4e97bc0 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -410,6 +410,41 @@ func (s SqlChannelStore) PermanentDeleteByTeam(teamId string) StoreChannel { return storeChannel } +func (s SqlChannelStore) PermanentDelete(channelId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + if _, err := s.GetMaster().Exec("DELETE FROM Channels WHERE Id = :ChannelId", map[string]interface{}{"ChannelId": channelId}); err != nil { + result.Err = model.NewLocAppError("SqlChannelStore.PermanentDelete", "store.sql_channel.permanent_delete.app_error", nil, "channel_id="+channelId+", "+err.Error()) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (s SqlChannelStore) PermanentDeleteMembersByChannel(channelId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + _, err := s.GetMaster().Exec("DELETE FROM ChannelMembers WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}) + if err != nil { + result.Err = model.NewLocAppError("SqlChannelStore.RemoveAllMembersByChannel", "store.sql_channel.remove_member.app_error", nil, "channel_id="+channelId+", "+err.Error()) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + type channelWithMember struct { model.Channel model.ChannelMember diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index 90a5f4479..51ca11e8e 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -340,6 +340,14 @@ func TestChannelStoreDelete(t *testing.T) { if len(*list) != 1 { t.Fatal("invalid number of channels") } + + <-store.Channel().PermanentDelete(o2.Id) + + cresult = <-store.Channel().GetChannels(o1.TeamId, m1.UserId) + t.Log(cresult.Err) + if cresult.Err.Id != "store.sql_channel.get_channels.not_found.app_error" { + t.Fatal("no channels should be found") + } } func TestChannelStoreGetByName(t *testing.T) { @@ -560,6 +568,15 @@ func TestChannelDeleteMemberStore(t *testing.T) { if count != 1 { t.Fatal("should have removed 1 member") } + + if r1 := <-store.Channel().PermanentDeleteMembersByChannel(o1.ChannelId); r1.Err != nil { + t.Fatal(r1.Err) + } + + count = (<-store.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) + if count != 0 { + t.Fatal("should have removed all members") + } } func TestChannelStoreGetChannels(t *testing.T) { diff --git a/store/sql_post_store.go b/store/sql_post_store.go index a8fe52380..96cfc071e 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -409,6 +409,23 @@ func (s SqlPostStore) PermanentDeleteByUser(userId string) StoreChannel { return storeChannel } +func (s SqlPostStore) PermanentDeleteByChannel(channelId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + if _, err := s.GetMaster().Exec("DELETE FROM Posts WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}); err != nil { + result.Err = model.NewLocAppError("SqlPostStore.PermanentDeleteByChannel", "store.sql_post.permanent_delete_by_channel.app_error", nil, "channel_id="+channelId+", "+err.Error()) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlPostStore) GetPosts(channelId string, offset int, limit int, allowFromCache bool) StoreChannel { storeChannel := make(StoreChannel, 1) diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index 7416e0ab7..626894a2a 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -352,6 +352,12 @@ func TestPostStorePermDelete1Level(t *testing.T) { o2.RootId = o1.Id o2 = (<-store.Post().Save(o2)).Data.(*model.Post) + o3 := &model.Post{} + o3.ChannelId = model.NewId() + o3.UserId = model.NewId() + o3.Message = "a" + model.NewId() + "b" + o3 = (<-store.Post().Save(o3)).Data.(*model.Post) + if r2 := <-store.Post().PermanentDeleteByUser(o2.UserId); r2.Err != nil { t.Fatal(r2.Err) } @@ -363,6 +369,14 @@ func TestPostStorePermDelete1Level(t *testing.T) { if r4 := (<-store.Post().Get(o2.Id)); r4.Err == nil { t.Fatal("Deleted id should have failed") } + + if r2 := <-store.Post().PermanentDeleteByChannel(o3.ChannelId); r2.Err != nil { + t.Fatal(r2.Err) + } + + if r3 := (<-store.Post().Get(o3.Id)); r3.Err == nil { + t.Fatal("Deleted id should have failed") + } } func TestPostStorePermDelete1Level2(t *testing.T) { diff --git a/store/sql_team_store.go b/store/sql_team_store.go index 9620feb11..f1b023854 100644 --- a/store/sql_team_store.go +++ b/store/sql_team_store.go @@ -236,6 +236,27 @@ func (s SqlTeamStore) GetByName(name string) StoreChannel { return storeChannel } +func (s SqlTeamStore) SearchByName(name string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + var teams []*model.Team + + if _, err := s.GetReplica().Select(&teams, "SELECT * FROM Teams WHERE Name LIKE :Name", map[string]interface{}{"Name": name + "%"}); err != nil { + result.Err = model.NewLocAppError("SqlTeamStore.SearchByName", "store.sql_team.get_by_name.app_error", nil, "name="+name+", "+err.Error()) + } + + result.Data = teams + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlTeamStore) GetAll() StoreChannel { storeChannel := make(StoreChannel, 1) diff --git a/store/sql_team_store_test.go b/store/sql_team_store_test.go index 0ea499d7e..62efa4d1a 100644 --- a/store/sql_team_store_test.go +++ b/store/sql_team_store_test.go @@ -132,6 +132,28 @@ func TestTeamStoreGetByName(t *testing.T) { } } +func TestTeamStoreSearchByName(t *testing.T) { + Setup() + + o1 := model.Team{} + o1.DisplayName = "DisplayName" + o1.Name = "zzz" + model.NewId() + "b" + o1.Email = model.NewId() + "@nowhere.com" + o1.Type = model.TEAM_OPEN + + if err := (<-store.Team().Save(&o1)).Err; err != nil { + t.Fatal(err) + } + + if r1 := <-store.Team().SearchByName("zzz"); r1.Err != nil { + t.Fatal(r1.Err) + } else { + if r1.Data.([]*model.Team)[0].ToJson() != o1.ToJson() { + t.Fatal("invalid returned team") + } + } +} + func TestTeamStoreGetByIniviteId(t *testing.T) { Setup() diff --git a/store/store.go b/store/store.go index 980ff7b1f..c75ec4554 100644 --- a/store/store.go +++ b/store/store.go @@ -60,6 +60,7 @@ type TeamStore interface { UpdateDisplayName(name string, teamId string) StoreChannel Get(id string) StoreChannel GetByName(name string) StoreChannel + SearchByName(name string) StoreChannel GetAll() StoreChannel GetAllTeamListing() StoreChannel GetTeamsByUserId(userId string) StoreChannel @@ -92,6 +93,7 @@ type ChannelStore interface { Delete(channelId string, time int64) StoreChannel SetDeleteAt(channelId string, deleteAt int64, updateAt int64) StoreChannel PermanentDeleteByTeam(teamId string) StoreChannel + PermanentDelete(channelId string) StoreChannel GetByName(team_id string, name string, allowFromCache bool) StoreChannel GetByNameIncludeDeleted(team_id string, name string, allowFromCache bool) StoreChannel GetDeletedByName(team_id string, name string) StoreChannel @@ -114,6 +116,7 @@ type ChannelStore interface { GetMemberCount(channelId string, allowFromCache bool) StoreChannel RemoveMember(channelId string, userId string) StoreChannel PermanentDeleteMembersByUser(userId string) StoreChannel + PermanentDeleteMembersByChannel(channelId string) StoreChannel UpdateLastViewedAt(channelIds []string, userId string) StoreChannel SetLastViewedAt(channelId string, userId string, newLastViewedAt int64) StoreChannel IncrementMentionCount(channelId string, userId string) StoreChannel @@ -132,6 +135,7 @@ type PostStore interface { GetSingle(id string) StoreChannel Delete(postId string, time int64) StoreChannel PermanentDeleteByUser(userId string) StoreChannel + PermanentDeleteByChannel(channelId string) StoreChannel GetPosts(channelId string, offset int, limit int, allowFromCache bool) StoreChannel GetFlaggedPosts(userId string, offset int, limit int) StoreChannel GetPostsBefore(channelId string, postId string, numPosts int, offset int) StoreChannel -- cgit v1.2.3-1-g7c22