Compare commits

..

6 Commits

9 changed files with 103 additions and 46 deletions

View File

@ -17,6 +17,7 @@ import (
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/handlers/server"
httpserver "git.grassecon.net/urdt/ussd/internal/http" httpserver "git.grassecon.net/urdt/ussd/internal/http"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
) )
@ -127,7 +128,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }
hl, err := lhs.GetHandler() accountService := server.AccountService{}
hl, err := lhs.GetHandler(&accountService)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

View File

@ -14,6 +14,7 @@ import (
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/handlers/server"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
) )
@ -95,7 +96,9 @@ func main() {
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs) lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
lhs.SetDataStore(&userdataStore) lhs.SetDataStore(&userdataStore)
hl, err := lhs.GetHandler() accountService := server.AccountService{}
hl, err := lhs.GetHandler(&accountService)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

View File

@ -16,6 +16,7 @@ import (
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/handlers/server"
httpserver "git.grassecon.net/urdt/ussd/internal/http" httpserver "git.grassecon.net/urdt/ussd/internal/http"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
) )
@ -88,7 +89,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }
hl, err := lhs.GetHandler() accountService := server.AccountService{}
hl, err := lhs.GetHandler(&accountService)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

View File

@ -11,6 +11,7 @@ import (
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/handlers/server"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
) )
@ -84,8 +85,8 @@ func main() {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
accountService := server.AccountService{}
hl, err := lhs.GetHandler() hl, err := lhs.GetHandler(&accountService)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

View File

@ -6,11 +6,11 @@ import (
"os" "os"
"path" "path"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/handlers/server"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
testdataloader "github.com/peteole/testdata-loader" testdataloader "github.com/peteole/testdata-loader"
) )
@ -21,7 +21,7 @@ var (
scriptDir = path.Join(baseDir, "services", "registration") scriptDir = path.Join(baseDir, "services", "registration")
) )
func TestEngine(sessionId string) (engine.Engine, func(), *db.Db) { func TestEngine(sessionId string) (engine.Engine, func()) {
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "SessionId", sessionId)
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
@ -76,7 +76,8 @@ func TestEngine(sessionId string) (engine.Engine, func(), *db.Db) {
os.Exit(1) os.Exit(1)
} }
hl, err := lhs.GetHandler() mockAccountService := server.MockAccountService{}
hl, err := lhs.GetHandler(&mockAccountService)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
@ -99,5 +100,5 @@ func TestEngine(sessionId string) (engine.Engine, func(), *db.Db) {
} }
//en = en.WithDebug(nil) //en = en.WithDebug(nil)
return en, cleanFn, lhs.UserdataStore return en, cleanFn
} }

View File

