Skip to content

Commit

Permalink
fix #3102: add unit test for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
dgnorton authored and otoolep committed Jun 30, 2015
1 parent d76b363 commit 0eedfef
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
1 change: 0 additions & 1 deletion meta/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,6 @@ func (s *Store) Authenticate(username, password string) (ui *UserInfo, err error
ui = u
return nil
}

// Compare password with user hash.
if err := bcrypt.CompareHashAndPassword([]byte(u.Hash), []byte(password)); err != nil {
return err
Expand Down
55 changes: 55 additions & 0 deletions meta/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/influxdb/influxdb/meta"
"github.com/influxdb/influxdb/tcp"
"github.com/influxdb/influxdb/toml"
"golang.org/x/crypto/bcrypt"
)

func init() {
Expand Down Expand Up @@ -586,6 +587,60 @@ func TestStore_UpdateUser(t *testing.T) {
}
}

// Ensure Authentication works.
func TestStore_Authentication(t *testing.T) {
t.Parallel()

if testing.Short() {
t.SkipNow()
}

s := MustOpenStore()
defer s.Close()

// Set the hash function back to the real thing for this test.
oldHashFn := meta.HashPassword
meta.HashPassword = func(password string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(password), 4)
}
defer func() { meta.HashPassword = oldHashFn }()

// Create user.
s.CreateUser("susy", "pass", true)

// Authenticate user.
if ui, err := s.Authenticate("susy", "pass"); err != nil {
t.Fatal(err)
} else if ui.Name != "susy" {
t.Fatalf(`expected "susy", got "%s"`, ui.Name)
}

// Update user's password.
s.UpdateUser("susy", "pass2")

// Make sure authentication with old password does NOT work.
if _, err := s.Authenticate("susy", "pass"); err == nil {
t.Fatal("expected authentication error")
}

// Authenticate user with new password
if ui, err := s.Authenticate("susy", "pass2"); err != nil {
t.Fatal(err)
} else if ui.Name != "susy" {
t.Fatalf(`expected "susy", got "%s"`, ui.Name)
}

// Drop user.
s.DropUser("susy")

// Make sure authentication with both old passwords does NOT work.
if _, err := s.Authenticate("susy", "pass"); err == nil {
t.Fatal("expected authentication error")
} else if _, err := s.Authenticate("susy", "pass2"); err == nil {
t.Fatal("expected authentication error")
}
}

// Ensure the store can return the count of users in it.
func TestStore_UserCount(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 0eedfef

Please sign in to comment.