2016-08-18 21:16:44 +00:00
|
|
|
package kolide
|
|
|
|
|
2016-09-14 18:40:51 +00:00
|
|
|
import (
|
2016-09-15 14:52:17 +00:00
|
|
|
"fmt"
|
2016-09-14 18:40:51 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-09-15 14:52:17 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2016-09-14 18:40:51 +00:00
|
|
|
)
|
2016-08-18 21:16:44 +00:00
|
|
|
|
|
|
|
func TestValidatePassword(t *testing.T) {
|
|
|
|
|
|
|
|
var passwordTests = []struct {
|
|
|
|
Username, Password, Email string
|
|
|
|
Admin, PasswordReset bool
|
|
|
|
}{
|
|
|
|
{"marpaia", "foobar", "mike@kolide.co", true, false},
|
|
|
|
{"jason", "bar0baz!?", "jason@kolide.co", true, false},
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:11:06 +00:00
|
|
|
const bcryptCost = 6
|
|
|
|
|
2016-08-18 21:16:44 +00:00
|
|
|
for _, tt := range passwordTests {
|
2016-09-15 14:52:17 +00:00
|
|
|
user := newTestUser(t, tt.Username, tt.Password, tt.Email)
|
2016-09-14 18:40:51 +00:00
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
err := user.ValidatePassword(tt.Password)
|
2016-09-14 18:40:51 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = user.ValidatePassword("different")
|
|
|
|
assert.NotNil(t, err)
|
2016-08-18 21:16:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-15 14:52:17 +00:00
|
|
|
|
|
|
|
func newTestUser(t *testing.T, username, password, email string) *User {
|
|
|
|
var (
|
|
|
|
salt = "test-salt"
|
|
|
|
cost = 10
|
|
|
|
)
|
|
|
|
withSalt := []byte(fmt.Sprintf("%s%s", password, salt))
|
|
|
|
hashed, _ := bcrypt.GenerateFromPassword(withSalt, cost)
|
|
|
|
return &User{
|
|
|
|
Username: username,
|
|
|
|
Salt: salt,
|
|
|
|
Password: hashed,
|
|
|
|
Email: email,
|
|
|
|
}
|
|
|
|
}
|