Compare commits

..

No commits in common. "86ba03b375728288f2d35cc161d37f424825b141" and "9647d29bd7a75cc12fef512dd219b2a6e8199a4e" have entirely different histories.

9 changed files with 46 additions and 103 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,8 +18,7 @@ type AccountServiceInterface interface {
type AccountService struct {
}
type MockAccountService struct {
}
// CheckAccountStatus retrieves the status of an account transaction based on the provided tracking ID.
//
@ -28,10 +27,12 @@ type MockAccountService struct {
// 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.
//
//
// 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.
// - 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) CheckAccountStatus(trackingId string) (string, error) {
resp, err := http.Get(config.TrackStatusURL + trackingId)
if err != nil {
@ -55,6 +56,7 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (string, error)
return status, 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.
@ -81,7 +83,8 @@ func (as *AccountService) CheckBalance(publicKey string) (string, error) {
return balance, nil
}
// CreateAccount creates a new account in the custodial system.
//CreateAccount creates a new account in the custodial system.
// Returns:
// - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account.
// If there is an error during the request or processing, this will be nil.
@ -107,38 +110,3 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) {
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
}
func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, accountService server.AccountServiceInterface) (*Handlers, error) {
func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db) (*Handlers, error) {
if userdataStore == nil {
return nil, fmt.Errorf("cannot create handler with nil userdata store")
}
@ -71,7 +71,7 @@ func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, accountService s
h := &Handlers{
userdataStore: userDb,
flagManager: appFlags,
accountService: accountService,
accountService: &server.AccountService{},
}
return h, nil
}

View File

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