2018-05-31 21:24:59 +00:00
|
|
|
package storage_test
|
2016-01-07 22:59:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2016-01-19 21:02:12 +00:00
|
|
|
"os"
|
2016-01-17 20:15:29 +00:00
|
|
|
"strconv"
|
2016-01-07 22:59:38 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
"github.com/khlieng/dispatch/storage"
|
|
|
|
"github.com/khlieng/dispatch/storage/bleve"
|
|
|
|
"github.com/khlieng/dispatch/storage/boltdb"
|
2017-04-20 03:32:22 +00:00
|
|
|
"github.com/kjk/betterguid"
|
2016-03-01 00:51:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-07 22:59:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func tempdir() string {
|
|
|
|
f, _ := ioutil.TempDir("", "")
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser(t *testing.T) {
|
2020-04-20 01:02:15 +00:00
|
|
|
storage.Initialize(tempdir(), "", "")
|
2016-01-15 01:27:30 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
db, err := boltdb.New(storage.Path.Database())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
user, err := storage.NewUser(db)
|
|
|
|
assert.Nil(t, err)
|
2016-01-07 22:59:38 +00:00
|
|
|
|
2018-06-01 02:16:38 +00:00
|
|
|
srv := &storage.Server{
|
2016-01-14 05:17:53 +00:00
|
|
|
Name: "Freenode",
|
|
|
|
Host: "irc.freenode.net",
|
|
|
|
Nick: "test",
|
2016-01-07 22:59:38 +00:00
|
|
|
}
|
2018-06-01 02:16:38 +00:00
|
|
|
chan1 := &storage.Channel{
|
2016-01-14 05:17:53 +00:00
|
|
|
Server: srv.Host,
|
2016-01-07 22:59:38 +00:00
|
|
|
Name: "#test",
|
|
|
|
}
|
2018-06-01 02:16:38 +00:00
|
|
|
chan2 := &storage.Channel{
|
2016-01-14 05:17:53 +00:00
|
|
|
Server: srv.Host,
|
2016-01-07 22:59:38 +00:00
|
|
|
Name: "#testing",
|
|
|
|
}
|
|
|
|
|
2018-06-01 02:16:38 +00:00
|
|
|
user.AddServer(srv)
|
|
|
|
user.AddChannel(chan1)
|
|
|
|
user.AddChannel(chan2)
|
2016-01-07 22:59:38 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
users, err := storage.LoadUsers(db)
|
|
|
|
assert.Nil(t, err)
|
2016-01-07 22:59:38 +00:00
|
|
|
assert.Len(t, users, 1)
|
|
|
|
|
2018-06-01 02:16:38 +00:00
|
|
|
user = users[0]
|
2016-01-15 01:27:30 +00:00
|
|
|
assert.Equal(t, uint64(1), user.ID)
|
2016-01-07 22:59:38 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
servers, err := user.GetServers()
|
2016-01-07 22:59:38 +00:00
|
|
|
assert.Len(t, servers, 1)
|
|
|
|
assert.Equal(t, srv, servers[0])
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
channels, err := user.GetChannels()
|
2016-01-07 22:59:38 +00:00
|
|
|
assert.Len(t, channels, 2)
|
|
|
|
assert.Equal(t, chan1, channels[0])
|
|
|
|
assert.Equal(t, chan2, channels[1])
|
|
|
|
|
2016-01-14 05:17:53 +00:00
|
|
|
user.SetNick("bob", srv.Host)
|
2018-05-31 21:24:59 +00:00
|
|
|
servers, err = user.GetServers()
|
|
|
|
assert.Equal(t, "bob", servers[0].Nick)
|
2016-01-07 22:59:38 +00:00
|
|
|
|
2017-06-12 04:18:32 +00:00
|
|
|
user.SetServerName("cake", srv.Host)
|
2018-05-31 21:24:59 +00:00
|
|
|
servers, err = user.GetServers()
|
|
|
|
assert.Equal(t, "cake", servers[0].Name)
|
2017-06-12 04:18:32 +00:00
|
|
|
|
2016-01-14 05:17:53 +00:00
|
|
|
user.RemoveChannel(srv.Host, chan1.Name)
|
2018-05-31 21:24:59 +00:00
|
|
|
channels, err = user.GetChannels()
|
2016-01-07 22:59:38 +00:00
|
|
|
assert.Len(t, channels, 1)
|
|
|
|
assert.Equal(t, chan2, channels[0])
|
|
|
|
|
2016-01-14 05:17:53 +00:00
|
|
|
user.RemoveServer(srv.Host)
|
2018-05-31 21:24:59 +00:00
|
|
|
servers, err = user.GetServers()
|
|
|
|
assert.Len(t, servers, 0)
|
|
|
|
channels, err = user.GetChannels()
|
|
|
|
assert.Len(t, channels, 0)
|
2016-01-19 21:02:12 +00:00
|
|
|
|
2018-10-15 06:56:17 +00:00
|
|
|
settings := user.GetClientSettings()
|
|
|
|
assert.NotNil(t, settings)
|
|
|
|
assert.Equal(t, storage.DefaultClientSettings(), settings)
|
|
|
|
|
|
|
|
settings.ColoredNicks = !settings.ColoredNicks
|
|
|
|
err = user.SetClientSettings(settings)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, settings, user.GetClientSettings())
|
|
|
|
assert.NotEqual(t, settings, storage.DefaultClientSettings())
|
|
|
|
|
2016-01-19 21:02:12 +00:00
|
|
|
user.Remove()
|
2018-05-31 21:24:59 +00:00
|
|
|
_, err = os.Stat(storage.Path.User(user.Username))
|
2016-01-19 21:02:12 +00:00
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
users, err = storage.LoadUsers(db)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
for i := range users {
|
|
|
|
assert.NotEqual(t, user.ID, users[i].ID)
|
2016-01-19 21:02:12 +00:00
|
|
|
}
|
2016-01-07 22:59:38 +00:00
|
|
|
}
|
2016-01-17 20:15:29 +00:00
|
|
|
|
|
|
|
func TestMessages(t *testing.T) {
|
2020-04-20 01:02:15 +00:00
|
|
|
storage.Initialize(tempdir(), "", "")
|
2016-01-17 20:15:29 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
db, err := boltdb.New(storage.Path.Database())
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
user, err := storage.NewUser(db)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
os.MkdirAll(storage.Path.User(user.Username), 0700)
|
|
|
|
|
|
|
|
search, err := bleve.New(storage.Path.Index(user.Username))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
user.SetMessageStore(db)
|
|
|
|
user.SetMessageSearchProvider(search)
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages, hasMore, err := user.GetMessages("irc.freenode.net", "#go-nuts", 10, "6")
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
2017-05-02 21:21:25 +00:00
|
|
|
assert.False(t, hasMore)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Len(t, messages, 0)
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages, hasMore, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 10)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
2017-05-02 21:21:25 +00:00
|
|
|
assert.False(t, hasMore)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Len(t, messages, 0)
|
|
|
|
|
|
|
|
messages, err = user.SearchMessages("irc.freenode.net", "#go-nuts", "message")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 0)
|
|
|
|
|
2017-04-20 03:32:22 +00:00
|
|
|
ids := []string{}
|
2016-01-17 20:15:29 +00:00
|
|
|
for i := 0; i < 5; i++ {
|
2017-04-20 03:32:22 +00:00
|
|
|
id := betterguid.New()
|
|
|
|
ids = append(ids, id)
|
|
|
|
err = user.LogMessage(id, "irc.freenode.net", "nick", "#go-nuts", "message"+strconv.Itoa(i))
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages, hasMore, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, ids[4])
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Equal(t, "message0", messages[0].Content)
|
2017-04-20 03:32:22 +00:00
|
|
|
assert.Equal(t, "message3", messages[3].Content)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
2017-05-02 21:21:25 +00:00
|
|
|
assert.False(t, hasMore)
|
2017-04-20 03:32:22 +00:00
|
|
|
assert.Len(t, messages, 4)
|
2016-01-17 20:15:29 +00:00
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages, hasMore, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, betterguid.New())
|
2016-01-17 22:33:52 +00:00
|
|
|
assert.Equal(t, "message0", messages[0].Content)
|
|
|
|
assert.Equal(t, "message4", messages[4].Content)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
2017-05-02 21:21:25 +00:00
|
|
|
assert.False(t, hasMore)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Len(t, messages, 5)
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages, hasMore, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, ids[2])
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Equal(t, "message0", messages[0].Content)
|
2017-04-20 03:32:22 +00:00
|
|
|
assert.Equal(t, "message1", messages[1].Content)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
2017-05-02 21:21:25 +00:00
|
|
|
assert.False(t, hasMore)
|
2017-04-20 03:32:22 +00:00
|
|
|
assert.Len(t, messages, 2)
|
2016-01-17 20:15:29 +00:00
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages, hasMore, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 10)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Equal(t, "message0", messages[0].Content)
|
2017-05-02 21:21:25 +00:00
|
|
|
assert.Equal(t, "message4", messages[4].Content)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
2017-05-02 21:21:25 +00:00
|
|
|
assert.False(t, hasMore)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Len(t, messages, 5)
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages, hasMore, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 4)
|
|
|
|
assert.Equal(t, "message1", messages[0].Content)
|
|
|
|
assert.Equal(t, "message4", messages[3].Content)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
2017-05-02 21:21:25 +00:00
|
|
|
assert.True(t, hasMore)
|
|
|
|
assert.Len(t, messages, 4)
|
2016-01-17 20:15:29 +00:00
|
|
|
|
|
|
|
messages, err = user.SearchMessages("irc.freenode.net", "#go-nuts", "message")
|
|
|
|
assert.Nil(t, err)
|
2016-01-22 18:07:47 +00:00
|
|
|
assert.True(t, len(messages) > 0)
|
2016-01-17 20:15:29 +00:00
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
db.Close()
|
2016-01-17 20:15:29 +00:00
|
|
|
}
|