fleet/server/datastore/datastore_users_test.go
John Murphy 6a825c11e3 Datastore refactor (#439)
Removed Gorm, replaced it with Sqlx

* Added SQL bundling command to Makfile

* Using go-kit logger

* Added soft delete capability

* Changed SearchLabel to accept a variadic param for optional omit list
instead of array

* Gorm removed

* Refactor table structures to use CURRENT_TIMESTAMP mysql function

* Moved Inmem datastore into it's own package

* Updated README

* Implemented code review suggestions from @zwass

* Removed reference to Gorm from glide.yaml
2016-11-16 21:47:49 +08:00

126 lines
3.0 KiB
Go

package datastore
import (
"fmt"
"testing"
"github.com/kolide/kolide-ose/server/kolide"
"github.com/stretchr/testify/assert"
)
func testCreateUser(t *testing.T, ds kolide.Datastore) {
var createTests = []struct {
username, password, email string
isAdmin, passwordReset bool
}{
{"marpaia", "foobar", "mike@kolide.co", true, false},
{"jason", "foobar", "jason@kolide.co", true, false},
}
for _, tt := range createTests {
u := &kolide.User{
Username: tt.username,
Password: []byte(tt.password),
Admin: tt.isAdmin,
AdminForcedPasswordReset: tt.passwordReset,
Email: tt.email,
}
user, err := ds.NewUser(u)
assert.Nil(t, err)
verify, err := ds.User(tt.username)
assert.Nil(t, err)
assert.Equal(t, user.ID, verify.ID)
assert.Equal(t, tt.username, verify.Username)
assert.Equal(t, tt.email, verify.Email)
assert.Equal(t, tt.email, verify.Email)
}
}
func testUserByID(t *testing.T, ds kolide.Datastore) {
users := createTestUsers(t, ds)
for _, tt := range users {
returned, err := ds.UserByID(tt.ID)
assert.Nil(t, err)
assert.Equal(t, tt.ID, returned.ID)
}
// test missing user
_, err := ds.UserByID(10000000000)
assert.NotNil(t, err)
}
func createTestUsers(t *testing.T, ds kolide.Datastore) []*kolide.User {
var createTests = []struct {
username, password, email string
isAdmin, passwordReset bool
}{
{"marpaia", "foobar", "mike@kolide.co", true, false},
{"jason", "foobar", "jason@kolide.co", false, false},
}
var users []*kolide.User
for _, tt := range createTests {
u := &kolide.User{
Username: tt.username,
Password: []byte(tt.password),
Admin: tt.isAdmin,
AdminForcedPasswordReset: tt.passwordReset,
Email: tt.email,
}
user, err := ds.NewUser(u)
assert.Nil(t, err)
users = append(users, user)
}
assert.NotEmpty(t, users)
return users
}
func testSaveUser(t *testing.T, ds kolide.Datastore) {
users := createTestUsers(t, ds)
testAdminAttribute(t, ds, users)
testEmailAttribute(t, ds, users)
testPasswordAttribute(t, ds, users)
}
func testPasswordAttribute(t *testing.T, ds kolide.Datastore, users []*kolide.User) {
for _, user := range users {
randomText, err := kolide.RandomText(8) //GenerateRandomText(8)
assert.Nil(t, err)
user.Password = []byte(randomText)
err = ds.SaveUser(user)
assert.Nil(t, err)
verify, err := ds.User(user.Username)
assert.Nil(t, err)
assert.Equal(t, user.Password, verify.Password)
}
}
func testEmailAttribute(t *testing.T, ds kolide.Datastore, users []*kolide.User) {
for _, user := range users {
user.Email = fmt.Sprintf("test.%s", user.Email)
err := ds.SaveUser(user)
assert.Nil(t, err)
verify, err := ds.User(user.Username)
assert.Nil(t, err)
assert.Equal(t, user.Email, verify.Email)
}
}
func testAdminAttribute(t *testing.T, ds kolide.Datastore, users []*kolide.User) {
for _, user := range users {
user.Admin = false
err := ds.SaveUser(user)
assert.Nil(t, err)
verify, err := ds.User(user.Username)
assert.Nil(t, err)
assert.Equal(t, user.Admin, verify.Admin)
}
}