Compare commits
No commits in common. "4cc0de59a7c8bb7290da317066aa9c18def83b76" and "ecee5b4dee6720eff843afecf002415ca47eac97" have entirely different histories.
4cc0de59a7
...
ecee5b4dee
@ -70,6 +70,7 @@ type MenuHandlers struct {
|
||||
st *state.State
|
||||
ca cache.Memory
|
||||
userdataStore store.DataStore
|
||||
adminstore *store.AdminStore
|
||||
flagManager *FlagManager
|
||||
accountService remote.AccountService
|
||||
prefixDb storedb.PrefixDb
|
||||
@ -78,7 +79,7 @@ type MenuHandlers struct {
|
||||
}
|
||||
|
||||
// NewHandlers creates a new instance of the Handlers struct with the provided dependencies.
|
||||
func NewMenuHandlers(appFlags *FlagManager, userdataStore db.Db, accountService remote.AccountService, replaceSeparatorFunc func(string) string) (*MenuHandlers, error) {
|
||||
func NewMenuHandlers(appFlags *FlagManager, userdataStore db.Db, adminstore *store.AdminStore, accountService remote.AccountService, replaceSeparatorFunc func(string) string) (*MenuHandlers, error) {
|
||||
if userdataStore == nil {
|
||||
return nil, fmt.Errorf("cannot create handler with nil userdata store")
|
||||
}
|
||||
@ -93,6 +94,7 @@ func NewMenuHandlers(appFlags *FlagManager, userdataStore db.Db, accountService
|
||||
h := &MenuHandlers{
|
||||
userdataStore: userDb,
|
||||
flagManager: appFlags,
|
||||
adminstore: adminstore,
|
||||
accountService: accountService,
|
||||
prefixDb: prefixDb,
|
||||
profile: &profile.Profile{Max: 6},
|
||||
|
@ -14,11 +14,11 @@ import (
|
||||
"git.defalsify.org/vise.git/persist"
|
||||
"git.defalsify.org/vise.git/resource"
|
||||
"git.defalsify.org/vise.git/state"
|
||||
"git.grassecon.net/grassrootseconomics/common/pin"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/testservice"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||
"git.grassecon.net/grassrootseconomics/common/pin"
|
||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||
|
||||
"github.com/alecthomas/assert/v2"
|
||||
@ -84,7 +84,7 @@ func TestNewMenuHandlers(t *testing.T) {
|
||||
|
||||
// Test case for valid UserDataStore
|
||||
t.Run("Valid UserDataStore", func(t *testing.T) {
|
||||
handlers, err := NewMenuHandlers(fm, store, &accountService, mockReplaceSeparator)
|
||||
handlers, err := NewMenuHandlers(fm, store, nil, &accountService, mockReplaceSeparator)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
@ -108,7 +108,7 @@ func TestNewMenuHandlers(t *testing.T) {
|
||||
|
||||
// Test case for nil UserDataStore
|
||||
t.Run("Nil UserDataStore", func(t *testing.T) {
|
||||
handlers, err := NewMenuHandlers(fm, nil, &accountService, mockReplaceSeparator)
|
||||
handlers, err := NewMenuHandlers(fm, nil, nil, &accountService, mockReplaceSeparator)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error, got none")
|
||||
}
|
||||
@ -132,9 +132,16 @@ func TestInit(t *testing.T) {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
adminstore, err := store.NewAdminStore(ctx, "admin_numbers")
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
st := state.NewState(128)
|
||||
ca := cache.NewCache()
|
||||
|
||||
flag_admin_privilege, _ := fm.GetFlag("flag_admin_privilege")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func() (*MenuHandlers, context.Context)
|
||||
@ -155,12 +162,15 @@ func TestInit(t *testing.T) {
|
||||
pe := persist.NewPersister(testStore).WithSession(sessionId).WithContent(st, ca)
|
||||
h := &MenuHandlers{
|
||||
flagManager: fm,
|
||||
adminstore: adminstore,
|
||||
pe: pe,
|
||||
}
|
||||
return h, context.WithValue(ctx, "SessionId", sessionId)
|
||||
},
|
||||
input: []byte("1"),
|
||||
expectedResult: resource.Result{},
|
||||
input: []byte("1"),
|
||||
expectedResult: resource.Result{
|
||||
FlagReset: []uint32{flag_admin_privilege},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Non-admin session initialization",
|
||||
@ -168,12 +178,15 @@ func TestInit(t *testing.T) {
|
||||
pe := persist.NewPersister(testStore).WithSession("0712345678").WithContent(st, ca)
|
||||
h := &MenuHandlers{
|
||||
flagManager: fm,
|
||||
adminstore: adminstore,
|
||||
pe: pe,
|
||||
}
|
||||
return h, context.WithValue(context.Background(), "SessionId", "0712345678")
|
||||
},
|
||||
input: []byte("1"),
|
||||
expectedResult: resource.Result{},
|
||||
input: []byte("1"),
|
||||
expectedResult: resource.Result{
|
||||
FlagReset: []uint32{flag_admin_privilege},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Move to top node on empty input",
|
||||
@ -181,13 +194,16 @@ func TestInit(t *testing.T) {
|
||||
pe := persist.NewPersister(testStore).WithSession(sessionId).WithContent(st, ca)
|
||||
h := &MenuHandlers{
|
||||
flagManager: fm,
|
||||
adminstore: adminstore,
|
||||
pe: pe,
|
||||
}
|
||||
st.Code = []byte("some pending bytecode")
|
||||
return h, context.WithValue(ctx, "SessionId", sessionId)
|
||||
},
|
||||
input: []byte(""),
|
||||
expectedResult: resource.Result{},
|
||||
input: []byte(""),
|
||||
expectedResult: resource.Result{
|
||||
FlagReset: []uint32{flag_admin_privilege},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||
)
|
||||
|
||||
type HandlerService interface {
|
||||
@ -22,6 +23,7 @@ type LocalHandlerService struct {
|
||||
DbRs *resource.DbResource
|
||||
Pe *persist.Persister
|
||||
UserdataStore *db.Db
|
||||
AdminStore *store.AdminStore
|
||||
Cfg engine.Config
|
||||
Rs resource.Resource
|
||||
}
|
||||
@ -34,12 +36,16 @@ func NewLocalHandlerService(ctx context.Context, fp string, debug bool, dbResour
|
||||
if debug {
|
||||
parser.SetDebug()
|
||||
}
|
||||
|
||||
adminstore, err := store.NewAdminStore(ctx, "admin_numbers")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &LocalHandlerService{
|
||||
Parser: parser,
|
||||
DbRs: dbResource,
|
||||
Cfg: cfg,
|
||||
Rs: rs,
|
||||
Parser: parser,
|
||||
DbRs: dbResource,
|
||||
AdminStore: adminstore,
|
||||
Cfg: cfg,
|
||||
Rs: rs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -56,7 +62,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
|
||||
return strings.ReplaceAll(input, ":", ls.Cfg.MenuSeparator)
|
||||
}
|
||||
|
||||
appHandlers, err := application.NewMenuHandlers(ls.Parser, *ls.UserdataStore, accountService, replaceSeparatorFunc)
|
||||
appHandlers, err := application.NewMenuHandlers(ls.Parser, *ls.UserdataStore, ls.AdminStore, accountService, replaceSeparatorFunc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
51
store/adminstore.go
Normal file
51
store/adminstore.go
Normal file
@ -0,0 +1,51 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
fsdb "git.defalsify.org/vise.git/db/fs"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla().WithDomain("adminstore")
|
||||
)
|
||||
|
||||
type AdminStore struct {
|
||||
ctx context.Context
|
||||
FsStore db.Db
|
||||
}
|
||||
|
||||
func NewAdminStore(ctx context.Context, fileName string) (*AdminStore, error) {
|
||||
fsStore, err := getFsStore(ctx, fileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &AdminStore{ctx: ctx, FsStore: fsStore}, nil
|
||||
}
|
||||
|
||||
func getFsStore(ctx context.Context, connectStr string) (db.Db, error) {
|
||||
fsStore := fsdb.NewFsDb()
|
||||
err := fsStore.Connect(ctx, connectStr)
|
||||
fsStore.SetPrefix(db.DATATYPE_USERDATA)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fsStore, nil
|
||||
}
|
||||
|
||||
// Checks if the given sessionId is listed as an admin.
|
||||
func (as *AdminStore) IsAdmin(sessionId string) (bool, error) {
|
||||
_, err := as.FsStore.Get(as.ctx, []byte(sessionId))
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
logg.Printf(logging.LVL_INFO, "Returning false because session id was not found")
|
||||
return false, nil
|
||||
} else {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
@ -6,15 +6,10 @@ import (
|
||||
"math/big"
|
||||
"strings"
|
||||
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla().WithDomain("vouchers").WithContextKey("SessionId")
|
||||
)
|
||||
|
||||
// VoucherMetadata helps organize data fields
|
||||
type VoucherMetadata struct {
|
||||
Symbols string
|
||||
|
Loading…
Reference in New Issue
Block a user