From 72f61ab96aabf65c162c8d94b5b843b5108ee1a9 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 31 Jul 2017 08:52:45 -0700 Subject: make cli team / channel delete operations also delete webhooks and slash commands (#7028) --- store/sql_command_store.go | 18 ++++++++++++++ store/sql_command_store_test.go | 30 ++++++++++++++++++++++ store/sql_webhook_store.go | 44 +++++++++++++++++++++++++++++++++ store/sql_webhook_store_test.go | 55 ++++++++++++++++++++++++++++++++++++++--- store/store.go | 3 +++ 5 files changed, 147 insertions(+), 3 deletions(-) (limited to 'store') 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 -- cgit v1.2.3-1-g7c22