This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
JISQueueing/db/staff_test.go

155 lines
4.4 KiB
Go

package db_test
import (
"JISQueueing/common"
"JISQueueing/db"
"testing"
)
func TestRegister(t *testing.T) {
db.UseInMemoryDatabase()
staff := exampleStaffUser()
result := db.Register(staff, "test123")
if result != db.RegisterSuccessful {
t.Errorf("Registration should be successful. Got: %d", result)
}
result = db.Register(staff, "test123")
if result != db.UsernameAlreadyExists {
t.Errorf("Registration with the same name should fail. Got: %d", result)
}
result = db.Register(staff, "test456")
if result != db.UsernameAlreadyExists {
t.Errorf("Registration with the same name should fail. Got: %d", result)
}
result = db.Register(staff, "test123")
if result != db.UsernameAlreadyExists {
t.Errorf("Registration with the same name should fail. Got: %d", result)
}
token, _ := db.Login(staff.Username, "test123")
result = db.Register(staff, "test123")
if result != db.UsernameAlreadyExists {
t.Errorf("Registration with same username should fail. Got: %d", result)
}
if !db.VerifyToken(token) {
t.Error("Registering a logged-in user logs the user out!")
}
db.Logout(token)
}
func TestLogin(t *testing.T) {
db.UseInMemoryDatabase()
staff1 := exampleStaffUser()
staff2 := exampleStaffUser2()
staff3 := exampleStaffUser3()
db.Register(staff1, "test123")
db.Register(staff2, "test456")
token1, result := db.Login(staff1.Username, "test123")
if result != db.LoginSuccessful {
t.Errorf("Login should be successful. Got: %d", result)
}
token2, result := db.Login(staff2.Username, "test456")
if result != db.LoginSuccessful {
t.Errorf("Login should be successful. Got: %d", result)
}
_, result = db.Login(staff1.Username, "test456")
if result != db.InvalidPassword {
t.Errorf("Password should be invalid. Got: %d", result)
}
_, result = db.Login(staff3.Username, "test123")
if result != db.InvalidUser {
t.Errorf("User should be invalid. Got: %d", result)
}
db.Register(staff3, "test789")
token3, result := db.Login(staff3.Username, "test789")
if result != db.LoginSuccessful {
t.Errorf("Login should be successful. Got: %d", result)
}
newToken1, result := db.Login(staff1.Username, "test123")
if result != db.ReloginRequest {
t.Errorf("DB should acknowledge login as a relogin. Got %d instead.", result)
}
if newToken1 != token1 {
t.Errorf("Old token doesn't match new token! old=%s, new=%s", token1, newToken1)
}
newToken2, result := db.Login(staff2.Username, "test456")
if result != db.ReloginRequest {
t.Errorf("DB should acknowledge login as a relogin. Got %d instead.", result)
}
if newToken2 != token2 {
t.Errorf("Old token doesn't match new token! old=%s, new=%s", token2, newToken2)
}
newToken3, result := db.Login(staff3.Username, "test789")
if result != db.ReloginRequest {
t.Errorf("DB should acknowledge login as a relogin. Got %d instead.", result)
}
if newToken3 != token3 {
t.Errorf("Old token doesn't match new token! old=%s, new=%s", token3, newToken3)
}
// Logout everyone and make sure they are no longer acknowledged as relogin requests.
if !db.Logout(token1) {
t.Errorf("Failed to log out a valid token!")
}
if !db.Logout(token2) {
t.Errorf("Failed to log out a valid token!")
}
if !db.Logout(token3) {
t.Errorf("Failed to log out a valid token!")
}
token1, result = db.Login(staff1.Username, "test123")
if result != db.LoginSuccessful {
t.Errorf("Login should be successful. Got: %d", result)
}
token2, result = db.Login(staff2.Username, "test456")
if result != db.LoginSuccessful {
t.Errorf("Login should be successful. Got: %d", result)
}
token3, result = db.Login(staff3.Username, "test789")
if result != db.LoginSuccessful {
t.Errorf("Login should be successful. Got: %d", result)
}
// Logout everyone again so we have a consistent token cache.
if !db.Logout(token1) {
t.Errorf("Failed to log out a valid token!")
}
if !db.Logout(token2) {
t.Errorf("Failed to log out a valid token!")
}
if !db.Logout(token3) {
t.Errorf("Failed to log out a valid token!")
}
}
func exampleStaffUser() common.Staff {
return common.Staff{
Username: "test",
Name: "Test Member",
Email: "test@example.com",
Admin: false,
}
}
func exampleStaffUser2() common.Staff {
return common.Staff{
Username: "test2",
Name: "Test Member2",
Email: "test2@example.com",
Admin: false,
}
}
func exampleStaffUser3() common.Staff {
return common.Staff {
Username: "test3",
Name: "Test Member3",
Email: "test3@example.com",
Admin: false,
}
}