2024-11-03 01:34:28 +01:00
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
2024-11-03 16:13:59 +01:00
|
|
|
"bytes"
|
2024-11-03 01:34:28 +01:00
|
|
|
"context"
|
2024-11-03 17:25:43 +01:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2024-11-03 01:34:28 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
nats "github.com/nats-io/nats.go"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
|
|
|
"git.defalsify.org/vise.git/db"
|
|
|
|
memdb "git.defalsify.org/vise.git/db/mem"
|
|
|
|
"git.grassecon.net/urdt/ussd/common"
|
|
|
|
"git.grassecon.net/urdt/ussd/config"
|
|
|
|
"git.grassecon.net/urdt/ussd/models"
|
2024-11-03 15:35:37 +01:00
|
|
|
"git.grassecon.net/term/lookup"
|
2024-11-03 16:37:46 +01:00
|
|
|
"git.grassecon.net/term/event"
|
2024-11-03 23:25:51 +01:00
|
|
|
"git.grassecon.net/term/internal/testutil"
|
2024-11-03 01:34:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-11-03 17:25:43 +01:00
|
|
|
txBlock = 42
|
|
|
|
tokenAddress = "0x765DE816845861e75A25fCA122bb6898B8B1282a"
|
|
|
|
tokenSymbol = "FOO"
|
|
|
|
tokenName = "Foo Token"
|
|
|
|
tokenDecimals = 6
|
|
|
|
txValue = 1337
|
|
|
|
tokenBalance = 362436
|
|
|
|
txTimestamp = 1730592500
|
|
|
|
txHash = "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
|
|
|
|
sinkAddress = "0xb42C5920014eE152F2225285219407938469BBfA"
|
2024-11-03 01:34:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: jetstream, would have been nice of you to provide an easier way to make a mock msg
|
|
|
|
type testMsg struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) Ack() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) Nak() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) NakWithDelay(time.Duration) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) Data() []byte {
|
|
|
|
return m.data
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) Reply() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) Subject() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) Term() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) TermWithReason(string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) DoubleAck(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) Headers() nats.Header {
|
|
|
|
return nats.Header{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) InProgress() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func(m *testMsg) Metadata() (*jetstream.MsgMetadata, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-11-03 23:25:51 +01:00
|
|
|
func TestHandleMsg(t *testing.T) {
|
|
|
|
err := config.LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-11-03 01:34:28 +01:00
|
|
|
|
2024-11-03 23:25:51 +01:00
|
|
|
api := &testutil.MockApi{}
|
|
|
|
api.TransactionsContent = []dataserviceapi.Last10TxResponse{
|
2024-11-03 16:13:59 +01:00
|
|
|
dataserviceapi.Last10TxResponse{
|
2024-11-03 23:25:51 +01:00
|
|
|
Sender: testutil.AliceChecksum,
|
|
|
|
Recipient: testutil.BobChecksum,
|
2024-11-03 17:25:43 +01:00
|
|
|
TransferValue: strconv.Itoa(txValue),
|
|
|
|
ContractAddress: tokenAddress,
|
|
|
|
TxHash: txHash,
|
|
|
|
DateBlock: time.Unix(txTimestamp, 0),
|
|
|
|
TokenSymbol: tokenSymbol,
|
|
|
|
TokenDecimals: strconv.Itoa(tokenDecimals),
|
2024-11-03 16:13:59 +01:00
|
|
|
},
|
2024-11-03 23:25:51 +01:00
|
|
|
}
|
|
|
|
api.VoucherDataContent = &models.VoucherDataResult{
|
2024-11-03 17:25:43 +01:00
|
|
|
TokenSymbol: tokenSymbol,
|
|
|
|
TokenName: tokenName,
|
|
|
|
TokenDecimals: strconv.Itoa(tokenDecimals),
|
|
|
|
SinkAddress: sinkAddress,
|
2024-11-03 01:34:28 +01:00
|
|
|
}
|
2024-11-03 23:25:51 +01:00
|
|
|
api.VouchersContent = []dataserviceapi.TokenHoldings{
|
|
|
|
dataserviceapi.TokenHoldings{
|
|
|
|
ContractAddress: tokenAddress,
|
|
|
|
TokenSymbol: tokenSymbol,
|
|
|
|
TokenDecimals: strconv.Itoa(tokenDecimals),
|
|
|
|
Balance: strconv.Itoa(tokenBalance),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
lookup.Api = api
|
2024-11-03 01:34:28 +01:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
userDb := memdb.NewMemDb()
|
|
|
|
err = userDb.Connect(ctx, "")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-11-03 23:25:51 +01:00
|
|
|
alice, err := common.NormalizeHex(testutil.AliceChecksum)
|
2024-11-03 01:34:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userDb.SetSession(alice)
|
|
|
|
userDb.SetPrefix(db.DATATYPE_USERDATA)
|
2024-11-03 23:25:51 +01:00
|
|
|
err = userDb.Put(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(testutil.AliceSession))
|
2024-11-03 01:34:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-11-03 23:25:51 +01:00
|
|
|
|
2024-11-03 01:34:28 +01:00
|
|
|
sub := NewNatsSubscription(userDb)
|
|
|
|
|
2024-11-03 17:25:43 +01:00
|
|
|
data := fmt.Sprintf(`{
|
|
|
|
"block": %d,
|
|
|
|
"contractAddress": "%s",
|
2024-11-03 01:34:28 +01:00
|
|
|
"success": true,
|
2024-11-03 17:25:43 +01:00
|
|
|
"timestamp": %d,
|
|
|
|
"transactionHash": "%s",
|
2024-11-03 01:34:28 +01:00
|
|
|
"transactionType": "TOKEN_TRANSFER",
|
|
|
|
"payload": {
|
2024-11-03 17:25:43 +01:00
|
|
|
"from": "%s",
|
|
|
|
"to": "%s",
|
|
|
|
"value": "%d"
|
2024-11-03 01:34:28 +01:00
|
|
|
}
|
2024-11-03 23:25:51 +01:00
|
|
|
}`, txBlock, tokenAddress, txTimestamp, txHash, testutil.AliceChecksum, testutil.BobChecksum, txValue)
|
2024-11-03 01:34:28 +01:00
|
|
|
msg := &testMsg{
|
|
|
|
data: []byte(data),
|
|
|
|
}
|
|
|
|
sub.handleEvent(msg)
|
2024-11-03 16:13:59 +01:00
|
|
|
|
|
|
|
store := common.UserDataStore{
|
|
|
|
Db: userDb,
|
|
|
|
}
|
2024-11-03 23:25:51 +01:00
|
|
|
v, err := store.ReadEntry(ctx, testutil.AliceSession, common.DATA_ACTIVE_SYM)
|
2024-11-03 16:13:59 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-11-03 17:25:43 +01:00
|
|
|
if !bytes.Equal(v, []byte(tokenSymbol)) {
|
|
|
|
t.Fatalf("expected '%s', got %s", tokenSymbol, v)
|
2024-11-03 16:13:59 +01:00
|
|
|
}
|
|
|
|
|
2024-11-03 23:25:51 +01:00
|
|
|
v, err = store.ReadEntry(ctx, testutil.AliceSession, common.DATA_ACTIVE_BAL)
|
2024-11-03 16:13:59 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-11-03 17:25:43 +01:00
|
|
|
if !bytes.Equal(v, []byte(strconv.Itoa(tokenBalance))) {
|
|
|
|
t.Fatalf("expected '%d', got %s", tokenBalance, v)
|
2024-11-03 16:13:59 +01:00
|
|
|
}
|
|
|
|
|
2024-11-03 23:25:51 +01:00
|
|
|
v, err = store.ReadEntry(ctx, testutil.AliceSession, common.DATA_TRANSACTIONS)
|
2024-11-03 16:13:59 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-11-03 16:37:46 +01:00
|
|
|
if !bytes.Contains(v, []byte("abcdef")) {
|
2024-11-03 16:13:59 +01:00
|
|
|
t.Fatal("no transaction data")
|
|
|
|
}
|
2024-11-03 16:37:46 +01:00
|
|
|
|
|
|
|
userDb.SetPrefix(event.DATATYPE_USERSUB)
|
2024-11-03 23:25:51 +01:00
|
|
|
userDb.SetSession(testutil.AliceSession)
|
2024-11-03 16:37:46 +01:00
|
|
|
k := append([]byte("vouchers"), []byte("sym")...)
|
|
|
|
v, err = userDb.Get(ctx, k)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-11-03 17:25:43 +01:00
|
|
|
if !bytes.Contains(v, []byte(fmt.Sprintf("1:%s", tokenSymbol))) {
|
|
|
|
t.Fatalf("expected '1:%s', got %s", tokenSymbol, v)
|
2024-11-03 16:37:46 +01:00
|
|
|
}
|
2024-11-03 01:34:28 +01:00
|
|
|
}
|