Make a test streamline instead of modular

pull/97/head
Andy Wang 5 years ago
parent 3d3d06ec96
commit af5c8a381f

@ -27,7 +27,7 @@ func getSeshConfig(unordered bool) *mux.SessionConfig {
func TestActiveUser_Bypass(t *testing.T) { func TestActiveUser_Bypass(t *testing.T) {
manager, err := usermanager.MakeLocalManager(MOCK_DB_NAME) manager, err := usermanager.MakeLocalManager(MOCK_DB_NAME)
if err != nil { if err != nil {
t.Error("failed to make local manager", err) t.Fatal("failed to make local manager", err)
} }
panel := MakeUserPanel(manager) panel := MakeUserPanel(manager)
UID, _ := base64.StdEncoding.DecodeString("u97xvcc5YoQA8obCyt9q/w==") UID, _ := base64.StdEncoding.DecodeString("u97xvcc5YoQA8obCyt9q/w==")
@ -35,92 +35,89 @@ func TestActiveUser_Bypass(t *testing.T) {
var sesh0 *mux.Session var sesh0 *mux.Session
var existing bool var existing bool
var sesh1 *mux.Session var sesh1 *mux.Session
t.Run("get first session", func(t *testing.T) {
sesh0, existing, err = user.GetSession(0, getSeshConfig(false)) // get first session
if err != nil { sesh0, existing, err = user.GetSession(0, getSeshConfig(false))
t.Error(err) if err != nil {
} t.Fatal(err)
if existing { }
t.Error("first session returned as existing") if existing {
} t.Fatal("get first session: first session returned as existing")
if sesh0 == nil { }
t.Error("no session returned") if sesh0 == nil {
} t.Fatal("get first session: no session returned")
}) }
t.Run("get first session again", func(t *testing.T) {
seshx, existing, err := user.GetSession(0, &mux.SessionConfig{}) // get first session again
if err != nil { seshx, existing, err := user.GetSession(0, &mux.SessionConfig{})
t.Error(err) if err != nil {
} t.Fatal(err)
if !existing { }
t.Error("first session get again returned as not existing") if !existing {
} t.Fatal("get first session again: first session get again returned as not existing")
if seshx == nil { }
t.Error("no session returned") if seshx == nil {
} t.Fatal("get first session again: no session returned")
if seshx != sesh0 { }
t.Error("returned a different instance") if seshx != sesh0 {
} t.Fatal("returned a different instance")
}) }
t.Run("get second session", func(t *testing.T) {
sesh1, existing, err = user.GetSession(1, getSeshConfig(false)) // get second session
if err != nil { sesh1, existing, err = user.GetSession(1, getSeshConfig(false))
t.Error(err) if err != nil {
} t.Fatal(err)
if existing { }
t.Error("second session returned as existing") if existing {
} t.Fatal("get second session: second session returned as existing")
if sesh0 == nil { }
t.Error("no session returned") if sesh1 == nil {
} t.Fatal("get second session: no session returned")
}) }
t.Run("number of sessions", func(t *testing.T) {
if user.NumSession() != 2 { if user.NumSession() != 2 {
t.Error("number of session is not 2") t.Fatal("number of session is not 2")
} }
})
t.Run("delete a session", func(t *testing.T) { user.CloseSession(0, "")
user.CloseSession(0, "") if user.NumSession() != 1 {
if user.NumSession() != 1 { t.Fatal("number of session is not 1 after deleting one")
t.Error("number of session is not 1 after deleting one") }
} if !sesh0.IsClosed() {
if !sesh0.IsClosed() { t.Fatal("session not closed after deletion")
t.Error("session not closed after deletion") }
}
}) user.closeAllSessions("")
t.Run("close all sessions", func(t *testing.T) { if !sesh1.IsClosed() {
user.closeAllSessions("") t.Fatal("session not closed after user termination")
if !sesh1.IsClosed() { }
t.Error("session not closed after user termination")
} // get session again after termination
}) seshy, existing, err := user.GetSession(0, getSeshConfig(false))
t.Run("get session again after termination", func(t *testing.T) { if err != nil {
seshx, existing, err := user.GetSession(0, getSeshConfig(false)) t.Fatal(err)
if err != nil { }
t.Error(err) if existing {
} t.Fatal("get session again after termination: session returned as existing")
if existing { }
t.Error("session returned as existing") if seshy == nil {
} t.Fatal("get session again after termination: no session returned")
if seshx == nil { }
t.Error("no session returned") if seshy == sesh0 || seshy == sesh1 {
} t.Fatal("get session after termination returned the same instance")
if seshx == sesh0 || seshx == sesh1 { }
t.Error("get session after termination returned the same instance")
} user.CloseSession(0, "")
}) if panel.isActive(user.arrUID[:]) {
t.Run("delete last session", func(t *testing.T) { t.Fatal("user still active after last session deleted")
user.CloseSession(0, "") }
if panel.isActive(user.arrUID[:]) {
t.Error("user still active after last session deleted")
}
})
err = manager.Close() err = manager.Close()
if err != nil { if err != nil {
t.Error("failed to close localmanager", err) t.Fatal("failed to close localmanager", err)
} }
err = os.Remove(MOCK_DB_NAME) err = os.Remove(MOCK_DB_NAME)
if err != nil { if err != nil {
t.Error("failed to delete mockdb", err) t.Fatal("failed to delete mockdb", err)
} }
} }

Loading…
Cancel
Save