@ -6,6 +6,7 @@ import (
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/handlers/server"
"git.grassecon.net/urdt/ussd/internal/handlers/ussd" "git.grassecon.net/urdt/ussd/internal/handlers/ussd"
) )
@ -52,8 +53,8 @@ func (ls *LocalHandlerService) SetDataStore(db *db.Db) {
ls.UserdataStore = db ls.UserdataStore = db
} }
func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) { func (ls *LocalHandlerService) GetHandler(accountService server.AccountServiceInterface) (*ussd.Handlers, error) {
ussdHandlers, err := ussd.NewHandlers(ls.Parser, *ls.UserdataStore) ussdHandlers, err := ussd.NewHandlers(ls.Parser, *ls.UserdataStore, accountService)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -18,7 +18,8 @@ type AccountServiceInterface interface {
type AccountService struct { type AccountService struct {
} }
type MockAccountService struct {
}
// CheckAccountStatus retrieves the status of an account transaction based on the provided tracking ID. // CheckAccountStatus retrieves the status of an account transaction based on the provided tracking ID.
// //
@ -27,12 +28,10 @@ type AccountService struct {
// 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
// AccountResponse struct can be used here to check the account status during a transaction. // AccountResponse struct can be used here to check the account status during a transaction.
// //
//
// 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) (string, error) { func (as *AccountService) CheckAccountStatus(trackingId string) (string, error) {
resp, err := http.Get(config.TrackStatusURL + trackingId) resp, err := http.Get(config.TrackStatusURL + trackingId)
if err != nil { if err != nil {
@ -56,7 +55,6 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (string, error)
return status, nil return status, nil
} }
// 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.
// Parameters: // Parameters:
// - publicKey: The public key associated with the account whose balance needs to be checked. // - publicKey: The public key associated with the account whose balance needs to be checked.
@ -83,7 +81,6 @@ func (as *AccountService) CheckBalance(publicKey string) (string, error) {
return balance, nil return balance, nil
} }
// CreateAccount creates a new account in the custodial system. // CreateAccount creates a new account in the custodial system.
// Returns: // Returns:
// - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account. // - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account.
@ -110,3 +107,38 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) {
return &accountResp, nil return &accountResp, nil
} }
func (mas *MockAccountService) CreateAccount() (*models.AccountResponse, error) {
return &models.AccountResponse{
Ok: true,
Result: struct {
CustodialId json.Number `json:"custodialId"`
PublicKey string `json:"publicKey"`
TrackingId string `json:"trackingId"`
}{
CustodialId: json.Number("182"),
PublicKey: "0x48ADca309b5085852207FAaf2816eD72B52F527C",
TrackingId: "28ebe84d-b925-472c-87ae-bbdfa1fb97be",
},
}, nil
}
func (mas *MockAccountService) CheckBalance(publicKey string) (string, error) {
balanceResponse := &models.BalanceResponse{
Ok: true,
Result: struct {
Balance string `json:"balance"`
Nonce json.Number `json:"nonce"`
}{
Balance: "0.003 CELO",
Nonce: json.Number("0"),
},
}
return balanceResponse.Result.Balance, nil
}
func (mas *MockAccountService) CheckAccountStatus(trackingId string) (string, error) {
return "SUCCESS", nil
}

View File

@ -61,7 +61,7 @@ type Handlers struct {
accountService server.AccountServiceInterface accountService server.AccountServiceInterface
} }
func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db) (*Handlers, error) { func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, accountService server.AccountServiceInterface) (*Handlers, error) {
if userdataStore == nil { if userdataStore == nil {
return nil, fmt.Errorf("cannot create handler with nil userdata store") return nil, fmt.Errorf("cannot create handler with nil userdata store")
} }
@ -71,7 +71,7 @@ func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db) (*Handlers, erro
h := &Handlers{ h := &Handlers{
userdataStore: userDb, userdataStore: userDb,
flagManager: appFlags, flagManager: appFlags,
accountService: &server.AccountService{}, accountService: accountService,
} }
return h, nil return h, nil
} }

View File

