Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Ensure Webhook msg filter tests test all Message event types.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtrox committed Jan 5, 2021
1 parent 8936bc3 commit 31f2f05
Showing 1 changed file with 62 additions and 28 deletions.
90 changes: 62 additions & 28 deletions std/msgfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,48 +99,82 @@ func TestMsgFilter_IsByBot(t *testing.T) {
}

func TestMsgFilter_NotByWebhook(t *testing.T) {
var botID disgord.Snowflake = 123
var webhookID disgord.Snowflake = 456
filter, _ := newMsgFilter(context.Background(), &clientRESTMock{id: botID})
evt := &disgord.MessageCreate{
Message: &disgord.Message{
Author: &disgord.User{Bot: false},
WebhookID: webhookID,
},
var botID disgord.Snowflake = 123

messageWithWebhookID := &disgord.Message{
Author: &disgord.User{},
WebhookID: webhookID,
}

result := filter.NotByWebhook(evt)
if result != nil {
t.Error("expected a match")
messageWithoutWebhookID := &disgord.Message{
Author: &disgord.User{},
WebhookID: 0,
}

evt.Message.WebhookID = 0
result = filter.NotByWebhook(evt)
if result == nil {
t.Error("expected pass-through")
testCases := []struct {
name string
evt interface{}
shouldPassThrough bool
}{
{"MessageCreate_FromWebhook", &disgord.MessageCreate{Message: messageWithWebhookID}, false},
{"MessageUpdate_FromWebhook", &disgord.MessageUpdate{Message: messageWithWebhookID}, false},
{"MessageCreate_NotWebhook", &disgord.MessageCreate{Message: messageWithoutWebhookID}, true},
{"MessageUpdate_NotWebhook", &disgord.MessageUpdate{Message: messageWithoutWebhookID}, true},
}

filter, _ := newMsgFilter(context.Background(), &clientRESTMock{id: botID})

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := filter.NotByWebhook(tc.evt)
if tc.shouldPassThrough && result == nil {
t.Error("expected to passthrough")
}
if !tc.shouldPassThrough && result != nil {
t.Error("expected a filter match")
}
})
}
}

func TestMsgFilter_IsByWebhook(t *testing.T) {
var botID disgord.Snowflake = 123
var webhookID disgord.Snowflake = 456
filter, _ := newMsgFilter(context.Background(), &clientRESTMock{id: botID})
evt := &disgord.MessageCreate{
Message: &disgord.Message{
Author: &disgord.User{Bot: false},
WebhookID: 0,
},
var botID disgord.Snowflake = 123

messageWithWebhookID := &disgord.Message{
Author: &disgord.User{},
WebhookID: webhookID,
}

result := filter.IsByWebhook(evt)
if result != nil {
t.Error("expected a match")
messageWithoutWebhookID := &disgord.Message{
Author: &disgord.User{},
WebhookID: 0,
}

evt.Message.WebhookID = webhookID
result = filter.IsByWebhook(evt)
if result == nil {
t.Error("expected pass-through")
testCases := []struct {
name string
evt interface{}
shouldPassThrough bool
}{
{"MessageCreate_FromWebhook", &disgord.MessageCreate{Message: messageWithWebhookID}, true},
{"MessageUpdate_FromWebhook", &disgord.MessageUpdate{Message: messageWithWebhookID}, true},
{"MessageCreate_NotWebhook", &disgord.MessageCreate{Message: messageWithoutWebhookID}, false},
{"MessageUpdate_NotWebhook", &disgord.MessageUpdate{Message: messageWithoutWebhookID}, false},
}

filter, _ := newMsgFilter(context.Background(), &clientRESTMock{id: botID})

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := filter.IsByWebhook(tc.evt)
if tc.shouldPassThrough && result == nil {
t.Error("expected to passthrough")
}
if !tc.shouldPassThrough && result != nil {
t.Error("expected a filter match")
}
})
}
}

Expand Down

0 comments on commit 31f2f05

Please sign in to comment.