dispatch/server/irc_handler_test.go

308 lines
6.4 KiB
Go
Raw Normal View History

2015-06-16 21:20:53 +00:00
package server
import (
"io/ioutil"
"log"
"os"
"testing"
"github.com/khlieng/dispatch/pkg/irc"
2015-12-11 03:35:48 +00:00
"github.com/khlieng/dispatch/storage"
2018-06-01 01:37:22 +00:00
"github.com/khlieng/dispatch/storage/bleve"
"github.com/khlieng/dispatch/storage/boltdb"
"github.com/stretchr/testify/assert"
2015-06-16 21:20:53 +00:00
)
var user *storage.User
func TestMain(m *testing.M) {
tempdir, err := ioutil.TempDir("", "test_")
if err != nil {
log.Fatal(err)
}
2020-04-20 01:02:15 +00:00
storage.Initialize(tempdir, "", "")
db, err := boltdb.New(storage.Path.Database())
2016-01-17 20:15:29 +00:00
if err != nil {
log.Fatal(err)
2016-01-17 20:15:29 +00:00
}
storage.GetMessageStore = func(_ *storage.User) (storage.MessageStore, error) {
return db, nil
}
storage.GetMessageSearchProvider = func(user *storage.User) (storage.MessageSearchProvider, error) {
return bleve.New(storage.Path.Index(user.Username))
}
user, err = storage.NewUser(db)
2018-06-01 01:37:22 +00:00
if err != nil {
log.Fatal(err)
}
2015-06-16 21:20:53 +00:00
code := m.Run()
os.RemoveAll(tempdir)
os.Exit(code)
}
func dispatchMessage(msg *irc.Message) WSResponse {
2017-04-12 23:59:16 +00:00
return <-dispatchMessageMulti(msg)
}
func dispatchMessageMulti(msg *irc.Message) chan WSResponse {
c := irc.NewClient(&irc.Config{
2020-05-23 07:42:20 +00:00
Nick: "nick",
Username: "user",
Host: "host.com",
})
2020-06-16 09:26:07 +00:00
s := NewState(user, &Dispatch{})
2015-06-16 21:20:53 +00:00
newIRCHandler(c, s).dispatchMessage(msg)
2017-04-12 23:59:16 +00:00
return s.broadcast
2015-06-16 21:20:53 +00:00
}
2015-06-16 22:46:58 +00:00
func checkResponse(t *testing.T, expectedType string, expectedData interface{}, res WSResponse) {
2015-06-16 21:20:53 +00:00
assert.Equal(t, expectedType, res.Type)
2015-06-16 22:46:58 +00:00
assert.Equal(t, expectedData, res.Data)
2015-06-16 21:20:53 +00:00
}
func TestHandleIRCNick(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.NICK,
Sender: "old",
2016-12-12 06:19:22 +00:00
Params: []string{"new"},
2015-06-16 21:20:53 +00:00
})
checkResponse(t, "nick", Nick{
2020-06-15 08:58:51 +00:00
Network: "host.com",
Old: "old",
New: "new",
2015-06-16 21:20:53 +00:00
}, res)
}
func TestHandleIRCJoin(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.JOIN,
Sender: "joining",
2015-06-16 21:20:53 +00:00
Params: []string{"#chan"},
})
checkResponse(t, "join", Join{
2020-06-15 08:58:51 +00:00
Network: "host.com",
2015-06-16 21:20:53 +00:00
User: "joining",
Channels: []string{"#chan"},
}, res)
}
func TestHandleIRCPart(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.PART,
Sender: "parting",
2016-12-12 06:19:22 +00:00
Params: []string{"#chan", "the reason"},
2015-06-16 21:20:53 +00:00
})
checkResponse(t, "part", Part{
2020-06-15 08:58:51 +00:00
Network: "host.com",
User: "parting",
Channel: "#chan",
Reason: "the reason",
}, res)
res = dispatchMessage(&irc.Message{
Command: irc.PART,
Sender: "parting",
Params: []string{"#chan"},
})
checkResponse(t, "part", Part{
2020-06-15 08:58:51 +00:00
Network: "host.com",
User: "parting",
Channel: "#chan",
2015-06-16 21:20:53 +00:00
}, res)
}
func TestHandleIRCMessage(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.PRIVMSG,
Sender: "nick",
2016-12-12 06:19:22 +00:00
Params: []string{"#chan", "the message"},
2015-06-16 21:20:53 +00:00
})
2017-04-20 03:32:22 +00:00
assert.Equal(t, "message", res.Type)
msg, ok := res.Data.(Message)
assert.True(t, ok)
assert.Equal(t, "nick", msg.From)
assert.Equal(t, "#chan", msg.To)
assert.Equal(t, "the message", msg.Content)
2015-06-16 21:20:53 +00:00
res = dispatchMessage(&irc.Message{
Command: irc.PRIVMSG,
Sender: "someone",
2016-12-12 06:19:22 +00:00
Params: []string{"nick", "the message"},
2015-06-16 21:20:53 +00:00
})
2017-04-20 03:32:22 +00:00
assert.Equal(t, "pm", res.Type)
msg, ok = res.Data.(Message)
assert.True(t, ok)
assert.Equal(t, "someone", msg.From)
assert.Empty(t, msg.To)
assert.Equal(t, "the message", msg.Content)
2015-06-16 21:20:53 +00:00
}
func TestHandleIRCQuit(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.QUIT,
Sender: "nick",
2016-12-12 06:19:22 +00:00
Params: []string{"the reason"},
2015-06-16 21:20:53 +00:00
})
checkResponse(t, "quit", Quit{
2020-06-15 08:58:51 +00:00
Network: "host.com",
User: "nick",
Reason: "the reason",
2015-06-16 21:20:53 +00:00
}, res)
}
func TestHandleIRCWelcome(t *testing.T) {
2017-04-12 23:59:16 +00:00
res := dispatchMessageMulti(&irc.Message{
Command: irc.RPL_WELCOME,
Sender: "nick",
2017-04-12 23:59:16 +00:00
Params: []string{"nick", "some", "text"},
2015-06-16 21:20:53 +00:00
})
2017-04-12 23:59:16 +00:00
checkResponse(t, "nick", Nick{
2020-06-15 08:58:51 +00:00
Network: "host.com",
New: "nick",
2017-04-12 23:59:16 +00:00
}, <-res)
checkResponse(t, "pm", Message{
2020-06-15 08:58:51 +00:00
Network: "host.com",
2015-06-16 21:20:53 +00:00
From: "nick",
Content: "some text",
2017-04-12 23:59:16 +00:00
}, <-res)
2015-06-16 21:20:53 +00:00
}
func TestHandleIRCWhois(t *testing.T) {
c := irc.NewClient(&irc.Config{
2020-05-23 07:42:20 +00:00
Nick: "nick",
Username: "user",
Host: "host.com",
})
s := NewState(nil, nil)
2015-06-16 21:20:53 +00:00
i := newIRCHandler(c, s)
i.dispatchMessage(&irc.Message{
Command: irc.RPL_WHOISUSER,
2015-06-16 21:20:53 +00:00
Params: []string{"", "nick", "user", "host", "", "realname"},
})
i.dispatchMessage(&irc.Message{
Command: irc.RPL_WHOISSERVER,
2015-06-16 21:20:53 +00:00
Params: []string{"", "", "srv.com"},
})
i.dispatchMessage(&irc.Message{
Command: irc.RPL_WHOISCHANNELS,
2016-12-12 06:19:22 +00:00
Params: []string{"#chan #chan1"},
2015-06-16 21:20:53 +00:00
})
i.dispatchMessage(&irc.Message{Command: irc.RPL_ENDOFWHOIS})
2015-06-16 21:20:53 +00:00
checkResponse(t, "whois", WhoisReply{
Nick: "nick",
Username: "user",
Host: "host",
Realname: "realname",
Server: "srv.com",
Channels: []string{"#chan", "#chan1"},
}, <-s.broadcast)
2015-06-16 21:20:53 +00:00
}
func TestHandleIRCTopic(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.RPL_TOPIC,
2016-12-12 06:19:22 +00:00
Params: []string{"target", "#chan", "the topic"},
2015-06-16 21:20:53 +00:00
})
checkResponse(t, "topic", Topic{
2020-06-15 08:58:51 +00:00
Network: "host.com",
2015-06-16 21:20:53 +00:00
Channel: "#chan",
Topic: "the topic",
}, res)
res = dispatchMessage(&irc.Message{
Command: irc.TOPIC,
Params: []string{"#chan", "the topic"},
Sender: "bob",
})
checkResponse(t, "topic", Topic{
2020-06-15 08:58:51 +00:00
Network: "host.com",
Channel: "#chan",
Topic: "the topic",
Nick: "bob",
}, res)
}
func TestHandleIRCNoTopic(t *testing.T) {
res := dispatchMessage(&irc.Message{
Command: irc.RPL_NOTOPIC,
Params: []string{"target", "#chan", "No topic set."},
})
checkResponse(t, "topic", Topic{
2020-06-15 08:58:51 +00:00
Network: "host.com",
Channel: "#chan",
}, res)
2015-06-16 21:20:53 +00:00
}
func TestHandleIRCMotd(t *testing.T) {
c := irc.NewClient(&irc.Config{
2020-05-23 07:42:20 +00:00
Nick: "nick",
Username: "user",
Host: "host.com",
})
s := NewState(nil, nil)
2015-06-16 21:20:53 +00:00
i := newIRCHandler(c, s)
i.dispatchMessage(&irc.Message{
Command: irc.RPL_MOTDSTART,
2016-12-12 06:19:22 +00:00
Params: []string{"motd title"},
2015-06-16 21:20:53 +00:00
})
i.dispatchMessage(&irc.Message{
Command: irc.RPL_MOTD,
2016-12-12 06:19:22 +00:00
Params: []string{"line 1"},
2015-06-16 21:20:53 +00:00
})
i.dispatchMessage(&irc.Message{
Command: irc.RPL_MOTD,
2016-12-12 06:19:22 +00:00
Params: []string{"line 2"},
2015-06-16 21:20:53 +00:00
})
i.dispatchMessage(&irc.Message{Command: irc.RPL_ENDOFMOTD})
2015-06-16 21:20:53 +00:00
checkResponse(t, "motd", MOTD{
2020-06-15 08:58:51 +00:00
Network: "host.com",
2015-06-16 21:20:53 +00:00
Title: "motd title",
Content: []string{"line 1", "line 2"},
}, <-s.broadcast)
2015-06-16 21:20:53 +00:00
}
func TestHandleIRCBadNick(t *testing.T) {
c := irc.NewClient(&irc.Config{
2020-05-23 07:42:20 +00:00
Nick: "nick",
Username: "user",
Host: "host.com",
})
s := NewState(nil, nil)
i := newIRCHandler(c, s)
i.dispatchMessage(&irc.Message{
Command: irc.ERR_ERRONEUSNICKNAME,
})
// It should print the error message first
<-s.broadcast
checkResponse(t, "nick_fail", NickFail{
2020-06-15 08:58:51 +00:00
Network: "host.com",
}, <-s.broadcast)
}