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.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"
) )
@ -128,8 +127,7 @@ func main() {
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

@ -14,7 +14,6 @@ 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"
) )
@ -96,9 +95,7 @@ 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)
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

@ -16,7 +16,6 @@ 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"
) )
@ -89,8 +88,7 @@ func main() {
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

@ -11,7 +11,6 @@ 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"
) )
@ -85,8 +84,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(&accountService) hl, err := lhs.GetHandler()
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()) { func TestEngine(sessionId string) (engine.Engine, func(), *db.Db) {
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,8 +76,7 @@ func TestEngine(sessionId string) (engine.Engine, func()) {
os.Exit(1) os.Exit(1)
} }
mockAccountService := server.MockAccountService{} hl, err := lhs.GetHandler()
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)
@ -100,5 +99,5 @@ func TestEngine(sessionId string) (engine.Engine, func()) {
} }
//en = en.WithDebug(nil) //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/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"
) )
@ -53,8 +52,8 @@ func (ls *LocalHandlerService) SetDataStore(db *db.Db) {
ls.UserdataStore = db ls.UserdataStore = db
} }
func (ls *LocalHandlerService) GetHandler(accountService server.AccountServiceInterface) (*ussd.Handlers, error) { func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) {
ussdHandlers, err := ussd.NewHandlers(ls.Parser, *ls.UserdataStore, accountService) ussdHandlers, err := ussd.NewHandlers(ls.Parser, *ls.UserdataStore)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -18,8 +18,7 @@ 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.
// //
@ -28,10 +27,12 @@ type MockAccountService 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 {
@ -55,6 +56,7 @@ 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.
@ -81,6 +83,7 @@ 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.
@ -107,38 +110,3 @@ 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, accountService server.AccountServiceInterface) (*Handlers, error) { func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db) (*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, accountService s
h := &Handlers{ h := &Handlers{
userdataStore: userDb, userdataStore: userDb,
flagManager: appFlags, flagManager: appFlags,
accountService: accountService, accountService: &server.AccountService{},
} }
return h, nil return h, nil
} }

View File

@ -3,9 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"context" "context"
"log" "fmt"
"math/rand"
"os"
"regexp" "regexp"
"testing" "testing"
"time" "time"
@ -17,19 +15,16 @@ import (
var ( var (
testData = driver.ReadData() testData = driver.ReadData()
testStore = ".test_state"
sessionID string sessionID string
src = rand.NewSource(42)
g = rand.New(src)
) )
func GenerateSessionId() string { // GenerateRandomSessionID generates a random UUID for the sessionID
uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g)) func GenerateUUID() string {
v, err := uu.NewV4() u, err := uuid.NewV4()
if err != nil { if err != nil {
panic(err) return "default_uuid"
} }
return v.String() return u.String()
} }
// Extract the public key from the engine response // Extract the public key from the engine response
@ -44,18 +39,12 @@ func extractPublicKey(response []byte) string {
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
sessionID = GenerateSessionId() sessionID = GenerateUUID()
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
@ -88,13 +77,8 @@ 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
uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g)) edgeCaseSessionID := GenerateUUID()
v, err := uu.NewV4() en, fn, _ := enginetest.TestEngine(edgeCaseSessionID)
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
@ -125,7 +109,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
@ -163,7 +147,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
@ -194,7 +178,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
@ -225,7 +209,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
@ -256,7 +240,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
@ -287,7 +271,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
@ -318,7 +302,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
@ -349,7 +333,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
@ -380,7 +364,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
@ -411,7 +395,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
@ -433,6 +417,7 @@ 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)
} }
@ -442,7 +427,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
@ -473,7 +458,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
@ -504,7 +489,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
@ -536,7 +521,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
@ -572,7 +557,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