forked from urdt/ussd
Compare commits
24 Commits
postgres-s
...
menu-trave
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c21a48c78e
|
||
|
|
70e234aa7c
|
||
|
|
554cecc7f3
|
||
|
|
d9e922d5be
|
||
|
|
0837933b6c
|
||
|
|
4d5735f85f
|
||
|
|
85efd2ac6e
|
||
|
|
8791753b2a
|
||
|
|
c8a5391435
|
||
|
|
c6227acba1
|
||
|
|
e30a7ad3e3
|
||
|
|
96aec1fd67
|
||
|
|
810dd74e43
|
||
|
|
3afe35b44f
|
||
|
|
d01af48216
|
||
|
|
d7bfdc62e9
|
||
|
|
360b51a3dc
|
||
|
|
e1f3ab78f1
|
||
|
|
5de82fd08a
|
||
|
|
2220d286b7
|
||
|
|
c39c4acc86
|
||
|
|
5937e7ea9d
|
||
|
|
35acf75f6b
|
||
|
|
8313ca3509
|
17
.env.example
17
.env.example
@@ -1,17 +0,0 @@
|
|||||||
#Serve Http
|
|
||||||
PORT=7123
|
|
||||||
HOST=127.0.0.1
|
|
||||||
|
|
||||||
#PostgreSQL
|
|
||||||
DB_HOST=localhost
|
|
||||||
DB_USER=postgres
|
|
||||||
DB_PASSWORD=strongpass
|
|
||||||
DB_NAME=urdt_ussd
|
|
||||||
DB_PORT=5432
|
|
||||||
DB_SSLMODE=disable
|
|
||||||
DB_TIMEZONE=Africa/Nairobi
|
|
||||||
|
|
||||||
#External API Calls
|
|
||||||
CREATE_ACCOUNT_URL=https://custodial.sarafu.africa/api/account/create
|
|
||||||
TRACK_STATUS_URL=https://custodial.sarafu.africa/api/track/
|
|
||||||
BALANCE_URL=https://custodial.sarafu.africa/api/account/status/
|
|
||||||
@@ -15,11 +15,9 @@ import (
|
|||||||
"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/config"
|
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||||
|
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||||
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"
|
||||||
)
|
)
|
||||||
@@ -29,10 +27,6 @@ var (
|
|||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
initializers.LoadEnvVariables()
|
|
||||||
}
|
|
||||||
|
|
||||||
type atRequestParser struct{}
|
type atRequestParser struct{}
|
||||||
|
|
||||||
func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
|
func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
|
||||||
@@ -72,34 +66,29 @@ func (arp *atRequestParser) GetInput(rq any) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config.LoadConfig()
|
|
||||||
|
|
||||||
var dbDir string
|
var dbDir string
|
||||||
var resourceDir string
|
var resourceDir string
|
||||||
var size uint
|
var size uint
|
||||||
var database string
|
|
||||||
var engineDebug bool
|
var engineDebug bool
|
||||||
var host string
|
var host string
|
||||||
var port uint
|
var port uint
|
||||||
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
||||||
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
||||||
flag.StringVar(&database, "db", "gdbm", "database to be used")
|
|
||||||
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
||||||
flag.UintVar(&size, "s", 160, "max size of output")
|
flag.UintVar(&size, "s", 160, "max size of output")
|
||||||
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")
|
flag.StringVar(&host, "h", "127.0.0.1", "http host")
|
||||||
flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port")
|
flag.UintVar(&port, "p", 7123, "http port")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size)
|
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, "Database", database)
|
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
|
|
||||||
cfg := engine.Config{
|
cfg := engine.Config{
|
||||||
Root: "root",
|
Root: "root",
|
||||||
OutputSize: uint32(size),
|
OutputSize: uint32(size),
|
||||||
FlagCount: uint32(128),
|
FlagCount: uint32(16),
|
||||||
}
|
}
|
||||||
|
|
||||||
if engineDebug {
|
if engineDebug {
|
||||||
|
|||||||
@@ -13,11 +13,9 @@ 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/config"
|
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
|
||||||
"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"
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -25,10 +23,6 @@ var (
|
|||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
initializers.LoadEnvVariables()
|
|
||||||
}
|
|
||||||
|
|
||||||
type asyncRequestParser struct {
|
type asyncRequestParser struct {
|
||||||
sessionId string
|
sessionId string
|
||||||
input []byte
|
input []byte
|
||||||
@@ -43,36 +37,31 @@ func (p *asyncRequestParser) GetInput(r any) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config.LoadConfig()
|
|
||||||
|
|
||||||
var sessionId string
|
var sessionId string
|
||||||
var dbDir string
|
var dbDir string
|
||||||
var resourceDir string
|
var resourceDir string
|
||||||
var size uint
|
var size uint
|
||||||
var database string
|
|
||||||
var engineDebug bool
|
var engineDebug bool
|
||||||
var host string
|
var host string
|
||||||
var port uint
|
var port uint
|
||||||
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
|
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
|
||||||
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
||||||
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
||||||
flag.StringVar(&database, "db", "gdbm", "database to be used")
|
|
||||||
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
||||||
flag.UintVar(&size, "s", 160, "max size of output")
|
flag.UintVar(&size, "s", 160, "max size of output")
|
||||||
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")
|
flag.StringVar(&host, "h", "127.0.0.1", "http host")
|
||||||
flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port")
|
flag.UintVar(&port, "p", 7123, "http port")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId)
|
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, "Database", database)
|
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
|
|
||||||
cfg := engine.Config{
|
cfg := engine.Config{
|
||||||
Root: "root",
|
Root: "root",
|
||||||
OutputSize: uint32(size),
|
OutputSize: uint32(size),
|
||||||
FlagCount: uint32(128),
|
FlagCount: uint32(16),
|
||||||
}
|
}
|
||||||
|
|
||||||
if engineDebug {
|
if engineDebug {
|
||||||
|
|||||||
@@ -15,12 +15,10 @@ 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/config"
|
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
|
||||||
"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"
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -28,39 +26,30 @@ var (
|
|||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
initializers.LoadEnvVariables()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config.LoadConfig()
|
|
||||||
|
|
||||||
var dbDir string
|
var dbDir string
|
||||||
var resourceDir string
|
var resourceDir string
|
||||||
var size uint
|
var size uint
|
||||||
var database string
|
|
||||||
var engineDebug bool
|
var engineDebug bool
|
||||||
var host string
|
var host string
|
||||||
var port uint
|
var port uint
|
||||||
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
||||||
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
||||||
flag.StringVar(&database, "db", "gdbm", "database to be used")
|
|
||||||
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
||||||
flag.UintVar(&size, "s", 160, "max size of output")
|
flag.UintVar(&size, "s", 160, "max size of output")
|
||||||
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")
|
flag.StringVar(&host, "h", "127.0.0.1", "http host")
|
||||||
flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port")
|
flag.UintVar(&port, "p", 7123, "http port")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size)
|
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, "Database", database)
|
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
|
|
||||||
cfg := engine.Config{
|
cfg := engine.Config{
|
||||||
Root: "root",
|
Root: "root",
|
||||||
OutputSize: uint32(size),
|
OutputSize: uint32(size),
|
||||||
FlagCount: uint32(128),
|
FlagCount: uint32(16),
|
||||||
}
|
}
|
||||||
|
|
||||||
if engineDebug {
|
if engineDebug {
|
||||||
@@ -99,7 +88,9 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
accountService := server.AccountService{}
|
accountService := server.AccountService{}
|
||||||
|
|
||||||
hl, err := lhs.GetHandler(&accountService)
|
hl, err := lhs.GetHandler(&accountService)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
|
|||||||
14
cmd/main.go
14
cmd/main.go
@@ -10,8 +10,6 @@ import (
|
|||||||
"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/config"
|
|
||||||
"git.grassecon.net/urdt/ussd/initializers"
|
|
||||||
"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/handlers/server"
|
||||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
||||||
@@ -22,20 +20,12 @@ var (
|
|||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
initializers.LoadEnvVariables()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config.LoadConfig()
|
|
||||||
|
|
||||||
var dbDir string
|
var dbDir string
|
||||||
var size uint
|
var size uint
|
||||||
var sessionId string
|
var sessionId string
|
||||||
var database string
|
|
||||||
var engineDebug bool
|
var engineDebug bool
|
||||||
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
|
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
|
||||||
flag.StringVar(&database, "db", "gdbm", "database to be used")
|
|
||||||
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
||||||
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
|
||||||
flag.UintVar(&size, "s", 160, "max size of output")
|
flag.UintVar(&size, "s", 160, "max size of output")
|
||||||
@@ -45,14 +35,13 @@ func main() {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
ctx = context.WithValue(ctx, "Database", database)
|
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
|
|
||||||
cfg := engine.Config{
|
cfg := engine.Config{
|
||||||
Root: "root",
|
Root: "root",
|
||||||
SessionId: sessionId,
|
SessionId: sessionId,
|
||||||
OutputSize: uint32(size),
|
OutputSize: uint32(size),
|
||||||
FlagCount: uint32(128),
|
FlagCount: uint32(16),
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceDir := scriptDir
|
resourceDir := scriptDir
|
||||||
@@ -96,7 +85,6 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
accountService := server.AccountService{}
|
accountService := server.AccountService{}
|
||||||
hl, err := lhs.GetHandler(&accountService)
|
hl, err := lhs.GetHandler(&accountService)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,16 +1,10 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import "git.grassecon.net/urdt/ussd/initializers"
|
|
||||||
|
|
||||||
var (
|
|
||||||
CreateAccountURL string
|
const (
|
||||||
TrackStatusURL string
|
CreateAccountURL = "https://custodial.sarafu.africa/api/account/create"
|
||||||
BalanceURL string
|
TrackStatusURL = "https://custodial.sarafu.africa/api/track/"
|
||||||
|
BalanceURL = "https://custodial.sarafu.africa/api/account/status/"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadConfig initializes the configuration values after environment variables are loaded.
|
|
||||||
func LoadConfig() {
|
|
||||||
CreateAccountURL = initializers.GetEnv("CREATE_ACCOUNT_URL", "https://custodial.sarafu.africa/api/account/create")
|
|
||||||
TrackStatusURL = initializers.GetEnv("TRACK_STATUS_URL", "https://custodial.sarafu.africa/api/track/")
|
|
||||||
BalanceURL = initializers.GetEnv("BALANCE_URL", "https://custodial.sarafu.africa/api/account/status/")
|
|
||||||
}
|
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -3,14 +3,12 @@ module git.grassecon.net/urdt/ussd
|
|||||||
go 1.22.6
|
go 1.22.6
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.defalsify.org/vise.git v0.2.1-0.20241017112704-307fa6fcdc6b
|
git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb
|
||||||
github.com/alecthomas/assert/v2 v2.2.2
|
github.com/alecthomas/assert/v2 v2.2.2
|
||||||
github.com/peteole/testdata-loader v0.3.0
|
github.com/peteole/testdata-loader v0.3.0
|
||||||
gopkg.in/leonelquinteros/gotext.v1 v1.3.1
|
gopkg.in/leonelquinteros/gotext.v1 v1.3.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/joho/godotenv v1.5.1 // indirect
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/alecthomas/participle/v2 v2.0.0 // indirect
|
github.com/alecthomas/participle/v2 v2.0.0 // indirect
|
||||||
github.com/alecthomas/repr v0.2.0 // indirect
|
github.com/alecthomas/repr v0.2.0 // indirect
|
||||||
|
|||||||
6
go.sum
6
go.sum
@@ -1,9 +1,5 @@
|
|||||||
git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb h1:6P4kxihcwMjDKzvUFC6t2zGNb7MDW+l/ACGlSAN1N8Y=
|
git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb h1:6P4kxihcwMjDKzvUFC6t2zGNb7MDW+l/ACGlSAN1N8Y=
|
||||||
git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M=
|
git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M=
|
||||||
git.defalsify.org/vise.git v0.2.0 h1:X2ZgiGRq4C+9qOlDMP0b/oE5QHjVQNT4aEFZB88ST0Q=
|
|
||||||
git.defalsify.org/vise.git v0.2.0/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M=
|
|
||||||
git.defalsify.org/vise.git v0.2.1-0.20241017112704-307fa6fcdc6b h1:dxBplsIlzJHV+5EH+gzB+w08Blt7IJbb2jeRe1OEjLU=
|
|
||||||
git.defalsify.org/vise.git v0.2.1-0.20241017112704-307fa6fcdc6b/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
|
||||||
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
|
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
|
||||||
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
|
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
|
||||||
github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g=
|
github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g=
|
||||||
@@ -22,8 +18,6 @@ github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 h1:U4kkNYryi/qf
|
|||||||
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y=
|
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y=
|
||||||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
|
||||||
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a h1:0Q3H0YXzMHiciXtRcM+j0jiCe8WKPQHoRgQiRTnfcLY=
|
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a h1:0Q3H0YXzMHiciXtRcM+j0jiCe8WKPQHoRgQiRTnfcLY=
|
||||||
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a/go.mod h1:CdTTBOYzS5E4mWS1N8NWP6AHI19MP0A2B18n3hLzRMk=
|
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a/go.mod h1:CdTTBOYzS5E4mWS1N8NWP6AHI19MP0A2B18n3hLzRMk=
|
||||||
github.com/peteole/testdata-loader v0.3.0 h1:8jckE9KcyNHgyv/VPoaljvKZE0Rqr8+dPVYH6rfNr9I=
|
github.com/peteole/testdata-loader v0.3.0 h1:8jckE9KcyNHgyv/VPoaljvKZE0Rqr8+dPVYH6rfNr9I=
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
package initializers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
)
|
|
||||||
|
|
||||||
func LoadEnvVariables() {
|
|
||||||
err := godotenv.Load()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("Error loading .env file")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to get environment variables with a default fallback
|
|
||||||
func GetEnv(key, defaultVal string) string {
|
|
||||||
if value, exists := os.LookupEnv(key); exists {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
return defaultVal
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to safely convert environment variables to uint
|
|
||||||
func GetEnvUint(key string, defaultVal uint) uint {
|
|
||||||
if value, exists := os.LookupEnv(key); exists {
|
|
||||||
if parsed, err := strconv.Atoi(value); err == nil && parsed >= 0 {
|
|
||||||
return uint(parsed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultVal
|
|
||||||
}
|
|
||||||
@@ -6,8 +6,8 @@ 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"
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HandlerService interface {
|
type HandlerService interface {
|
||||||
@@ -83,6 +83,7 @@ func (ls *LocalHandlerService) GetHandler(accountService server.AccountServiceIn
|
|||||||
ls.DbRs.AddLocalFunc("save_location", ussdHandlers.SaveLocation)
|
ls.DbRs.AddLocalFunc("save_location", ussdHandlers.SaveLocation)
|
||||||
ls.DbRs.AddLocalFunc("save_yob", ussdHandlers.SaveYob)
|
ls.DbRs.AddLocalFunc("save_yob", ussdHandlers.SaveYob)
|
||||||
ls.DbRs.AddLocalFunc("save_offerings", ussdHandlers.SaveOfferings)
|
ls.DbRs.AddLocalFunc("save_offerings", ussdHandlers.SaveOfferings)
|
||||||
|
ls.DbRs.AddLocalFunc("quit_with_balance", ussdHandlers.QuitWithBalance)
|
||||||
ls.DbRs.AddLocalFunc("reset_account_authorized", ussdHandlers.ResetAccountAuthorized)
|
ls.DbRs.AddLocalFunc("reset_account_authorized", ussdHandlers.ResetAccountAuthorized)
|
||||||
ls.DbRs.AddLocalFunc("reset_allow_update", ussdHandlers.ResetAllowUpdate)
|
ls.DbRs.AddLocalFunc("reset_allow_update", ussdHandlers.ResetAllowUpdate)
|
||||||
ls.DbRs.AddLocalFunc("get_profile_info", ussdHandlers.GetProfileInfo)
|
ls.DbRs.AddLocalFunc("get_profile_info", ussdHandlers.GetProfileInfo)
|
||||||
@@ -93,7 +94,6 @@ func (ls *LocalHandlerService) GetHandler(accountService server.AccountServiceIn
|
|||||||
ls.DbRs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
|
ls.DbRs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
|
||||||
ls.DbRs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
|
ls.DbRs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
|
||||||
ls.DbRs.AddLocalFunc("quit_with_help", ussdHandlers.QuitWithHelp)
|
ls.DbRs.AddLocalFunc("quit_with_help", ussdHandlers.QuitWithHelp)
|
||||||
ls.DbRs.AddLocalFunc("fetch_custodial_balances", ussdHandlers.FetchCustodialBalances)
|
|
||||||
|
|
||||||
return ussdHandlers, nil
|
return ussdHandlers, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,15 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/internal/models"
|
"git.grassecon.net/urdt/ussd/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountServiceInterface interface {
|
type AccountServiceInterface interface {
|
||||||
CheckBalance(publicKey string) (*models.BalanceResponse, error)
|
CheckBalance(publicKey string) (string, error)
|
||||||
CreateAccount() (*models.AccountResponse, error)
|
CreateAccount() (*models.AccountResponse, error)
|
||||||
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error)
|
CheckAccountStatus(trackingId string) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountService struct {
|
type AccountService struct {
|
||||||
@@ -33,44 +32,51 @@ type TestAccountService 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.
|
// - 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) (string, error) {
|
||||||
resp, err := http.Get(config.TrackStatusURL + trackingId)
|
resp, err := http.Get(config.TrackStatusURL + trackingId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
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 {
|
||||||
return nil, err
|
return "", 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 "", err
|
||||||
}
|
}
|
||||||
return &trackResp, nil
|
status := trackResp.Result.Transaction.Status
|
||||||
|
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.
|
||||||
func (as *AccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) {
|
func (as *AccountService) CheckBalance(publicKey string) (string, error) {
|
||||||
|
|
||||||
resp, err := http.Get(config.BalanceURL + publicKey)
|
resp, err := http.Get(config.BalanceURL + publicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "0.0", err
|
||||||
}
|
}
|
||||||
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 {
|
||||||
return nil, err
|
return "0.0", err
|
||||||
}
|
}
|
||||||
|
|
||||||
var balanceResp models.BalanceResponse
|
var balanceResp models.BalanceResponse
|
||||||
err = json.Unmarshal(body, &balanceResp)
|
err = json.Unmarshal(body, &balanceResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "0.0", err
|
||||||
}
|
}
|
||||||
return &balanceResp, nil
|
|
||||||
|
balance := balanceResp.Result.Balance
|
||||||
|
return balance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateAccount creates a new account in the custodial system.
|
// CreateAccount creates a new account in the custodial system.
|
||||||
@@ -85,15 +91,18 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
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 {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var accountResp models.AccountResponse
|
var accountResp models.AccountResponse
|
||||||
err = json.Unmarshal(body, &accountResp)
|
err = json.Unmarshal(body, &accountResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &accountResp, nil
|
return &accountResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +121,7 @@ func (tas *TestAccountService) CreateAccount() (*models.AccountResponse, error)
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) {
|
func (tas *TestAccountService) CheckBalance(publicKey string) (string, error) {
|
||||||
|
|
||||||
balanceResponse := &models.BalanceResponse{
|
balanceResponse := &models.BalanceResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
@@ -125,29 +134,9 @@ func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceRe
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return balanceResponse, nil
|
return balanceResponse.Result.Balance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) {
|
func (tas *TestAccountService) CheckAccountStatus(trackingId string) (string, error) {
|
||||||
trackResponse := &models.TrackStatusResponse{
|
return "SUCCESS", nil
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Transaction struct {
|
|
||||||
CreatedAt time.Time "json:\"createdAt\""
|
|
||||||
Status string "json:\"status\""
|
|
||||||
TransferValue json.Number "json:\"transferValue\""
|
|
||||||
TxHash string "json:\"txHash\""
|
|
||||||
TxType string "json:\"txType\""
|
|
||||||
}
|
|
||||||
}{
|
|
||||||
Transaction: models.Transaction{
|
|
||||||
CreatedAt: time.Now(),
|
|
||||||
Status: "SUCCESS",
|
|
||||||
TransferValue: json.Number("0.5"),
|
|
||||||
TxHash: "0x123abc456def",
|
|
||||||
TxType: "transfer",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return trackResponse, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -290,6 +290,8 @@ func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (res
|
|||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//AccountPin, _ := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_ACCOUNT_PIN)
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
AccountPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACCOUNT_PIN)
|
AccountPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACCOUNT_PIN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -523,7 +525,9 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res
|
|||||||
// ResetIncorrectPin resets the incorrect pin flag after a new PIN attempt.
|
// ResetIncorrectPin resets the incorrect pin flag after a new PIN attempt.
|
||||||
func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
|
|
||||||
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
||||||
|
|
||||||
res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
|
res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@@ -535,7 +539,6 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
|||||||
|
|
||||||
flag_account_success, _ := h.flagManager.GetFlag("flag_account_success")
|
flag_account_success, _ := h.flagManager.GetFlag("flag_account_success")
|
||||||
flag_account_pending, _ := h.flagManager.GetFlag("flag_account_pending")
|
flag_account_pending, _ := h.flagManager.GetFlag("flag_account_pending")
|
||||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
|
||||||
|
|
||||||
sessionId, ok := ctx.Value("SessionId").(string)
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -547,23 +550,18 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
accountStatus, err := h.accountService.CheckAccountStatus(string(trackingId))
|
status, err := h.accountService.CheckAccountStatus(string(trackingId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error checking account status:", err)
|
fmt.Println("Error checking account status:", err)
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
if !accountStatus.Ok {
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
|
||||||
status := accountStatus.Result.Transaction.Status
|
|
||||||
|
|
||||||
err = store.WriteEntry(ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(status))
|
err = store.WriteEntry(ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(status))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
if accountStatus.Result.Transaction.Status == "SUCCESS" {
|
|
||||||
|
if 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 {
|
||||||
@@ -643,8 +641,6 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
|
|||||||
var res resource.Result
|
var res resource.Result
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_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")
|
||||||
@@ -656,60 +652,15 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
balanceResponse, err := h.accountService.CheckBalance(string(publicKey))
|
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
if !balanceResponse.Ok {
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
|
||||||
balance := balanceResponse.Result.Balance
|
|
||||||
res.Content = balance
|
res.Content = balance
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
||||||
var res resource.Result
|
|
||||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
|
||||||
|
|
||||||
sessionId, ok := ctx.Value("SessionId").(string)
|
|
||||||
if !ok {
|
|
||||||
return res, fmt.Errorf("missing session")
|
|
||||||
}
|
|
||||||
symbol, _ := h.st.Where()
|
|
||||||
balanceType := strings.Split(symbol, "_")[0]
|
|
||||||
|
|
||||||
store := h.userdataStore
|
|
||||||
publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
balanceResponse, err := h.accountService.CheckBalance(string(publicKey))
|
|
||||||
if err != nil {
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
if !balanceResponse.Ok {
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
|
||||||
balance := balanceResponse.Result.Balance
|
|
||||||
|
|
||||||
switch balanceType {
|
|
||||||
case "my":
|
|
||||||
res.Content = fmt.Sprintf("Your balance is %s", balance)
|
|
||||||
case "community":
|
|
||||||
res.Content = fmt.Sprintf("Your community balance is %s", balance)
|
|
||||||
default:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidateRecipient validates that the given input is a valid phone number.
|
// ValidateRecipient validates that the given input is a valid phone number.
|
||||||
func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
@@ -806,11 +757,10 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res
|
|||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||||
|
|
||||||
balanceResp, err := h.accountService.CheckBalance(string(publicKey))
|
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
balance := balanceResp.Result.Balance
|
|
||||||
|
|
||||||
res.Content = balance
|
res.Content = balance
|
||||||
|
|
||||||
@@ -829,25 +779,18 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
|
|||||||
}
|
}
|
||||||
|
|
||||||
flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
|
flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
|
||||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
|
||||||
|
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||||
|
|
||||||
amountStr := string(input)
|
amountStr := string(input)
|
||||||
|
|
||||||
balanceRes, err := h.accountService.CheckBalance(string(publicKey))
|
balanceStr, err := h.accountService.CheckBalance(string(publicKey))
|
||||||
balanceStr := balanceRes.Result.Balance
|
|
||||||
|
|
||||||
if !balanceRes.Ok {
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
res.Content = balanceStr
|
res.Content = balanceStr
|
||||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
|
||||||
|
|
||||||
// Parse the balance
|
// Parse the balance
|
||||||
balanceParts := strings.Split(balanceStr, " ")
|
balanceParts := strings.Split(balanceStr, " ")
|
||||||
@@ -939,6 +882,36 @@ func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (res
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QuickWithBalance retrieves the balance for a given public key from the custodial balance API endpoint before
|
||||||
|
// gracefully exiting the session.
|
||||||
|
func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
|
var res resource.Result
|
||||||
|
var err error
|
||||||
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
|
if !ok {
|
||||||
|
return res, fmt.Errorf("missing session")
|
||||||
|
}
|
||||||
|
|
||||||
|
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
||||||
|
|
||||||
|
code := codeFromCtx(ctx)
|
||||||
|
l := gotext.NewLocale(translationDir, code)
|
||||||
|
l.AddDomain("default")
|
||||||
|
|
||||||
|
store := h.userdataStore
|
||||||
|
publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||||
|
if err != nil {
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
res.Content = l.Get("Your account balance is %s", balance)
|
||||||
|
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
// InitiateTransaction returns a confirmation and resets the transaction data
|
// InitiateTransaction returns a confirmation and resets the transaction data
|
||||||
// on the gdbm store.
|
// on the gdbm store.
|
||||||
func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
@@ -972,23 +945,16 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetProfileInfo retrieves and formats the profile information of a user from a Gdbm backed storage.
|
||||||
func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
var defaultValue string
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
language, ok := ctx.Value("Language").(lang.Language)
|
|
||||||
if !ok {
|
// Default value when an entry is not found
|
||||||
return res, fmt.Errorf("value for 'Language' is not of type lang.Language")
|
defaultValue := "Not Provided"
|
||||||
}
|
|
||||||
code := language.Code
|
|
||||||
if code == "swa" {
|
|
||||||
defaultValue = "Haipo"
|
|
||||||
} else {
|
|
||||||
defaultValue = "Not Provided"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to handle nil byte slices and convert them to string
|
// Helper function to handle nil byte slices and convert them to string
|
||||||
getEntryOrDefault := func(entry []byte, err error) string {
|
getEntryOrDefault := func(entry []byte, err error) string {
|
||||||
@@ -1025,23 +991,12 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte)
|
|||||||
return res, fmt.Errorf("invalid year of birth: %v", err)
|
return res, fmt.Errorf("invalid year of birth: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch language.Code {
|
|
||||||
case "eng":
|
// Format the result
|
||||||
res.Content = fmt.Sprintf(
|
res.Content = fmt.Sprintf(
|
||||||
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
||||||
name, gender, age, location, offerings,
|
name, gender, age, location, offerings,
|
||||||
)
|
)
|
||||||
case "swa":
|
|
||||||
res.Content = fmt.Sprintf(
|
|
||||||
"Jina: %s\nJinsia: %s\nUmri: %s\nEneo: %s\nUnauza: %s\n",
|
|
||||||
name, gender, age, location, offerings,
|
|
||||||
)
|
|
||||||
default:
|
|
||||||
res.Content = fmt.Sprintf(
|
|
||||||
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
|
||||||
name, gender, age, location, offerings,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,15 +7,11 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/asm"
|
|
||||||
"git.defalsify.org/vise.git/db"
|
"git.defalsify.org/vise.git/db"
|
||||||
"git.defalsify.org/vise.git/lang"
|
|
||||||
"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.defalsify.org/vise.git/state"
|
"git.defalsify.org/vise.git/state"
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/mocks"
|
"git.grassecon.net/urdt/ussd/internal/mocks"
|
||||||
"git.grassecon.net/urdt/ussd/internal/models"
|
"git.grassecon.net/urdt/ussd/internal/models"
|
||||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||||
@@ -29,46 +25,10 @@ var (
|
|||||||
flagsPath = path.Join(baseDir, "services", "registration", "pp.csv")
|
flagsPath = path.Join(baseDir, "services", "registration", "pp.csv")
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewHandlers(t *testing.T) {
|
|
||||||
fm, err := NewFlagManager(flagsPath)
|
|
||||||
accountService := server.TestAccountService{}
|
|
||||||
if err != nil {
|
|
||||||
t.Logf(err.Error())
|
|
||||||
}
|
|
||||||
t.Run("Valid UserDataStore", func(t *testing.T) {
|
|
||||||
mockStore := &mocks.MockUserDataStore{}
|
|
||||||
handlers, err := NewHandlers(fm.parser, mockStore, &accountService)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("expected no error, got %v", err)
|
|
||||||
}
|
|
||||||
if handlers == nil {
|
|
||||||
t.Fatal("expected handlers to be non-nil")
|
|
||||||
}
|
|
||||||
if handlers.userdataStore == nil {
|
|
||||||
t.Fatal("expected userdataStore to be set in handlers")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Test case for nil userdataStore
|
|
||||||
t.Run("Nil UserDataStore", func(t *testing.T) {
|
|
||||||
appFlags := &asm.FlagParser{}
|
|
||||||
|
|
||||||
handlers, err := NewHandlers(appFlags, nil, &accountService)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected an error, got none")
|
|
||||||
}
|
|
||||||
if handlers != nil {
|
|
||||||
t.Fatal("expected handlers to be nil")
|
|
||||||
}
|
|
||||||
if err.Error() != "cannot create handler with nil userdata store" {
|
|
||||||
t.Fatalf("expected specific error, got %v", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateAccount(t *testing.T) {
|
func TestCreateAccount(t *testing.T) {
|
||||||
|
|
||||||
fm, err := NewFlagManager(flagsPath)
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
@@ -482,10 +442,7 @@ func TestMaxAmount(t *testing.T) {
|
|||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||||
publicKey := "0xcasgatweksalw1018221"
|
publicKey := "0xcasgatweksalw1018221"
|
||||||
|
expectedBalance := "0.003CELO"
|
||||||
expectedBalance := &models.BalanceResponse{
|
|
||||||
Ok: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the expected behavior of the mock
|
// Set up the expected behavior of the mock
|
||||||
mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
|
mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
|
||||||
@@ -501,7 +458,7 @@ func TestMaxAmount(t *testing.T) {
|
|||||||
res, _ := h.MaxAmount(ctx, "max_amount", []byte("check_balance"))
|
res, _ := h.MaxAmount(ctx, "max_amount", []byte("check_balance"))
|
||||||
|
|
||||||
//Assert that the balance that was set as the result content is what was returned by Check Balance
|
//Assert that the balance that was set as the result content is what was returned by Check Balance
|
||||||
assert.Equal(t, expectedBalance.Result.Balance, res.Content)
|
assert.Equal(t, expectedBalance, res.Content)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -580,10 +537,12 @@ func TestGetRecipient(t *testing.T) {
|
|||||||
func TestGetFlag(t *testing.T) {
|
func TestGetFlag(t *testing.T) {
|
||||||
fm, err := NewFlagManager(flagsPath)
|
fm, err := NewFlagManager(flagsPath)
|
||||||
expectedFlag := uint32(9)
|
expectedFlag := uint32(9)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
flag, err := fm.GetFlag("flag_account_created")
|
flag, err := fm.GetFlag("flag_account_created")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
@@ -1053,109 +1012,53 @@ func TestVerifyPin(t *testing.T) {
|
|||||||
|
|
||||||
func TestCheckAccountStatus(t *testing.T) {
|
func TestCheckAccountStatus(t *testing.T) {
|
||||||
fm, err := NewFlagManager(flagsPath)
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
|
mockDataStore := new(mocks.MockUserDataStore)
|
||||||
|
mockCreateAccountService := new(mocks.MockAccountService)
|
||||||
|
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
flag_account_success, _ := fm.GetFlag("flag_account_success")
|
flag_account_success, _ := fm.GetFlag("flag_account_success")
|
||||||
flag_account_pending, _ := fm.GetFlag("flag_account_pending")
|
flag_account_pending, _ := fm.GetFlag("flag_account_pending")
|
||||||
flag_api_error, _ := fm.GetFlag("flag_api_call_error")
|
|
||||||
|
|
||||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||||
|
|
||||||
|
h := &Handlers{
|
||||||
|
userdataStore: mockDataStore,
|
||||||
|
accountService: mockCreateAccountService,
|
||||||
|
flagManager: fm.parser,
|
||||||
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input []byte
|
input []byte
|
||||||
response *models.TrackStatusResponse
|
status string
|
||||||
expectedResult resource.Result
|
expectedResult resource.Result
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test when account status is Success",
|
name: "Test when account status is Success",
|
||||||
input: []byte("TrackingId1234"),
|
input: []byte("TrackingId1234"),
|
||||||
response: &models.TrackStatusResponse{
|
status: "SUCCESS",
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Transaction struct {
|
|
||||||
CreatedAt time.Time "json:\"createdAt\""
|
|
||||||
Status string "json:\"status\""
|
|
||||||
TransferValue json.Number "json:\"transferValue\""
|
|
||||||
TxHash string "json:\"txHash\""
|
|
||||||
TxType string "json:\"txType\""
|
|
||||||
}
|
|
||||||
}{
|
|
||||||
Transaction: models.Transaction{
|
|
||||||
CreatedAt: time.Now(),
|
|
||||||
Status: "SUCCESS",
|
|
||||||
TransferValue: json.Number("0.5"),
|
|
||||||
TxHash: "0x123abc456def",
|
|
||||||
TxType: "transfer",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResult: resource.Result{
|
expectedResult: resource.Result{
|
||||||
FlagSet: []uint32{flag_account_success},
|
FlagSet: []uint32{flag_account_success},
|
||||||
FlagReset: []uint32{flag_api_error, flag_account_pending},
|
FlagReset: []uint32{flag_account_pending},
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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"),
|
|
||||||
response: &models.TrackStatusResponse{
|
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Transaction struct {
|
|
||||||
CreatedAt time.Time "json:\"createdAt\""
|
|
||||||
Status string "json:\"status\""
|
|
||||||
TransferValue json.Number "json:\"transferValue\""
|
|
||||||
TxHash string "json:\"txHash\""
|
|
||||||
TxType string "json:\"txType\""
|
|
||||||
}
|
|
||||||
}{
|
|
||||||
Transaction: models.Transaction{
|
|
||||||
CreatedAt: time.Now(),
|
|
||||||
Status: "IN_NETWORK",
|
|
||||||
TransferValue: json.Number("0.5"),
|
|
||||||
TxHash: "0x123abc456def",
|
|
||||||
TxType: "transfer",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagSet: []uint32{flag_account_pending},
|
|
||||||
FlagReset: []uint32{flag_api_error, flag_account_success},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typ := utils.DATA_TRACKING_ID
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
mockDataStore := new(mocks.MockUserDataStore)
|
|
||||||
mockCreateAccountService := new(mocks.MockAccountService)
|
|
||||||
|
|
||||||
h := &Handlers{
|
|
||||||
userdataStore: mockDataStore,
|
|
||||||
accountService: mockCreateAccountService,
|
|
||||||
flagManager: fm.parser,
|
|
||||||
}
|
|
||||||
|
|
||||||
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_TRACKING_ID).Return(tt.input, nil)
|
mockDataStore.On("ReadEntry", ctx, sessionId, typ).Return(tt.input, nil)
|
||||||
|
|
||||||
mockCreateAccountService.On("CheckAccountStatus", string(tt.input)).Return(tt.response, nil)
|
mockCreateAccountService.On("CheckAccountStatus", string(tt.input)).Return(tt.status, 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(tt.status)).Return(nil)
|
||||||
|
|
||||||
// Call the method under test
|
// Call the method under test
|
||||||
res, _ := h.CheckAccountStatus(ctx, "check_account_status", tt.input)
|
res, _ := h.CheckAccountStatus(ctx, "check_status", tt.input)
|
||||||
|
|
||||||
// Assert that no errors occurred
|
// Assert that no errors occurred
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@@ -1455,14 +1358,14 @@ func TestIsValidPIN(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidateAmount(t *testing.T) {
|
func TestQuitWithBalance(t *testing.T) {
|
||||||
fm, err := NewFlagManager(flagsPath)
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount")
|
flag_account_authorized, _ := fm.parser.GetFlag("flag_account_authorized")
|
||||||
flag_api_error, _ := fm.GetFlag("flag_api_call_error")
|
|
||||||
mockDataStore := new(mocks.MockUserDataStore)
|
mockDataStore := new(mocks.MockUserDataStore)
|
||||||
mockCreateAccountService := new(mocks.MockAccountService)
|
mockCreateAccountService := new(mocks.MockAccountService)
|
||||||
|
|
||||||
@@ -1476,69 +1379,19 @@ func TestValidateAmount(t *testing.T) {
|
|||||||
flagManager: fm.parser,
|
flagManager: fm.parser,
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input []byte
|
input []byte
|
||||||
publicKey []byte
|
publicKey []byte
|
||||||
balanceResponse *models.BalanceResponse
|
balance string
|
||||||
expectedResult resource.Result
|
expectedResult resource.Result
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test with valid amount",
|
name: "Test quit with balance",
|
||||||
input: []byte("0.001"),
|
balance: "0.02CELO",
|
||||||
balanceResponse: &models.BalanceResponse{
|
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Balance string `json:"balance"`
|
|
||||||
Nonce json.Number `json:"nonce"`
|
|
||||||
}{
|
|
||||||
Balance: "0.003 CELO",
|
|
||||||
Nonce: json.Number("0"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
publicKey: []byte("0xrqeqrequuq"),
|
publicKey: []byte("0xrqeqrequuq"),
|
||||||
expectedResult: resource.Result{
|
expectedResult: resource.Result{
|
||||||
Content: "0.001",
|
FlagReset: []uint32{flag_account_authorized},
|
||||||
FlagReset: []uint32{flag_api_error},
|
Content: fmt.Sprintf("Your account balance is %s", "0.02CELO"),
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test with amount larger than balance",
|
|
||||||
input: []byte("0.02"),
|
|
||||||
balanceResponse: &models.BalanceResponse{
|
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Balance string `json:"balance"`
|
|
||||||
Nonce json.Number `json:"nonce"`
|
|
||||||
}{
|
|
||||||
Balance: "0.003 CELO",
|
|
||||||
Nonce: json.Number("0"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
publicKey: []byte("0xrqeqrequuq"),
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagSet: []uint32{flag_invalid_amount},
|
|
||||||
FlagReset: []uint32{flag_api_error},
|
|
||||||
Content: "0.02",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test with invalid amount",
|
|
||||||
input: []byte("0.02ms"),
|
|
||||||
balanceResponse: &models.BalanceResponse{
|
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Balance string `json:"balance"`
|
|
||||||
Nonce json.Number `json:"nonce"`
|
|
||||||
}{
|
|
||||||
Balance: "0.003 CELO",
|
|
||||||
Nonce: json.Number("0"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
publicKey: []byte("0xrqeqrequuq"),
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagSet: []uint32{flag_invalid_amount},
|
|
||||||
FlagReset: []uint32{flag_api_error},
|
|
||||||
Content: "0.02ms",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -1547,7 +1400,86 @@ func TestValidateAmount(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
||||||
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.publicKey, nil)
|
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.publicKey, nil)
|
||||||
mockCreateAccountService.On("CheckBalance", string(tt.publicKey)).Return(tt.balanceResponse, nil)
|
mockCreateAccountService.On("CheckBalance", string(tt.publicKey)).Return(tt.balance, nil)
|
||||||
|
|
||||||
|
// Call the method under test
|
||||||
|
res, _ := h.QuitWithBalance(ctx, "test_quit_with_balance", tt.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)
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateAmount(t *testing.T) {
|
||||||
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount")
|
||||||
|
mockDataStore := new(mocks.MockUserDataStore)
|
||||||
|
mockCreateAccountService := new(mocks.MockAccountService)
|
||||||
|
|
||||||
|
sessionId := "session123"
|
||||||
|
|
||||||
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||||
|
|
||||||
|
h := &Handlers{
|
||||||
|
userdataStore: mockDataStore,
|
||||||
|
accountService: mockCreateAccountService,
|
||||||
|
flagManager: fm.parser,
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input []byte
|
||||||
|
publicKey []byte
|
||||||
|
balance string
|
||||||
|
expectedResult resource.Result
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Test with valid amount",
|
||||||
|
input: []byte("0.001"),
|
||||||
|
balance: "0.003 CELO",
|
||||||
|
publicKey: []byte("0xrqeqrequuq"),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
Content: "0.001",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test with amount larger than balance",
|
||||||
|
input: []byte("0.02"),
|
||||||
|
balance: "0.003 CELO",
|
||||||
|
publicKey: []byte("0xrqeqrequuq"),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagSet: []uint32{flag_invalid_amount},
|
||||||
|
Content: "0.02",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test with invalid amount",
|
||||||
|
input: []byte("0.02ms"),
|
||||||
|
balance: "0.003 CELO",
|
||||||
|
publicKey: []byte("0xrqeqrequuq"),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagSet: []uint32{flag_invalid_amount},
|
||||||
|
Content: "0.02ms",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
||||||
|
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.publicKey, nil)
|
||||||
|
mockCreateAccountService.On("CheckBalance", string(tt.publicKey)).Return(tt.balance, nil)
|
||||||
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, tt.input).Return(nil).Maybe()
|
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, tt.input).Return(nil).Maybe()
|
||||||
|
|
||||||
// Call the method under test
|
// Call the method under test
|
||||||
@@ -1626,83 +1558,31 @@ func TestValidateRecipient(t *testing.T) {
|
|||||||
|
|
||||||
func TestCheckBalance(t *testing.T) {
|
func TestCheckBalance(t *testing.T) {
|
||||||
|
|
||||||
|
mockDataStore := new(mocks.MockUserDataStore)
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
publicKey := "0X13242618721"
|
publicKey := "0X13242618721"
|
||||||
fm, _ := NewFlagManager(flagsPath)
|
balance := "0.003 CELO"
|
||||||
flag_api_error, _ := fm.GetFlag("flag_api_call_error")
|
|
||||||
|
expectedResult := resource.Result{
|
||||||
|
Content: "0.003 CELO",
|
||||||
|
}
|
||||||
|
|
||||||
|
mockCreateAccountService := new(mocks.MockAccountService)
|
||||||
|
|
||||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||||
|
|
||||||
tests := []struct {
|
h := &Handlers{
|
||||||
name string
|
userdataStore: mockDataStore,
|
||||||
balanceResonse *models.BalanceResponse
|
accountService: mockCreateAccountService,
|
||||||
expectedResult resource.Result
|
//flagManager: fm.parser,
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test when checking a balance is not a success",
|
|
||||||
balanceResonse: &models.BalanceResponse{
|
|
||||||
Ok: false,
|
|
||||||
Result: struct {
|
|
||||||
Balance string `json:"balance"`
|
|
||||||
Nonce json.Number `json:"nonce"`
|
|
||||||
}{
|
|
||||||
Balance: "0.003 CELO",
|
|
||||||
Nonce: json.Number("0"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagSet: []uint32{flag_api_error},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test when checking a balance is a success",
|
|
||||||
balanceResonse: &models.BalanceResponse{
|
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Balance string `json:"balance"`
|
|
||||||
Nonce json.Number `json:"nonce"`
|
|
||||||
}{
|
|
||||||
Balance: "0.003 CELO",
|
|
||||||
Nonce: json.Number("0"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
Content: "0.003 CELO",
|
|
||||||
FlagReset: []uint32{flag_api_error},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
//mock call operations
|
||||||
|
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
|
||||||
|
mockCreateAccountService.On("CheckBalance", string(publicKey)).Return(balance, nil)
|
||||||
|
|
||||||
for _, tt := range tests {
|
res, _ := h.CheckBalance(ctx, "check_balance", []byte("123456"))
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
|
|
||||||
mockDataStore := new(mocks.MockUserDataStore)
|
assert.Equal(t, res, expectedResult, "Result should contain flag(s) that have been reset")
|
||||||
mockCreateAccountService := new(mocks.MockAccountService)
|
|
||||||
mockState := state.NewState(16)
|
|
||||||
|
|
||||||
// Create the Handlers instance with the mock store
|
|
||||||
h := &Handlers{
|
|
||||||
userdataStore: mockDataStore,
|
|
||||||
flagManager: fm.parser,
|
|
||||||
st: mockState,
|
|
||||||
accountService: mockCreateAccountService,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the expected behavior of the mock
|
|
||||||
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
|
|
||||||
mockCreateAccountService.On("CheckBalance", string(publicKey)).Return(tt.balanceResonse, nil)
|
|
||||||
|
|
||||||
// Call the method
|
|
||||||
res, _ := h.CheckBalance(ctx, "check_balance", []byte(""))
|
|
||||||
|
|
||||||
// Assert that expectations were met
|
|
||||||
mockDataStore.AssertExpectations(t)
|
|
||||||
|
|
||||||
//Assert that the result set to content is what was expected
|
|
||||||
assert.Equal(t, res, tt.expectedResult, "Result should contain flags set according to user input")
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1712,50 +1592,23 @@ func TestGetProfile(t *testing.T) {
|
|||||||
|
|
||||||
mockDataStore := new(mocks.MockUserDataStore)
|
mockDataStore := new(mocks.MockUserDataStore)
|
||||||
mockCreateAccountService := new(mocks.MockAccountService)
|
mockCreateAccountService := new(mocks.MockAccountService)
|
||||||
mockState := state.NewState(16)
|
|
||||||
|
|
||||||
h := &Handlers{
|
h := &Handlers{
|
||||||
userdataStore: mockDataStore,
|
userdataStore: mockDataStore,
|
||||||
accountService: mockCreateAccountService,
|
accountService: mockCreateAccountService,
|
||||||
st: mockState,
|
|
||||||
}
|
}
|
||||||
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
languageCode string
|
keys []utils.DataTyp
|
||||||
keys []utils.DataTyp
|
profileInfo []string
|
||||||
profileInfo []string
|
result resource.Result
|
||||||
result resource.Result
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test with full profile information in eng",
|
name: "Test with full profile information",
|
||||||
keys: []utils.DataTyp{utils.DATA_FAMILY_NAME, utils.DATA_FIRST_NAME, utils.DATA_GENDER, utils.DATA_OFFERINGS, utils.DATA_LOCATION, utils.DATA_YOB},
|
keys: []utils.DataTyp{utils.DATA_FAMILY_NAME, utils.DATA_FIRST_NAME, utils.DATA_GENDER, utils.DATA_OFFERINGS, utils.DATA_LOCATION, utils.DATA_YOB},
|
||||||
profileInfo: []string{"Doee", "John", "Male", "Bananas", "Kilifi", "1976"},
|
profileInfo: []string{"Doee", "John", "Male", "Bananas", "Kilifi", "1976"},
|
||||||
languageCode: "eng",
|
|
||||||
result: resource.Result{
|
|
||||||
Content: fmt.Sprintf(
|
|
||||||
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
|
||||||
"John Doee", "Male", "48", "Kilifi", "Bananas",
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test with with profile information in swa ",
|
|
||||||
keys: []utils.DataTyp{utils.DATA_FAMILY_NAME, utils.DATA_FIRST_NAME, utils.DATA_GENDER, utils.DATA_OFFERINGS, utils.DATA_LOCATION, utils.DATA_YOB},
|
|
||||||
profileInfo: []string{"Doee", "John", "Male", "Bananas", "Kilifi", "1976"},
|
|
||||||
languageCode: "swa",
|
|
||||||
result: resource.Result{
|
|
||||||
Content: fmt.Sprintf(
|
|
||||||
"Jina: %s\nJinsia: %s\nUmri: %s\nEneo: %s\nUnauza: %s\n",
|
|
||||||
"John Doee", "Male", "48", "Kilifi", "Bananas",
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test with with profile information with language that is not yet supported",
|
|
||||||
keys: []utils.DataTyp{utils.DATA_FAMILY_NAME, utils.DATA_FIRST_NAME, utils.DATA_GENDER, utils.DATA_OFFERINGS, utils.DATA_LOCATION, utils.DATA_YOB},
|
|
||||||
profileInfo: []string{"Doee", "John", "Male", "Bananas", "Kilifi", "1976"},
|
|
||||||
languageCode: "nor",
|
|
||||||
result: resource.Result{
|
result: resource.Result{
|
||||||
Content: fmt.Sprintf(
|
Content: fmt.Sprintf(
|
||||||
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
||||||
@@ -1766,14 +1619,9 @@ func TestGetProfile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
||||||
ctx = context.WithValue(ctx, "Language", lang.Language{
|
|
||||||
Code: tt.languageCode,
|
|
||||||
})
|
|
||||||
for index, key := range tt.keys {
|
for index, key := range tt.keys {
|
||||||
mockDataStore.On("ReadEntry", ctx, sessionId, key).Return([]byte(tt.profileInfo[index]), nil).Maybe()
|
mockDataStore.On("ReadEntry", ctx, sessionId, key).Return([]byte(tt.profileInfo[index]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, _ := h.GetProfileInfo(ctx, "get_profile_info", []byte(""))
|
res, _ := h.GetProfileInfo(ctx, "get_profile_info", []byte(""))
|
||||||
|
|
||||||
// Assert that expectations were met
|
// Assert that expectations were met
|
||||||
@@ -1930,85 +1778,3 @@ func TestConfirmPin(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFetchCustodialBalances(t *testing.T) {
|
|
||||||
fm, err := NewFlagManager(flagsPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Logf(err.Error())
|
|
||||||
}
|
|
||||||
flag_api_error, _ := fm.GetFlag("flag_api_call_error")
|
|
||||||
|
|
||||||
// Define test data
|
|
||||||
sessionId := "session123"
|
|
||||||
publicKey := "0X13242618721"
|
|
||||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
balanceResonse *models.BalanceResponse
|
|
||||||
expectedResult resource.Result
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test when fetch custodial balances is not a success",
|
|
||||||
balanceResonse: &models.BalanceResponse{
|
|
||||||
Ok: false,
|
|
||||||
Result: struct {
|
|
||||||
Balance string `json:"balance"`
|
|
||||||
Nonce json.Number `json:"nonce"`
|
|
||||||
}{
|
|
||||||
Balance: "0.003 CELO",
|
|
||||||
Nonce: json.Number("0"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagSet: []uint32{flag_api_error},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test when fetch custodial balances is a success",
|
|
||||||
balanceResonse: &models.BalanceResponse{
|
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
Balance string `json:"balance"`
|
|
||||||
Nonce json.Number `json:"nonce"`
|
|
||||||
}{
|
|
||||||
Balance: "0.003 CELO",
|
|
||||||
Nonce: json.Number("0"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagReset: []uint32{flag_api_error},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
|
|
||||||
mockDataStore := new(mocks.MockUserDataStore)
|
|
||||||
mockCreateAccountService := new(mocks.MockAccountService)
|
|
||||||
mockState := state.NewState(16)
|
|
||||||
|
|
||||||
// Create the Handlers instance with the mock store
|
|
||||||
h := &Handlers{
|
|
||||||
userdataStore: mockDataStore,
|
|
||||||
flagManager: fm.parser,
|
|
||||||
st: mockState,
|
|
||||||
accountService: mockCreateAccountService,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the expected behavior of the mock
|
|
||||||
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
|
|
||||||
mockCreateAccountService.On("CheckBalance", string(publicKey)).Return(tt.balanceResonse, nil)
|
|
||||||
|
|
||||||
// Call the method
|
|
||||||
res, _ := h.FetchCustodialBalances(ctx, "fetch_custodial_balances", []byte(""))
|
|
||||||
|
|
||||||
// Assert that expectations were met
|
|
||||||
mockDataStore.AssertExpectations(t)
|
|
||||||
|
|
||||||
//Assert that the result set to content is what was expected
|
|
||||||
assert.Equal(t, res, tt.expectedResult, "Result should contain flags set according to user input")
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -15,12 +15,12 @@ func (m *MockAccountService) CreateAccount() (*models.AccountResponse, error) {
|
|||||||
return args.Get(0).(*models.AccountResponse), args.Error(1)
|
return args.Get(0).(*models.AccountResponse), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) {
|
func (m *MockAccountService) CheckBalance(publicKey string) (string, error) {
|
||||||
args := m.Called(publicKey)
|
args := m.Called(publicKey)
|
||||||
return args.Get(0).(*models.BalanceResponse), args.Error(1)
|
return args.String(0), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) {
|
func (m *MockAccountService) CheckAccountStatus(trackingId string) (string, error) {
|
||||||
args := m.Called(trackingId)
|
args := m.Called(trackingId)
|
||||||
return args.Get(0).(*models.TrackStatusResponse), args.Error(1)
|
return args.String(0), args.Error(1)
|
||||||
}
|
}
|
||||||
@@ -5,13 +5,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Transaction struct {
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
TransferValue json.Number `json:"transferValue"`
|
|
||||||
TxHash string `json:"txHash"`
|
|
||||||
TxType string `json:"txType"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type TrackStatusResponse struct {
|
type TrackStatusResponse struct {
|
||||||
Ok bool `json:"ok"`
|
Ok bool `json:"ok"`
|
||||||
@@ -24,4 +17,4 @@ type TrackStatusResponse struct {
|
|||||||
TxType string `json:"txType"`
|
TxType string `json:"txType"`
|
||||||
}
|
}
|
||||||
} `json:"result"`
|
} `json:"result"`
|
||||||
}
|
}
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package storage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/db"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
DATATYPE_USERSUB = 64
|
|
||||||
)
|
|
||||||
|
|
||||||
type SubPrefixDb struct {
|
|
||||||
store db.Db
|
|
||||||
pfx []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSubPrefixDb(store db.Db, pfx []byte) *SubPrefixDb {
|
|
||||||
return &SubPrefixDb{
|
|
||||||
store: store,
|
|
||||||
pfx: pfx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func(s *SubPrefixDb) toKey(k []byte) []byte {
|
|
||||||
return append(s.pfx, k...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func(s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
|
|
||||||
s.store.SetPrefix(DATATYPE_USERSUB)
|
|
||||||
key = s.toKey(key)
|
|
||||||
v, err := s.store.Get(ctx, key)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return v, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func(s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error {
|
|
||||||
s.store.SetPrefix(DATATYPE_USERSUB)
|
|
||||||
key = s.toKey(key)
|
|
||||||
return s.store.Put(ctx, key, val)
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
package storage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
memdb "git.defalsify.org/vise.git/db/mem"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSubPrefix(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
db := memdb.NewMemDb()
|
|
||||||
err := db.Connect(ctx, "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
sdba := NewSubPrefixDb(db, []byte("tinkywinky"))
|
|
||||||
err = sdba.Put(ctx, []byte("foo"), []byte("dipsy"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err := sdba.Get(ctx, []byte("foo"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(r, []byte("dipsy")) {
|
|
||||||
t.Fatalf("expected 'dipsy', got %s", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
sdbb := NewSubPrefixDb(db, []byte("lala"))
|
|
||||||
r, err = sdbb.Get(ctx, []byte("foo"))
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = sdbb.Put(ctx, []byte("foo"), []byte("pu"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
r, err = sdbb.Get(ctx, []byte("foo"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(r, []byte("pu")) {
|
|
||||||
t.Fatalf("expected 'pu', got %s", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err = sdba.Get(ctx, []byte("foo"))
|
|
||||||
if !bytes.Equal(r, []byte("dipsy")) {
|
|
||||||
t.Fatalf("expected 'dipsy', got %s", r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,6 +5,10 @@ import (
|
|||||||
"git.defalsify.org/vise.git/persist"
|
"git.defalsify.org/vise.git/persist"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DATATYPE_CUSTOM = 128
|
||||||
|
)
|
||||||
|
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
Persister *persist.Persister
|
Persister *persist.Persister
|
||||||
UserdataDb db.Db
|
UserdataDb db.Db
|
||||||
|
|||||||
@@ -8,16 +8,14 @@ import (
|
|||||||
|
|
||||||
"git.defalsify.org/vise.git/db"
|
"git.defalsify.org/vise.git/db"
|
||||||
fsdb "git.defalsify.org/vise.git/db/fs"
|
fsdb "git.defalsify.org/vise.git/db/fs"
|
||||||
"git.defalsify.org/vise.git/db/postgres"
|
|
||||||
"git.defalsify.org/vise.git/logging"
|
|
||||||
"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/initializers"
|
"git.defalsify.org/vise.git/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logg = logging.NewVanilla().WithDomain("storage")
|
logg = logging.NewVanilla().WithDomain("storage")
|
||||||
)
|
)
|
||||||
|
|
||||||
type StorageService interface {
|
type StorageService interface {
|
||||||
GetPersister(ctx context.Context) (*persist.Persister, error)
|
GetPersister(ctx context.Context) (*persist.Persister, error)
|
||||||
@@ -26,86 +24,40 @@ type StorageService interface {
|
|||||||
EnsureDbDir() error
|
EnsureDbDir() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type MenuStorageService struct {
|
type MenuStorageService struct{
|
||||||
dbDir string
|
dbDir string
|
||||||
resourceDir string
|
resourceDir string
|
||||||
resourceStore db.Db
|
resourceStore db.Db
|
||||||
stateStore db.Db
|
stateStore db.Db
|
||||||
userDataStore db.Db
|
userDataStore db.Db
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildConnStr() string {
|
|
||||||
host := initializers.GetEnv("DB_HOST", "localhost")
|
|
||||||
user := initializers.GetEnv("DB_USER", "postgres")
|
|
||||||
password := initializers.GetEnv("DB_PASSWORD", "")
|
|
||||||
dbName := initializers.GetEnv("DB_NAME", "")
|
|
||||||
port := initializers.GetEnv("DB_PORT", "5432")
|
|
||||||
|
|
||||||
return fmt.Sprintf(
|
|
||||||
"postgres://%s:%s@%s:%s/%s",
|
|
||||||
user, password, host, port, dbName,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService {
|
func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService {
|
||||||
return &MenuStorageService{
|
return &MenuStorageService{
|
||||||
dbDir: dbDir,
|
dbDir: dbDir,
|
||||||
resourceDir: resourceDir,
|
resourceDir: resourceDir,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, fileName string) (db.Db, error) {
|
|
||||||
database, ok := ctx.Value("Database").(string)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("failed to select the database")
|
|
||||||
}
|
|
||||||
|
|
||||||
if existingDb != nil {
|
|
||||||
return existingDb, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var newDb db.Db
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if database == "postgres" {
|
|
||||||
newDb = postgres.NewPgDb()
|
|
||||||
connStr := buildConnStr()
|
|
||||||
err = newDb.Connect(ctx, connStr)
|
|
||||||
} else {
|
|
||||||
newDb = NewThreadGdbmDb()
|
|
||||||
storeFile := path.Join(ms.dbDir, fileName)
|
|
||||||
err = newDb.Connect(ctx, storeFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return newDb, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ms *MenuStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
|
func (ms *MenuStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
|
||||||
stateStore, err := ms.GetStateStore(ctx)
|
ms.stateStore = NewThreadGdbmDb()
|
||||||
|
storeFile := path.Join(ms.dbDir, "state.gdbm")
|
||||||
|
err := ms.stateStore.Connect(ctx, storeFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
pr := persist.NewPersister(ms.stateStore)
|
||||||
pr := persist.NewPersister(stateStore)
|
logg.TraceCtxf(ctx, "menu storage service", "persist", pr, "store", ms.stateStore)
|
||||||
logg.TraceCtxf(ctx, "menu storage service", "persist", pr, "store", stateStore)
|
|
||||||
return pr, nil
|
return pr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) (db.Db, error) {
|
func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) (db.Db, error) {
|
||||||
if ms.userDataStore != nil {
|
ms.userDataStore = NewThreadGdbmDb()
|
||||||
return ms.userDataStore, nil
|
storeFile := path.Join(ms.dbDir, "userdata.gdbm")
|
||||||
}
|
err := ms.userDataStore.Connect(ctx, storeFile)
|
||||||
|
|
||||||
userDataStore, err := ms.getOrCreateDb(ctx, ms.userDataStore, "userdata.gdbm")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ms.userDataStore = userDataStore
|
|
||||||
return ms.userDataStore, nil
|
return ms.userDataStore, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,15 +73,14 @@ func (ms *MenuStorageService) GetResource(ctx context.Context) (resource.Resourc
|
|||||||
|
|
||||||
func (ms *MenuStorageService) GetStateStore(ctx context.Context) (db.Db, error) {
|
func (ms *MenuStorageService) GetStateStore(ctx context.Context) (db.Db, error) {
|
||||||
if ms.stateStore != nil {
|
if ms.stateStore != nil {
|
||||||
return ms.stateStore, nil
|
panic("set up store when already exists")
|
||||||
}
|
}
|
||||||
|
ms.stateStore = NewThreadGdbmDb()
|
||||||
stateStore, err := ms.getOrCreateDb(ctx, ms.stateStore, "state.gdbm")
|
storeFile := path.Join(ms.dbDir, "state.gdbm")
|
||||||
|
err := ms.stateStore.Connect(ctx, storeFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ms.stateStore = stateStore
|
|
||||||
return ms.stateStore, nil
|
return ms.stateStore, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ var (
|
|||||||
func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
ctx = context.WithValue(ctx, "Database", "gdbm")
|
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
|
|
||||||
var eventChannel = make(chan bool)
|
var eventChannel = make(chan bool)
|
||||||
@@ -34,7 +33,7 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
|||||||
Root: "root",
|
Root: "root",
|
||||||
SessionId: sessionId,
|
SessionId: sessionId,
|
||||||
OutputSize: uint32(160),
|
OutputSize: uint32(160),
|
||||||
FlagCount: uint32(128),
|
FlagCount: uint32(16),
|
||||||
}
|
}
|
||||||
|
|
||||||
dbDir := ".test_state"
|
dbDir := ".test_state"
|
||||||
@@ -118,5 +117,7 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
|||||||
}
|
}
|
||||||
logg.Infof("testengine storage closed")
|
logg.Infof("testengine storage closed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//en = en.WithDebug(nil)
|
||||||
return en, cleanFn, eventChannel
|
return en, cleanFn, eventChannel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build !online
|
||||||
// +build !online
|
// +build !online
|
||||||
|
|
||||||
package testutil
|
package testutil
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build online
|
||||||
// +build online
|
// +build online
|
||||||
|
|
||||||
package testutil
|
package testutil
|
||||||
@@ -5,7 +5,6 @@ MOUT back 0
|
|||||||
HALT
|
HALT
|
||||||
LOAD validate_amount 64
|
LOAD validate_amount 64
|
||||||
RELOAD validate_amount
|
RELOAD validate_amount
|
||||||
CATCH api_failure flag_api_call_error 1
|
|
||||||
CATCH invalid_amount flag_invalid_amount 1
|
CATCH invalid_amount flag_invalid_amount 1
|
||||||
INCMP _ 0
|
INCMP _ 0
|
||||||
LOAD get_recipient 12
|
LOAD get_recipient 12
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
Failed to connect to the custodial service.Please try again.
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
MOUT retry 0
|
|
||||||
MOUT quit 9
|
|
||||||
HALT
|
|
||||||
INCMP _ 0
|
|
||||||
INCMP quit 9
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
LOAD reset_account_authorized 0
|
LOAD reset_account_authorized 0
|
||||||
RELOAD reset_account_authorized
|
|
||||||
MOUT my_balance 1
|
MOUT my_balance 1
|
||||||
MOUT community_balance 2
|
MOUT community_balance 2
|
||||||
MOUT back 0
|
MOUT back 0
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
{{.fetch_custodial_balances}}
|
Your community balance is: 0.00SRF
|
||||||
@@ -1,11 +1,5 @@
|
|||||||
LOAD reset_incorrect 6
|
LOAD reset_incorrect 0
|
||||||
LOAD fetch_custodial_balances 0
|
|
||||||
CATCH api_failure flag_api_call_error 1
|
|
||||||
MAP fetch_custodial_balances
|
|
||||||
CATCH incorrect_pin flag_incorrect_pin 1
|
CATCH incorrect_pin flag_incorrect_pin 1
|
||||||
CATCH pin_entry flag_account_authorized 0
|
CATCH pin_entry flag_account_authorized 0
|
||||||
MOUT back 0
|
LOAD quit_with_balance 0
|
||||||
MOUT quit 9
|
|
||||||
HALT
|
HALT
|
||||||
INCMP _ 0
|
|
||||||
INCMP quit 9
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
Confirm your new PIN:
|
Confirm your new PIN:
|
||||||
@@ -2,6 +2,5 @@ CATCH invalid_pin flag_valid_pin 0
|
|||||||
MOUT back 0
|
MOUT back 0
|
||||||
HALT
|
HALT
|
||||||
INCMP _ 0
|
INCMP _ 0
|
||||||
INCMP * pin_reset_success
|
INCMP pin_reset_success *
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
RELOAD reset_account_authorized
|
||||||
MOUT back 0
|
MOUT back 0
|
||||||
MOUT quit 9
|
MOUT quit 9
|
||||||
HALT
|
HALT
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
LOAD check_balance 64
|
LOAD check_balance 64
|
||||||
RELOAD check_balance
|
RELOAD check_balance
|
||||||
CATCH api_failure flag_api_call_error 1
|
|
||||||
MAP check_balance
|
MAP check_balance
|
||||||
MOUT send 1
|
MOUT send 1
|
||||||
MOUT vouchers 2
|
MOUT vouchers 2
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
{{.fetch_custodial_balances}}
|
Your balance is: 0.00 SRF
|
||||||
@@ -1,11 +1,5 @@
|
|||||||
LOAD reset_incorrect 6
|
LOAD reset_incorrect 0
|
||||||
LOAD fetch_custodial_balances 0
|
|
||||||
CATCH api_failure flag_api_call_error 1
|
|
||||||
MAP fetch_custodial_balances
|
|
||||||
CATCH incorrect_pin flag_incorrect_pin 1
|
CATCH incorrect_pin flag_incorrect_pin 1
|
||||||
CATCH pin_entry flag_account_authorized 0
|
CATCH pin_entry flag_account_authorized 0
|
||||||
MOUT back 0
|
LOAD quit_with_balance 0
|
||||||
MOUT quit 9
|
|
||||||
HALT
|
HALT
|
||||||
INCMP _ 0
|
|
||||||
INCMP quit 9
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
Enter a new four number PIN:
|
Enter a new four number PIN:
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
LOAD authorize_account 12
|
CATCH _ flag_allow_update 0
|
||||||
RELOAD authorize_account
|
|
||||||
CATCH incorrect_pin flag_incorrect_pin 1
|
|
||||||
CATCH old_pin flag_allow_update 0
|
|
||||||
MOUT back 0
|
MOUT back 0
|
||||||
HALT
|
HALT
|
||||||
INCMP _ 0
|
INCMP _ 0
|
||||||
LOAD save_temporary_pin 6
|
LOAD save_temporary_pin 6
|
||||||
LOAD verify_new_pin 0
|
|
||||||
RELOAD save_temporary_pin
|
RELOAD save_temporary_pin
|
||||||
|
LOAD verify_new_pin 8
|
||||||
RELOAD verify_new_pin
|
RELOAD verify_new_pin
|
||||||
INCMP * confirm_pin_change
|
CATCH incorrect_pin flag_incorrect_pin 1
|
||||||
|
INCMP confirm_pin_change *
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1 @@
|
|||||||
Weka PIN mpya ya nne nambari:
|
Weka PIN mpya ya nne nambari:
|
||||||
|
|
||||||
@@ -1 +1 @@
|
|||||||
Enter your old PIN
|
Enter your old PIN
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
LOAD reset_allow_update 0
|
LOAD reset_allow_update 0
|
||||||
|
RELOAD reset_allow_update
|
||||||
MOUT back 0
|
MOUT back 0
|
||||||
HALT
|
HALT
|
||||||
RELOAD reset_allow_update
|
LOAD authorize_account 12
|
||||||
|
RELOAD authorize_account
|
||||||
|
CATCH incorrect_pin flag_incorrect_pin 1
|
||||||
INCMP _ 0
|
INCMP _ 0
|
||||||
INCMP new_pin *
|
INCMP new_pin *
|
||||||
|
|
||||||
|
|||||||
@@ -4,5 +4,5 @@ MOUT guard_pin 3
|
|||||||
MOUT back 0
|
MOUT back 0
|
||||||
HALT
|
HALT
|
||||||
INCMP _ 0
|
INCMP _ 0
|
||||||
INCMP old_pin 1
|
INCMP old_pin 1
|
||||||
|
|
||||||
|
|||||||
@@ -3,4 +3,3 @@ MOUT quit 9
|
|||||||
HALT
|
HALT
|
||||||
INCMP confirm_pin_change 1
|
INCMP confirm_pin_change 1
|
||||||
INCMP quit 9
|
INCMP quit 9
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
Your PIN change request has been successful
|
Your PIN change request has been successful
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
LOAD confirm_pin_change 0
|
LOAD confirm_pin_change 0
|
||||||
RELOAD confirm_pin_change
|
RELOAD confirm_pin_change
|
||||||
CATCH pin_reset_mismatch flag_pin_mismatch 1
|
CATCH pin_reset_mismatch flag_pin_mismatch 1
|
||||||
MOUT back 0
|
MOUT back 0
|
||||||
MOUT quit 9
|
MOUT quit 9
|
||||||
HALT
|
HALT
|
||||||
INCMP main 0
|
INCMP main 0
|
||||||
INCMP quit 9
|
INCMP quit 9
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,4 +14,3 @@ flag,flag_valid_pin,20,this is set when the given PIN is valid
|
|||||||
flag,flag_allow_update,21,this is set to allow a user to update their profile data
|
flag,flag_allow_update,21,this is set to allow a user to update their profile data
|
||||||
flag,flag_single_edit,22,this is set to allow a user to edit a single profile item such as year of birth
|
flag,flag_single_edit,22,this is set to allow a user to edit a single profile item such as year of birth
|
||||||
flag,flag_incorrect_date_format,23,this is set when the given year of birth is invalid
|
flag,flag_incorrect_date_format,23,this is set when the given year of birth is invalid
|
||||||
flag,flag_api_call_error,25,this is set when communication to an external service fails
|
|
||||||
|
|||||||
|
@@ -1 +1 @@
|
|||||||
Profile updated successfully
|
Profile updated successfully
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
CATCH select_language flag_language_set 0
|
CATCH select_language flag_language_set 0
|
||||||
CATCH terms flag_account_created 0
|
CATCH terms flag_account_created 0
|
||||||
LOAD check_account_status 0
|
LOAD check_account_status 0
|
||||||
RELOAD check_account_status
|
|
||||||
CATCH api_failure flag_api_call_error 1
|
|
||||||
CATCH account_pending flag_account_pending 1
|
CATCH account_pending flag_account_pending 1
|
||||||
CATCH create_pin flag_pin_set 0
|
CATCH create_pin flag_pin_set 0
|
||||||
CATCH main flag_account_success 1
|
CATCH main flag_account_success 1
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
Angalia Wasifu
|
|
||||||
@@ -17,19 +17,19 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1",
|
"input": "1",
|
||||||
"expectedContent": "Enter your old PIN\n\n0:Back"
|
"expectedContent": "Enter your old PIN\n0:Back"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1234",
|
"input": "1234",
|
||||||
"expectedContent": "Enter a new four number PIN:\n\n0:Back"
|
"expectedContent": "Enter a new four number PIN:\n0:Back"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1234",
|
"input": "1234",
|
||||||
"expectedContent": "Confirm your new PIN:\n\n0:Back"
|
"expectedContent": "Confirm your new PIN:\n0:Back"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1234",
|
"input": "1234",
|
||||||
"expectedContent": "Your PIN change request has been successful\n\n0:Back\n9:Quit"
|
"expectedContent": "Your PIN change request has been successful\n0:Back\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "0",
|
"input": "0",
|
||||||
@@ -74,98 +74,6 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "menu_my_account_check_my_balance",
|
|
||||||
"steps": [
|
|
||||||
{
|
|
||||||
"input": "",
|
|
||||||
"expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "3",
|
|
||||||
"expectedContent": "My Account\n1:Profile\n2:Change language\n3:Check balances\n4:Check statement\n5:PIN options\n6:My Address\n0:Back"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "3",
|
|
||||||
"expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "1",
|
|
||||||
"expectedContent": "Please enter your PIN:"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "1235",
|
|
||||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "1",
|
|
||||||
"expectedContent": "Please enter your PIN:"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "1234",
|
|
||||||
"expectedContent": "Your balance is 0.003 CELO\n0:Back\n9:Quit"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "0",
|
|
||||||
"expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back"
|
|
||||||
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "0",
|
|
||||||
"expectedContent": "My Account\n1:Profile\n2:Change language\n3:Check balances\n4:Check statement\n5:PIN options\n6:My Address\n0:Back"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "0",
|
|
||||||
"expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "menu_my_account_check_community_balance",
|
|
||||||
"steps": [
|
|
||||||
{
|
|
||||||
"input": "",
|
|
||||||
"expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "3",
|
|
||||||
"expectedContent": "My Account\n1:Profile\n2:Change language\n3:Check balances\n4:Check statement\n5:PIN options\n6:My Address\n0:Back"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "3",
|
|
||||||
"expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "2",
|
|
||||||
"expectedContent": "Please enter your PIN:"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "1235",
|
|
||||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "1",
|
|
||||||
"expectedContent": "Please enter your PIN:"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "1234",
|
|
||||||
"expectedContent": "Your community balance is 0.003 CELO\n0:Back\n9:Quit"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "0",
|
|
||||||
"expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back"
|
|
||||||
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "0",
|
|
||||||
"expectedContent": "My Account\n1:Profile\n2:Change language\n3:Check balances\n4:Check statement\n5:PIN options\n6:My Address\n0:Back"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input": "0",
|
|
||||||
"expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"name": "menu_my_account_edit_firstname",
|
"name": "menu_my_account_edit_firstname",
|
||||||
"steps": [
|
"steps": [
|
||||||
@@ -191,7 +99,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1234",
|
"input": "1234",
|
||||||
"expectedContent": "Profile updated successfully\n\n0:Back\n9:Quit"
|
"expectedContent": "Profile updated successfully\n0:Back\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "0",
|
"input": "0",
|
||||||
@@ -232,7 +140,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1234",
|
"input": "1234",
|
||||||
"expectedContent": "Profile updated successfully\n\n0:Back\n9:Quit"
|
"expectedContent": "Profile updated successfully\n0:Back\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "0",
|
"input": "0",
|
||||||
@@ -274,7 +182,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1234",
|
"input": "1234",
|
||||||
"expectedContent": "Profile updated successfully\n\n0:Back\n9:Quit"
|
"expectedContent": "Profile updated successfully\n0:Back\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "0",
|
"input": "0",
|
||||||
@@ -315,7 +223,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1234",
|
"input": "1234",
|
||||||
"expectedContent": "Profile updated successfully\n\n0:Back\n9:Quit"
|
"expectedContent": "Profile updated successfully\n0:Back\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "0",
|
"input": "0",
|
||||||
@@ -356,7 +264,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1234",
|
"input": "1234",
|
||||||
"expectedContent": "Profile updated successfully\n\n0:Back\n9:Quit"
|
"expectedContent": "Profile updated successfully\n0:Back\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "0",
|
"input": "0",
|
||||||
@@ -397,7 +305,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "1234",
|
"input": "1234",
|
||||||
"expectedContent": "Profile updated successfully\n\n0:Back\n9:Quit"
|
"expectedContent": "Profile updated successfully\n0:Back\n9:Quit"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"input": "0",
|
"input": "0",
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package menutraversaltest
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -131,6 +131,83 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSendWithInvalidInputs(t *testing.T) {
|
||||||
|
en, fn, _ := testutil.TestEngine(sessionID)
|
||||||
|
defer fn()
|
||||||
|
ctx := context.Background()
|
||||||
|
sessions := testData
|
||||||
|
for _, session := range sessions {
|
||||||
|
groups := driver.FilterGroupsByName(session.Groups, "send_with_invalid_inputs")
|
||||||
|
for _, group := range groups {
|
||||||
|
for _, step := range group.Steps {
|
||||||
|
cont, err := en.Exec(ctx, []byte(step.Input))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !cont {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
w := bytes.NewBuffer(nil)
|
||||||
|
if _, err := en.Flush(ctx, w); err != nil {
|
||||||
|
t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b := w.Bytes()
|
||||||
|
|
||||||
|
// Extract the dynamic public key from the output
|
||||||
|
publicKey := extractPublicKey(b)
|
||||||
|
|
||||||
|
// Replace placeholder {public_key} with the actual dynamic public key
|
||||||
|
expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1)
|
||||||
|
step.ExpectedContent = string(expectedContent)
|
||||||
|
match, err := step.MatchesExpectedContent(b)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expectedContent, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMyAccount_Check_My_Balance(t *testing.T) {
|
||||||
|
en, fn, _ := testutil.TestEngine(sessionID)
|
||||||
|
defer fn()
|
||||||
|
ctx := context.Background()
|
||||||
|
sessions := testData
|
||||||
|
for _, session := range sessions {
|
||||||
|
groups := driver.FilterGroupsByName(session.Groups, "menu_my_account_check_my_balance")
|
||||||
|
for _, group := range groups {
|
||||||
|
for index, step := range group.Steps {
|
||||||
|
t.Logf("step %v with input %v", index, step.Input)
|
||||||
|
cont, err := en.Exec(ctx, []byte(step.Input))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !cont {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
w := bytes.NewBuffer(nil)
|
||||||
|
if _, err := en.Flush(ctx, w); err != nil {
|
||||||
|
t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err)
|
||||||
|
}
|
||||||
|
b := w.Bytes()
|
||||||
|
match, err := step.MatchesExpectedContent(b)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMainMenuHelp(t *testing.T) {
|
func TestMainMenuHelp(t *testing.T) {
|
||||||
en, fn, _ := testutil.TestEngine(sessionID)
|
en, fn, _ := testutil.TestEngine(sessionID)
|
||||||
defer fn()
|
defer fn()
|
||||||
@@ -201,6 +278,41 @@ func TestMainMenuQuit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMyAccount_Check_Community_Balance(t *testing.T) {
|
||||||
|
en, fn, _ := testutil.TestEngine(sessionID)
|
||||||
|
defer fn()
|
||||||
|
ctx := context.Background()
|
||||||
|
sessions := testData
|
||||||
|
for _, session := range sessions {
|
||||||
|
groups := driver.FilterGroupsByName(session.Groups, "menu_my_account_check_community_balance")
|
||||||
|
for _, group := range groups {
|
||||||
|
for index, step := range group.Steps {
|
||||||
|
t.Logf("step %v with input %v", index, step.Input)
|
||||||
|
cont, err := en.Exec(ctx, []byte(step.Input))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !cont {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
w := bytes.NewBuffer(nil)
|
||||||
|
if _, err := en.Flush(ctx, w); err != nil {
|
||||||
|
t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err)
|
||||||
|
}
|
||||||
|
b := w.Bytes()
|
||||||
|
match, err := step.MatchesExpectedContent(b)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMyAccount_MyAddress(t *testing.T) {
|
func TestMyAccount_MyAddress(t *testing.T) {
|
||||||
en, fn, _ := testutil.TestEngine(sessionID)
|
en, fn, _ := testutil.TestEngine(sessionID)
|
||||||
defer fn()
|
defer fn()
|
||||||
@@ -127,6 +127,72 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "menu_my_account_check_my_balance",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"input": "",
|
||||||
|
"expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "3",
|
||||||
|
"expectedContent": "My Account\n1:Profile\n2:Change language\n3:Check balances\n4:Check statement\n5:PIN options\n6:My Address\n0:Back"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "3",
|
||||||
|
"expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "1",
|
||||||
|
"expectedContent": "Please enter your PIN:"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "1235",
|
||||||
|
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "1",
|
||||||
|
"expectedContent": "Please enter your PIN:"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "1234",
|
||||||
|
"expectedContent": "Your balance is: 0.00 SRFYour account balance is 0.003 CELO"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "menu_my_account_check_community_balance",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"input": "",
|
||||||
|
"expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "3",
|
||||||
|
"expectedContent": "My Account\n1:Profile\n2:Change language\n3:Check balances\n4:Check statement\n5:PIN options\n6:My Address\n0:Back"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "3",
|
||||||
|
"expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "2",
|
||||||
|
"expectedContent": "Please enter your PIN:"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "1235",
|
||||||
|
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "1",
|
||||||
|
"expectedContent": "Please enter your PIN:"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": "1234",
|
||||||
|
"expectedContent": "Your balance is: 0.00 SRFYour account balance is 0.003 CELO"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "menu_my_account_my_address",
|
"name": "menu_my_account_my_address",
|
||||||
"steps": [
|
"steps": [
|
||||||
Reference in New Issue
Block a user