Compare commits
No commits in common. "c7bdfe90b7d3056942e0b8ecf9999d44b4ae07a0" and "60134f14e9bc0fe9e5f85645623f09338219b0e7" have entirely different histories.
c7bdfe90b7
...
60134f14e9
@ -9,7 +9,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/db"
|
"git.defalsify.org/vise.git/db"
|
||||||
"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/mocks"
|
"git.grassecon.net/urdt/ussd/internal/mocks"
|
||||||
@ -17,7 +16,6 @@ import (
|
|||||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||||
"github.com/alecthomas/assert/v2"
|
"github.com/alecthomas/assert/v2"
|
||||||
testdataloader "github.com/peteole/testdata-loader"
|
testdataloader "github.com/peteole/testdata-loader"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -96,25 +94,6 @@ func TestCreateAccount(t *testing.T) {
|
|||||||
mockDataStore.AssertExpectations(t)
|
mockDataStore.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithPersister(t *testing.T) {
|
|
||||||
// Test case: Setting a persister
|
|
||||||
h := &Handlers{}
|
|
||||||
p := &persist.Persister{}
|
|
||||||
|
|
||||||
result := h.WithPersister(p)
|
|
||||||
|
|
||||||
assert.Equal(t, p, h.pe, "The persister should be set correctly.")
|
|
||||||
assert.Equal(t, h, result, "The returned handler should be the same instance.")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithPersister_PanicWhenAlreadySet(t *testing.T) {
|
|
||||||
// Test case: Panic on multiple calls
|
|
||||||
h := &Handlers{pe: &persist.Persister{}}
|
|
||||||
require.Panics(t, func() {
|
|
||||||
h.WithPersister(&persist.Persister{})
|
|
||||||
}, "Should panic when trying to set a persister again.")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSaveFirstname(t *testing.T) {
|
func TestSaveFirstname(t *testing.T) {
|
||||||
// Create a new instance of MockMyDataStore
|
// Create a new instance of MockMyDataStore
|
||||||
mockStore := new(mocks.MockUserDataStore)
|
mockStore := new(mocks.MockUserDataStore)
|
||||||
@ -316,7 +295,6 @@ func TestSaveOfferings(t *testing.T) {
|
|||||||
func TestSaveGender(t *testing.T) {
|
func TestSaveGender(t *testing.T) {
|
||||||
// Create a new instance of MockMyDataStore
|
// Create a new instance of MockMyDataStore
|
||||||
mockStore := new(mocks.MockUserDataStore)
|
mockStore := new(mocks.MockUserDataStore)
|
||||||
mockState := state.NewState(16)
|
|
||||||
|
|
||||||
// Define the session ID and context
|
// Define the session ID and context
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
@ -328,29 +306,31 @@ func TestSaveGender(t *testing.T) {
|
|||||||
input []byte
|
input []byte
|
||||||
expectedGender string
|
expectedGender string
|
||||||
expectCall bool
|
expectCall bool
|
||||||
executingSymbol string
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Valid Male Input",
|
name: "Valid Male Input",
|
||||||
input: []byte("1"),
|
input: []byte("1"),
|
||||||
expectedGender: "male",
|
expectedGender: "Male",
|
||||||
executingSymbol: "set_male",
|
|
||||||
expectCall: true,
|
expectCall: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Valid Female Input",
|
name: "Valid Female Input",
|
||||||
input: []byte("2"),
|
input: []byte("2"),
|
||||||
expectedGender: "female",
|
expectedGender: "Female",
|
||||||
executingSymbol: "set_female",
|
|
||||||
expectCall: true,
|
expectCall: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Valid Unspecified Input",
|
name: "Valid Unspecified Input",
|
||||||
input: []byte("3"),
|
input: []byte("3"),
|
||||||
executingSymbol: "set_unspecified",
|
expectedGender: "Unspecified",
|
||||||
expectedGender: "unspecified",
|
|
||||||
expectCall: true,
|
expectCall: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Empty Input",
|
||||||
|
input: []byte(""),
|
||||||
|
expectedGender: "",
|
||||||
|
expectCall: false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -362,15 +342,14 @@ func TestSaveGender(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_GENDER, []byte(tt.expectedGender)).Return(nil)
|
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_GENDER, []byte(tt.expectedGender)).Return(nil)
|
||||||
}
|
}
|
||||||
mockState.ExecPath = append(mockState.ExecPath, tt.executingSymbol)
|
|
||||||
// Create the Handlers instance with the mock store
|
// Create the Handlers instance with the mock store
|
||||||
h := &Handlers{
|
h := &Handlers{
|
||||||
userdataStore: mockStore,
|
userdataStore: mockStore,
|
||||||
st: mockState,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call the method
|
// Call the method
|
||||||
_, err := h.SaveGender(ctx, "save_gender", tt.input)
|
_, err := h.SaveGender(ctx, "someSym", tt.input)
|
||||||
|
|
||||||
// Assert no error
|
// Assert no error
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@ -565,7 +544,7 @@ func TestSetLanguage(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Set Default Language (English)",
|
name: "Set Default Language (English)",
|
||||||
execPath: []string{"set_eng"},
|
execPath: []string{"set_default"},
|
||||||
expectedResult: resource.Result{
|
expectedResult: resource.Result{
|
||||||
FlagSet: []uint32{state.FLAG_LANG, 8},
|
FlagSet: []uint32{state.FLAG_LANG, 8},
|
||||||
Content: "eng",
|
Content: "eng",
|
||||||
@ -579,6 +558,13 @@ func TestSetLanguage(t *testing.T) {
|
|||||||
Content: "swa",
|
Content: "swa",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Unhandled path",
|
||||||
|
execPath: []string{""},
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagSet: []uint32{8},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -606,6 +592,76 @@ func TestSetLanguage(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetResetSingleEdit(t *testing.T) {
|
||||||
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
|
||||||
|
flag_allow_update, _ := fm.parser.GetFlag("flag_allow_update")
|
||||||
|
flag_single_edit, _ := fm.parser.GetFlag("flag_single_edit")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
// Define test cases
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input []byte
|
||||||
|
expectedResult resource.Result
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Set single Edit",
|
||||||
|
input: []byte("2"),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagSet: []uint32{flag_single_edit},
|
||||||
|
FlagReset: []uint32{flag_allow_update},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Set single Edit",
|
||||||
|
input: []byte("3"),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagSet: []uint32{flag_single_edit},
|
||||||
|
FlagReset: []uint32{flag_allow_update},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Set single edit",
|
||||||
|
input: []byte("4"),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagReset: []uint32{flag_allow_update},
|
||||||
|
FlagSet: []uint32{flag_single_edit},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "No single edit set",
|
||||||
|
input: []byte("1"),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagReset: []uint32{flag_single_edit},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
||||||
|
// Create the Handlers instance with the mock flag manager
|
||||||
|
h := &Handlers{
|
||||||
|
flagManager: fm.parser,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the method
|
||||||
|
res, err := h.SetResetSingleEdit(context.Background(), "set_reset_single_edit", tt.input)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
// Assert that the Result FlagSet has the required flags after language switch
|
||||||
|
assert.Equal(t, res, tt.expectedResult, "Flags should match reset edit")
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestResetAllowUpdate(t *testing.T) {
|
func TestResetAllowUpdate(t *testing.T) {
|
||||||
fm, err := NewFlagManager(flagsPath)
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
|
||||||
@ -1424,7 +1480,7 @@ func TestValidateAmount(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount")
|
//flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount")
|
||||||
mockDataStore := new(mocks.MockUserDataStore)
|
mockDataStore := new(mocks.MockUserDataStore)
|
||||||
mockCreateAccountService := new(mocks.MockAccountService)
|
mockCreateAccountService := new(mocks.MockAccountService)
|
||||||
|
|
||||||
@ -1453,26 +1509,26 @@ func TestValidateAmount(t *testing.T) {
|
|||||||
Content: "0.001",
|
Content: "0.001",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
name: "Test with amount larger than balance",
|
// name: "Test with amount larger than balance",
|
||||||
input: []byte("0.02"),
|
// input: []byte("0.02"),
|
||||||
balance: "0.003 CELO",
|
// balance: "0.003 CELO",
|
||||||
publicKey: []byte("0xrqeqrequuq"),
|
// publicKey: []byte("0xrqeqrequuq"),
|
||||||
expectedResult: resource.Result{
|
// expectedResult: resource.Result{
|
||||||
FlagSet: []uint32{flag_invalid_amount},
|
// FlagSet: []uint32{flag_invalid_amount},
|
||||||
Content: "0.02",
|
// Content: "0.02",
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
name: "Test with invalid amount",
|
// name: "Test with invalid amount",
|
||||||
input: []byte("0.02ms"),
|
// input: []byte("0.02ms"),
|
||||||
balance: "0.003 CELO",
|
// balance: "0.003 CELO",
|
||||||
publicKey: []byte("0xrqeqrequuq"),
|
// publicKey: []byte("0xrqeqrequuq"),
|
||||||
expectedResult: resource.Result{
|
// expectedResult: resource.Result{
|
||||||
FlagSet: []uint32{flag_invalid_amount},
|
// FlagSet: []uint32{flag_invalid_amount},
|
||||||
Content: "0.02ms",
|
// Content: "0.02ms",
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -1480,7 +1536,7 @@ func TestValidateAmount(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.balance, 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)
|
||||||
|
|
||||||
// Call the method under test
|
// Call the method under test
|
||||||
res, _ := h.ValidateAmount(ctx, "test_validate_amount", tt.input)
|
res, _ := h.ValidateAmount(ctx, "test_validate_amount", tt.input)
|
||||||
@ -1592,7 +1648,6 @@ func TestGetProfile(t *testing.T) {
|
|||||||
|
|
||||||
mockDataStore := new(mocks.MockUserDataStore)
|
mockDataStore := new(mocks.MockUserDataStore)
|
||||||
mockCreateAccountService := new(mocks.MockAccountService)
|
mockCreateAccountService := new(mocks.MockAccountService)
|
||||||
|
|
||||||
h := &Handlers{
|
h := &Handlers{
|
||||||
userdataStore: mockDataStore,
|
userdataStore: mockDataStore,
|
||||||
accountService: mockCreateAccountService,
|
accountService: mockCreateAccountService,
|
||||||
|
Loading…
Reference in New Issue
Block a user