Add share login handling tests

This commit is contained in:
Andreas Schneider 2020-10-13 19:59:35 +02:00
parent 66c525aa7b
commit 3d4f6f681c
2 changed files with 95 additions and 0 deletions

View File

@ -166,6 +166,7 @@ func (u User) merge(updates User) (User, error) {
} }
var ErrShareNotFound = errors.New("share not found") var ErrShareNotFound = errors.New("share not found")
var ErrLoginDuplicate = errors.New("login already exists")
func (store *DBStore) AddUser(user User) (err error) { func (store *DBStore) AddUser(user User) (err error) {
if strings.Contains(user.Username, ":") { if strings.Contains(user.Username, ":") {

View File

@ -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) { t.Run("database should be empty now", func(t *testing.T) {
// checks that we properly deleted all keys // checks that we properly deleted all keys
if err := store.db.View(func(tx *buntdb.Tx) error { if err := store.db.View(func(tx *buntdb.Tx) error {