fleet/server/service_users_test.go

129 lines
2.9 KiB
Go
Raw Normal View History

package server
2016-08-28 03:59:17 +00:00
import (
"testing"
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) {
ds, _ := datastore.New("inmem", "")
svc, _ := NewService(ds, kitlog.NewNopLogger(), config.TestConfig())
ctx := context.Background()
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"),
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{
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)
switch err.(type) {
case nil:
case invalidArgumentError:
2016-08-28 03:59:17 +00:00
continue
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) {
ds, _ := datastore.New("inmem", "")
svc, _ := NewService(ds, kitlog.NewNopLogger(), config.TestConfig())
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"),
},
"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
}