fleet/kolide/users_test.go
Victor Vrantchan df19fd4b7c Update users service (#156)
Closes #144 #145 #160 
Implements PATCH method on user and endpoint middleware for authnz
Implements `reset_password` (with token) and `forgot_password` endpoints
Added godoc comments for UserService interface
Shift to using testify/assert in test code
Multiple fixes/changes to the UserService API
2016-09-15 10:52:17 -04:00

48 lines
970 B
Go

package kolide
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
)
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)
err := user.ValidatePassword(tt.Password)
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,
}
}