2020-05-11 01:45:42 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/bot"
|
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v3/session"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state/store"
|
2021-09-10 01:45:08 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
2020-05-11 01:45:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAdminOnly(t *testing.T) {
|
|
|
|
var ctx = &bot.Context{
|
|
|
|
State: &state.State{
|
2020-11-19 18:43:31 +00:00
|
|
|
Session: &session.Session{
|
|
|
|
Gateway: &gateway.Gateway{
|
|
|
|
Identifier: &gateway.Identifier{
|
|
|
|
IdentifyData: gateway.IdentifyData{
|
2021-09-10 01:45:08 +00:00
|
|
|
Intents: option.NewUint(uint(gateway.IntentGuilds | gateway.IntentGuildMembers)),
|
2020-11-19 18:43:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-11-30 01:19:59 +00:00
|
|
|
Cabinet: mockCabinet(),
|
2020-05-11 01:45:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var middleware = AdminOnly(ctx)
|
|
|
|
|
|
|
|
t.Run("allow message", func(t *testing.T) {
|
|
|
|
var msg = &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
ID: 1,
|
2021-01-06 05:15:38 +00:00
|
|
|
ChannelID: 69420,
|
2020-05-11 01:45:42 +00:00
|
|
|
Author: discord.User{ID: 69420},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectNil(t, middleware(msg))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("deny message", func(t *testing.T) {
|
|
|
|
var msg = &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
ID: 2,
|
|
|
|
ChannelID: 1337,
|
|
|
|
Author: discord.User{ID: 1337},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectBreak(t, middleware(msg))
|
|
|
|
var pin = &gateway.ChannelPinsUpdateEvent{
|
|
|
|
ChannelID: 120,
|
|
|
|
}
|
|
|
|
expectBreak(t, middleware(pin))
|
|
|
|
var tpg = &gateway.TypingStartEvent{}
|
|
|
|
expectBreak(t, middleware(tpg))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGuildOnly(t *testing.T) {
|
|
|
|
var ctx = &bot.Context{
|
|
|
|
State: &state.State{
|
2020-11-19 18:43:31 +00:00
|
|
|
Session: &session.Session{
|
|
|
|
Gateway: &gateway.Gateway{
|
|
|
|
Identifier: &gateway.Identifier{
|
|
|
|
IdentifyData: gateway.IdentifyData{
|
2021-09-10 01:45:08 +00:00
|
|
|
Intents: option.NewUint(uint(gateway.IntentGuilds)),
|
2020-11-19 18:43:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-11-30 01:19:59 +00:00
|
|
|
Cabinet: mockCabinet(),
|
2020-05-11 01:45:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var middleware = GuildOnly(ctx)
|
|
|
|
|
2021-05-30 21:16:16 +00:00
|
|
|
t.Run("allow message with GuildID", func(t *testing.T) {
|
2020-05-11 01:45:42 +00:00
|
|
|
var msg = &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
ID: 3,
|
|
|
|
GuildID: 1337,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectNil(t, middleware(msg))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("allow message with ChannelID", func(t *testing.T) {
|
|
|
|
var msg = &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
ID: 3,
|
|
|
|
ChannelID: 69420,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectNil(t, middleware(msg))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("deny message", func(t *testing.T) {
|
|
|
|
var msg = &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
ID: 1,
|
|
|
|
ChannelID: 12,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectBreak(t, middleware(msg))
|
|
|
|
|
|
|
|
var msg2 = &gateway.MessageCreateEvent{}
|
|
|
|
expectBreak(t, middleware(msg2))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectNil(t *testing.T, err error) {
|
|
|
|
t.Helper()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectBreak(t *testing.T, err error) {
|
|
|
|
t.Helper()
|
|
|
|
if errors.Is(err, bot.Break) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
t.Fatal("Expected error, got nothing.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkGuildOnly runs a message through the GuildOnly middleware to
|
|
|
|
// calculate the overhead of reflection.
|
|
|
|
func BenchmarkGuildOnly(b *testing.B) {
|
|
|
|
var ctx = &bot.Context{
|
|
|
|
State: &state.State{
|
2020-11-30 01:19:59 +00:00
|
|
|
Cabinet: mockCabinet(),
|
2020-05-11 01:45:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var middleware = GuildOnly(ctx)
|
|
|
|
var msg = &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
ID: 3,
|
|
|
|
GuildID: 1337,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if err := middleware(msg); err != nil {
|
|
|
|
b.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkAdminOnly runs a message through the GuildOnly middleware to
|
|
|
|
// calculate the overhead of reflection.
|
|
|
|
func BenchmarkAdminOnly(b *testing.B) {
|
|
|
|
var ctx = &bot.Context{
|
|
|
|
State: &state.State{
|
2020-11-30 01:19:59 +00:00
|
|
|
Cabinet: mockCabinet(),
|
2020-05-11 01:45:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var middleware = AdminOnly(ctx)
|
|
|
|
var msg = &gateway.MessageCreateEvent{
|
|
|
|
Message: discord.Message{
|
|
|
|
ID: 1,
|
|
|
|
ChannelID: 1337,
|
|
|
|
Author: discord.User{ID: 69420},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if err := middleware(msg); err != nil {
|
|
|
|
b.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockStore struct {
|
2020-11-30 01:19:59 +00:00
|
|
|
store.NoopStore
|
|
|
|
}
|
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
func mockCabinet() *store.Cabinet {
|
|
|
|
c := *store.NoopCabinet
|
2020-11-30 01:19:59 +00:00
|
|
|
c.GuildStore = &mockStore{}
|
|
|
|
c.MemberStore = &mockStore{}
|
|
|
|
c.ChannelStore = &mockStore{}
|
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
return &c
|
2020-05-11 01:45:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *mockStore) Guild(id discord.GuildID) (*discord.Guild, error) {
|
2020-05-11 01:45:42 +00:00
|
|
|
return &discord.Guild{
|
|
|
|
ID: id,
|
|
|
|
Roles: []discord.Role{{
|
|
|
|
ID: 69420,
|
|
|
|
Permissions: discord.PermissionAdministrator,
|
|
|
|
}},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
func (s *mockStore) Member(_ discord.GuildID, userID discord.UserID) (*discord.Member, error) {
|
2020-05-11 01:45:42 +00:00
|
|
|
return &discord.Member{
|
2020-07-21 20:27:59 +00:00
|
|
|
User: discord.User{ID: userID},
|
|
|
|
RoleIDs: []discord.RoleID{discord.RoleID(userID)},
|
2020-05-11 01:45:42 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channel returns a channel with a guildID for #69420.
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *mockStore) Channel(id discord.ChannelID) (*discord.Channel, error) {
|
|
|
|
if id == 69420 {
|
2020-05-11 01:45:42 +00:00
|
|
|
return &discord.Channel{
|
2020-07-21 20:27:59 +00:00
|
|
|
ID: id,
|
2020-05-11 01:45:42 +00:00
|
|
|
GuildID: 1337,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &discord.Channel{
|
2020-07-21 20:27:59 +00:00
|
|
|
ID: id,
|
2020-05-11 01:45:42 +00:00
|
|
|
}, nil
|
|
|
|
}
|