fleet/server/datastore/datastore_users_test.go
Mike Arpaia 12f8c0b671 Datastore cleaning (#262)
This PR reorganizes a bunch of the files in datastore such that all datastore implementations are consistently broken up into multiple files. Additionally, the datastore tests follow a similar pattern and can easily be applied to any complete datastore implementation.
2016-10-04 16:34:36 -04: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 := 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)
}
}