@ -3,7 +3,9 @@ package main
import ( import (
"bytes" "bytes"
"context" "context"
"fmt" "log"
"math/rand"
"os"
"regexp" "regexp"
"testing" "testing"
"time" "time"
@ -15,16 +17,19 @@ import (
var ( var (
testData = driver.ReadData() testData = driver.ReadData()
testStore = ".test_state"
sessionID string sessionID string
src = rand.NewSource(42)
g = rand.New(src)
) )
// GenerateRandomSessionID generates a random UUID for the sessionID func GenerateSessionId() string {
func GenerateUUID() string { uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g))
u, err := uuid.NewV4() v, err := uu.NewV4()
if err != nil { if err != nil {
return "default_uuid" panic(err)
} }
return u.String() return v.String()
} }
// Extract the public key from the engine response // Extract the public key from the engine response
@ -39,12 +44,18 @@ func extractPublicKey(response []byte) string {
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
sessionID = GenerateUUID() sessionID = GenerateSessionId()
testStore = ".test_state"
defer func() {
if err := os.RemoveAll(testStore); err != nil {
log.Fatalf("Failed to delete state store %s: %v", testStore, err)
}
}()
m.Run() m.Run()
} }
func TestAccountCreationSuccessful(t *testing.T) { func TestAccountCreationSuccessful(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -77,8 +88,13 @@ func TestAccountCreationSuccessful(t *testing.T) {
func TestAccountRegistrationRejectTerms(t *testing.T) { func TestAccountRegistrationRejectTerms(t *testing.T) {
// Generate a new UUID for this edge case test // Generate a new UUID for this edge case test
edgeCaseSessionID := GenerateUUID() uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g))
en, fn, _ := enginetest.TestEngine(edgeCaseSessionID) v, err := uu.NewV4()
if err != nil {
t.Fail()
}
edgeCaseSessionID := v.String()
en, fn := enginetest.TestEngine(edgeCaseSessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -109,7 +125,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
} }
func TestSendWithInvalidInputs(t *testing.T) { func TestSendWithInvalidInputs(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -147,7 +163,7 @@ func TestSendWithInvalidInputs(t *testing.T) {
} }
func TestMyAccount_Check_My_Balance(t *testing.T) { func TestMyAccount_Check_My_Balance(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -178,7 +194,7 @@ func TestMyAccount_Check_My_Balance(t *testing.T) {
} }
func TestMainMenuHelp(t *testing.T) { func TestMainMenuHelp(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -209,7 +225,7 @@ func TestMainMenuHelp(t *testing.T) {
} }
func TestMainMenuQuit(t *testing.T) { func TestMainMenuQuit(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -240,7 +256,7 @@ func TestMainMenuQuit(t *testing.T) {
} }
func TestMyAccount_Check_Community_Balance(t *testing.T) { func TestMyAccount_Check_Community_Balance(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -271,7 +287,7 @@ func TestMyAccount_Check_Community_Balance(t *testing.T) {
} }
func TestMyAccountChangePin(t *testing.T) { func TestMyAccountChangePin(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -302,7 +318,7 @@ func TestMyAccountChangePin(t *testing.T) {
} }
func TestMyAccount_Change_Language(t *testing.T) { func TestMyAccount_Change_Language(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -333,7 +349,7 @@ func TestMyAccount_Change_Language(t *testing.T) {
} }
func TestMyAccount_Edit_firstname(t *testing.T) { func TestMyAccount_Edit_firstname(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -364,7 +380,7 @@ func TestMyAccount_Edit_firstname(t *testing.T) {
} }
func TestMyAccount_Edit_familyname(t *testing.T) { func TestMyAccount_Edit_familyname(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -395,7 +411,7 @@ func TestMyAccount_Edit_familyname(t *testing.T) {
} }
func TestMyAccount_Edit_gender(t *testing.T) { func TestMyAccount_Edit_gender(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -417,7 +433,6 @@ func TestMyAccount_Edit_gender(t *testing.T) {
t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err) t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err)
} }
b := w.Bytes() b := w.Bytes()
fmt.Println("Content:", string(b))
if !bytes.Equal(b, []byte(step.ExpectedContent)) { if !bytes.Equal(b, []byte(step.ExpectedContent)) {
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
} }
@ -427,7 +442,7 @@ func TestMyAccount_Edit_gender(t *testing.T) {
} }
func TestMyAccount_Edit_yob(t *testing.T) { func TestMyAccount_Edit_yob(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -458,7 +473,7 @@ func TestMyAccount_Edit_yob(t *testing.T) {
} }
func TestMyAccount_Edit_location(t *testing.T) { func TestMyAccount_Edit_location(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -489,7 +504,7 @@ func TestMyAccount_Edit_location(t *testing.T) {
} }
func TestMyAccount_Edit_offerings(t *testing.T) { func TestMyAccount_Edit_offerings(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -521,7 +536,7 @@ func TestMyAccount_Edit_offerings(t *testing.T) {
} }
func TestMyAccount_MyAddress(t *testing.T) { func TestMyAccount_MyAddress(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -557,7 +572,7 @@ func TestMyAccount_MyAddress(t *testing.T) {
} }
func TestMyAccount_View_Profile(t *testing.T) { func TestMyAccount_View_Profile(t *testing.T) {
en, fn, _ := enginetest.TestEngine(sessionID) en, fn := enginetest.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData