2020-05-31 19:15:57 +01:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-06-02 17:44:39 +01:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-05-31 19:15:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewAccountant(t *testing.T) {
|
|
|
|
// Very basic verify here for now
|
2020-06-02 19:16:02 +01:00
|
|
|
accountant := NewAccountant()
|
2020-05-31 19:15:57 +01:00
|
|
|
if accountant == nil {
|
|
|
|
t.Error("Failed to create accountant")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountant_RegisterAccount(t *testing.T) {
|
|
|
|
|
2020-06-02 19:16:02 +01:00
|
|
|
accountant := NewAccountant()
|
2020-05-31 19:15:57 +01:00
|
|
|
|
|
|
|
// Start by making two accounts
|
|
|
|
|
2020-06-02 18:06:34 +01:00
|
|
|
namea := uuid.New().String()
|
2020-06-02 17:59:33 +01:00
|
|
|
acca, err := accountant.RegisterAccount(namea)
|
2020-05-31 19:15:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if acca.Name != namea {
|
|
|
|
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", namea, acca.Name)
|
|
|
|
}
|
|
|
|
|
2020-06-02 18:06:34 +01:00
|
|
|
nameb := uuid.New().String()
|
2020-06-02 17:59:33 +01:00
|
|
|
accb, err := accountant.RegisterAccount(nameb)
|
2020-05-31 19:15:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if accb.Name != nameb {
|
|
|
|
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify another request gets rejected
|
2020-06-02 17:59:33 +01:00
|
|
|
_, err = accountant.RegisterAccount(namea)
|
2020-05-31 19:15:57 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Duplicate account name did not produce error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
func TestAccountant_AssignGetRover(t *testing.T) {
|
2020-06-02 19:16:02 +01:00
|
|
|
accountant := NewAccountant()
|
2020-06-02 17:51:54 +01:00
|
|
|
if len(accountant.Accounts) != 0 {
|
2020-06-02 17:44:39 +01:00
|
|
|
t.Error("New accountant created with non-zero account number")
|
|
|
|
}
|
|
|
|
|
2020-06-02 18:06:34 +01:00
|
|
|
name := uuid.New().String()
|
2020-06-02 17:59:33 +01:00
|
|
|
a, err := accountant.RegisterAccount(name)
|
2020-06-02 17:44:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
inst := uuid.New()
|
|
|
|
|
2020-06-10 18:56:24 +01:00
|
|
|
err = accountant.AssignRover(a.Name, inst)
|
2020-06-02 17:44:39 +01:00
|
|
|
if err != nil {
|
2020-06-04 21:17:43 +01:00
|
|
|
t.Error("Failed to set rover for created account")
|
2020-06-10 18:56:24 +01:00
|
|
|
} else if accountant.Accounts[a.Name].Rover != inst {
|
2020-06-04 21:17:43 +01:00
|
|
|
t.Error("Rover for assigned account is incorrect")
|
2020-06-10 18:56:24 +01:00
|
|
|
} else if id, err := accountant.GetRover(a.Name); err != nil {
|
2020-06-04 21:17:43 +01:00
|
|
|
t.Error("Failed to get rover for account")
|
2020-06-03 18:40:19 +01:00
|
|
|
} else if id != inst {
|
2020-06-04 21:17:43 +01:00
|
|
|
t.Error("Fetched rover is incorrect for account")
|
2020-06-02 17:44:39 +01:00
|
|
|
}
|
|
|
|
}
|