Core functionality for new aiterp.net servers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB

package auth
import "testing"
func TestSession(t *testing.T) {
auther := testAuther{FullName: "Test64"}
Register(&auther)
user, err := auther.Register("Tester1401", "1234", nil)
if err != nil {
t.Error("Failed to register test user:", err)
t.Fail()
return
}
sessions := []*Session{OpenSession(user), OpenSession(user), OpenSession(user)}
ids := []string{sessions[0].ID, sessions[1].ID, sessions[2].ID}
t.Run("Find", func(t *testing.T) {
for i, id := range ids {
found := FindSession(id)
if found != sessions[i] {
t.Errorf("Find(\"%s\") == %+v", id, found)
t.Fail()
}
}
})
t.Run("Auth", func(t *testing.T) {
for _, id := range ids {
found := FindSession(id)
foundUser, err := FindUser(found.UserID)
if foundUser == nil {
t.Error("User", found.UserID, "not found", err)
t.Fail()
}
if foundUser != nil && foundUser != user {
t.Error("User", found.UserID, "is not correct", foundUser)
t.Fail()
}
}
})
t.Run("Close", func(t *testing.T) {
CloseSession(ids[2])
if FindSession(ids[0]) == nil || FindSession(ids[1]) == nil || FindSession(ids[2]) != nil {
t.Errorf("Find(\"%s\") == %+v", ids[0], FindSession(ids[0]))
t.Errorf("Find(\"%s\") == %+v", ids[1], FindSession(ids[1]))
t.Errorf("Find(\"%s\") == %+v", ids[2], FindSession(ids[2]))
t.Fail()
}
})
t.Run("Clear", func(t *testing.T) {
ClearSessions(user)
if FindSession(ids[0]) != nil || FindSession(ids[1]) != nil || FindSession(ids[2]) != nil {
t.Errorf("Find(\"%s\") == %+v", ids[0], FindSession(ids[0]))
t.Errorf("Find(\"%s\") == %+v", ids[1], FindSession(ids[1]))
t.Errorf("Find(\"%s\") == %+v", ids[2], FindSession(ids[2]))
t.Fail()
}
})
}