forked from urdt/ussd
update tests
This commit is contained in:
parent
b8e12e5215
commit
81c4189c8e
@ -15,6 +15,7 @@ import (
|
||||
"git.defalsify.org/vise.git/persist"
|
||||
"git.defalsify.org/vise.git/resource"
|
||||
"git.defalsify.org/vise.git/state"
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||
"git.grassecon.net/urdt/ussd/internal/mocks"
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||
@ -78,49 +79,42 @@ func TestCreateAccount(t *testing.T) {
|
||||
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")
|
||||
|
||||
flag_account_created, 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
|
||||
notFoundErr := db.ErrNotFound{}
|
||||
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"`
|
||||
tests := []struct {
|
||||
name string
|
||||
serverResponse *server.OKResponse
|
||||
expectedResult resource.Result
|
||||
}{
|
||||
CustodialId: "12",
|
||||
PublicKey: "0x8E0XSCSVA",
|
||||
TrackingId: "d95a7e83-196c-4fd0-866fSGAGA",
|
||||
{
|
||||
name: "Test account creation success",
|
||||
serverResponse: &server.OKResponse{
|
||||
Ok: true,
|
||||
Description: "Account creation successed",
|
||||
Result: map[string]any{
|
||||
"trackingId": "1234567890",
|
||||
"publicKey": "1235QERYU",
|
||||
},
|
||||
},
|
||||
expectedResult: resource.Result{
|
||||
FlagSet: []uint32{flag_account_created},
|
||||
},
|
||||
},
|
||||
}
|
||||
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 _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
for key, value := range data {
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, key, []byte(value)).Return(nil)
|
||||
}
|
||||
mockDataStore := new(mocks.MockUserDataStore)
|
||||
mockCreateAccountService := new(mocks.MockAccountService)
|
||||
|
||||
// Create a Handlers instance with the mock data store
|
||||
h := &Handlers{
|
||||
@ -129,6 +123,18 @@ func TestCreateAccount(t *testing.T) {
|
||||
flagManager: fm.parser,
|
||||
}
|
||||
|
||||
data := map[utils.DataTyp]string{
|
||||
utils.DATA_TRACKING_ID: tt.serverResponse.Result["trackingId"].(string),
|
||||
utils.DATA_PUBLIC_KEY: tt.serverResponse.Result["publicKey"].(string),
|
||||
}
|
||||
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACCOUNT_CREATED).Return([]byte(""), notFoundErr)
|
||||
mockCreateAccountService.On("CreateAccount").Return(tt.serverResponse, nil)
|
||||
|
||||
for key, value := range data {
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, key, []byte(value)).Return(nil)
|
||||
}
|
||||
|
||||
// Call the method you want to test
|
||||
res, err := h.CreateAccount(ctx, "create_account", []byte("some-input"))
|
||||
|
||||
@ -136,10 +142,12 @@ func TestCreateAccount(t *testing.T) {
|
||||
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.Equal(t, res, tt.expectedResult, "Expected result should be equal to the actual result")
|
||||
|
||||
// Assert that expectations were met
|
||||
mockDataStore.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithPersister(t *testing.T) {
|
||||
@ -1072,12 +1080,20 @@ func TestCheckAccountStatus(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input []byte
|
||||
serverResponse *server.OKResponse
|
||||
response *models.TrackStatusResponse
|
||||
expectedResult resource.Result
|
||||
}{
|
||||
{
|
||||
name: "Test when account status is Success",
|
||||
name: "Test when account is on the Sarafu network",
|
||||
input: []byte("TrackingId1234"),
|
||||
serverResponse: &server.OKResponse{
|
||||
Ok: true,
|
||||
Description: "Account creation successed",
|
||||
Result: map[string]any{
|
||||
"active": true,
|
||||
},
|
||||
},
|
||||
response: &models.TrackStatusResponse{
|
||||
Ok: true,
|
||||
Result: struct {
|
||||
@ -1104,17 +1120,7 @@ func TestCheckAccountStatus(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test when fetching account status is not Success",
|
||||
input: []byte("TrackingId1234"),
|
||||
response: &models.TrackStatusResponse{
|
||||
Ok: false,
|
||||
},
|
||||
expectedResult: resource.Result{
|
||||
FlagSet: []uint32{flag_api_error},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test when checking account status api call is a SUCCESS but an account is not yet ready",
|
||||
name: "Test when the account is not yet on the sarafu network",
|
||||
input: []byte("TrackingId1234"),
|
||||
response: &models.TrackStatusResponse{
|
||||
Ok: true,
|
||||
@ -1129,13 +1135,20 @@ func TestCheckAccountStatus(t *testing.T) {
|
||||
}{
|
||||
Transaction: Transaction{
|
||||
CreatedAt: time.Now(),
|
||||
Status: "IN_NETWORK",
|
||||
Status: "SUCCESS",
|
||||
TransferValue: json.Number("0.5"),
|
||||
TxHash: "0x123abc456def",
|
||||
TxType: "transfer",
|
||||
},
|
||||
},
|
||||
},
|
||||
serverResponse: &server.OKResponse{
|
||||
Ok: true,
|
||||
Description: "Account creation successed",
|
||||
Result: map[string]any{
|
||||
"active": false,
|
||||
},
|
||||
},
|
||||
expectedResult: resource.Result{
|
||||
FlagSet: []uint32{flag_account_pending},
|
||||
FlagReset: []uint32{flag_api_error, flag_account_success},
|
||||
@ -1155,9 +1168,10 @@ func TestCheckAccountStatus(t *testing.T) {
|
||||
|
||||
status := tt.response.Result.Transaction.Status
|
||||
// Define expected interactions with the mock
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TRACKING_ID).Return(tt.input, nil)
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.input, nil)
|
||||
|
||||
mockCreateAccountService.On("CheckAccountStatus", string(tt.input)).Return(tt.response, nil)
|
||||
mockCreateAccountService.On("TrackAccountStatus", string(tt.input)).Return(tt.serverResponse, nil)
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(status)).Return(nil).Maybe()
|
||||
|
||||
// Call the method under test
|
||||
|
Loading…
Reference in New Issue
Block a user