diff options
-rw-r--r-- | app/channel.go | 8 | ||||
-rw-r--r-- | app/channel_test.go | 66 | ||||
-rw-r--r-- | app/team.go | 6 | ||||
-rw-r--r-- | app/team_test.go | 41 | ||||
-rw-r--r-- | i18n/en.json | 8 | ||||
-rw-r--r-- | store/sql_command_store.go | 18 | ||||
-rw-r--r-- | store/sql_command_store_test.go | 30 | ||||
-rw-r--r-- | store/sql_webhook_store.go | 44 | ||||
-rw-r--r-- | store/sql_webhook_store_test.go | 55 | ||||
-rw-r--r-- | store/store.go | 3 |
10 files changed, 275 insertions, 4 deletions
diff --git a/app/channel.go b/app/channel.go index b077c0399..03df0e800 100644 --- a/app/channel.go +++ b/app/channel.go @@ -1184,6 +1184,14 @@ func PermanentDeleteChannel(channel *model.Channel) *model.AppError { return result.Err } + if result := <-Srv.Store.Webhook().PermanentDeleteIncomingByChannel(channel.Id); result.Err != nil { + return result.Err + } + + if result := <-Srv.Store.Webhook().PermanentDeleteOutgoingByChannel(channel.Id); result.Err != nil { + return result.Err + } + if result := <-Srv.Store.Channel().PermanentDelete(channel.Id); result.Err != nil { return result.Err } diff --git a/app/channel_test.go b/app/channel_test.go new file mode 100644 index 000000000..438eb959b --- /dev/null +++ b/app/channel_test.go @@ -0,0 +1,66 @@ +package app + +import ( + "testing" + + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" +) + +func TestPermanentDeleteChannel(t *testing.T) { + th := Setup().InitBasic() + + incomingWasEnabled := utils.Cfg.ServiceSettings.EnableIncomingWebhooks + outgoingWasEnabled := utils.Cfg.ServiceSettings.EnableOutgoingWebhooks + utils.Cfg.ServiceSettings.EnableIncomingWebhooks = true + utils.Cfg.ServiceSettings.EnableOutgoingWebhooks = true + defer func() { + utils.Cfg.ServiceSettings.EnableIncomingWebhooks = incomingWasEnabled + utils.Cfg.ServiceSettings.EnableOutgoingWebhooks = outgoingWasEnabled + }() + + channel, err := CreateChannel(&model.Channel{DisplayName: "deletion-test", Name: "deletion-test", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id}, false) + if err != nil { + t.Fatal(err.Error()) + } + defer func() { + PermanentDeleteChannel(channel) + }() + + incoming, err := CreateIncomingWebhookForChannel(th.BasicUser.Id, channel, &model.IncomingWebhook{ChannelId: channel.Id}) + if err != nil { + t.Fatal(err.Error()) + } + defer DeleteIncomingWebhook(incoming.Id) + + if incoming, err = GetIncomingWebhook(incoming.Id); incoming == nil || err != nil { + t.Fatal("unable to get new incoming webhook") + } + + outgoing, err := CreateOutgoingWebhook(&model.OutgoingWebhook{ + ChannelId: channel.Id, + TeamId: channel.TeamId, + CreatorId: th.BasicUser.Id, + CallbackURLs: []string{"http://foo"}, + }) + if err != nil { + t.Fatal(err.Error()) + } + defer DeleteOutgoingWebhook(outgoing.Id) + + if outgoing, err = GetOutgoingWebhook(outgoing.Id); outgoing == nil || err != nil { + t.Fatal("unable to get new outgoing webhook") + } + + if err := PermanentDeleteChannel(channel); err != nil { + t.Fatal(err.Error()) + } + + if incoming, err = GetIncomingWebhook(incoming.Id); incoming != nil || err == nil { + t.Error("incoming webhook wasn't deleted") + } + + if outgoing, err = GetOutgoingWebhook(outgoing.Id); outgoing != nil || err == nil { + t.Error("outgoing webhook wasn't deleted") + } +} diff --git a/app/team.go b/app/team.go index bff448f7c..e4a71d7d5 100644 --- a/app/team.go +++ b/app/team.go @@ -638,7 +638,7 @@ func InviteNewUsersToTeam(emailList []string, teamId, senderId string) *model.Ap var invalidEmailList []string for _, email := range emailList { - if ! isTeamEmailAddressAllowed(email) { + if !isTeamEmailAddressAllowed(email) { invalidEmailList = append(invalidEmailList, email) } } @@ -747,6 +747,10 @@ func PermanentDeleteTeam(team *model.Team) *model.AppError { return result.Err } + if result := <-Srv.Store.Command().PermanentDeleteByTeam(team.Id); result.Err != nil { + return result.Err + } + if result := <-Srv.Store.Team().PermanentDelete(team.Id); result.Err != nil { return result.Err } diff --git a/app/team_test.go b/app/team_test.go index f2356d562..a410d6652 100644 --- a/app/team_test.go +++ b/app/team_test.go @@ -112,3 +112,44 @@ func TestAddUserToTeamByTeamId(t *testing.T) { t.Fatal("Should add user to the team") } } + +func TestPermanentDeleteTeam(t *testing.T) { + th := Setup().InitBasic() + + team, err := CreateTeam(&model.Team{ + DisplayName: "deletion-test", + Name: "deletion-test", + Email: "foo@foo.com", + Type: model.TEAM_OPEN, + }) + if err != nil { + t.Fatal(err.Error()) + } + defer func() { + PermanentDeleteTeam(team) + }() + + command, err := CreateCommand(&model.Command{ + CreatorId: th.BasicUser.Id, + TeamId: team.Id, + Trigger: "foo", + URL: "http://foo", + Method: model.COMMAND_METHOD_POST, + }) + if err != nil { + t.Fatal(err.Error()) + } + defer DeleteCommand(command.Id) + + if command, err = GetCommand(command.Id); command == nil || err != nil { + t.Fatal("unable to get new command") + } + + if err := PermanentDeleteTeam(team); err != nil { + t.Fatal(err.Error()) + } + + if command, err = GetCommand(command.Id); command != nil || err == nil { + t.Fatal("command wasn't deleted") + } +} diff --git a/i18n/en.json b/i18n/en.json index 251b1d929..f1657efcb 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -6252,10 +6252,18 @@ "translation": "We couldn't delete the webhook" }, { + "id": "store.sql_webhooks.permanent_delete_incoming_by_channel.app_error", + "translation": "We couldn't delete the webhook" + }, + { "id": "store.sql_webhooks.permanent_delete_outgoing_by_user.app_error", "translation": "We couldn't delete the webhook" }, { + "id": "store.sql_webhooks.permanent_delete_outgoing_by_channel.app_error", + "translation": "We couldn't delete the webhook" + }, + { "id": "store.sql_webhooks.save_incoming.app_error", "translation": "We couldn't save the IncomingWebhook" }, diff --git a/store/sql_command_store.go b/store/sql_command_store.go index a80d1dbf4..6b2338380 100644 --- a/store/sql_command_store.go +++ b/store/sql_command_store.go @@ -134,6 +134,24 @@ func (s SqlCommandStore) Delete(commandId string, time int64) StoreChannel { return storeChannel } +func (s SqlCommandStore) PermanentDeleteByTeam(teamId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + _, err := s.GetMaster().Exec("DELETE FROM Commands WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId}) + if err != nil { + result.Err = model.NewLocAppError("SqlCommandStore.DeleteByTeam", "store.sql_command.save.delete_perm.app_error", nil, "id="+teamId+", err="+err.Error()) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlCommandStore) PermanentDeleteByUser(userId string) StoreChannel { storeChannel := make(StoreChannel, 1) diff --git a/store/sql_command_store_test.go b/store/sql_command_store_test.go index ff6e3b9eb..cd7db4c1d 100644 --- a/store/sql_command_store_test.go +++ b/store/sql_command_store_test.go @@ -111,6 +111,36 @@ func TestCommandStoreDelete(t *testing.T) { } } +func TestCommandStoreDeleteByTeam(t *testing.T) { + Setup() + + o1 := &model.Command{} + o1.CreatorId = model.NewId() + o1.Method = model.COMMAND_METHOD_POST + o1.TeamId = model.NewId() + o1.URL = "http://nowhere.com/" + o1.Trigger = "trigger" + + o1 = (<-store.Command().Save(o1)).Data.(*model.Command) + + if r1 := <-store.Command().Get(o1.Id); r1.Err != nil { + t.Fatal(r1.Err) + } else { + if r1.Data.(*model.Command).CreateAt != o1.CreateAt { + t.Fatal("invalid returned command") + } + } + + if r2 := <-store.Command().PermanentDeleteByTeam(o1.TeamId); r2.Err != nil { + t.Fatal(r2.Err) + } + + if r3 := (<-store.Command().Get(o1.Id)); r3.Err == nil { + t.Log(r3.Data) + t.Fatal("Missing id should have failed") + } +} + func TestCommandStoreDeleteByUser(t *testing.T) { Setup() diff --git a/store/sql_webhook_store.go b/store/sql_webhook_store.go index d59d7e03a..56c1b1643 100644 --- a/store/sql_webhook_store.go +++ b/store/sql_webhook_store.go @@ -186,6 +186,8 @@ func (s SqlWebhookStore) DeleteIncoming(webhookId string, time int64) StoreChann result.Err = model.NewLocAppError("SqlWebhookStore.DeleteIncoming", "store.sql_webhooks.delete_incoming.app_error", nil, "id="+webhookId+", err="+err.Error()) } + s.InvalidateWebhookCache(webhookId) + storeChannel <- result close(storeChannel) }() @@ -204,6 +206,28 @@ func (s SqlWebhookStore) PermanentDeleteIncomingByUser(userId string) StoreChann result.Err = model.NewLocAppError("SqlWebhookStore.DeleteIncomingByUser", "store.sql_webhooks.permanent_delete_incoming_by_user.app_error", nil, "id="+userId+", err="+err.Error()) } + ClearWebhookCaches() + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (s SqlWebhookStore) PermanentDeleteIncomingByChannel(channelId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + _, err := s.GetMaster().Exec("DELETE FROM IncomingWebhooks WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}) + if err != nil { + result.Err = model.NewLocAppError("SqlWebhookStore.DeleteIncomingByChannel", "store.sql_webhooks.permanent_delete_incoming_by_channel.app_error", nil, "id="+channelId+", err="+err.Error()) + } + + ClearWebhookCaches() + storeChannel <- result close(storeChannel) }() @@ -442,6 +466,26 @@ func (s SqlWebhookStore) PermanentDeleteOutgoingByUser(userId string) StoreChann return storeChannel } +func (s SqlWebhookStore) PermanentDeleteOutgoingByChannel(channelId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + _, err := s.GetMaster().Exec("DELETE FROM OutgoingWebhooks WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}) + if err != nil { + result.Err = model.NewLocAppError("SqlWebhookStore.DeleteOutgoingByChannel", "store.sql_webhooks.permanent_delete_outgoing_by_channel.app_error", nil, "id="+channelId+", err="+err.Error()) + } + + ClearWebhookCaches() + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlWebhookStore) UpdateOutgoing(hook *model.OutgoingWebhook) StoreChannel { storeChannel := make(StoreChannel, 1) diff --git a/store/sql_webhook_store_test.go b/store/sql_webhook_store_test.go index f5e328b32..ea15b9caa 100644 --- a/store/sql_webhook_store_test.go +++ b/store/sql_webhook_store_test.go @@ -157,7 +157,29 @@ func TestWebhookStoreDeleteIncoming(t *testing.T) { t.Fatal(r2.Err) } - ClearWebhookCaches() + if r3 := (<-store.Webhook().GetIncoming(o1.Id, true)); r3.Err == nil { + t.Log(r3.Data) + t.Fatal("Missing id should have failed") + } +} + +func TestWebhookStoreDeleteIncomingByChannel(t *testing.T) { + Setup() + o1 := buildIncomingWebhook() + + o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) + + if r1 := <-store.Webhook().GetIncoming(o1.Id, true); r1.Err != nil { + t.Fatal(r1.Err) + } else { + if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt { + t.Fatal("invalid returned webhook") + } + } + + if r2 := <-store.Webhook().PermanentDeleteIncomingByChannel(o1.ChannelId); r2.Err != nil { + t.Fatal(r2.Err) + } if r3 := (<-store.Webhook().GetIncoming(o1.Id, true)); r3.Err == nil { t.Log(r3.Data) @@ -183,8 +205,6 @@ func TestWebhookStoreDeleteIncomingByUser(t *testing.T) { t.Fatal(r2.Err) } - ClearWebhookCaches() - if r3 := (<-store.Webhook().GetIncoming(o1.Id, true)); r3.Err == nil { t.Log(r3.Data) t.Fatal("Missing id should have failed") @@ -380,6 +400,35 @@ func TestWebhookStoreDeleteOutgoing(t *testing.T) { } } +func TestWebhookStoreDeleteOutgoingByChannel(t *testing.T) { + Setup() + + o1 := &model.OutgoingWebhook{} + o1.ChannelId = model.NewId() + o1.CreatorId = model.NewId() + o1.TeamId = model.NewId() + o1.CallbackURLs = []string{"http://nowhere.com/"} + + o1 = (<-store.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) + + if r1 := <-store.Webhook().GetOutgoing(o1.Id); r1.Err != nil { + t.Fatal(r1.Err) + } else { + if r1.Data.(*model.OutgoingWebhook).CreateAt != o1.CreateAt { + t.Fatal("invalid returned webhook") + } + } + + if r2 := <-store.Webhook().PermanentDeleteOutgoingByChannel(o1.ChannelId); r2.Err != nil { + t.Fatal(r2.Err) + } + + if r3 := (<-store.Webhook().GetOutgoing(o1.Id)); r3.Err == nil { + t.Log(r3.Data) + t.Fatal("Missing id should have failed") + } +} + func TestWebhookStoreDeleteOutgoingByUser(t *testing.T) { Setup() diff --git a/store/store.go b/store/store.go index 0fa2a96b3..6d84a0919 100644 --- a/store/store.go +++ b/store/store.go @@ -296,6 +296,7 @@ type WebhookStore interface { UpdateIncoming(webhook *model.IncomingWebhook) StoreChannel GetIncomingByChannel(channelId string) StoreChannel DeleteIncoming(webhookId string, time int64) StoreChannel + PermanentDeleteIncomingByChannel(channelId string) StoreChannel PermanentDeleteIncomingByUser(userId string) StoreChannel SaveOutgoing(webhook *model.OutgoingWebhook) StoreChannel @@ -304,6 +305,7 @@ type WebhookStore interface { GetOutgoingByChannel(channelId string, offset, limit int) StoreChannel GetOutgoingByTeam(teamId string, offset, limit int) StoreChannel DeleteOutgoing(webhookId string, time int64) StoreChannel + PermanentDeleteOutgoingByChannel(channelId string) StoreChannel PermanentDeleteOutgoingByUser(userId string) StoreChannel UpdateOutgoing(hook *model.OutgoingWebhook) StoreChannel @@ -317,6 +319,7 @@ type CommandStore interface { Get(id string) StoreChannel GetByTeam(teamId string) StoreChannel Delete(commandId string, time int64) StoreChannel + PermanentDeleteByTeam(teamId string) StoreChannel PermanentDeleteByUser(userId string) StoreChannel Update(hook *model.Command) StoreChannel AnalyticsCommandCount(teamId string) StoreChannel |