otfe/models/session_test.go

52 lines
1.0 KiB
Go
Raw Normal View History

2022-02-23 12:07:09 +00:00
package models
import (
"net/http"
"testing"
"git.1248.nz/1248/Otfe/misc/b64"
"git.1248.nz/1248/Otfe/misc/helpers"
"git.1248.nz/1248/Otfe/misc/rand"
)
func TestSessionCreate(t *testing.T) {
var s1, s2 Session
s1.ID, _ = rand.B64String(32)
if s1.Create() != nil {
t.Fail()
}
read("session", "_id", s1.ID, &s2)
if s1.ID != s2.ID {
t.Fail()
}
}
func TestSessionRead(t *testing.T) {
var s1, s2 Session
s1.ID, _ = rand.B64String(32)
if create("session", &s1) != nil {
t.Fatal("Failed to create session")
}
if s2.Read(s1.ID) != nil {
t.Fatal("Failed to read session")
}
if s1.ID != s2.ID {
t.Fatal("Ids don't match")
}
}
func TestGet(t *testing.T) {
DBWipeCollection("session")
var s1, s2 Session
s1.ID, _ = rand.B64String(32)
s1.Create()
c := &http.Cookie{Name: "session",
Value: b64.Encode(s1.ID)}
request, err := http.NewRequest("GET", "/", nil)
helpers.Ok(t, err)
request.AddCookie(c)
s2.Get(request)
helpers.Equals(t, s1, s2)
}