220 lines
5.6 KiB
Go
220 lines
5.6 KiB
Go
package main
|
|
|
|
import "strconv"
|
|
import "testing"
|
|
|
|
// TODO: Generate a test database to work with rather than a live one
|
|
func TestUserStore(t *testing.T) {
|
|
if !gloinited {
|
|
err := gloinit()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if !pluginsInited {
|
|
initPlugins()
|
|
}
|
|
|
|
var user *User
|
|
var err error
|
|
|
|
_, err = users.CascadeGet(-1)
|
|
if err == nil {
|
|
t.Error("UID #-1 shouldn't exist")
|
|
} else if err != ErrNoRows {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = users.CascadeGet(0)
|
|
if err == nil {
|
|
t.Error("UID #0 shouldn't exist")
|
|
} else if err != ErrNoRows {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
user, err = users.CascadeGet(1)
|
|
if err == ErrNoRows {
|
|
t.Error("Couldn't find UID #1")
|
|
} else if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if user.ID != 1 {
|
|
t.Error("user.ID does not match the requested UID. Got '" + strconv.Itoa(user.ID) + "' instead.")
|
|
}
|
|
|
|
// TODO: Lock onto the specific error type. Is this even possible without sacrificing the detailed information in the error message?
|
|
var userList map[int]*User
|
|
_, err = users.BulkCascadeGetMap([]int{-1})
|
|
if err == nil {
|
|
t.Error("UID #-1 shouldn't exist")
|
|
}
|
|
|
|
_, err = users.BulkCascadeGetMap([]int{0})
|
|
if err == nil {
|
|
t.Error("UID #0 shouldn't exist")
|
|
}
|
|
|
|
userList, err = users.BulkCascadeGetMap([]int{1})
|
|
if err == ErrNoRows {
|
|
t.Error("Couldn't find UID #1")
|
|
} else if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(userList) == 0 {
|
|
t.Error("The returned map is empty for UID #0")
|
|
} else if len(userList) > 1 {
|
|
t.Error("Too many results were returned for UID #0")
|
|
}
|
|
|
|
user, ok := userList[1]
|
|
if !ok {
|
|
t.Error("We couldn't find UID #0 in the returned map")
|
|
t.Error("userList", userList)
|
|
}
|
|
|
|
if user.ID != 1 {
|
|
t.Error("user.ID does not match the requested UID. Got '" + strconv.Itoa(user.ID) + "' instead.")
|
|
}
|
|
}
|
|
|
|
func TestForumStore(t *testing.T) {
|
|
if !gloinited {
|
|
gloinit()
|
|
}
|
|
if !pluginsInited {
|
|
initPlugins()
|
|
}
|
|
|
|
var forum *Forum
|
|
var err error
|
|
|
|
_, err = fstore.CascadeGet(-1)
|
|
if err == nil {
|
|
t.Error("FID #-1 shouldn't exist")
|
|
} else if err != ErrNoRows {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
forum, err = fstore.CascadeGet(0)
|
|
if err == ErrNoRows {
|
|
t.Error("Couldn't find FID #0")
|
|
} else if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if forum.ID != 0 {
|
|
t.Error("forum.ID doesn't not match the requested UID. Got '" + strconv.Itoa(forum.ID) + "' instead.")
|
|
}
|
|
if forum.Name != "Uncategorised" {
|
|
t.Error("FID #0 is named '" + forum.Name + "' and not 'Uncategorised'")
|
|
}
|
|
|
|
forum, err = fstore.CascadeGet(1)
|
|
if err == ErrNoRows {
|
|
t.Error("Couldn't find FID #1")
|
|
} else if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if forum.ID != 1 {
|
|
t.Error("forum.ID doesn't not match the requested UID. Got '" + strconv.Itoa(forum.ID) + "' instead.'")
|
|
}
|
|
if forum.Name != "Reports" {
|
|
t.Error("FID #0 is named '" + forum.Name + "' and not 'Reports'")
|
|
}
|
|
|
|
forum, err = fstore.CascadeGet(2)
|
|
if err == ErrNoRows {
|
|
t.Error("Couldn't find FID #2")
|
|
} else if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_ = forum
|
|
}
|
|
|
|
func TestSlugs(t *testing.T) {
|
|
var res string
|
|
var msgList []ME_Pair
|
|
|
|
msgList = addMEPair(msgList, "Unknown", "unknown")
|
|
msgList = addMEPair(msgList, "Unknown2", "unknown2")
|
|
msgList = addMEPair(msgList, "Unknown ", "unknown")
|
|
msgList = addMEPair(msgList, "Unknown 2", "unknown-2")
|
|
msgList = addMEPair(msgList, "Unknown 2", "unknown-2")
|
|
msgList = addMEPair(msgList, "Admin Alice", "admin-alice")
|
|
msgList = addMEPair(msgList, "Admin_Alice", "adminalice")
|
|
msgList = addMEPair(msgList, "Admin_Alice-", "adminalice")
|
|
msgList = addMEPair(msgList, "-Admin_Alice-", "adminalice")
|
|
msgList = addMEPair(msgList, "-Admin@Alice-", "adminalice")
|
|
msgList = addMEPair(msgList, "-Admin😀Alice-", "adminalice")
|
|
msgList = addMEPair(msgList, "u", "u")
|
|
msgList = addMEPair(msgList, "", "untitled")
|
|
msgList = addMEPair(msgList, " ", "untitled")
|
|
msgList = addMEPair(msgList, "-", "untitled")
|
|
msgList = addMEPair(msgList, "--", "untitled")
|
|
msgList = addMEPair(msgList, "é", "é")
|
|
msgList = addMEPair(msgList, "-é-", "é")
|
|
|
|
for _, item := range msgList {
|
|
t.Log("Testing string '" + item.Msg + "'")
|
|
res = nameToSlug(item.Msg)
|
|
if res != item.Expects {
|
|
t.Error("Bad output:", "'"+res+"'")
|
|
t.Error("Expected:", item.Expects)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAuth(t *testing.T) {
|
|
// bcrypt likes doing stupid things, so this test will probably fail
|
|
var realPassword string
|
|
var hashedPassword string
|
|
var password string
|
|
var salt string
|
|
var err error
|
|
|
|
/* No extra salt tests, we might not need this extra salt, as bcrypt has it's own? */
|
|
realPassword = "Madame Cassandra's Mystic Orb"
|
|
t.Log("Set real_password to '" + realPassword + "'")
|
|
t.Log("Hashing the real password")
|
|
hashedPassword, err = BcryptGeneratePasswordNoSalt(realPassword)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
password = realPassword
|
|
t.Log("Testing password '" + password + "'")
|
|
t.Log("Testing salt '" + salt + "'")
|
|
err = CheckPassword(hashedPassword, password, salt)
|
|
if err == ErrMismatchedHashAndPassword {
|
|
t.Error("The two don't match")
|
|
} else if err == ErrPasswordTooLong {
|
|
t.Error("CheckPassword thinks the password is too long")
|
|
} else if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
password = "hahaha"
|
|
t.Log("Testing password '" + password + "'")
|
|
t.Log("Testing salt '" + salt + "'")
|
|
err = CheckPassword(hashedPassword, password, salt)
|
|
if err == ErrPasswordTooLong {
|
|
t.Error("CheckPassword thinks the password is too long")
|
|
} else if err == nil {
|
|
t.Error("The two shouldn't match!")
|
|
}
|
|
|
|
password = "Madame Cassandra's Mystic"
|
|
t.Log("Testing password '" + password + "'")
|
|
t.Log("Testing salt '" + salt + "'")
|
|
err = CheckPassword(hashedPassword, password, salt)
|
|
if err == ErrPasswordTooLong {
|
|
t.Error("CheckPassword thinks the password is too long")
|
|
} else if err == nil {
|
|
t.Error("The two shouldn't match!")
|
|
}
|
|
}
|