package main import ( "testing" uuid "github.com/satori/go.uuid" "github.com/tidwall/buntdb" ) func TestStoreUserHandling(t *testing.T) { store, err := NewDBStore(":memory:") if err != nil { t.Fatalf("cannot create store: %v", err) } defer store.Close() t.Run("store should be empty initially", func(t *testing.T) { users, err := store.GetUsers() if err != nil { t.Errorf("no error should have been returned: %v", err) } if len(users) != 0 { t.Errorf("there should be no users") } }) t.Run("user that doesn't exist should return error", func(t *testing.T) { _, err := store.GetUser("someuser") if err != ErrUserNotFound { t.Errorf("unexpected error: %v", err) } }) t.Run("adding users should work", func(t *testing.T) { if err := store.AddUser(User{Username: "myuser", Password: "mypass", Role: GlobalRoleUser}); err != nil { t.Errorf("cannot add user: %v", err) } t.Run("retrieving that single user should work", func(t *testing.T) { user, err := store.GetUser("myuser") if err != nil { t.Errorf("cannot retrieve user: %v", err) } if user.Username != "myuser" { t.Errorf("retrieved user contains unexpected username") } if user.Role != GlobalRoleUser { t.Errorf("retrieved user contains unexpected role") } }) t.Run("retrieving multiple users should work", func(t *testing.T) { users, err := store.GetUsers() if err != nil { t.Errorf("cannot retrieve user list: %v", err) } if len(users) != 1 { t.Errorf("there should be only one user") } if users[0].Username != "myuser" { t.Errorf("retrieved user contains unexpected username") } if users[0].Role != GlobalRoleUser { t.Errorf("retrieved user contains unexpected role") } }) t.Run("deleting that user should work", func(t *testing.T) { err := store.RemoveUser("myuser") if err != nil { t.Errorf("cannot delete user: %v", err) } t.Run("user should no longer be found", func(t *testing.T) { _, err := store.GetUser("myuser") if err != ErrUserNotFound { t.Errorf("unexpected error: %v", err) } }) t.Run("user should no longer be listed", func(t *testing.T) { users, err := store.GetUsers() if err != nil { t.Errorf("unexpected error: %v", err) } if len(users) != 0 { t.Errorf("there should be no users") } }) }) }) t.Run("database should be empty now", func(t *testing.T) { // checks that we properly deleted all keys if err := store.db.View(func(tx *buntdb.Tx) error { return tx.Ascend("", func(key, value string) bool { t.Errorf("there should be no keys left") return false }) }); err != nil { t.Errorf("iterating keys failed: %v", err) } }) } func TestStoreShareHandling(t *testing.T) { store, err := NewDBStore(":memory:") if err != nil { t.Fatalf("cannot create store: %v", err) } defer store.Close() t.Run("store should be empty initially", func(t *testing.T) { shares, err := store.GetShares() if err != nil { t.Errorf("no error should have been returned: %v", err) } if len(shares) != 0 { t.Errorf("there should be no shares") } }) t.Run("creating a share should work", func(t *testing.T) { share, err := store.CreateShare() if err != nil { t.Errorf("error creating share: %v", err) return } var emptyUUID uuid.UUID if share.UUID == emptyUUID { t.Errorf("UUID is empty") return } if share.Name != "" || share.Description != "" { t.Errorf("share should not have attributes set (yet)") } t.Run("share attributes can be set", func(t *testing.T) { share.Name = "a name" share.Description = "some desc" if err := store.UpdateShareAttributes(share); err != nil { t.Errorf("cannot set attributes of share: %v", err) } }) t.Run("cannot set attributes of unknown share", func(t *testing.T) { otherShare := Share{UUID: uuid.NewV4()} otherShare.Name = "foo" otherShare.Description = "bar" err := store.UpdateShareAttributes(otherShare) if err == nil { t.Errorf("an error should have been returned") } else if err != ErrShareNotFound { t.Errorf("wrong error has been returned: %v", err) } }) t.Run("share can be listed", func(t *testing.T) { shares, err := store.GetShares() if err != nil { t.Errorf("error getting shares: %v", err) return } if len(shares) != 1 { t.Errorf("invalid number of shares: %d", len(shares)) return } if shares[0].UUID != share.UUID { t.Errorf("unexpected uuid") } if shares[0].Name != "a name" { t.Errorf("unexpected name") } if shares[0].Description != "some desc" { t.Errorf("unexpected description") } }) t.Run("share can be removed", func(t *testing.T) { if err := store.RemoveShare(share.UUID); err != nil { t.Errorf("removing share failed: %v", err) } }) }) t.Run("database should be empty now", func(t *testing.T) { // checks that we properly deleted all keys if err := store.db.View(func(tx *buntdb.Tx) error { return tx.Ascend("", func(key, value string) bool { t.Errorf("there should be no keys left") return false }) }); err != nil { t.Errorf("iterating keys failed: %v", err) } }) }