2016-09-05 20:03:58 +00:00
|
|
|
package server
|
2016-08-28 03:59:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-09-14 16:11:06 +00:00
|
|
|
kitlog "github.com/go-kit/kit/log"
|
|
|
|
"github.com/kolide/kolide-ose/config"
|
2016-08-28 03:59:17 +00:00
|
|
|
"github.com/kolide/kolide-ose/datastore"
|
|
|
|
"github.com/kolide/kolide-ose/kolide"
|
2016-09-14 18:40:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-09-03 17:25:16 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-08-28 03:59:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateUser(t *testing.T) {
|
2016-09-10 01:39:38 +00:00
|
|
|
ds, _ := datastore.New("inmem", "")
|
2016-09-14 16:11:06 +00:00
|
|
|
svc, _ := NewService(ds, kitlog.NewNopLogger(), config.TestConfig())
|
2016-09-01 04:51:38 +00:00
|
|
|
ctx := context.Background()
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
var createUserTests = []struct {
|
|
|
|
Username *string
|
|
|
|
Password *string
|
|
|
|
Email *string
|
|
|
|
NeedsPasswordReset *bool
|
|
|
|
Admin *bool
|
|
|
|
Err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Username: stringPtr("admin1"),
|
|
|
|
Password: stringPtr("foobar"),
|
2016-09-01 04:51:38 +00:00
|
|
|
Err: invalidArgumentError{},
|
2016-08-28 03:59:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Username: stringPtr("admin1"),
|
|
|
|
Password: stringPtr("foobar"),
|
|
|
|
Email: stringPtr("admin1@example.com"),
|
|
|
|
NeedsPasswordReset: boolPtr(true),
|
|
|
|
Admin: boolPtr(false),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range createUserTests {
|
|
|
|
payload := kolide.UserPayload{
|
2016-09-08 22:57:05 +00:00
|
|
|
Username: tt.Username,
|
|
|
|
Password: tt.Password,
|
|
|
|
Email: tt.Email,
|
|
|
|
Admin: tt.Admin,
|
|
|
|
AdminForcedPasswordReset: tt.NeedsPasswordReset,
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
|
|
|
user, err := svc.NewUser(ctx, payload)
|
2016-09-01 04:51:38 +00:00
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
|
|
|
case invalidArgumentError:
|
2016-08-28 03:59:17 +00:00
|
|
|
continue
|
2016-09-01 04:51:38 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("got %q, want %q", err, tt.Err)
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 18:40:51 +00:00
|
|
|
assert.NotZero(t, user.ID)
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2016-09-14 18:40:51 +00:00
|
|
|
err = user.ValidatePassword(*tt.Password)
|
|
|
|
assert.Nil(t, err)
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2016-09-14 18:40:51 +00:00
|
|
|
err = user.ValidatePassword("different_password")
|
|
|
|
assert.NotNil(t, err)
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2016-09-14 18:40:51 +00:00
|
|
|
assert.Equal(t, user.AdminForcedPasswordReset, *tt.NeedsPasswordReset)
|
|
|
|
assert.Equal(t, user.Admin, *tt.Admin)
|
2016-08-28 03:59:17 +00:00
|
|
|
|
|
|
|
// check duplicate creation
|
|
|
|
_, err = svc.NewUser(ctx, payload)
|
2016-09-14 18:40:51 +00:00
|
|
|
assert.Equal(t, datastore.ErrExists, err)
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:04:07 +00:00
|
|
|
func TestChangeUserPassword(t *testing.T) {
|
2016-09-10 01:39:38 +00:00
|
|
|
ds, _ := datastore.New("inmem", "")
|
2016-09-14 16:11:06 +00:00
|
|
|
svc, _ := NewService(ds, kitlog.NewNopLogger(), config.TestConfig())
|
2016-09-01 04:51:38 +00:00
|
|
|
createTestUsers(t, ds)
|
2016-08-29 00:29:56 +00:00
|
|
|
|
|
|
|
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)
|
2016-09-14 18:40:51 +00:00
|
|
|
assert.Nil(t, err)
|
2016-08-29 00:29:56 +00:00
|
|
|
|
2016-08-29 17:04:07 +00:00
|
|
|
err = svc.ChangePassword(ctx, user.ID, tt.currentPassword, tt.newPassword)
|
2016-09-14 18:40:51 +00:00
|
|
|
assert.Nil(t, err)
|
2016-08-29 00:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"),
|
|
|
|
},
|
2016-09-01 04:51:38 +00:00
|
|
|
"user2": {
|
|
|
|
Username: stringPtr("user2"),
|
|
|
|
Password: stringPtr("bazfoo"),
|
|
|
|
Email: stringPtr("user2@example.com"),
|
|
|
|
},
|
2016-08-29 00:29:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
func stringPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func boolPtr(b bool) *bool {
|
|
|
|
return &b
|
|
|
|
}
|