Compare commits

..

No commits in common. "a17150962e91650300dba046d21c6fc68e3b1d20" and "bab3f673ebb028d4ad2c174aa7f86a6721b80067" have entirely different histories.

6 changed files with 124 additions and 29 deletions

View File

@ -557,16 +557,17 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
if err != nil {
return res, err
}
r, err := h.accountService.TrackAccountStatus(ctx, string(publicKey))
okResponse, err = h.accountService.TrackAccountStatus(ctx, string(publicKey))
if err != nil {
res.FlagSet = append(res.FlagSet, flag_api_error)
return res, err
}
res.FlagReset = append(res.FlagReset, flag_api_error)
isActive := okResponse.Result["active"].(bool)
if !ok {
return res, err
}
if r.Active {
if isActive {
res.FlagSet = append(res.FlagSet, flag_account_success)
res.FlagReset = append(res.FlagReset, flag_account_pending)
} else {

View File

@ -7,6 +7,7 @@ import (
"log"
"path"
"testing"
"time"
"git.defalsify.org/vise.git/asm"
"git.defalsify.org/vise.git/db"
@ -20,7 +21,7 @@ import (
"git.grassecon.net/urdt/ussd/common"
"github.com/alecthomas/assert/v2"
// "github.com/grassrootseconomics/eth-custodial/pkg/api"
"github.com/grassrootseconomics/eth-custodial/pkg/api"
testdataloader "github.com/peteole/testdata-loader"
"github.com/stretchr/testify/require"
@ -1048,23 +1049,49 @@ func TestCheckAccountStatus(t *testing.T) {
tests := []struct {
name string
input []byte
//serverResponse *api.OKResponse
serverResponse *api.OKResponse
response *models.TrackStatusResult
expectedResult resource.Result
}{
{
name: "Test when account is on the Sarafu network",
input: []byte("TrackingId1234"),
serverResponse: &api.OKResponse{
Ok: true,
Description: "Account creation succeeded",
Result: map[string]any{
"active": true,
},
},
response: &models.TrackStatusResult {
CreatedAt: time.Now(),
Status: "SUCCESS",
TransferValue: json.Number("0.5"),
TxHash: "0x123abc456def",
TxType: "transfer",
},
expectedResult: resource.Result{
FlagSet: []uint32{flag_account_success},
FlagReset: []uint32{flag_api_error, flag_account_pending},
},
},
{
name: "Test when the account is not yet on the sarafu network",
input: []byte("TrackingId1234"),
response: &models.TrackStatusResult{
Active: false,
CreatedAt: time.Now(),
Status: "SUCCESS",
TransferValue: json.Number("0.5"),
TxHash: "0x123abc456def",
TxType: "transfer",
},
serverResponse: &api.OKResponse{
Ok: true,
Description: "Account creation succeeded",
Result: map[string]any{
"active": false,
},
},
// serverResponse: &api.OKResponse{
// Ok: true,
// Description: "Account creation succeeded",
// Result: map[string]any{
// "active": false,
// },
// },
expectedResult: resource.Result{
FlagSet: []uint32{flag_account_pending},
FlagReset: []uint32{flag_api_error, flag_account_success},
@ -1082,13 +1109,13 @@ func TestCheckAccountStatus(t *testing.T) {
flagManager: fm.parser,
}
status := tt.response.Active
status := tt.response.Status
// Define expected interactions with the mock
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_PUBLIC_KEY).Return(tt.input, nil)
//mockCreateAccountService.On("TrackAccountStatus", string(tt.input)).Return(tt.serverResponse, nil)
mockCreateAccountService.On("TrackAccountStatus", string(tt.input)).Return(tt.response, nil)
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACCOUNT_STATUS, status).Return(nil).Maybe()
mockCreateAccountService.On("CheckAccountStatus", string(tt.input)).Return(tt.response, nil)
mockCreateAccountService.On("TrackAccountStatus", string(tt.input)).Return(tt.serverResponse, nil)
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACCOUNT_STATUS, []byte(status)).Return(nil).Maybe()
// Call the method under test
res, _ := h.CheckAccountStatus(ctx, "check_account_status", tt.input)

View File

@ -5,14 +5,10 @@ import (
"time"
)
type Transaction struct {
type TrackStatusResult struct {
CreatedAt time.Time `json:"createdAt"`
Status string `json:"status"`
TransferValue json.Number `json:"transferValue"`
TxHash string `json:"txHash"`
TxType string `json:"txType"`
}
type TrackStatusResult struct {
Active bool `json:"active"`
}

View File

@ -4,6 +4,7 @@ import (
"context"
"git.grassecon.net/urdt/ussd/internal/models"
"github.com/grassrootseconomics/eth-custodial/pkg/api"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
"github.com/stretchr/testify/mock"
)
@ -23,8 +24,13 @@ func (m *MockAccountService) CheckBalance(ctx context.Context, publicKey string)
return args.Get(0).(*models.BalanceResult), args.Error(1)
}
func (m *MockAccountService) TrackAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error) {
func (m *MockAccountService) TrackAccountStatus(ctx context.Context, trackingId string) (*api.OKResponse, error) {
args := m.Called(trackingId)
return args.Get(0).(*api.OKResponse), args.Error(1)
}
func (m *MockAccountService) CheckAccountStatus(ctx context.Context,publicKey string) (*models.TrackStatusResult, error) {
args := m.Called(publicKey)
return args.Get(0).(*models.TrackStatusResult), args.Error(1)
}

View File

@ -3,8 +3,10 @@ package testservice
import (
"context"
"encoding/json"
"time"
"git.grassecon.net/urdt/ussd/internal/models"
"github.com/grassrootseconomics/eth-custodial/pkg/api"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
)
@ -26,9 +28,23 @@ func (tas *TestAccountService) CheckBalance(ctx context.Context, publicKey strin
return balanceResponse, nil
}
func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) {
func (tas *TestAccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error) {
return &models.TrackStatusResult {
Active: true,
CreatedAt: time.Now(),
Status: "SUCCESS",
TransferValue: json.Number("0.5"),
TxHash: "0x123abc456def",
TxType: "transfer",
}, nil
}
func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
return &api.OKResponse{
Ok: true,
Description: "Account creation succeeded",
Result: map[string]any{
"active": true,
},
}, nil
}

View File

@ -20,7 +20,9 @@ var (
type AccountServiceInterface interface {
CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error)
CreateAccount(ctx context.Context) (*models.AccountResult, error)
TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error)
// TODO: poorly named method seems to be checking transaction
CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error)
TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error)
FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error)
}
@ -36,10 +38,10 @@ type AccountService struct {
// - string: The status of the transaction as a string. If there is an error during the request or processing, this will be an empty string.
// - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data.
// If no error occurs, this will be nil
func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) {
func (as *AccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error) {
var r models.TrackStatusResult
ep, err := url.JoinPath(config.TrackURL, publicKey)
ep, err := url.JoinPath(config.TrackURL, trackingId)
if err != nil {
return nil, err
}
@ -57,6 +59,53 @@ func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey stri
return &r, nil
}
func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
var okResponse api.OKResponse
var errResponse api.ErrResponse
var err error
ep, err := url.JoinPath(config.TrackURL, publicKey)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", ep, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-GE-KEY", "xd")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
errResponse.Description = err.Error()
return nil, err
}
if resp.StatusCode >= http.StatusBadRequest {
err := json.Unmarshal([]byte(body), &errResponse)
if err != nil {
return nil, err
}
return nil, errors.New(errResponse.Description)
}
err = json.Unmarshal([]byte(body), &okResponse)
if err != nil {
return nil, err
}
if len(okResponse.Result) == 0 {
return nil, errors.New("Empty api result")
}
return &okResponse, nil
}
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
// Parameters:
// - publicKey: The public key associated with the account whose balance needs to be checked.