Add user storage test
This commit is contained in:
parent
0c5b4bb04e
commit
137cf3224d
@ -1,6 +1,9 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/cobra"
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/khlieng/dispatch/storage"
|
"github.com/khlieng/dispatch/storage"
|
||||||
@ -8,8 +11,20 @@ import (
|
|||||||
|
|
||||||
var clearCmd = &cobra.Command{
|
var clearCmd = &cobra.Command{
|
||||||
Use: "clear",
|
Use: "clear",
|
||||||
Short: "Clear all application data",
|
Short: "Clear database and message logs",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
storage.Clear()
|
err := os.Remove(storage.Path.Database())
|
||||||
|
if err == nil || os.IsNotExist(err) {
|
||||||
|
log.Println("Database cleared")
|
||||||
|
} else {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.RemoveAll(storage.Path.Logs())
|
||||||
|
if err == nil {
|
||||||
|
log.Println("Logs cleared")
|
||||||
|
} else {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -27,29 +27,26 @@ const logo = `
|
|||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "dispatch",
|
Use: "dispatch",
|
||||||
Short: "Web-based IRC client in Go.",
|
Short: "Web-based IRC client in Go.",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
storage.Initialize()
|
|
||||||
server.Run()
|
|
||||||
},
|
|
||||||
|
|
||||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||||
if cmd.Use == "dispatch" {
|
if cmd.Use == "dispatch" {
|
||||||
fmt.Println(logo)
|
fmt.Println(logo)
|
||||||
}
|
}
|
||||||
|
|
||||||
storage.SetDirectory(viper.GetString("dir"))
|
storage.Initialize(viper.GetString("dir"))
|
||||||
|
|
||||||
err := os.MkdirAll(storage.Path.Logs(), 0700)
|
initConfig(storage.Path.Config())
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
initConfig()
|
|
||||||
|
|
||||||
viper.SetConfigName("config")
|
viper.SetConfigName("config")
|
||||||
viper.AddConfigPath(storage.Path.Root())
|
viper.AddConfigPath(storage.Path.Root())
|
||||||
viper.ReadInConfig()
|
viper.ReadInConfig()
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
log.Println("Storing data at", storage.Path.Root())
|
||||||
|
|
||||||
|
storage.Open()
|
||||||
|
server.Run()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
@ -67,9 +64,7 @@ func init() {
|
|||||||
viper.BindPFlag("dir", rootCmd.PersistentFlags().Lookup("dir"))
|
viper.BindPFlag("dir", rootCmd.PersistentFlags().Lookup("dir"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig(configPath string) {
|
||||||
configPath := storage.Path.Config()
|
|
||||||
|
|
||||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||||
config, err := assets.Asset("config.default.toml")
|
config, err := assets.Asset("config.default.toml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -20,9 +20,8 @@ func TestMain(m *testing.M) {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
storage.SetDirectory(tempdir)
|
storage.Initialize(tempdir)
|
||||||
os.MkdirAll(storage.Path.Logs(), 0700)
|
storage.Open()
|
||||||
storage.Initialize()
|
|
||||||
user = storage.NewUser("uuid")
|
user = storage.NewUser("uuid")
|
||||||
channelStore = storage.NewChannelStore()
|
channelStore = storage.NewChannelStore()
|
||||||
|
|
||||||
|
@ -6,12 +6,6 @@ import (
|
|||||||
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Path directory
|
|
||||||
|
|
||||||
func SetDirectory(dir string) {
|
|
||||||
Path = directory(dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
func DefaultDirectory() string {
|
func DefaultDirectory() string {
|
||||||
home, _ := homedir.Dir()
|
home, _ := homedir.Dir()
|
||||||
return filepath.Join(home, ".dispatch")
|
return filepath.Join(home, ".dispatch")
|
||||||
|
@ -8,6 +8,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
Path directory
|
||||||
|
|
||||||
db *bolt.DB
|
db *bolt.DB
|
||||||
|
|
||||||
bucketUsers = []byte("Users")
|
bucketUsers = []byte("Users")
|
||||||
@ -16,9 +18,16 @@ var (
|
|||||||
bucketMessages = []byte("Messages")
|
bucketMessages = []byte("Messages")
|
||||||
)
|
)
|
||||||
|
|
||||||
func Initialize() {
|
func Initialize(dir string) {
|
||||||
log.Println("Storing data at", Path.Root())
|
Path = directory(dir)
|
||||||
|
|
||||||
|
err := os.MkdirAll(Path.Logs(), 0700)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Open() {
|
||||||
var err error
|
var err error
|
||||||
db, err = bolt.Open(Path.Database(), 0600, nil)
|
db, err = bolt.Open(Path.Database(), 0600, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -37,8 +46,3 @@ func Initialize() {
|
|||||||
func Close() {
|
func Close() {
|
||||||
db.Close()
|
db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Clear() {
|
|
||||||
os.RemoveAll(Path.Logs())
|
|
||||||
os.Remove(Path.Database())
|
|
||||||
}
|
|
||||||
|
65
storage/user_test.go
Normal file
65
storage/user_test.go
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func tempdir() string {
|
||||||
|
f, _ := ioutil.TempDir("", "")
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUser(t *testing.T) {
|
||||||
|
Initialize(tempdir())
|
||||||
|
Open()
|
||||||
|
|
||||||
|
srv := Server{
|
||||||
|
Name: "Freenode",
|
||||||
|
Address: "irc.freenode.net",
|
||||||
|
Nick: "test",
|
||||||
|
}
|
||||||
|
chan1 := Channel{
|
||||||
|
Server: srv.Address,
|
||||||
|
Name: "#test",
|
||||||
|
}
|
||||||
|
chan2 := Channel{
|
||||||
|
Server: srv.Address,
|
||||||
|
Name: "#testing",
|
||||||
|
}
|
||||||
|
|
||||||
|
user := NewUser("unique")
|
||||||
|
user.AddServer(srv)
|
||||||
|
user.AddChannel(chan1)
|
||||||
|
user.AddChannel(chan2)
|
||||||
|
user.Close()
|
||||||
|
|
||||||
|
users := LoadUsers()
|
||||||
|
assert.Len(t, users, 1)
|
||||||
|
|
||||||
|
user = users[0]
|
||||||
|
assert.Equal(t, "unique", user.UUID)
|
||||||
|
|
||||||
|
servers := user.GetServers()
|
||||||
|
assert.Len(t, servers, 1)
|
||||||
|
assert.Equal(t, srv, servers[0])
|
||||||
|
|
||||||
|
channels := user.GetChannels()
|
||||||
|
assert.Len(t, channels, 2)
|
||||||
|
assert.Equal(t, chan1, channels[0])
|
||||||
|
assert.Equal(t, chan2, channels[1])
|
||||||
|
|
||||||
|
user.SetNick("bob", srv.Address)
|
||||||
|
assert.Equal(t, "bob", user.GetServers()[0].Nick)
|
||||||
|
|
||||||
|
user.RemoveChannel(srv.Address, chan1.Name)
|
||||||
|
channels = user.GetChannels()
|
||||||
|
assert.Len(t, channels, 1)
|
||||||
|
assert.Equal(t, chan2, channels[0])
|
||||||
|
|
||||||
|
user.RemoveServer(srv.Address)
|
||||||
|
assert.Len(t, user.GetServers(), 0)
|
||||||
|
assert.Len(t, user.GetChannels(), 0)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user