Files
kontor/go/pkg/auth/session_test.go
T
2025-04-30 17:31:18 +02:00

104 lines
1.8 KiB
Go

package auth
import (
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
"reflect"
"testing"
"gopkg.in/mgo.v2/bson"
)
var sessionModelTestTable = []struct {
name string
typeName string
}{
{"Id", "string"},
{"Username", "string"},
{"IsAdmin", "bool"},
{"Model", "string"},
}
func TestSessionModel(t *testing.T) {
m := Session{}
if reflect.TypeOf(m).NumField() != len(sessionModelTestTable) {
t.Fail()
}
for index, testData := range sessionModelTestTable {
givenType := reflect.TypeOf(m).Field(index).Type.Kind().String()
if givenType != testData.typeName {
t.Fail()
}
}
}
func TestListSessions(t *testing.T) {
var (
sessionDao = SessionDAO{Db: dao.TestDb}
)
sessions, err := sessionDao.FindAll()
if err != nil {
t.Fail()
}
if sessions != nil {
t.Fail()
}
}
func TestInsertSession(t *testing.T) {
var (
sessionDao = SessionDAO{Db: dao.TestDb}
session = Session{}
sessions []Session
)
session.ID = bson.NewObjectId()
session.Username = "test"
err := sessionDao.Insert(session)
if err != nil {
t.Fail()
}
sessions, err = sessionDao.FindAll()
if err != nil {
t.Fail()
}
if len(sessions) != 1 {
t.Fail()
}
}
func TestUpsertSession(t *testing.T) {
var (
sessionDao = SessionDAO{Db: dao.TestDb}
session = Session{}
)
session.ID = bson.NewObjectId()
session.Username = "test2"
sessionDao.Upsert(session)
sessions, err := sessionDao.FindAll()
if err != nil {
t.Fail()
}
if len(sessions) != 2 {
t.Fail()
}
}
func TestDeleteSession(t *testing.T) {
var (
sessionDao = SessionDAO{Db: dao.TestDb}
)
sessions, err := sessionDao.FindAll()
if err != nil {
t.Fail()
}
for _, session := range sessions {
sessionDao.Delete(session)
}
sessions, err = sessionDao.FindAll()
if err != nil {
t.Fail()
}
if len(sessions) != 0 {
t.Fail()
}
}