✨ Implement share CRUD operations
This commit is contained in:
+28
-9
@@ -196,6 +196,11 @@ func TestStoreShareHandling(t *testing.T) {
|
||||
_ = store.AddUser(user1)
|
||||
_ = store.AddUser(user2)
|
||||
|
||||
defer func() {
|
||||
_ = store.RemoveUser(user1.Username)
|
||||
_ = store.RemoveUser(user2.Username)
|
||||
}()
|
||||
|
||||
t.Run("multiple shares should exist", func(t *testing.T) {
|
||||
shares, _ := store.GetShares()
|
||||
if len(shares) != 3 {
|
||||
@@ -238,23 +243,25 @@ func TestStoreShareHandling(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run("duplicate login not allowed", func(t *testing.T) {
|
||||
if err := store.AddLogin(share1, user2.Username, login1); err != ErrLoginDuplicate {
|
||||
// Different share, but same user:login pair as above. Must be a share where
|
||||
// that user is already assigned, though.
|
||||
if err := store.AddLogin(share2, user2.Username, login2); 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)
|
||||
share, err := store.FindShareByLogin(user1.Username, login1.LoginName)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if share.UUID != share1.UUID {
|
||||
t.Errorf("wrong store returned")
|
||||
t.Errorf("wrong share returned")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unknown login/share combination returns error", func(t *testing.T) {
|
||||
if _, err := store.FindByLogin(user1.Username, login3.LoginName); err != ErrShareNotFound {
|
||||
if _, err := store.FindShareByLogin(user1.Username, login3.LoginName); err != ErrShareNotFound {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
})
|
||||
@@ -263,29 +270,41 @@ func TestStoreShareHandling(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 {
|
||||
if _, err := store.FindShareByLogin(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 {
|
||||
if err := store.RemoveUserFromShare(share1, user2.Username); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if _, err := store.FindByLogin(user2.Username, login2.LoginName); err != ErrShareNotFound {
|
||||
if _, err := store.FindShareByLogin(user2.Username, login2.LoginName); err != ErrShareNotFound {
|
||||
t.Errorf("share should not be found now, but returned: %v", err)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("can remove shares", func(t *testing.T) {
|
||||
if err := store.RemoveShare(share1.UUID); err != nil {
|
||||
t.Errorf("cannot remove share1: %v", err)
|
||||
}
|
||||
if err := store.RemoveShare(share2.UUID); err != nil {
|
||||
t.Errorf("cannot remove share2: %v", err)
|
||||
}
|
||||
if err := store.RemoveShare(share3.UUID); err != nil {
|
||||
t.Errorf("cannot remove share3: %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
|
||||
t.Errorf("leftover key found: %v", key)
|
||||
return true
|
||||
})
|
||||
}); err != nil {
|
||||
t.Errorf("iterating keys failed: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user