Compare commits

..

49 Commits

Author SHA1 Message Date
lash
1b712dbbb7 Instantiate config in main, add outputsize param 2024-09-05 04:11:20 +01:00
lash
3c6585e387 Add missing user db create 2024-09-05 04:07:45 +01:00
lash
7fc235c84c Draft example of dev-0.1.0 refactor 2024-09-05 03:29:44 +01:00
Carlosokumu
220c5b2081 remove commented code 2024-09-04 20:22:25 +03:00
Carlosokumu
2fc8a0e5a7 use fsDb for the resources 2024-09-04 19:57:08 +03:00
alfred-mk
8ffc5d67cc Removed mutex 2024-09-04 12:01:51 +03:00
alfred-mk
2d7b6bd125 Removed unused code 2024-09-03 22:28:48 +03:00
alfred-mk
5105e902f1 Process flags once and make them available across functions 2024-09-03 19:06:37 +03:00
Carlosokumu
19b2fa65fa Merge remote-tracking branch 'refs/remotes/origin/wip-flag-migration' into wip-flag-migration 2024-09-03 18:00:06 +03:00
Carlosokumu
ee4db50e00 remove unused code 2024-09-03 17:59:03 +03:00
alfred-mk
99d18322b2 Append to the correct flag set/reset 2024-09-03 15:34:19 +03:00
Carlosokumu
a07a8703f4 remove flag setting invalid pin entry 2024-09-03 15:13:55 +03:00
Carlosokumu
94f12edeaf Merge remote-tracking branch 'refs/remotes/origin/wip-flag-migration' into wip-flag-migration 2024-09-03 15:08:45 +03:00
Carlosokumu
fee51edade append sessionid to the gdbm file 2024-09-03 15:07:49 +03:00
alfred-mk
f90fde90fb Register flags in debugger from the csv 2024-09-03 15:04:09 +03:00
alfred-mk
ab6433168a Resolve bug on transaction confirmation PIN 2024-09-03 15:02:31 +03:00
Carlosokumu
0c360c0cc4 remove panics after failed execution 2024-09-03 13:19:32 +03:00
Carlosokumu
560714838f fix menu termination on invalid pin entry 2024-09-03 11:51:14 +03:00
Carlosokumu
c365eaa1d1 remove unused code 2024-09-02 21:24:16 +03:00
Carlosokumu
337419c9f2 Merge remote-tracking branch 'refs/remotes/origin/wip-flag-migration' into wip-flag-migration 2024-09-02 21:10:41 +03:00
Carlosokumu
d4c4db09f3 load profile data from gdbm 2024-09-02 21:09:39 +03:00
alfred-mk
3a3ddc9922 Use FlagParser on main.go and delete the flags.go file 2024-09-02 21:06:42 +03:00
Carlosokumu
1c1ea74088 Merge remote-tracking branch 'refs/remotes/origin/wip-flag-migration' into wip-flag-migration 2024-09-02 17:12:01 +03:00
Carlosokumu
14c8230fd4 code cleanup 2024-09-02 17:11:08 +03:00
alfred-mk
b6e4ba7ede Verify PIN using the one in the db 2024-09-02 17:08:57 +03:00
Carlosokumu
2b511aba4c remove commented code 2024-09-02 16:39:04 +03:00
Carlosokumu
626fca1a55 Merge remote-tracking branch 'refs/remotes/origin/wip-flag-migration' into wip-flag-migration 2024-09-02 16:30:58 +03:00
Carlosokumu
0c96831378 use gdbm 2024-09-02 16:05:13 +03:00
Carlosokumu
fe4df78f80 use gdbm for user info storage 2024-09-02 16:04:53 +03:00
alfred-mk
31be1fa221 Use flags from pp.csv on the menuhandler functions and tests 2024-09-02 14:00:02 +03:00
Carlosokumu
ba4d0abcda improve doc line 2024-09-02 10:03:57 +03:00
alfred-mk
632dd860ff Use flags from pp.csv on the Authorize function 2024-08-31 17:16:57 +03:00
Carlosokumu
07c8638a6d use correct symbol name 2024-08-31 15:09:34 +03:00
Carlosokumu
c71f6e76d7 Merge remote-tracking branch 'refs/remotes/origin/wip-flag-migration' into wip-flag-migration 2024-08-31 14:56:18 +03:00
Carlosokumu
2559b4e604 add test profile 2024-08-31 14:55:54 +03:00
alfred-mk
460e6fa2c8 Renamed duplicate flag 2024-08-31 14:50:45 +03:00
alfred-mk
c3c5c83662 Change the order of flags 2024-08-31 14:48:02 +03:00
alfred-mk
143c9b14a1 Renamed the flags 2024-08-31 12:58:48 +03:00
alfred-mk
42ab2dd577 Use preprocessor flags 2024-08-31 12:16:36 +03:00
alfred-mk
26959be149 Fixed bug on transaction pin node 2024-08-31 12:09:02 +03:00
Carlosokumu
118b5577bb Merge remote-tracking branch 'refs/remotes/origin/wip-flag-migration' into wip-flag-migration 2024-08-31 12:06:42 +03:00
Carlosokumu
4e69fa454d use string flags 2024-08-31 12:02:44 +03:00
alfred-mk
9fb3ef8e2a Use preprocessor flags 2024-08-31 11:56:36 +03:00
Carlosokumu
ec4201139a add _flag suffix 2024-08-31 11:35:29 +03:00
alfred-mk
bcc73dec23 Add the flag file during build 2024-08-31 11:27:47 +03:00
alfred-mk
828f9a17fc Added the csv containing string flags 2024-08-31 10:30:10 +03:00
Carlosokumu
de4aaeb02b use script dir for parser 2024-08-31 10:26:49 +03:00
Carlosokumu
cc323707fc setup parser 2024-08-31 10:21:20 +03:00
ef0c207fc4 Merge pull request 'wip-account-creation' (#4) from wip-account-creation into master
Reviewed-on: urdt/ussd#4
Reviewed-by: lash <accounts-grassrootseconomics@holbrook.no>
2024-08-30 14:37:58 +02:00
26 changed files with 665 additions and 512 deletions

View File

@@ -2,142 +2,173 @@ package main
import (
"context"
"encoding/csv"
"flag"
"fmt"
"io"
"os"
"path"
"strconv"
"git.defalsify.org/vise.git/cache"
fsdb "git.defalsify.org/vise.git/db/fs"
gdbmdb "git.defalsify.org/vise.git/db/gdbm"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
"git.grassecon.net/urdt/ussd/internal/models"
)
var (
scriptDir = path.Join("services", "registration")
logg = logging.NewVanilla()
flags *FlagParser
)
func getFlags(fp string, debug bool) error {
Flags = NewFlagParser().WithDebug()
flags, err := Flags.Load(fp)
if err != nil {
return err
}
}
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, pe *persist.Persister, userdataStore db.Db) (*ussd.Handlers, error) {
ussdHandlers, err := ussd.NewHandlers(appFlags pr.GetState(), dataStore)
if err != nil {
return nil, err
}
rs.AddLocalFunc("select_language", ussdHandlers.SetLanguage)
rs.AddLocalFunc("create_account", ussdHandlers.CreateAccount)
rs.AddLocalFunc("save_pin", ussdHandlers.SavePin)
rs.AddLocalFunc("verify_pin", ussdHandlers.VerifyPin)
rs.AddLocalFunc("check_identifier", ussdHandlers.CheckIdentifier)
rs.AddLocalFunc("check_account_status", ussdHandlers.CheckAccountStatus)
rs.AddLocalFunc("authorize_account", ussdHandlers.Authorize)
rs.AddLocalFunc("quit", ussdHandlers.Quit)
rs.AddLocalFunc("check_balance", ussdHandlers.CheckBalance)
rs.AddLocalFunc("validate_recipient", ussdHandlers.ValidateRecipient)
rs.AddLocalFunc("transaction_reset", ussdHandlers.TransactionReset)
rs.AddLocalFunc("max_amount", ussdHandlers.MaxAmount)
rs.AddLocalFunc("validate_amount", ussdHandlers.ValidateAmount)
rs.AddLocalFunc("reset_transaction_amount", ussdHandlers.ResetTransactionAmount)
rs.AddLocalFunc("get_recipient", ussdHandlers.GetRecipient)
rs.AddLocalFunc("get_sender", ussdHandlers.GetSender)
rs.AddLocalFunc("get_amount", ussdHandlers.GetAmount)
rs.AddLocalFunc("reset_incorrect", ussdHandlers.ResetIncorrectPin)
rs.AddLocalFunc("save_firstname", ussdHandlers.SaveFirstname)
rs.AddLocalFunc("save_familyname", ussdHandlers.SaveFamilyname)
rs.AddLocalFunc("save_gender", ussdHandlers.SaveGender)
rs.AddLocalFunc("save_location", ussdHandlers.SaveLocation)
rs.AddLocalFunc("save_yob", ussdHandlers.SaveYob)
rs.AddLocalFunc("save_offerings", ussdHandlers.SaveOfferings)
rs.AddLocalFunc("quit_with_balance", ussdHandlers.QuitWithBalance)
rs.AddLocalFunc("reset_account_authorized", ussdHandlers.ResetAccountAuthorized)
rs.AddLocalFunc("reset_allow_update", ussdHandlers.ResetAllowUpdate)
rs.AddLocalFunc("get_profile_info", ussdHandlers.GetProfileInfo)
rs.AddLocalFunc("verify_yob", ussdHandlers.VerifyYob)
rs.AddLocalFunc("reset_incorrect_date_format", ussdHandlers.ResetIncorrectYob)
rs.AddLocalFunc("set_reset_single_edit", ussdHandlers.SetResetSingleEdit)
rs.AddLocalFunc("initiate_transaction", ussdHandlers.InitiateTransaction)
return ussdHandlers, nil
}
func getPersister(dbDir string) (*persist.Persister, error) {
err = os.MkdirAll(dp, 0700)
if err != nil {
return nil, fmt.Errorf("state dir create exited with error: %v\n", err)
}
store := gdbmdb.NewGdbmDb()
storeFile := path.Join(dbDir, "states.gdbm")
store.Connect(ctx, storeFile)
pr := persist.NewPersister(store)
return pr
}
func getUserdataDb(dbDir string) {
store := gdbmdb.NewGdbmDb()
storeFile := path.Join(dbDir, "userdata.gdbm")
store.Connect(ctx, storeFile)
return store
}
func getResource(resourceDir string) (resource.Resource, error) {
store := fsdb.NewFsDb()
err = store.Connect(ctx, resourceDir)
if err != nil {
return err
}
rfs := resource.NewDbResource(store)
}
func getEngine(cfg Config, rs resource.Resource, pr *persister.Persister) {
en := engine.NewEngine(cfg, rfs)
en = en.WithPersister(pr)
return en
}
func main() {
var dir string
var dbDir string
var resourceDir string
var root string
var size uint
var sessionId string
flag.StringVar(&dir, "d", ".", "resource dir to read from")
flag.UintVar(&size, "s", 0, "max size of output")
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&root, "root", "root", "entry point symbol")
flag.StringVar(&sessionId, "session-id", "default", "session id")
flag.Parse()
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
logg.Infof("starting session", "symbol", root, "dbdir", dbDir, "sessionid", sessionId, "outsize", size)
ctx := context.Background()
st := state.NewState(16)
st.UseDebug()
state.FlagDebugger.Register(models.USERFLAG_LANGUAGE_SET, "LANGUAGE_CHANGE")
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_CREATED, "ACCOUNT_CREATED")
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_SUCCESS, "ACCOUNT_SUCCESS")
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_PENDING, "ACCOUNT_PENDING")
state.FlagDebugger.Register(models.USERFLAG_INCORRECTPIN, "INCORRECTPIN")
state.FlagDebugger.Register(models.USERFLAG_INCORRECTDATEFORMAT, "INVALIDDATEFORMAT")
state.FlagDebugger.Register(models.USERFLAG_INVALID_RECIPIENT, "INVALIDRECIPIENT")
state.FlagDebugger.Register(models.USERFLAG_PINMISMATCH, "PINMISMATCH")
state.FlagDebugger.Register(models.USERFLAG_PIN_SET, "PIN_SET")
state.FlagDebugger.Register(models.USERFLAG_INVALID_RECIPIENT_WITH_INVITE, "INVALIDRECIPIENT_WITH_INVITE")
state.FlagDebugger.Register(models.USERFLAG_INVALID_AMOUNT, "INVALIDAMOUNT")
state.FlagDebugger.Register(models.USERFLAG_ALLOW_UPDATE, "UNLOCKFORUPDATE")
state.FlagDebugger.Register(models.USERFLAG_VALIDPIN, "VALIDPIN")
state.FlagDebugger.Register(models.USERFLAG_VALIDPIN, "ACCOUNTUNLOCKED")
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_CREATION_FAILED, "ACCOUNT_CREATION_FAILED")
state.FlagDebugger.Register(models.USERFLAG_SINGLE_EDIT, "SINGLEEDIT")
rfs := resource.NewFsResource(scriptDir)
ca := cache.NewCache()
cfg := engine.Config{
Root: "root",
SessionId: sessionId,
}
dp := path.Join(scriptDir, ".state")
err := os.MkdirAll(dp, 0700)
fl, err := getFlags()
if err != nil {
fmt.Fprintf(os.Stderr, "state dir create exited with error: %v\n", err)
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
pr := persist.NewFsPersister(dp)
en, err := engine.NewPersistedEngine(ctx, cfg, pr, rfs)
rs, err := getResource(resourceDir)
if err != nil {
pr = pr.WithContent(&st, ca)
err = pr.Save(cfg.SessionId)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to save state with error: %v\n", err)
}
en, err = engine.NewPersistedEngine(ctx, cfg, pr, rfs)
if err != nil {
fmt.Fprintf(os.Stderr, "engine create exited with error: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
fp := path.Join(dp, sessionId)
pr, err := getDataPersister(dbDir)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
ussdHandlers := ussd.NewHandlers(fp, &st)
store, err := getUserdataDb(dataDir)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
rfs.AddLocalFunc("select_language", ussdHandlers.SetLanguage)
rfs.AddLocalFunc("create_account", ussdHandlers.CreateAccount)
rfs.AddLocalFunc("save_pin", ussdHandlers.SavePin)
rfs.AddLocalFunc("verify_pin", ussdHandlers.VerifyPin)
rfs.AddLocalFunc("check_identifier", ussdHandlers.CheckIdentifier)
rfs.AddLocalFunc("check_account_status", ussdHandlers.CheckAccountStatus)
rfs.AddLocalFunc("authorize_account", ussdHandlers.Authorize)
rfs.AddLocalFunc("quit", ussdHandlers.Quit)
rfs.AddLocalFunc("check_balance", ussdHandlers.CheckBalance)
rfs.AddLocalFunc("validate_recipient", ussdHandlers.ValidateRecipient)
rfs.AddLocalFunc("transaction_reset", ussdHandlers.TransactionReset)
rfs.AddLocalFunc("max_amount", ussdHandlers.MaxAmount)
rfs.AddLocalFunc("validate_amount", ussdHandlers.ValidateAmount)
rfs.AddLocalFunc("reset_transaction_amount", ussdHandlers.ResetTransactionAmount)
rfs.AddLocalFunc("get_recipient", ussdHandlers.GetRecipient)
rfs.AddLocalFunc("get_sender", ussdHandlers.GetSender)
rfs.AddLocalFunc("get_amount", ussdHandlers.GetAmount)
rfs.AddLocalFunc("reset_incorrect", ussdHandlers.ResetIncorrectPin)
rfs.AddLocalFunc("save_firstname", ussdHandlers.SaveFirstname)
rfs.AddLocalFunc("save_familyname", ussdHandlers.SaveFamilyname)
rfs.AddLocalFunc("save_gender", ussdHandlers.SaveGender)
rfs.AddLocalFunc("save_location", ussdHandlers.SaveLocation)
rfs.AddLocalFunc("save_yob", ussdHandlers.SaveYob)
rfs.AddLocalFunc("save_offerings", ussdHandlers.SaveOfferings)
rfs.AddLocalFunc("quit_with_balance", ussdHandlers.QuitWithBalance)
rfs.AddLocalFunc("reset_account_authorized", ussdHandlers.ResetAccountAuthorized)
rfs.AddLocalFunc("reset_allow_update", ussdHandlers.ResetAllowUpdate)
rfs.AddLocalFunc("get_profile_info", ussdHandlers.GetProfileInfo)
rfs.AddLocalFunc("verify_yob", ussdHandlers.VerifyYob)
rfs.AddLocalFunc("reset_incorrect_date_format", ussdHandlers.ResetIncorrectYob)
rfs.AddLocalFunc("set_reset_single_edit", ussdHandlers.SetResetSingleEdit)
rfs.AddLocalFunc("initiate_transaction", ussdHandlers.InitiateTransaction)
hn, err := getHandlers(fl, rs, pr, store)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
cont, err := en.Init(ctx)
en.SetDebugger(engine.NewSimpleDebug(nil))
cfg := engine.Config{
Root: sym,
SessionId: sessionId,
OutputSize: size,
FlagCount: uint32(16),
}
en := getEngine(cfg, rs, pr)
_, err = en.Init(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err)
os.Exit(1)
}
if !cont {
_, err = en.WriteResult(ctx, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "dead init write error: %v\n", err)
os.Exit(1)
}
err = en.Finish()
if err != nil {
fmt.Fprintf(os.Stderr, "engine finish error: %v\n", err)
os.Exit(1)
}
os.Stdout.Write([]byte{0x0a})
os.Exit(0)
}
err = engine.Loop(ctx, en, os.Stdin, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)

Submodule go-vise updated: 1f47a674d9...326bdb5018

File diff suppressed because it is too large Load Diff

View File

@@ -3,6 +3,7 @@ package ussd
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
@@ -20,6 +21,15 @@ type MockAccountService struct {
mock.Mock
}
type MockFlagParser struct {
mock.Mock
}
func (m *MockFlagParser) GetFlag(key string) (uint32, error) {
args := m.Called(key)
return args.Get(0).(uint32), args.Error(1)
}
func (m *MockAccountService) CreateAccount() (*models.AccountResponse, error) {
args := m.Called()
return args.Get(0).(*models.AccountResponse), args.Error(1)
@@ -69,11 +79,20 @@ func TestCreateAccount(t *testing.T) {
// Set up expectations for the mock account service
mockAccountService.On("CreateAccount").Return(mockAccountResponse, nil)
mockParser := new(MockFlagParser)
flag_account_created := uint32(1)
flag_account_creation_failed := uint32(2)
mockParser.On("GetFlag", "flag_account_created").Return(flag_account_created, nil)
mockParser.On("GetFlag", "flag_account_creation_failed").Return(flag_account_creation_failed, nil)
// Initialize Handlers with mock account service
h := &Handlers{
fs: &FSData{Path: accountFilePath},
accountFileHandler: accountFileHandler,
accountService: mockAccountService,
parser: mockParser,
}
tests := []struct {
@@ -86,7 +105,7 @@ func TestCreateAccount(t *testing.T) {
name: "New account creation",
existingData: nil,
expectedResult: resource.Result{
FlagSet: []uint32{models.USERFLAG_ACCOUNT_CREATED},
FlagSet: []uint32{flag_account_created},
},
expectedData: map[string]string{
"TrackingId": "test-tracking-id",
@@ -247,10 +266,16 @@ func TestSavePin(t *testing.T) {
// Create a new AccountFileHandler and set it in the Handlers struct
accountFileHandler := utils.NewAccountFileHandler(accountFilePath)
mockParser := new(MockFlagParser)
h := &Handlers{
accountFileHandler: accountFileHandler,
parser: mockParser,
}
flag_incorrect_pin := uint32(1)
mockParser.On("GetFlag", "flag_incorrect_pin").Return(flag_incorrect_pin, nil)
tests := []struct {
name string
input []byte
@@ -271,21 +296,21 @@ func TestSavePin(t *testing.T) {
{
name: "Invalid PIN - non-numeric",
input: []byte("12ab"),
expectedFlags: []uint32{models.USERFLAG_INCORRECTPIN},
expectedFlags: []uint32{flag_incorrect_pin},
expectedData: initialAccountData, // No changes expected
expectedErrors: false,
},
{
name: "Invalid PIN - less than 4 digits",
input: []byte("123"),
expectedFlags: []uint32{models.USERFLAG_INCORRECTPIN},
expectedFlags: []uint32{flag_incorrect_pin},
expectedData: initialAccountData, // No changes expected
expectedErrors: false,
},
{
name: "Invalid PIN - more than 4 digits",
input: []byte("12345"),
expectedFlags: []uint32{models.USERFLAG_INCORRECTPIN},
expectedFlags: []uint32{flag_incorrect_pin},
expectedData: initialAccountData, // No changes expected
expectedErrors: false,
},
@@ -293,7 +318,6 @@ func TestSavePin(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Ensure the file exists before running the test
err := accountFileHandler.EnsureFileExists()
if err != nil {
t.Fatalf("Failed to ensure account file exists: %v", err)
@@ -876,10 +900,6 @@ func TestGetAmount(t *testing.T) {
}
func TestGetProfileInfo(t *testing.T) {
// Create a new instance of MockAccountFileHandler
mockFileHandler := new(mocks.MockAccountFileHandler)
// Define test cases
tests := []struct {
name string
accountData map[string]string
@@ -895,11 +915,52 @@ func TestGetProfileInfo(t *testing.T) {
"Gender": "Male",
"YOB": "1980",
"Location": "Mombasa",
"Offerings": "Bananas",
"Offerings": "Product A",
},
readError: nil,
expectedResult: resource.Result{
Content: "Name: John Doe\nGender: Male\nAge: 44\nLocation: Mombasa\nYou provide: Bananas\n",
Content: fmt.Sprintf(
"Name: %s %s\nGender: %s\nAge: %d\nLocation: %s\nYou provide: %s\n",
"John", "Doe", "Male", 44, "Mombasa", "Product A",
),
},
expectedError: nil,
},
{
name: "Profile with Not Provided Fields",
accountData: map[string]string{
"FirstName": "Not provided",
"FamilyName": "Doe",
"Gender": "Female",
"YOB": "1995",
"Location": "Not provided",
"Offerings": "Service B",
},
readError: nil,
expectedResult: resource.Result{
Content: fmt.Sprintf(
"Name: %s\nGender: %s\nAge: %d\nLocation: %s\nYou provide: %s\n",
"Not provided", "Female", 29, "Not provided", "Service B",
),
},
expectedError: nil,
},
{
name: "Profile with YOB as Not provided",
accountData: map[string]string{
"FirstName": "Not provided",
"FamilyName": "Doe",
"Gender": "Female",
"YOB": "Not provided",
"Location": "Not provided",
"Offerings": "Service B",
},
readError: nil,
expectedResult: resource.Result{
Content: fmt.Sprintf(
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
"Not provided", "Female", "Not provided", "Not provided", "Service B",
),
},
expectedError: nil,
},
@@ -907,6 +968,8 @@ func TestGetProfileInfo(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a new instance of MockAccountFileHandler
mockFileHandler := new(mocks.MockAccountFileHandler)
// Set up the mock expectations
mockFileHandler.On("ReadAccountData").Return(tt.accountData, tt.readError)
@@ -918,6 +981,7 @@ func TestGetProfileInfo(t *testing.T) {
// Call the method
result, err := h.GetProfileInfo(context.Background(), "get_profile_info", nil)
// Assert the results
assert.Equal(t, tt.expectedResult, result)
assert.Equal(t, tt.expectedError, err)

View File

@@ -1,22 +0,0 @@
package models
import "git.defalsify.org/vise.git/state"
const (
USERFLAG_LANGUAGE_SET = iota + state.FLAG_USERSTART
USERFLAG_ACCOUNT_CREATED
USERFLAG_ACCOUNT_PENDING
USERFLAG_ACCOUNT_SUCCESS
USERFLAG_ACCOUNT_AUTHORIZED
USERFLAG_INVALID_RECIPIENT
USERFLAG_INVALID_RECIPIENT_WITH_INVITE
USERFLAG_INCORRECTPIN
USERFLAG_ALLOW_UPDATE
USERFLAG_INVALID_AMOUNT
USERFLAG_PIN_SET
USERFLAG_VALIDPIN
USERFLAG_PINMISMATCH
USERFLAG_INCORRECTDATEFORMAT
USERFLAG_ACCOUNT_CREATION_FAILED
USERFLAG_SINGLE_EDIT
)

View File

@@ -1,25 +1,25 @@
package utils
import (
"context"
"encoding/json"
"os"
"git.defalsify.org/vise.git/db"
)
type AccountFileHandler struct {
FilePath string
store db.Db
}
func NewAccountFileHandler(path string) *AccountFileHandler {
return &AccountFileHandler{FilePath: path}
}
func (afh *AccountFileHandler) ReadAccountData() (map[string]string, error) {
jsonData, err := os.ReadFile(afh.FilePath)
if err != nil {
return nil, err
func NewAccountFileHandler(store db.Db) *AccountFileHandler {
return &AccountFileHandler{
store: store,
}
}
func (afh *AccountFileHandler) ReadAccountData(ctx context.Context, sessionId string) (map[string]string, error) {
var accountData map[string]string
jsonData, err := ReadEntry(ctx, afh.store, sessionId, DATA_ACCOUNT)
err = json.Unmarshal(jsonData, &accountData)
if err != nil {
return nil, err
@@ -28,19 +28,10 @@ func (afh *AccountFileHandler) ReadAccountData() (map[string]string, error) {
return accountData, nil
}
func (afh *AccountFileHandler) WriteAccountData(accountData map[string]string) error {
jsonData, err := json.Marshal(accountData)
func (afh *AccountFileHandler) WriteAccountData(ctx context.Context, sessionId string, accountData map[string]string) error {
b, err := json.Marshal(accountData)
if err != nil {
return err
}
return os.WriteFile(afh.FilePath, jsonData, 0644)
}
func (afh *AccountFileHandler) EnsureFileExists() error {
f, err := os.OpenFile(afh.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
return f.Close()
return WriteEntry(ctx, afh.store, sessionId, DATA_ACCOUNT, b)
}

48
internal/utils/db.go Normal file
View File

@@ -0,0 +1,48 @@
package utils
import (
"context"
"encoding/binary"
"git.defalsify.org/vise.git/db"
)
type DataTyp uint16
const (
DATA_ACCOUNT DataTyp = iota
DATA_ACCOUNT_CREATED
DATA_TRACKING_ID
DATA_PUBLIC_KEY
DATA_CUSTODIAL_ID
)
func typToBytes(typ DataTyp) []byte {
var b []byte
binary.BigEndian.PutUint16(b, uint16(typ))
return b
}
func packKey(typ DataTyp, data []byte) []byte {
v := typToBytes(typ)
return append(v, data...)
}
func ReadEntry(ctx context.Context, store db.Db, sessionId string, typ DataTyp) ([]byte, error) {
store.SetPrefix(db.DATATYPE_USERDATA)
store.SetSession(sessionId)
k := packKey(typ, []byte(sessionId))
b, err := store.Get(ctx, k)
if err != nil {
return nil, err
}
return b, nil
}
func WriteEntry(ctx context.Context, store db.Db, sessionId string, typ DataTyp, value []byte) error {
store.SetPrefix(db.DATATYPE_USERDATA)
store.SetSession(sessionId)
k := packKey(typ, []byte(sessionId))
return store.Put(ctx, k, value)
}

View File

@@ -1,13 +1 @@
package utils
type AccountFileHandlerInterface interface {
EnsureFileExists() error
ReadAccountData() (map[string]string, error)
WriteAccountData(data map[string]string) error
}

View File

@@ -4,7 +4,7 @@ TXTS = $(wildcard ./*.txt.orig)
# Rule to build .bin files from .vis files
%.vis:
go run ../../go-vise/dev/asm $(basename $@).vis > $(basename $@).bin
go run ../../go-vise/dev/asm -f pp.csv $(basename $@).vis > $(basename $@).bin
@echo "Built $(basename $@).bin from $(basename $@).vis"
# Rule to copy .orig files to .txt

View File

@@ -1,4 +1,4 @@
RELOAD verify_pin
CATCH create_pin_mismatch 20 1
CATCH create_pin_mismatch flag_pin_mismatch 1
LOAD quit 0
HALT

View File

@@ -1,3 +1,3 @@
RELOAD check_account_status
CATCH main 11 1
CATCH main flag_account_success 1
HALT

View File

@@ -5,7 +5,7 @@ MOUT back 0
HALT
LOAD validate_amount 64
RELOAD validate_amount
CATCH invalid_amount 17 1
CATCH invalid_amount flag_invalid_amount 1
INCMP _ 0
LOAD get_recipient 12
LOAD get_sender 64

View File

@@ -1,4 +1,4 @@
LOAD reset_unlocked 0
LOAD reset_account_authorized 0
MOUT my_balance 1
MOUT community_balance 2
MOUT back 0

View File

@@ -1,5 +1,5 @@
LOAD reset_incorrect 0
CATCH incorrect_pin 15 1
CATCH pin_entry 12 0
CATCH incorrect_pin flag_incorrect_pin 1
CATCH pin_entry flag_account_authorized 0
LOAD quit_with_balance 0
HALT

View File

@@ -1,9 +1,9 @@
LOAD create_account 0
CATCH account_creation_failed 22 1
CATCH account_creation_failed flag_account_creation_failed 1
MOUT exit 0
HALT
LOAD save_pin 0
RELOAD save_pin
CATCH . 15 1
CATCH . flag_incorrect_pin 1
INCMP quit 0
INCMP confirm_create_pin *

View File

@@ -1,12 +1,9 @@
CATCH incorrect_date_format 21 1
CATCH incorrect_date_format flag_incorrect_date_format 1
LOAD save_yob 0
CATCH update_success 16 1
CATCH update_success flag_allow_update 1
MOUT back 0
HALT
INCMP _ 0
LOAD save_location 0
CATCH pin_entry 23 1
CATCH pin_entry flag_single_edit 1
INCMP enter_offerings *

View File

@@ -1,6 +1,6 @@
LOAD save_location 0
CATCH incorrect_pin 15 1
CATCH update_success 16 1
CATCH incorrect_pin flag_incorrect_pin 1
CATCH update_success flag_allow_update 1
MOUT back 0
HALT
LOAD save_offerings 0

View File

@@ -1,9 +1,9 @@
LOAD save_gender 0
CATCH update_success 16 1
CATCH update_success flag_allow_update 1
MOUT back 0
HALT
INCMP _ 0
LOAD verify_yob 8
LOAD save_yob 0
CATCH pin_entry 23 1
CATCH pin_entry flag_single_edit 1
INCMP enter_location *

View File

@@ -1,5 +1,5 @@
LOAD reset_incorrect 0
CATCH incorrect_pin 15 1
CATCH pin_entry 12 0
CATCH incorrect_pin flag_incorrect_pin 1
CATCH pin_entry flag_account_authorized 0
LOAD quit_with_balance 0
HALT

View File

@@ -0,0 +1,16 @@
flag,flag_language_set,8,checks whether the user has set their prefered language
flag,flag_account_created,9,this is set when an account has been created on the API
flag,flag_account_creation_failed,10,this is set when there's an error from the API during account creation
flag,flag_account_pending,11,this is set when an account does not have a status of SUCCESS
flag,flag_account_success,12,this is set when an account has a status of SUCCESS
flag,flag_pin_mismatch,13,this is set when the confirmation PIN matches the initial PIN during registration
flag,flag_pin_set,14,this is set when a newly registered user sets a PIN. This must be present for an account to access the main menu
flag,flag_account_authorized,15,this is set to allow a user access guarded nodes after providing a correct PIN
flag,flag_invalid_recipient,16,this is set when the transaction recipient is invalid
flag,flag_invalid_recipient_with_invite,17,this is set when the transaction recipient is valid but not on the platform
flag,flag_invalid_amount,18,this is set when the given transaction amount is invalid
flag,flag_incorrect_pin,19,this is set when the provided PIN is invalid or does not match the current account's PIN
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_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
1 flag flag_language_set 8 checks whether the user has set their prefered language
2 flag flag_account_created 9 this is set when an account has been created on the API
3 flag flag_account_creation_failed 10 this is set when there's an error from the API during account creation
4 flag flag_account_pending 11 this is set when an account does not have a status of SUCCESS
5 flag flag_account_success 12 this is set when an account has a status of SUCCESS
6 flag flag_pin_mismatch 13 this is set when the confirmation PIN matches the initial PIN during registration
7 flag flag_pin_set 14 this is set when a newly registered user sets a PIN. This must be present for an account to access the main menu
8 flag flag_account_authorized 15 this is set to allow a user access guarded nodes after providing a correct PIN
9 flag flag_invalid_recipient 16 this is set when the transaction recipient is invalid
10 flag flag_invalid_recipient_with_invite 17 this is set when the transaction recipient is valid but not on the platform
11 flag flag_invalid_amount 18 this is set when the given transaction amount is invalid
12 flag flag_incorrect_pin 19 this is set when the provided PIN is invalid or does not match the current account's PIN
13 flag flag_valid_pin 20 this is set when the given PIN is valid
14 flag flag_allow_update 21 this is set to allow a user to update their profile data
15 flag flag_single_edit 22 this is set to allow a user to edit a single profile item such as year of birth
16 flag flag_incorrect_date_format 23 this is set when the given year of birth is invalid

View File

@@ -1,7 +1,7 @@
CATCH select_language 8 0
CATCH terms 9 0
CATCH select_language flag_language_set 0
CATCH terms flag_account_created 0
LOAD check_account_status 0
CATCH account_pending 10 1
CATCH create_pin 18 0
CATCH main 11 1
CATCH account_pending flag_account_pending 1
CATCH create_pin flag_pin_set 0
CATCH main flag_account_success 1
HALT

View File

@@ -1,12 +1,12 @@
LOAD save_familyname 0
CATCH update_success 16 1
CATCH update_success flag_allow_update 1
MOUT male 1
MOUT female 2
MOUT unspecified 3
MOUT back 0
HALT
LOAD save_gender 0
CATCH pin_entry 23 1
CATCH pin_entry flag_single_edit 1
INCMP _ 0
INCMP enter_yob 1
INCMP enter_yob 2

View File

@@ -3,6 +3,6 @@ MOUT back 0
HALT
LOAD validate_recipient 20
RELOAD validate_recipient
CATCH invalid_recipient 13 1
CATCH invalid_recipient flag_invalid_recipient 1
INCMP _ 0
INCMP amount *

View File

@@ -1,4 +1,6 @@
LOAD reset_incorrect 0
LOAD reset_incorrect 6
CATCH incorrect_pin flag_incorrect_pin 1
CATCH _ flag_account_authorized 0
LOAD get_amount 10
MAP get_amount
RELOAD get_recipient

View File

@@ -6,10 +6,9 @@ MAP get_sender
MOUT back 0
MOUT quit 9
HALT
LOAD authorize_account 1
LOAD authorize_account 6
RELOAD authorize_account
CATCH incorrect_pin 15 1
CATCH incorrect_pin flag_incorrect_pin 1
INCMP _ 0
INCMP quit 9
MOVE transaction_initiated
INCMP transaction_initiated *

View File

@@ -1,8 +1,8 @@
LOAD get_profile_info 0
MAP get_profile_info
LOAD reset_incorrect 0
CATCH incorrect_pin 15 1
CATCH pin_entry 12 0
LOAD reset_incorrect 6
CATCH incorrect_pin flag_incorrect_pin 1
CATCH pin_entry flag_account_authorized 0
MOUT back 0
HALT
INCMP _ 0