diff --git a/store.go b/store.go index b1016b1..86d18cf 100644 --- a/store.go +++ b/store.go @@ -166,6 +166,7 @@ func (u User) merge(updates User) (User, error) { } var ErrShareNotFound = errors.New("share not found") +var ErrLoginDuplicate = errors.New("login already exists") func (store *DBStore) AddUser(user User) (err error) { if strings.Contains(user.Username, ":") { diff --git a/store_test.go b/store_test.go index a32b861..fecf480 100644 --- a/store_test.go +++ b/store_test.go @@ -186,6 +186,100 @@ func TestStoreShareHandling(t *testing.T) { }) }) + t.Run("share access should work", func(t *testing.T) { + share1, _ := store.CreateShare() + share2, _ := store.CreateShare() + share3, _ := store.CreateShare() + + user1 := User{Username: "user1"} + user2 := User{Username: "user2"} + _ = store.AddUser(user1) + _ = store.AddUser(user2) + + t.Run("multiple shares should exist", func(t *testing.T) { + shares, _ := store.GetShares() + if len(shares) != 3 { + t.Errorf("3 shares should exist") + } + }) + + t.Run("can add users to shares", func(t *testing.T) { + if err := store.AddUserToShare(share1, user1.Username, ShareRoleAdmin); err != nil { + t.Errorf("cannot add user1 to share1: %v", err) + } + if err := store.AddUserToShare(share1, user2.Username, ShareRoleReader); err != nil { + t.Errorf("cannot add user2 to share1: %v", err) + } + if err := store.AddUserToShare(share2, user2.Username, ShareRoleAdmin); err != nil { + t.Errorf("cannot add user2 to share2: %v", err) + } + + t.Run("cannot add login if user doesn't exist", func(t *testing.T) { + if err := store.AddLogin(share3, user1.Username, Login{"foo", ""}); err == nil { + t.Errorf("an error should have been returned") + } else if err != ErrUserNotFound { + t.Errorf("wrong error has been returned: %v", err) + } + }) + + t.Run("logins can be added", func(t *testing.T) { + login1 := Login{LoginName: "login1"} + login2 := Login{LoginName: "login2"} + login3 := Login{LoginName: "login3"} + + if err := store.AddLogin(share1, user1.Username, login1); err != nil { + t.Errorf("adding login returned error: %v", err) + } + if err := store.AddLogin(share1, user2.Username, login2); err != nil { + t.Errorf("adding login returned error: %v", err) + } + if err := store.AddLogin(share1, user2.Username, login3); err != nil { + t.Errorf("adding login returned error: %v", err) + } + + t.Run("duplicate login not allowed", func(t *testing.T) { + if err := store.AddLogin(share1, user2.Username, login1); err != ErrLoginDuplicate { + t.Errorf("unexpected error: %v", err) + } + }) + + t.Run("share is found by login", func(t *testing.T) { + share, err := store.FindByLogin(user1.Username, login1.LoginName) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if share.UUID != share1.UUID { + t.Errorf("wrong store returned") + } + }) + + t.Run("unknown login/share combination returns error", func(t *testing.T) { + if _, err := store.FindByLogin(user1.Username, login3.LoginName); err != ErrShareNotFound { + t.Errorf("unexpected error: %v", err) + } + }) + + t.Run("login can be removed", func(t *testing.T) { + if err := store.RemoveLogin(share1, user1.Username, login1.LoginName); err != nil { + t.Errorf("unexpected error: %v", err) + } + if _, err := store.FindByLogin(user1.Username, login1.LoginName); err != ErrShareNotFound { + t.Errorf("share should not be found now, but returned: %v", err) + } + }) + + t.Run("user can be removed", func(t *testing.T) { + if err := store.RemoveUserFromShare(share2, user2.Username); err != nil { + t.Errorf("unexpected error: %v", err) + } + if _, err := store.FindByLogin(user2.Username, login2.LoginName); err != ErrShareNotFound { + t.Errorf("share should not be found now, but returned: %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 {