fleet/kolide/users_test.go

48 lines
970 B
Go
Raw Normal View History

package kolide
2016-09-14 18:40:51 +00:00
import (
"fmt"
2016-09-14 18:40:51 +00:00
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
2016-09-14 18:40:51 +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},
}
const bcryptCost = 6
for _, tt := range passwordTests {
user := newTestUser(t, tt.Username, tt.Password, tt.Email)
2016-09-14 18:40:51 +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)
}
}
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,
}
}