add tests for set password

This commit is contained in:
Victor Vrantchan 2016-08-28 20:29:56 -04:00
parent d611dc7786
commit 739bd2924c
3 changed files with 72 additions and 15 deletions

View File

@ -141,20 +141,6 @@ func TestLogin(t *testing.T) {
}
}
var testUsers = map[string]kolide.UserPayload{
"admin1": {
Username: stringPtr("admin1"),
Password: stringPtr("foobar"),
Email: stringPtr("admin1@example.com"),
Admin: boolPtr(true),
},
"user1": {
Username: stringPtr("user1"),
Password: stringPtr("foobar"),
Email: stringPtr("user1@example.com"),
},
}
func createTestUsers(t *testing.T, svc kolide.UserService) {
ctx := context.Background()
for _, tt := range testUsers {

View File

@ -22,6 +22,31 @@ func (s service) NewUser(ctx context.Context, p kolide.UserPayload) (*kolide.Use
return user, nil
}
func (s service) User(ctx context.Context, id uint) (*kolide.User, error) {
return s.ds.UserByID(id)
}
func (s service) SetPassword(ctx context.Context, userID uint, password string) error {
user, err := s.User(ctx, userID)
if err != nil {
return err
}
hashed, salt, err := hashPassword(password, s.saltKeySize, s.bcryptCost)
if err != nil {
return err
}
user.Salt = salt
user.Password = hashed
return s.saveUser(user)
}
// saves user in datastore.
// doesn't need to be exposed to the transport
// the service should expose actions for modifying a user instead
func (s service) saveUser(user *kolide.User) error {
return s.ds.SaveUser(user)
}
func userFromPayload(p kolide.UserPayload, keySize, cost int) (*kolide.User, error) {
hashed, salt, err := hashPassword(*p.Password, keySize, cost)
if err != nil {
@ -62,7 +87,6 @@ func generateRandomText(keySize int) (string, error) {
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(key), nil
}

View File

@ -84,6 +84,53 @@ func TestCreateUser(t *testing.T) {
}
}
func TestSetUserPassword(t *testing.T) {
ds, _ := datastore.New("mock", "")
svc, _ := NewService(ds)
createTestUsers(t, svc)
var passwordChangeTests = []struct {
username string
currentPassword string
newPassword string
err error
}{
{
username: "admin1",
currentPassword: *testUsers["admin1"].Password,
newPassword: "123cat!",
},
}
ctx := context.Background()
for _, tt := range passwordChangeTests {
user, err := ds.User(tt.username)
if err != nil {
t.Fatal(err)
}
err = svc.SetPassword(ctx, user.ID, tt.newPassword)
if err != nil {
t.Fatal(err)
}
}
}
var testUsers = map[string]kolide.UserPayload{
"admin1": {
Username: stringPtr("admin1"),
Password: stringPtr("foobar"),
Email: stringPtr("admin1@example.com"),
Admin: boolPtr(true),
},
"user1": {
Username: stringPtr("user1"),
Password: stringPtr("foobar"),
Email: stringPtr("user1@example.com"),
},
}
func stringPtr(s string) *string {
return &s
}