From e99a788c60f4781eee7208180f0507cb3367959b Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Sat, 14 Sep 2024 20:01:58 +0300 Subject: [PATCH 1/4] add tests --- internal/handlers/ussd/menuhandler.go | 10 +- internal/handlers/ussd/menuhandler_test.go | 1398 ++++++++++++++++++++ 2 files changed, 1403 insertions(+), 5 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 3eef63d..89ced5a 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -74,9 +74,9 @@ func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db) (*Handlers, erro Db: userdataStore, } h := &Handlers{ - userdataStore: userDb, - flagManager: appFlags, - accountService: &server.AccountService{}, + userdataStore: userDb, + flagManager: appFlags, + accountService: &server.AccountService{}, } return h, nil } @@ -748,7 +748,8 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount") - publicKey, _ := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_PUBLIC_KEY) + store := h.userdataStore + publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) amountStr := string(input) @@ -792,7 +793,6 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) } res.Content = fmt.Sprintf("%.3f", inputAmount) // Format to 3 decimal places - store := h.userdataStore err = store.WriteEntry(ctx, sessionId, utils.DATA_AMOUNT, []byte(amountStr)) if err != nil { return res, err diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 0a6aedb..e091bb9 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -2,14 +2,98 @@ package ussd import ( "context" + "encoding/json" + "fmt" + "log" + "path" "testing" + "git.defalsify.org/vise.git/db" "git.defalsify.org/vise.git/resource" + "git.defalsify.org/vise.git/state" "git.grassecon.net/urdt/ussd/internal/handlers/ussd/mocks" + "git.grassecon.net/urdt/ussd/internal/models" "git.grassecon.net/urdt/ussd/internal/utils" "github.com/alecthomas/assert/v2" + testdataloader "github.com/peteole/testdata-loader" ) +var ( + baseDir = testdataloader.GetBasePath() + flagsPath = path.Join(baseDir, "services", "registration", "pp.csv") +) + +func TestCreateAccount(t *testing.T) { + + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + + // Create required mocks + mockDataStore := new(mocks.MockUserDataStore) + mockCreateAccountService := new(mocks.MockAccountService) + expectedResult := resource.Result{} + accountCreatedFlag, err := fm.GetFlag("flag_account_created") + + if err != nil { + t.Logf(err.Error()) + } + expectedResult.FlagSet = append(expectedResult.FlagSet, accountCreatedFlag) + + // 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 { + mockDataStore.On("WriteEntry", ctx, sessionId, key, []byte(value)).Return(nil) + } + + // Create a Handlers instance with the mock data store + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockCreateAccountService, + flagManager: fm.parser, + } + + // Call the method you want to test + res, err := h.CreateAccount(ctx, "create_account", []byte("some-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, expectedResult, "Expected result should be equal to the actual result") + + // Assert that expectations were met + mockDataStore.AssertExpectations(t) +} + func TestSaveFirstname(t *testing.T) { // Create a new instance of MockMyDataStore mockStore := new(mocks.MockUserDataStore) @@ -66,6 +150,64 @@ func TestSaveFamilyname(t *testing.T) { mockStore.AssertExpectations(t) } +func TestSavePin(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + mockStore := new(mocks.MockUserDataStore) + if err != nil { + log.Fatal(err) + } + flag_incorrect_pin, _ := fm.parser.GetFlag("flag_incorrect_pin") + + // Create the Handlers instance with the mock flag manager + h := &Handlers{ + flagManager: fm.parser, + userdataStore: mockStore, + } + sessionId := "session123" + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + // Define test cases + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Valid Pin entry", + input: []byte("1234"), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_incorrect_pin}, + }, + }, + { + name: "Invalid Pin entry", + input: []byte("12343"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_incorrect_pin}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + // Set up the expected behavior of the mock + mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_PIN, []byte(tt.input)).Return(nil) + + // Call the method + res, err := h.SavePin(ctx, "save_pin", 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 be equal to account created") + + }) + } +} + func TestSaveYoB(t *testing.T) { // Create a new instance of MockMyDataStore mockStore := new(mocks.MockUserDataStore) @@ -122,6 +264,34 @@ func TestSaveLocation(t *testing.T) { mockStore.AssertExpectations(t) } +func TestSaveOfferings(t *testing.T) { + // Create a new instance of MockUserDataStore + mockStore := new(mocks.MockUserDataStore) + + // Define test data + sessionId := "session123" + offerings := "Bananas" + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + // Set up the expected behavior of the mock + mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_OFFERINGS, []byte(offerings)).Return(nil) + + // Create the Handlers instance with the mock store + h := &Handlers{ + userdataStore: mockStore, + } + + // Call the method + res, err := h.SaveOfferings(ctx, "save_offerings", []byte(offerings)) + + // 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) @@ -318,3 +488,1231 @@ func TestGetAmount(t *testing.T) { assert.Equal(t, Amount, res.Content) } + +func TestGetRecipient(t *testing.T) { + mockStore := new(mocks.MockUserDataStore) + + // Define test data + sessionId := "session123" + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + recepient := "0xcasgatweksalw1018221" + + // Set up the expected behavior of the mock + mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_RECIPIENT).Return([]byte(recepient), nil) + + // Create the Handlers instance with the mock store + h := &Handlers{ + userdataStore: mockStore, + } + + // Call the method + res, _ := h.GetRecipient(ctx, "get_recipient", []byte("Getting recipient...")) + + //Assert that the retrieved recepient is what was set as the content + assert.Equal(t, recepient, res.Content) + +} + +func TestGetFlag(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + expectedFlag := uint32(9) + + if err != nil { + t.Logf(err.Error()) + } + flag, err := fm.GetFlag("flag_account_created") + + if err != nil { + t.Logf(err.Error()) + } + + assert.Equal(t, uint32(flag), expectedFlag, "Flags should be equal to account created") +} + +func TestSetLanguage(t *testing.T) { + // Create a new instance of the Flag Manager + fm, err := NewFlagManager(flagsPath) + + if err != nil { + log.Fatal(err) + } + // Define test cases + tests := []struct { + name string + input []byte + expectedFlags []uint32 + expectedResult resource.Result + flagManagerResponse uint32 + flagManagerError error + }{ + { + name: "English language", + input: []byte("0"), + expectedFlags: []uint32{state.FLAG_LANG, 123}, + expectedResult: resource.Result{ + FlagSet: []uint32{state.FLAG_LANG, 8}, + Content: "eng", + }, + flagManagerResponse: 123, + flagManagerError: nil, + }, + { + name: "Swahili language", + input: []byte("1"), + expectedFlags: []uint32{state.FLAG_LANG, 123}, + expectedResult: resource.Result{ + FlagSet: []uint32{state.FLAG_LANG, 8}, + Content: "swa", + }, + flagManagerResponse: 123, + flagManagerError: nil, + }, + { + name: "Unhandled Input", + input: []byte("3"), + expectedFlags: []uint32{123}, + expectedResult: resource.Result{ + FlagSet: []uint32{8}, + }, + flagManagerResponse: 123, + flagManagerError: nil, + }, + } + + 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.SetLanguage(context.Background(), "set_language", 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 be equal to account created") + + }) + } +} + +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) { + fm, err := NewFlagManager(flagsPath) + + flag_allow_update, _ := fm.parser.GetFlag("flag_allow_update") + + if err != nil { + log.Fatal(err) + } + // Define test cases + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Resets allow update", + input: []byte(""), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_allow_update}, + }, + }, + } + + 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.ResetAllowUpdate(context.Background(), "reset_allow update", 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 be equal to account created") + + }) + } +} + +func TestResetAccountAuthorized(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + flag_account_authorized, _ := fm.parser.GetFlag("flag_account_authorized") + + if err != nil { + log.Fatal(err) + } + // Define test cases + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Resets account authorized", + input: []byte(""), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_account_authorized}, + }, + }, + } + + 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.ResetAccountAuthorized(context.Background(), "reset_account_authorized", 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, "Result should contain flag(s) that have been reset") + + }) + } +} + +func TestIncorrectPinReset(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + flag_incorrect_pin, _ := fm.parser.GetFlag("flag_incorrect_pin") + + if err != nil { + log.Fatal(err) + } + // Define test cases + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Test incorrect pin reset", + input: []byte(""), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_incorrect_pin}, + }, + }, + } + + 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.ResetIncorrectPin(context.Background(), "reset_incorrect_pin", 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, "Result should contain flag(s) that have been reset") + + }) + } +} + +func TestResetIncorrectYob(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + flag_incorrect_date_format, _ := fm.parser.GetFlag("flag_incorrect_date_format") + + if err != nil { + log.Fatal(err) + } + // Define test cases + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Test incorrect yob reset", + input: []byte(""), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_incorrect_date_format}, + }, + }, + } + + 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.ResetIncorrectYob(context.Background(), "reset_incorrect_yob", 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, "Result should contain flag(s) that have been reset") + + }) + } +} + +func TestAuthorize(t *testing.T) { + + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + + // Create required mocks + mockDataStore := new(mocks.MockUserDataStore) + mockCreateAccountService := new(mocks.MockAccountService) + //expectedResult := resource.Result{} + mockState := state.NewState(16) + flag_incorrect_pin, _ := fm.GetFlag("flag_incorrect_pin") + flag_account_authorized, _ := fm.GetFlag("flag_account_authorized") + flag_allow_update, _ := fm.GetFlag("flag_allow_update") + //Assuming 1234 is the correct account pin + accountPIN := "1234" + + // Define session ID and mock data + sessionId := "session123" + typ := utils.DATA_ACCOUNT_PIN + + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockCreateAccountService, + flagManager: fm.parser, + st: mockState, + } + + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Test with correct pin", + input: []byte("1234"), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_incorrect_pin}, + FlagSet: []uint32{flag_allow_update, flag_account_authorized}, + }, + }, + { + name: "Test with incorrect pin", + input: []byte("1235"), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_account_authorized}, + FlagSet: []uint32{flag_incorrect_pin}, + }, + }, + { + name: "Test with pin that is not a 4 digit", + input: []byte("1235aqds"), + expectedResult: resource.Result{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + // 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(accountPIN), nil) + + // Create a Handlers instance with the mock data store + + // Call the method under test + res, err := h.Authorize(ctx, "authorize", []byte(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 TestVerifyYob(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + + sessionId := "session123" + // Create required mocks + mockDataStore := new(mocks.MockUserDataStore) + mockCreateAccountService := new(mocks.MockAccountService) + mockState := state.NewState(16) + flag_incorrect_date_format, _ := fm.parser.GetFlag("flag_incorrect_date_format") + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockCreateAccountService, + flagManager: fm.parser, + st: mockState, + } + + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Test with correct yob", + input: []byte("1980"), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_incorrect_date_format}, + }, + }, + { + name: "Test with incorrect yob", + input: []byte("sgahaha"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_incorrect_date_format}, + }, + }, + { + name: "Test with numeric but less 4 digits", + input: []byte("123"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_incorrect_date_format}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + // Call the method under test + res, err := h.VerifyYob(ctx, "verify_yob", []byte(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 TestVerifyPin(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + + sessionId := "session123" + // Create required mocks + mockDataStore := new(mocks.MockUserDataStore) + mockCreateAccountService := new(mocks.MockAccountService) + mockState := state.NewState(16) + + flag_valid_pin, _ := fm.parser.GetFlag("flag_valid_pin") + flag_pin_mismatch, _ := fm.parser.GetFlag("flag_pin_mismatch") + flag_pin_set, _ := fm.parser.GetFlag("flag_pin_set") + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + //Assuming this was the first set PIN to verify against + firstSetPin := "1234" + + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockCreateAccountService, + flagManager: fm.parser, + st: mockState, + } + + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Test with correct PIN confirmation", + input: []byte("1234"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_valid_pin, flag_pin_set}, + FlagReset: []uint32{flag_pin_mismatch}, + }, + }, + { + name: "Test with PIN that does not match first ", + input: []byte("1324"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_pin_mismatch}, + }, + }, + } + + typ := utils.DATA_ACCOUNT_PIN + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + // Define expected interactions with the mock + mockDataStore.On("ReadEntry", ctx, sessionId, typ).Return([]byte(firstSetPin), nil) + + // Call the method under test + res, err := h.VerifyPin(ctx, "verify_pin", []byte(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 TestCheckAccountStatus(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + mockDataStore := new(mocks.MockUserDataStore) + mockCreateAccountService := new(mocks.MockAccountService) + + sessionId := "session123" + flag_account_success, _ := fm.GetFlag("flag_account_success") + flag_account_pending, _ := fm.GetFlag("flag_account_pending") + + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockCreateAccountService, + flagManager: fm.parser, + } + tests := []struct { + name string + input []byte + status string + expectedResult resource.Result + }{ + { + name: "Test when account status is Success", + input: []byte("TrackingId1234"), + status: "SUCCESS", + expectedResult: resource.Result{ + FlagSet: []uint32{flag_account_success}, + FlagReset: []uint32{flag_account_pending}, + }, + }, + // { + // name: "Test when account status is not Success", + // input: []byte("TrackingId1234"), + // status: "REVERTED", + // expectedResult: resource.Result{ + // FlagReset: []uint32{flag_account_pending}, + // FlagSet: []uint32{flag_account_success}, + // }, + // }, + } + + typ := utils.DATA_TRACKING_ID + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + // Define expected interactions with the mock + mockDataStore.On("ReadEntry", ctx, sessionId, typ).Return(tt.input, nil) + + mockCreateAccountService.On("CheckAccountStatus", string(tt.input)).Return(tt.status, nil) + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(tt.status)).Return(nil) + + // Call the method under test + res, _ := h.CheckAccountStatus(ctx, "check_status", 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 TestTransactionReset(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + flag_invalid_recipient, _ := fm.GetFlag("flag_invalid_recipient") + flag_invalid_recipient_with_invite, _ := fm.GetFlag("flag_invalid_recipient_with_invite") + + 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 + status string + expectedResult resource.Result + }{ + { + name: "Test transaction reset for amount and recipient", + expectedResult: resource.Result{ + FlagReset: []uint32{flag_invalid_recipient, flag_invalid_recipient_with_invite}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, []byte("")).Return(nil) + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_RECIPIENT, []byte("")).Return(nil) + + // Call the method under test + res, _ := h.TransactionReset(ctx, "transaction_reset", 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 TestResetInvalidAmount(t *testing.T) { + sessionId := "session123" + + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + + flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount") + + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + mockDataStore := new(mocks.MockUserDataStore) + mockCreateAccountService := new(mocks.MockAccountService) + + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockCreateAccountService, + flagManager: fm.parser, + } + + tests := []struct { + name string + input []byte + status string + expectedResult resource.Result + }{ + { + name: "Test amount reset", + expectedResult: resource.Result{ + FlagReset: []uint32{flag_invalid_amount}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, []byte("")).Return(nil) + + // Call the method under test + res, _ := h.ResetTransactionAmount(ctx, "transaction_reset_amount", 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 TestInitiateTransaction(t *testing.T) { + sessionId := "session123" + + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + account_authorized_flag, err := fm.parser.GetFlag("flag_account_authorized") + + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + mockDataStore := new(mocks.MockUserDataStore) + mockCreateAccountService := new(mocks.MockAccountService) + + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockCreateAccountService, + flagManager: fm.parser, + } + + tests := []struct { + name string + input []byte + PublicKey []byte + Recipient []byte + Amount []byte + status string + expectedResult resource.Result + }{ + { + name: "Test amount reset", + PublicKey: []byte("0x1241527192"), + Amount: []byte("0.002CELO"), + Recipient: []byte("0x12415ass27192"), + expectedResult: resource.Result{ + FlagReset: []uint32{account_authorized_flag}, + Content: "Your request has been sent. 0x12415ass27192 will receive 0.002CELO from 0x1241527192.", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Define expected interactions with the mock + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.PublicKey, nil) + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return(tt.Amount, nil) + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_RECIPIENT).Return(tt.Recipient, nil) + //mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, []byte("")).Return(nil) + + // Call the method under test + res, _ := h.InitiateTransaction(ctx, "transaction_reset_amount", 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 TestQuit(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + flag_account_authorized, _ := fm.parser.GetFlag("flag_account_authorized") + + 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 + status string + expectedResult resource.Result + }{ + { + name: "Test quit message", + expectedResult: resource.Result{ + FlagReset: []uint32{flag_account_authorized}, + Content: "Thank you for using Sarafu. Goodbye!", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + // Call the method under test + res, _ := h.Quit(ctx, "test_quit", 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 TestIsValidPIN(t *testing.T) { + tests := []struct { + name string + pin string + expected bool + }{ + { + name: "Valid PIN with 4 digits", + pin: "1234", + expected: true, + }, + { + name: "Valid PIN with leading zeros", + pin: "0001", + expected: true, + }, + { + name: "Invalid PIN with less than 4 digits", + pin: "123", + expected: false, + }, + { + name: "Invalid PIN with more than 4 digits", + pin: "12345", + expected: false, + }, + { + name: "Invalid PIN with letters", + pin: "abcd", + expected: false, + }, + { + name: "Invalid PIN with special characters", + pin: "12@#", + expected: false, + }, + { + name: "Empty PIN", + pin: "", + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := isValidPIN(tt.pin) + if actual != tt.expected { + t.Errorf("isValidPIN(%q) = %v; expected %v", tt.pin, actual, tt.expected) + } + }) + } +} + +func TestQuitWithBalance(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + if err != nil { + t.Logf(err.Error()) + } + flag_account_authorized, _ := fm.parser.GetFlag("flag_account_authorized") + + 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 quit with balance", + balance: "0.02CELO", + publicKey: []byte("0xrqeqrequuq"), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_account_authorized}, + Content: fmt.Sprintf("Your account balance is %s", "0.02CELO"), + }, + }, + } + + 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) + + // 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) + + // Call the method under test + res, _ := h.ValidateAmount(ctx, "test_validate_amount", 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 TestValidateRecipient(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + + flag_invalid_recipient, _ := fm.parser.GetFlag("flag_invalid_recipient") + mockDataStore := new(mocks.MockUserDataStore) + + sessionId := "session123" + + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + if err != nil { + log.Fatal(err) + } + // Define test cases + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Test with invalid recepient", + input: []byte("000"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_invalid_recipient}, + Content: "000", + }, + }, + { + name: "Test with valid recepient", + input: []byte("0705X2"), + expectedResult: resource.Result{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_RECIPIENT, tt.input).Return(nil) + + // Create the Handlers instance with the mock flag manager + h := &Handlers{ + flagManager: fm.parser, + userdataStore: mockDataStore, + } + + // Call the method + res, err := h.ValidateRecipient(ctx, "validate_recepient", 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, "Result should contain flag(s) that have been reset") + + }) + } +} + +func TestCheckBalance(t *testing.T) { + + mockDataStore := new(mocks.MockUserDataStore) + sessionId := "session123" + publicKey := "0X13242618721" + balance := "0.003 CELO" + + expectedResult := resource.Result{ + Content: "0.003 CELO", + } + + mockCreateAccountService := new(mocks.MockAccountService) + + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockCreateAccountService, + //flagManager: fm.parser, + } + //mock call operations + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil) + mockCreateAccountService.On("CheckBalance", string(publicKey)).Return(balance, nil) + + res, _ := h.CheckBalance(ctx, "check_balance", []byte("123456")) + + assert.Equal(t, res, expectedResult, "Result should contain flag(s) that have been reset") + +} + +func TestGetProfile(t *testing.T) { + + sessionId := "session123" + + mockDataStore := new(mocks.MockUserDataStore) + mockCreateAccountService := new(mocks.MockAccountService) + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockCreateAccountService, + } + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + tests := []struct { + name string + keys []utils.DataTyp + profileInfo []string + result resource.Result + }{ + { + 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}, + profileInfo: []string{"Doee", "John", "Male", "Bananas", "Kilifi", "1976"}, + 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 yob not provided", + // 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", "Not Provided"}, + // result: resource.Result{ + // Content: fmt.Sprintf( + // "Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", + // "John Doee", "Male", "Not Provided", "Kilifi", "Bananas", + // ), + // }, + // }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for index, key := range tt.keys { + mockDataStore.On("ReadEntry", ctx, sessionId, key).Return([]byte(tt.profileInfo[index]), nil) + } + res, _ := h.GetProfileInfo(ctx, "get_profile_info", []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.result, "Result should contain profile information served back to user") + + }) + } +} From 006eef0a28519540b75092f96ea47bdada4c6f5f Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Sat, 14 Sep 2024 20:02:21 +0300 Subject: [PATCH 2/4] add testdata loader --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index e317ed4..0ab8488 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.6 require ( git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f github.com/alecthomas/assert/v2 v2.2.2 + github.com/peteole/testdata-loader v0.3.0 gopkg.in/leonelquinteros/gotext.v1 v1.3.1 ) From b5ef483f34f39f3b6e3d6789abe0e65d642415b1 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Sat, 14 Sep 2024 20:07:17 +0300 Subject: [PATCH 3/4] clean up tests --- internal/handlers/ussd/menuhandler_test.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index e091bb9..67371cf 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -1109,15 +1109,6 @@ func TestCheckAccountStatus(t *testing.T) { FlagReset: []uint32{flag_account_pending}, }, }, - // { - // name: "Test when account status is not Success", - // input: []byte("TrackingId1234"), - // status: "REVERTED", - // expectedResult: resource.Result{ - // FlagReset: []uint32{flag_account_pending}, - // FlagSet: []uint32{flag_account_success}, - // }, - // }, } typ := utils.DATA_TRACKING_ID @@ -1688,17 +1679,6 @@ func TestGetProfile(t *testing.T) { ), }, }, - // { - // name: "Test with yob not provided", - // 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", "Not Provided"}, - // result: resource.Result{ - // Content: fmt.Sprintf( - // "Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", - // "John Doee", "Male", "Not Provided", "Kilifi", "Bananas", - // ), - // }, - // }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 78c033e61c6e7ffd97e37537bb8e2f93f2956f13 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Sat, 14 Sep 2024 20:59:08 +0300 Subject: [PATCH 4/4] remove deprecated code --- internal/utils/db.go | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/internal/utils/db.go b/internal/utils/db.go index 94ce250..5479f27 100644 --- a/internal/utils/db.go +++ b/internal/utils/db.go @@ -1,10 +1,7 @@ package utils import ( - "context" "encoding/binary" - - "git.defalsify.org/vise.git/db" ) type DataTyp uint16 @@ -37,21 +34,3 @@ 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) -}