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.
 
 

196 lines
5.0 KiB

package main
import (
"log"
"testing"
"git.sporestack.com/SporeStack/settlers"
)
const testAdminToken = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
const testToken = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
const testToken2 = "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
var testConfiguration = Configuration{SqliteFilename: inMemoryDatabase, AdminToken: testAdminToken, Port: ":2828"}
func TestSettlers(t *testing.T) {
db, err := dbConnect(testConfiguration)
if err != nil {
return
}
defer db.Close()
_, userErr, err := balance(testToken, db)
if err != nil {
t.Error(err.Error())
} else if userErr != settlers.ErrorTokenNotEnabled {
t.Error("Account should not be enabled.")
} else {
log.Print("Non-enabled account balance shows not enabled.")
}
depositToken := settlers.DepositToken(testToken)
userErr, err = enable(testToken, depositToken, testAdminToken, db, testConfiguration)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else {
log.Print("Enable worked.")
}
// Try to enable the same token again.
userErr, err = enable(testToken, depositToken, testAdminToken, db, testConfiguration)
if err != nil {
t.Error(err.Error())
} else if userErr != settlers.ErrorTokenAlreadyEnabled {
t.Error("Did not get TokenAlreadyEnabled error for twice enabled token.")
} else {
log.Print("Second enable for identical token errored correctly.")
}
depositToken2 := settlers.DepositToken(testToken2)
userErr, err = enable(testToken2, depositToken2, testAdminToken, db, testConfiguration)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else {
log.Print("Enable worked for token 2.")
}
amount, userErr, err := balance(testToken, db)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else if amount != startingBalance {
t.Errorf("Balance is not %d, it's %d.", startingBalance, amount)
} else {
log.Print("Balance is 0.")
}
userErr, err = add(testToken, 100, db)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else {
log.Print("Able to add 100 to token.")
}
userErr, err = subtract(testToken, 40, db)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else {
log.Print("Able to subtract 40 from token.")
}
amount, userErr, err = balance(testToken, db)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else if amount != 60 {
t.Errorf("Balance is not %d, it's %d.", 60, amount)
} else {
log.Print("Balance after subtract is correct.")
}
userErr, err = subtract(testToken, 61, db)
if err != nil {
t.Error(err.Error())
} else if userErr != settlers.ErrorInsufficientBalance {
t.Error("Should have errored insufficient balance.")
} else {
log.Print("Got insufficient balance when we were supposed to.")
}
userErr, err = depositOnlyTokenEnabled(depositToken, db)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else {
log.Print("depositTokenEnabled works.")
}
userErr, err = deposit(depositToken, 100, db)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else {
log.Print("deposit works.")
}
amount, userErr, err = balance(testToken, db)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else if amount != 160 {
t.Errorf("Balance is not %d, it's %d.", 160, amount)
} else {
log.Print("Balance is correct after deposit.")
}
// Make sure depositToken is restricted properly.
_, userErr, err = balance(depositToken, db)
if err != nil {
t.Error(err.Error())
} else if userErr != settlers.ErrorTokenNotEnabled {
t.Error(userErr.Error())
} else {
log.Print("Deposit token can't check balance.")
}
userErr, err = add(depositToken, 10, db)
if err != nil {
t.Error(err.Error())
} else if userErr != settlers.ErrorTokenNotEnabled {
t.Error(userErr.Error())
} else {
log.Print("Deposit token can't do regular add.")
}
userErr, err = subtract(depositToken, 10, db)
if err != nil {
t.Error(err.Error())
} else if userErr != settlers.ErrorTokenNotEnabled {
t.Error(userErr.Error())
} else {
log.Print("Deposit token can't do subtract.")
}
userErr, err = add(testToken2, 100, db)
if err != nil {
t.Error(err.Error())
} else if userErr != nil {
t.Error(userErr.Error())
} else {
log.Print("Able to add 100 to token 2.")
}
sumBalance, err := totalLiabilities(db)
if err != nil {
t.Error(err.Error())
} else if sumBalance != 260 {
t.Errorf("Total Balance is %d, not 260.", sumBalance)
} else {
log.Print("Total balance is correct.")
}
sumTokens, err := totalTokens(db)
if err != nil {
t.Error(err.Error())
} else if sumTokens != 2 {
t.Errorf("Total tokens is %d, not 2.", sumTokens)
} else {
log.Print("Total tokens is correct.")
}
}