author | Alberto Bertogli
<albertito@blitiri.com.ar> 2016-09-23 22:57:20 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2016-10-09 23:51:04 UTC |
parent | 667358d72e73ecf5f1ff3b086c4bbdd4ad7bb4af |
internal/userdb/userdb.go | +11 | -0 |
internal/userdb/userdb_test.go | +25 | -0 |
diff --git a/internal/userdb/userdb.go b/internal/userdb/userdb.go index 355cd00..a7a082d 100644 --- a/internal/userdb/userdb.go +++ b/internal/userdb/userdb.go @@ -191,6 +191,17 @@ func (db *DB) AddUser(name, plainPassword string) error { return nil } +// RemoveUser from the database. Returns True if the user was there, False +// otherwise. +func (db *DB) RemoveUser(name string) bool { + db.mu.Lock() + + _, present := db.db.Users[name] + delete(db.db.Users, name) + db.mu.Unlock() + return present +} + /////////////////////////////////////////////////////////// // Encryption schemes // diff --git a/internal/userdb/userdb_test.go b/internal/userdb/userdb_test.go index 2c2a0e6..32b0556 100644 --- a/internal/userdb/userdb_test.go +++ b/internal/userdb/userdb_test.go @@ -256,5 +256,30 @@ func TestReload(t *testing.T) { if len(db.db.Users) != 2 { t.Errorf("expected 2 users, got %d", len(db.db.Users)) } +} + +func TestRemoveUser(t *testing.T) { + fname := mustCreateDB(t, "") + defer removeIfSuccessful(t, fname) + db := mustLoad(t, fname) + + if ok := db.RemoveUser("unknown"); ok { + t.Errorf("removal of unknown user succeeded") + } + if err := db.AddUser("user", "passwd"); err != nil { + t.Fatalf("error adding user: %v", err) + } + + if ok := db.RemoveUser("unknown"); ok { + t.Errorf("removal of unknown user succeeded") + } + + if ok := db.RemoveUser("user"); !ok { + t.Errorf("removal of existing user failed") + } + + if ok := db.RemoveUser("user"); ok { + t.Errorf("removal of unknown user succeeded") + } }