2024-08-28 13:14:28 +02:00
|
|
|
package ussd
|
|
|
|
|
2024-08-29 18:24:43 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2024-08-28 13:14:28 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
"git.defalsify.org/vise.git/resource"
|
2024-09-07 08:55:59 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd/mocks"
|
2024-09-07 15:24:37 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/utils"
|
2024-08-29 19:07:50 +02:00
|
|
|
"github.com/alecthomas/assert/v2"
|
2024-08-29 18:24:43 +02:00
|
|
|
)
|
2024-08-28 13:14:28 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// func TestCreateAccount(t *testing.T) {
|
|
|
|
// // Create a new instance of MockMyDataStore
|
|
|
|
// mockDataStore := new(mocks.MockMyDataStore)
|
|
|
|
// mockCreateAccountService := new(mocks.MockAccountService)
|
|
|
|
|
|
|
|
// // Define session ID and mock data
|
|
|
|
// sessionId := "session123"
|
|
|
|
// typ := utils.DATA_ACCOUNT_CREATED
|
|
|
|
// fakeError := db.ErrNotFound{}
|
|
|
|
// // Create context with session ID
|
|
|
|
// ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
|
|
|
|
|
|
// // Define expected interactions with the mock
|
|
|
|
// mockDataStore.On("ReadEntry", ctx, sessionId, typ).Return([]byte("123"), fakeError)
|
|
|
|
// expectedAccountResp := &models.AccountResponse{
|
|
|
|
// Ok: true,
|
|
|
|
// Result: struct {
|
|
|
|
// CustodialId json.Number `json:"custodialId"`
|
|
|
|
// PublicKey string `json:"publicKey"`
|
|
|
|
// TrackingId string `json:"trackingId"`
|
|
|
|
// }{
|
|
|
|
// CustodialId: "12",
|
|
|
|
// PublicKey: "0x8E0XSCSVA",
|
|
|
|
// TrackingId: "d95a7e83-196c-4fd0-866fSGAGA",
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
// mockCreateAccountService.On("CreateAccount").Return(expectedAccountResp, nil)
|
|
|
|
// data := map[utils.DataTyp]string{
|
|
|
|
// utils.DATA_TRACKING_ID: expectedAccountResp.Result.TrackingId,
|
|
|
|
// utils.DATA_PUBLIC_KEY: expectedAccountResp.Result.PublicKey,
|
|
|
|
// utils.DATA_CUSTODIAL_ID: expectedAccountResp.Result.CustodialId.String(),
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for key, value := range data {
|
|
|
|
// //err := utils.WriteEntry(ctx, h.userdataStore, sessionId, key, []byte(value))
|
|
|
|
// mockDataStore.On("WriteEntry", ctx, sessionId, key, []byte(value)).Return(nil)
|
|
|
|
// }
|
|
|
|
// //mockDataStore.On("WriteEntry", mock.Anything, sessionId, mock.Anything, mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
// // Create a Handlers instance with the mock data store
|
|
|
|
// h := &Handlers{
|
|
|
|
// userdataStore: mockDataStore,
|
|
|
|
// accountService: mockCreateAccountService,
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Call the method you want to test
|
|
|
|
// _, err := h.CreateAccount(ctx, "some-symbol", []byte("some-input"))
|
|
|
|
|
|
|
|
// // Assert that no errors occurred
|
|
|
|
// assert.NoError(t, err)
|
2024-09-07 08:30:20 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// // Assert that expectations were met
|
|
|
|
// mockDataStore.AssertExpectations(t)
|
|
|
|
// }
|
|
|
|
|
|
|
|
func TestSaveFirstname(t *testing.T) {
|
|
|
|
// Create a new instance of MockMyDataStore
|
|
|
|
mockStore := new(mocks.MockUserDataStore)
|
|
|
|
|
|
|
|
// Define test data
|
|
|
|
sessionId := "session123"
|
|
|
|
firstName := "John"
|
|
|
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
|
|
|
|
|
|
// Set up the expected behavior of the mock
|
|
|
|
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_FIRST_NAME, []byte(firstName)).Return(nil)
|
|
|
|
|
|
|
|
// Create the Handlers instance with the mock store
|
2024-09-07 15:24:37 +02:00
|
|
|
h := &Handlers{
|
2024-09-09 16:15:04 +02:00
|
|
|
userdataStore: mockStore,
|
2024-09-07 15:24:37 +02:00
|
|
|
}
|
2024-09-09 16:15:04 +02:00
|
|
|
|
|
|
|
// Call the method
|
|
|
|
res, err := h.SaveFirstname(ctx, "save_firstname", []byte(firstName))
|
|
|
|
|
|
|
|
// Assert results
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, resource.Result{}, res)
|
|
|
|
|
|
|
|
// Assert all expectations were met
|
|
|
|
mockStore.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveFamilyname(t *testing.T) {
|
|
|
|
// Create a new instance of MockMyDataStore
|
|
|
|
mockStore := new(mocks.MockUserDataStore)
|
|
|
|
|
|
|
|
// Define test data
|
|
|
|
sessionId := "session123"
|
|
|
|
familyName := "Doeee"
|
|
|
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
|
|
|
|
|
|
// Set up the expected behavior of the mock
|
|
|
|
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_FAMILY_NAME, []byte(familyName)).Return(nil)
|
|
|
|
|
|
|
|
// Create the Handlers instance with the mock store
|
|
|
|
h := &Handlers{
|
|
|
|
userdataStore: mockStore,
|
2024-09-07 15:24:37 +02:00
|
|
|
}
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Call the method
|
|
|
|
res, err := h.SaveFamilyname(ctx, "save_familyname", []byte(familyName))
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-07 15:24:37 +02:00
|
|
|
// Assert results
|
|
|
|
assert.NoError(t, err)
|
2024-09-09 16:15:04 +02:00
|
|
|
assert.Equal(t, resource.Result{}, res)
|
|
|
|
|
|
|
|
// Assert all expectations were met
|
|
|
|
mockStore.AssertExpectations(t)
|
2024-09-07 15:24:37 +02:00
|
|
|
}
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
func TestSaveYoB(t *testing.T) {
|
|
|
|
// Create a new instance of MockMyDataStore
|
|
|
|
mockStore := new(mocks.MockUserDataStore)
|
|
|
|
|
|
|
|
// Define test data
|
|
|
|
sessionId := "session123"
|
|
|
|
yob := "1980"
|
|
|
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
|
|
|
|
|
|
// Set up the expected behavior of the mock
|
|
|
|
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_YOB, []byte(yob)).Return(nil)
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Create the Handlers instance with the mock store
|
2024-09-07 08:30:20 +02:00
|
|
|
h := &Handlers{
|
2024-09-09 16:15:04 +02:00
|
|
|
userdataStore: mockStore,
|
2024-09-07 08:30:20 +02:00
|
|
|
}
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Call the method
|
|
|
|
res, err := h.SaveYob(ctx, "save_yob", []byte(yob))
|
|
|
|
|
|
|
|
// Assert results
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, resource.Result{}, res)
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Assert all expectations were met
|
|
|
|
mockStore.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveLocation(t *testing.T) {
|
|
|
|
// Create a new instance of MockMyDataStore
|
|
|
|
mockStore := new(mocks.MockUserDataStore)
|
|
|
|
|
|
|
|
// Define test data
|
|
|
|
sessionId := "session123"
|
|
|
|
yob := "Kilifi"
|
|
|
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
|
|
|
|
|
|
// Set up the expected behavior of the mock
|
|
|
|
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_LOCATION, []byte(yob)).Return(nil)
|
|
|
|
|
|
|
|
// Create the Handlers instance with the mock store
|
|
|
|
h := &Handlers{
|
|
|
|
userdataStore: mockStore,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the method
|
|
|
|
res, err := h.SaveLocation(ctx, "save_location", []byte(yob))
|
|
|
|
|
|
|
|
// Assert results
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, resource.Result{}, res)
|
|
|
|
|
|
|
|
// Assert all expectations were met
|
|
|
|
mockStore.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveGender(t *testing.T) {
|
|
|
|
// Create a new instance of MockMyDataStore
|
|
|
|
mockStore := new(mocks.MockUserDataStore)
|
|
|
|
|
|
|
|
// Define the session ID and context
|
|
|
|
sessionId := "session123"
|
|
|
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
|
|
|
|
|
|
// Define test cases
|
2024-09-07 08:30:20 +02:00
|
|
|
tests := []struct {
|
2024-09-09 16:15:04 +02:00
|
|
|
name string
|
|
|
|
input []byte
|
|
|
|
expectedGender string
|
|
|
|
expectCall bool
|
2024-09-07 08:30:20 +02:00
|
|
|
}{
|
|
|
|
{
|
2024-09-09 16:15:04 +02:00
|
|
|
name: "Valid Male Input",
|
|
|
|
input: []byte("1"),
|
|
|
|
expectedGender: "Male",
|
|
|
|
expectCall: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid Female Input",
|
|
|
|
input: []byte("2"),
|
|
|
|
expectedGender: "Female",
|
|
|
|
expectCall: true,
|
2024-09-07 08:30:20 +02:00
|
|
|
},
|
|
|
|
{
|
2024-09-09 16:15:04 +02:00
|
|
|
name: "Valid Unspecified Input",
|
|
|
|
input: []byte("3"),
|
|
|
|
expectedGender: "Unspecified",
|
|
|
|
expectCall: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty Input",
|
|
|
|
input: []byte(""),
|
|
|
|
expectedGender: "",
|
|
|
|
expectCall: false,
|
2024-09-07 08:30:20 +02:00
|
|
|
},
|
|
|
|
}
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-07 08:30:20 +02:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-09-09 16:15:04 +02:00
|
|
|
// Set up expectations for the mock database
|
|
|
|
if tt.expectCall {
|
|
|
|
expectedKey := utils.DATA_GENDER
|
|
|
|
mockStore.On("WriteEntry", ctx, sessionId, expectedKey, []byte(tt.expectedGender)).Return(nil)
|
|
|
|
} else {
|
|
|
|
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_GENDER, []byte(tt.expectedGender)).Return(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the Handlers instance with the mock store
|
|
|
|
h := &Handlers{
|
|
|
|
userdataStore: mockStore,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the method
|
|
|
|
_, err := h.SaveGender(ctx, "someSym", tt.input)
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Assert no error
|
|
|
|
assert.NoError(t, err)
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Verify expectations
|
|
|
|
if tt.expectCall {
|
|
|
|
mockStore.AssertCalled(t, "WriteEntry", ctx, sessionId, utils.DATA_GENDER, []byte(tt.expectedGender))
|
2024-09-07 08:30:20 +02:00
|
|
|
} else {
|
2024-09-09 16:15:04 +02:00
|
|
|
mockStore.AssertNotCalled(t, "WriteEntry", ctx, sessionId, utils.DATA_GENDER, []byte(tt.expectedGender))
|
2024-09-07 08:30:20 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
func TestCheckIdentifier(t *testing.T) {
|
|
|
|
// Create a new instance of MockMyDataStore
|
|
|
|
mockStore := new(mocks.MockUserDataStore)
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Define the session ID and context
|
|
|
|
sessionId := "session123"
|
|
|
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Define test cases
|
2024-09-07 08:30:20 +02:00
|
|
|
tests := []struct {
|
2024-09-09 16:15:04 +02:00
|
|
|
name string
|
|
|
|
mockPublicKey []byte
|
|
|
|
mockErr error
|
|
|
|
expectedContent string
|
|
|
|
expectError bool
|
2024-09-07 08:30:20 +02:00
|
|
|
}{
|
|
|
|
{
|
2024-09-09 16:15:04 +02:00
|
|
|
name: "Saved public Key",
|
|
|
|
mockPublicKey: []byte("0xa8363"),
|
|
|
|
mockErr: nil,
|
|
|
|
expectedContent: "0xa8363",
|
|
|
|
expectError: false,
|
2024-09-07 08:30:20 +02:00
|
|
|
},
|
|
|
|
}
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-07 08:30:20 +02:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-09-09 16:15:04 +02:00
|
|
|
// Set up expectations for the mock database
|
|
|
|
mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.mockPublicKey, tt.mockErr)
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Create the Handlers instance with the mock store
|
|
|
|
h := &Handlers{
|
|
|
|
userdataStore: mockStore,
|
2024-09-07 08:30:20 +02:00
|
|
|
}
|
2024-09-06 16:51:07 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
// Call the method
|
|
|
|
res, err := h.CheckIdentifier(ctx, "check_identifier", nil)
|
|
|
|
|
|
|
|
// Assert results
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.expectedContent, res.Content)
|
|
|
|
|
|
|
|
// Verify expectations
|
|
|
|
mockStore.AssertExpectations(t)
|
2024-09-07 08:30:20 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-09-07 16:51:30 +02:00
|
|
|
|
2024-09-09 16:15:04 +02:00
|
|
|
func TestMaxAmount(t *testing.T) {
|
|
|
|
mockStore := new(mocks.MockUserDataStore)
|
|
|
|
mockCreateAccountService := new(mocks.MockAccountService)
|
|
|
|
|
|
|
|
// Define test data
|
|
|
|
sessionId := "session123"
|
|
|
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
|
|
publicKey := "0xcasgatweksalw1018221"
|
|
|
|
expectedBalance := "0.003CELO"
|
|
|
|
|
|
|
|
// Set up the expected behavior of the mock
|
|
|
|
mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
|
|
|
|
mockCreateAccountService.On("CheckBalance", publicKey).Return(expectedBalance, nil)
|
|
|
|
|
|
|
|
// Create the Handlers instance with the mock store
|
|
|
|
h := &Handlers{
|
|
|
|
userdataStore: mockStore,
|
|
|
|
accountService: mockCreateAccountService,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the method
|
|
|
|
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.Equal(t, expectedBalance, res.Content)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSender(t *testing.T) {
|
|
|
|
mockStore := new(mocks.MockUserDataStore)
|
|
|
|
|
|
|
|
// Define test data
|
|
|
|
sessionId := "session123"
|
|
|
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
|
|
publicKey := "0xcasgatweksalw1018221"
|
|
|
|
|
|
|
|
// Set up the expected behavior of the mock
|
|
|
|
mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
|
|
|
|
|
|
|
|
// Create the Handlers instance with the mock store
|
|
|
|
h := &Handlers{
|
|
|
|
userdataStore: mockStore,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the method
|
|
|
|
res, _ := h.GetSender(ctx, "max_amount", []byte("check_balance"))
|
|
|
|
|
|
|
|
//Assert that the public key from readentry operation is what was set as the result content.
|
|
|
|
assert.Equal(t, publicKey, res.Content)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAmount(t *testing.T) {
|
|
|
|
mockStore := new(mocks.MockUserDataStore)
|
|
|
|
|
|
|
|
// Define test data
|
|
|
|
sessionId := "session123"
|
|
|
|
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
|
|
|
Amount := "0.03CELO"
|
|
|
|
|
|
|
|
// Set up the expected behavior of the mock
|
|
|
|
mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return([]byte(Amount), nil)
|
|
|
|
|
|
|
|
// Create the Handlers instance with the mock store
|
|
|
|
h := &Handlers{
|
|
|
|
userdataStore: mockStore,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the method
|
|
|
|
res, _ := h.GetAmount(ctx, "get_amount", []byte("Getting amount..."))
|
|
|
|
|
|
|
|
//Assert that the retrieved amount is what was set as the content
|
|
|
|
assert.Equal(t, Amount, res.Content)
|
|
|
|
|
|
|
|
}
|