Compare commits
No commits in common. "847b91ca9e8f2f97242f3e01c4aa321cef7fda70" and "113f1a5b341d6ce9c6563f352f0b8f9fdd6815ef" have entirely different histories.
847b91ca9e
...
113f1a5b34
@ -1,7 +1,10 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CreateAccountURL = "http://localhost:5003/api/v2/account/create"
|
CreateAccountURL = "https://custodial.sarafu.africa/api/account/create"
|
||||||
BalanceURL = "https://custodial.sarafu.africa/api/account/status/"
|
TrackStatusURL = "https://custodial.sarafu.africa/api/track/"
|
||||||
TrackURL = "http://localhost:5003/api/v2/account/status"
|
BalanceURL = "https://custodial.sarafu.africa/api/account/status/"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@ -11,16 +10,10 @@ import (
|
|||||||
"git.grassecon.net/urdt/ussd/internal/models"
|
"git.grassecon.net/urdt/ussd/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
var apiResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AccountServiceInterface interface {
|
type AccountServiceInterface interface {
|
||||||
CheckBalance(publicKey string) (*models.BalanceResponse, error)
|
CheckBalance(publicKey string) (*models.BalanceResponse, error)
|
||||||
CreateAccount() (*OKResponse, *ErrResponse)
|
CreateAccount() (*models.AccountResponse, error)
|
||||||
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error)
|
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error)
|
||||||
TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountService struct {
|
type AccountService struct {
|
||||||
@ -29,6 +22,8 @@ type AccountService struct {
|
|||||||
type TestAccountService struct {
|
type TestAccountService struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckAccountStatus retrieves the status of an account transaction based on the provided tracking ID.
|
||||||
|
//
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// - trackingId: A unique identifier for the account.This should be obtained from a previous call to
|
// - trackingId: A unique identifier for the account.This should be obtained from a previous call to
|
||||||
// CreateAccount or a similar function that returns an AccountResponse. The `trackingId` field in the
|
// CreateAccount or a similar function that returns an AccountResponse. The `trackingId` field in the
|
||||||
@ -37,9 +32,9 @@ type TestAccountService struct {
|
|||||||
// Returns:
|
// Returns:
|
||||||
// - 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.
|
// - 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.
|
// - 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
|
// If no error occurs, this will be nil.
|
||||||
func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) {
|
func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) {
|
||||||
resp, err := http.Get(config.BalanceURL + trackingId)
|
resp, err := http.Get(config.TrackStatusURL + trackingId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -49,67 +44,12 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackSt
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var trackResp models.TrackStatusResponse
|
var trackResp models.TrackStatusResponse
|
||||||
err = json.Unmarshal(body, &trackResp)
|
err = json.Unmarshal(body, &trackResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &trackResp, nil
|
return &trackResp, nil
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (as *AccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) {
|
|
||||||
var errResponse ErrResponse
|
|
||||||
var okResponse OKResponse
|
|
||||||
var err error
|
|
||||||
// Construct the URL with the path parameter
|
|
||||||
url := fmt.Sprintf("%s/%s", config.TrackURL, publicKey)
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
errResponse.Description = err.Error()
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
// Set headers
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("X-GE-KEY", "xd")
|
|
||||||
|
|
||||||
// Send the request
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
errResponse.Description = err.Error()
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// Read the response body
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
errResponse.Description = err.Error()
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 2: Unmarshal into the generic struct
|
|
||||||
err = json.Unmarshal([]byte(body), &apiResponse)
|
|
||||||
if err != nil {
|
|
||||||
errResponse.Description = err.Error()
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
if apiResponse.Ok {
|
|
||||||
err = json.Unmarshal([]byte(body), &okResponse)
|
|
||||||
if err != nil {
|
|
||||||
errResponse.Description = err.Error()
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
return &okResponse, nil
|
|
||||||
} else {
|
|
||||||
err := json.Unmarshal([]byte(body), &errResponse)
|
|
||||||
if err != nil {
|
|
||||||
errResponse.Description = err.Error()
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
||||||
@ -139,52 +79,22 @@ func (as *AccountService) CheckBalance(publicKey string) (*models.BalanceRespons
|
|||||||
// If there is an error during the request or processing, this will be nil.
|
// If there is an error during the request or processing, this will be nil.
|
||||||
// - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data.
|
// - 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.
|
// If no error occurs, this will be nil.
|
||||||
func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) {
|
func (as *AccountService) CreateAccount() (*models.AccountResponse, error) {
|
||||||
|
resp, err := http.Post(config.CreateAccountURL, "application/json", nil)
|
||||||
var errResponse ErrResponse
|
|
||||||
var okResponse OKResponse
|
|
||||||
var err error
|
|
||||||
|
|
||||||
// Create a new request
|
|
||||||
req, err := http.NewRequest("POST", config.CreateAccountURL, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errResponse.Description = err.Error()
|
return nil, err
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("X-GE-KEY", "xd")
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
errResponse.Description = err.Error()
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errResponse.Description = err.Error()
|
return nil, err
|
||||||
return nil, &errResponse
|
|
||||||
}
|
}
|
||||||
err = json.Unmarshal([]byte(body), &apiResponse)
|
var accountResp models.AccountResponse
|
||||||
|
err = json.Unmarshal(body, &accountResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &errResponse
|
return nil, err
|
||||||
}
|
|
||||||
if apiResponse.Ok {
|
|
||||||
err = json.Unmarshal([]byte(body), &okResponse)
|
|
||||||
if err != nil {
|
|
||||||
errResponse.Description = err.Error()
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
return &okResponse, nil
|
|
||||||
} else {
|
|
||||||
err := json.Unmarshal([]byte(body), &errResponse)
|
|
||||||
if err != nil {
|
|
||||||
errResponse.Description = err.Error()
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
|
||||||
return nil, &errResponse
|
|
||||||
}
|
}
|
||||||
|
return &accountResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) CreateAccount() (*models.AccountResponse, error) {
|
func (tas *TestAccountService) CreateAccount() (*models.AccountResponse, error) {
|
||||||
|
|||||||
@ -1,60 +0,0 @@
|
|||||||
package server
|
|
||||||
|
|
||||||
type (
|
|
||||||
OKResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
Result map[string]any `json:"result"`
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
ErrCode string `json:"errorCode"`
|
|
||||||
}
|
|
||||||
|
|
||||||
TransferRequest struct {
|
|
||||||
From string `json:"from" validate:"required,eth_addr_checksum"`
|
|
||||||
To string `json:"to" validate:"required,eth_addr_checksum"`
|
|
||||||
TokenAddress string `json:"tokenAddress" validate:"required,eth_addr_checksum"`
|
|
||||||
Amount string `json:"amount" validate:"required,number,gt=0"`
|
|
||||||
}
|
|
||||||
|
|
||||||
PoolSwapRequest struct {
|
|
||||||
From string `json:"from" validate:"required,eth_addr_checksum"`
|
|
||||||
FromTokenAddress string `json:"fromTokenAddress" validate:"required,eth_addr_checksum"`
|
|
||||||
ToTokenAddress string `json:"toTokenAddress" validate:"required,eth_addr_checksum"`
|
|
||||||
PoolAddress string `json:"poolAddress" validate:"required,eth_addr_checksum"`
|
|
||||||
Amount string `json:"amount" validate:"required,number,gt=0"`
|
|
||||||
}
|
|
||||||
|
|
||||||
PoolDepositRequest struct {
|
|
||||||
From string `json:"from" validate:"required,eth_addr_checksum"`
|
|
||||||
TokenAddress string `json:"tokenAddress" validate:"required,eth_addr_checksum"`
|
|
||||||
PoolAddress string `json:"poolAddress" validate:"required,eth_addr_checksum"`
|
|
||||||
Amount string `json:"amount" validate:"required,number,gt=0"`
|
|
||||||
}
|
|
||||||
|
|
||||||
AccountAddressParam struct {
|
|
||||||
Address string `param:"address" validate:"required,eth_addr_checksum"`
|
|
||||||
}
|
|
||||||
|
|
||||||
TrackingIDParam struct {
|
|
||||||
TrackingID string `param:"trackingId" validate:"required,uuid"`
|
|
||||||
}
|
|
||||||
|
|
||||||
OTXByAccountRequest struct {
|
|
||||||
Address string `param:"address" validate:"required,eth_addr_checksum"`
|
|
||||||
PerPage int `query:"perPage" validate:"required,number,gt=0"`
|
|
||||||
Cursor int `query:"cursor" validate:"number"`
|
|
||||||
Next bool `query:"next"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ErrCodeInternalServerError = "E01"
|
|
||||||
ErrCodeInvalidJSON = "E02"
|
|
||||||
ErrCodeInvalidAPIKey = "E03"
|
|
||||||
ErrCodeValidationFailed = "E04"
|
|
||||||
ErrCodeAccountNotExists = "E05"
|
|
||||||
)
|
|
||||||
@ -27,8 +27,6 @@ var (
|
|||||||
logg = logging.NewVanilla().WithDomain("ussdmenuhandler")
|
logg = logging.NewVanilla().WithDomain("ussdmenuhandler")
|
||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
translationDir = path.Join(scriptDir, "locale")
|
translationDir = path.Join(scriptDir, "locale")
|
||||||
okResponse *server.OKResponse
|
|
||||||
errResponse *server.ErrResponse
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// FlagManager handles centralized flag management
|
// FlagManager handles centralized flag management
|
||||||
@ -138,16 +136,11 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
|
func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
|
||||||
okResponse, errResponse := h.accountService.CreateAccount()
|
accountResp, err := h.accountService.CreateAccount()
|
||||||
if errResponse != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
trackingId := okResponse.Result["trackingId"].(string)
|
|
||||||
publicKey := okResponse.Result["publicKey"].(string)
|
|
||||||
|
|
||||||
data := map[utils.DataTyp]string{
|
data := map[utils.DataTyp]string{
|
||||||
utils.DATA_TRACKING_ID: trackingId,
|
utils.DATA_TRACKING_ID: accountResp.Result.TrackingId,
|
||||||
utils.DATA_PUBLIC_KEY: publicKey,
|
utils.DATA_PUBLIC_KEY: accountResp.Result.PublicKey,
|
||||||
|
utils.DATA_CUSTODIAL_ID: accountResp.Result.CustodialId.String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range data {
|
for key, value := range data {
|
||||||
@ -159,7 +152,7 @@ func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, r
|
|||||||
}
|
}
|
||||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||||
res.FlagSet = append(res.FlagSet, flag_account_created)
|
res.FlagSet = append(res.FlagSet, flag_account_created)
|
||||||
return nil
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,6 +191,7 @@ func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resou
|
|||||||
}
|
}
|
||||||
|
|
||||||
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
||||||
|
|
||||||
accountPIN := string(input)
|
accountPIN := string(input)
|
||||||
// Validate that the PIN is a 4-digit number
|
// Validate that the PIN is a 4-digit number
|
||||||
if !isValidPIN(accountPIN) {
|
if !isValidPIN(accountPIN) {
|
||||||
@ -374,6 +368,7 @@ func (h *Handlers) SaveYob(ctx context.Context, sym string, input []byte) (resou
|
|||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(input) == 4 {
|
if len(input) == 4 {
|
||||||
yob := string(input)
|
yob := string(input)
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
@ -416,6 +411,7 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re
|
|||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
gender := strings.Split(symbol, "_")[1]
|
gender := strings.Split(symbol, "_")[1]
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
err = store.WriteEntry(ctx, sessionId, utils.DATA_GENDER, []byte(gender))
|
err = store.WriteEntry(ctx, sessionId, utils.DATA_GENDER, []byte(gender))
|
||||||
@ -434,6 +430,7 @@ func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte)
|
|||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(input) > 0 {
|
if len(input) > 0 {
|
||||||
offerings := string(input)
|
offerings := string(input)
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
@ -459,6 +456,7 @@ func (h *Handlers) ResetAllowUpdate(ctx context.Context, sym string, input []byt
|
|||||||
// ResetAccountAuthorized resets the account authorization flag after a successful PIN entry.
|
// ResetAccountAuthorized resets the account authorization flag after a successful PIN entry.
|
||||||
func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
|
|
||||||
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
||||||
|
|
||||||
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||||
@ -468,10 +466,12 @@ func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input
|
|||||||
// CheckIdentifier retrieves the PublicKey from the JSON data file.
|
// CheckIdentifier retrieves the PublicKey from the JSON data file.
|
||||||
func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
|
|
||||||
sessionId, ok := ctx.Value("SessionId").(string)
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||||
|
|
||||||
@ -485,10 +485,12 @@ func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte
|
|||||||
func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
sessionId, ok := ctx.Value("SessionId").(string)
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
||||||
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
||||||
flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update")
|
flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update")
|
||||||
@ -540,20 +542,28 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
|||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
trackingId, err := store.ReadEntry(ctx, sessionId, utils.DATA_TRACKING_ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
okResponse, errResponse = h.accountService.TrackAccountStatus(string(publicKey))
|
|
||||||
if errResponse != nil {
|
accountStatus, err := h.accountService.CheckAccountStatus(string(trackingId))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error checking account status:", err)
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
if !accountStatus.Ok {
|
||||||
|
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||||
isActive := okResponse.Result["active"].(bool)
|
status := accountStatus.Result.Transaction.Status
|
||||||
if !ok {
|
|
||||||
return res, err
|
err = store.WriteEntry(ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(status))
|
||||||
|
if err != nil {
|
||||||
|
return res, nil
|
||||||
}
|
}
|
||||||
if isActive {
|
if accountStatus.Result.Transaction.Status == "SUCCESS" {
|
||||||
res.FlagSet = append(res.FlagSet, flag_account_success)
|
res.FlagSet = append(res.FlagSet, flag_account_success)
|
||||||
res.FlagReset = append(res.FlagReset, flag_account_pending)
|
res.FlagReset = append(res.FlagReset, flag_account_pending)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -72,75 +72,68 @@ func TestCreateAccount(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create required mocks
|
// Create required mocks
|
||||||
flag_account_created, err := fm.GetFlag("flag_account_created")
|
mockDataStore := new(mocks.MockUserDataStore)
|
||||||
|
mockCreateAccountService := new(mocks.MockAccountService)
|
||||||
|
expectedResult := resource.Result{}
|
||||||
|
accountCreatedFlag, err := fm.GetFlag("flag_account_created")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
|
expectedResult.FlagSet = append(expectedResult.FlagSet, accountCreatedFlag)
|
||||||
|
|
||||||
// Define session ID and mock data
|
// Define session ID and mock data
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
notFoundErr := db.ErrNotFound{}
|
typ := utils.DATA_ACCOUNT_CREATED
|
||||||
|
fakeError := db.ErrNotFound{}
|
||||||
|
// Create context with session ID
|
||||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||||
|
|
||||||
tests := []struct {
|
// Define expected interactions with the mock
|
||||||
name string
|
mockDataStore.On("ReadEntry", ctx, sessionId, typ).Return([]byte("123"), fakeError)
|
||||||
serverResponse *server.OKResponse
|
expectedAccountResp := &models.AccountResponse{
|
||||||
expectedResult resource.Result
|
Ok: true,
|
||||||
}{
|
Result: struct {
|
||||||
{
|
CustodialId json.Number `json:"custodialId"`
|
||||||
name: "Test account creation success",
|
PublicKey string `json:"publicKey"`
|
||||||
serverResponse: &server.OKResponse{
|
TrackingId string `json:"trackingId"`
|
||||||
Ok: true,
|
}{
|
||||||
Description: "Account creation successed",
|
CustodialId: "12",
|
||||||
Result: map[string]any{
|
PublicKey: "0x8E0XSCSVA",
|
||||||
"trackingId": "1234567890",
|
TrackingId: "d95a7e83-196c-4fd0-866fSGAGA",
|
||||||
"publicKey": "1235QERYU",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagSet: []uint32{flag_account_created},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
mockCreateAccountService.On("CreateAccount").Return(expectedAccountResp, nil)
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
data := map[utils.DataTyp]string{
|
||||||
|
utils.DATA_TRACKING_ID: expectedAccountResp.Result.TrackingId,
|
||||||
mockDataStore := new(mocks.MockUserDataStore)
|
utils.DATA_PUBLIC_KEY: expectedAccountResp.Result.PublicKey,
|
||||||
mockCreateAccountService := new(mocks.MockAccountService)
|
utils.DATA_CUSTODIAL_ID: expectedAccountResp.Result.CustodialId.String(),
|
||||||
|
|
||||||
// Create a Handlers instance with the mock data store
|
|
||||||
h := &Handlers{
|
|
||||||
userdataStore: mockDataStore,
|
|
||||||
accountService: mockCreateAccountService,
|
|
||||||
flagManager: fm.parser,
|
|
||||||
}
|
|
||||||
|
|
||||||
data := map[utils.DataTyp]string{
|
|
||||||
utils.DATA_TRACKING_ID: tt.serverResponse.Result["trackingId"].(string),
|
|
||||||
utils.DATA_PUBLIC_KEY: tt.serverResponse.Result["publicKey"].(string),
|
|
||||||
}
|
|
||||||
|
|
||||||
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACCOUNT_CREATED).Return([]byte(""), notFoundErr)
|
|
||||||
mockCreateAccountService.On("CreateAccount").Return(tt.serverResponse, nil)
|
|
||||||
|
|
||||||
for key, value := range data {
|
|
||||||
mockDataStore.On("WriteEntry", ctx, sessionId, key, []byte(value)).Return(nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call the method you want to test
|
|
||||||
res, err := h.CreateAccount(ctx, "create_account", []byte("some-input"))
|
|
||||||
|
|
||||||
// Assert that no errors occurred
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
// Assert that the account created flag has been set to the result
|
|
||||||
assert.Equal(t, res, tt.expectedResult, "Expected result should be equal to the actual result")
|
|
||||||
|
|
||||||
// Assert that expectations were met
|
|
||||||
mockDataStore.AssertExpectations(t)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for key, value := range data {
|
||||||
|
mockDataStore.On("WriteEntry", ctx, sessionId, key, []byte(value)).Return(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a Handlers instance with the mock data store
|
||||||
|
h := &Handlers{
|
||||||
|
userdataStore: mockDataStore,
|
||||||
|
accountService: mockCreateAccountService,
|
||||||
|
flagManager: fm.parser,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the method you want to test
|
||||||
|
res, err := h.CreateAccount(ctx, "create_account", []byte("some-input"))
|
||||||
|
|
||||||
|
// Assert that no errors occurred
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
//Assert that the account created flag has been set to the result
|
||||||
|
assert.Equal(t, res, expectedResult, "Expected result should be equal to the actual result")
|
||||||
|
|
||||||
|
// Assert that expectations were met
|
||||||
|
mockDataStore.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithPersister(t *testing.T) {
|
func TestWithPersister(t *testing.T) {
|
||||||
@ -1073,20 +1066,12 @@ func TestCheckAccountStatus(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input []byte
|
input []byte
|
||||||
serverResponse *server.OKResponse
|
|
||||||
response *models.TrackStatusResponse
|
response *models.TrackStatusResponse
|
||||||
expectedResult resource.Result
|
expectedResult resource.Result
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test when account is on the Sarafu network",
|
name: "Test when account status is Success",
|
||||||
input: []byte("TrackingId1234"),
|
input: []byte("TrackingId1234"),
|
||||||
serverResponse: &server.OKResponse{
|
|
||||||
Ok: true,
|
|
||||||
Description: "Account creation successed",
|
|
||||||
Result: map[string]any{
|
|
||||||
"active": true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
response: &models.TrackStatusResponse{
|
response: &models.TrackStatusResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Result: struct {
|
Result: struct {
|
||||||
@ -1113,7 +1098,17 @@ func TestCheckAccountStatus(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test when the account is not yet on the sarafu network",
|
name: "Test when fetching account status is not Success",
|
||||||
|
input: []byte("TrackingId1234"),
|
||||||
|
response: &models.TrackStatusResponse{
|
||||||
|
Ok: false,
|
||||||
|
},
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagSet: []uint32{flag_api_error},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test when checking account status api call is a SUCCESS but an account is not yet ready",
|
||||||
input: []byte("TrackingId1234"),
|
input: []byte("TrackingId1234"),
|
||||||
response: &models.TrackStatusResponse{
|
response: &models.TrackStatusResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
@ -1128,20 +1123,13 @@ func TestCheckAccountStatus(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
Transaction: models.Transaction{
|
Transaction: models.Transaction{
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
Status: "SUCCESS",
|
Status: "IN_NETWORK",
|
||||||
TransferValue: json.Number("0.5"),
|
TransferValue: json.Number("0.5"),
|
||||||
TxHash: "0x123abc456def",
|
TxHash: "0x123abc456def",
|
||||||
TxType: "transfer",
|
TxType: "transfer",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
serverResponse: &server.OKResponse{
|
|
||||||
Ok: true,
|
|
||||||
Description: "Account creation successed",
|
|
||||||
Result: map[string]any{
|
|
||||||
"active": false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResult: resource.Result{
|
expectedResult: resource.Result{
|
||||||
FlagSet: []uint32{flag_account_pending},
|
FlagSet: []uint32{flag_account_pending},
|
||||||
FlagReset: []uint32{flag_api_error, flag_account_success},
|
FlagReset: []uint32{flag_api_error, flag_account_success},
|
||||||
@ -1161,10 +1149,9 @@ func TestCheckAccountStatus(t *testing.T) {
|
|||||||
|
|
||||||
status := tt.response.Result.Transaction.Status
|
status := tt.response.Result.Transaction.Status
|
||||||
// Define expected interactions with the mock
|
// Define expected interactions with the mock
|
||||||
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.input, nil)
|
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TRACKING_ID).Return(tt.input, nil)
|
||||||
|
|
||||||
mockCreateAccountService.On("CheckAccountStatus", string(tt.input)).Return(tt.response, nil)
|
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, utils.DATA_ACCOUNT_STATUS, []byte(status)).Return(nil).Maybe()
|
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(status)).Return(nil).Maybe()
|
||||||
|
|
||||||
// Call the method under test
|
// Call the method under test
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package mocks
|
package mocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/models"
|
"git.grassecon.net/urdt/ussd/internal/models"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
@ -11,19 +10,9 @@ type MockAccountService struct {
|
|||||||
mock.Mock
|
mock.Mock
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) CreateAccount() (*server.OKResponse, *server.ErrResponse) {
|
func (m *MockAccountService) CreateAccount() (*models.AccountResponse, error) {
|
||||||
args := m.Called()
|
args := m.Called()
|
||||||
okResponse, ok := args.Get(0).(*server.OKResponse)
|
return args.Get(0).(*models.AccountResponse), args.Error(1)
|
||||||
errResponse, err := args.Get(1).(*server.ErrResponse)
|
|
||||||
|
|
||||||
if ok {
|
|
||||||
return okResponse, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err {
|
|
||||||
return nil, errResponse
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) {
|
func (m *MockAccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) {
|
||||||
@ -34,17 +23,4 @@ func (m *MockAccountService) CheckBalance(publicKey string) (*models.BalanceResp
|
|||||||
func (m *MockAccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) {
|
func (m *MockAccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) {
|
||||||
args := m.Called(trackingId)
|
args := m.Called(trackingId)
|
||||||
return args.Get(0).(*models.TrackStatusResponse), args.Error(1)
|
return args.Get(0).(*models.TrackStatusResponse), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) TrackAccountStatus(publicKey string) (*server.OKResponse, *server.ErrResponse) {
|
|
||||||
args := m.Called(publicKey)
|
|
||||||
okResponse, ok := args.Get(0).(*server.OKResponse)
|
|
||||||
errResponse, err := args.Get(1).(*server.ErrResponse)
|
|
||||||
if ok {
|
|
||||||
return okResponse, nil
|
|
||||||
}
|
|
||||||
if err {
|
|
||||||
return nil, errResponse
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
@ -1,10 +1,15 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
type AccountResponse struct {
|
type AccountResponse struct {
|
||||||
Ok bool `json:"ok"`
|
Ok bool `json:"ok"`
|
||||||
Description string `json:"description"` // Include the description field
|
Result struct {
|
||||||
Result struct {
|
CustodialId json.Number `json:"custodialId"`
|
||||||
PublicKey string `json:"publicKey"`
|
PublicKey string `json:"publicKey"`
|
||||||
TrackingId string `json:"trackingId"`
|
TrackingId string `json:"trackingId"`
|
||||||
} `json:"result"`
|
} `json:"result"`
|
||||||
}
|
}
|
||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Transaction struct {
|
type Transaction struct {
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user