package auth import ( "testing" "github.com/stretchr/testify/assert" "code.chrissexton.org/cws/cabinet/db" ) func TestMakeKey(t *testing.T) { k, err := makeKey() assert.Nil(t, err) assert.NotEmpty(t, k) } func TestGetByKey(t *testing.T) { d, err := db.New(":memory:") assert.Nil(t, err) tx, err := d.Beginx() assert.Nil(t, err) err = PrepareTable(tx) assert.Nil(t, err) err = tx.Commit() assert.Nil(t, err) u, err := New(d, "test", "abc") assert.Nil(t, err) u2, err := GetByKey(d, u.AuthKey) assert.Nil(t, err) assert.Equal(t, u.ID, u2.ID) } func TestGetByKeyFailure(t *testing.T) { d, err := db.New(":memory:") assert.Nil(t, err) tx, err := d.Beginx() assert.Nil(t, err) err = PrepareTable(tx) assert.Nil(t, err) err = tx.Commit() assert.Nil(t, err) _, err = New(d, "test", "abc") assert.Nil(t, err) u2, err := GetByKey(d, "foobar") assert.Error(t, err) assert.Nil(t, u2) } func TestGet(t *testing.T) { d, err := db.New(":memory:") assert.Nil(t, err) tx, err := d.Beginx() assert.Nil(t, err) err = PrepareTable(tx) assert.Nil(t, err) err = tx.Commit() assert.Nil(t, err) u, err := New(d, "test", "abc") assert.Nil(t, err) u2, err := Get(d, "test") assert.Nil(t, err) assert.Equal(t, u.ID, u2.ID) } func TestUser_Validate(t *testing.T) { d, err := db.New(":memory:") assert.Nil(t, err) tx, err := d.Beginx() assert.Nil(t, err) err = PrepareTable(tx) assert.Nil(t, err) err = tx.Commit() assert.Nil(t, err) u, err := New(d, "test", "abc") assert.Nil(t, err) actual := u.Validate("abc") assert.True(t, actual) }