api-structs #117

Merged
lash merged 35 commits from api-structs into master 2024-10-24 15:53:46 +02:00
Showing only changes of commit 81c4189c8e - Show all commits

View File

@ -15,6 +15,7 @@ import (
"git.defalsify.org/vise.git/persist" "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/handlers/server"
"git.grassecon.net/urdt/ussd/internal/mocks" "git.grassecon.net/urdt/ussd/internal/mocks"
"git.grassecon.net/urdt/ussd/internal/models" "git.grassecon.net/urdt/ussd/internal/models"
"git.grassecon.net/urdt/ussd/internal/utils" "git.grassecon.net/urdt/ussd/internal/utils"
@ -78,49 +79,42 @@ func TestCreateAccount(t *testing.T) {
if err != nil { if err != nil {
t.Logf(err.Error()) t.Logf(err.Error())
} }
// Create required mocks // Create required mocks
mockDataStore := new(mocks.MockUserDataStore) flag_account_created, err := fm.GetFlag("flag_account_created")
mockCreateAccountService := new(mocks.MockAccountService)
expectedResult := resource.Result{}
accountCreatedFlag, err := fm.GetFlag("flag_account_created")
if err != nil { if err != nil {
t.Logf(err.Error()) t.Logf(err.Error())
} }
expectedResult.FlagSet = append(expectedResult.FlagSet, accountCreatedFlag)
// Define session ID and mock data // Define session ID and mock data
sessionId := "session123" sessionId := "session123"
typ := utils.DATA_ACCOUNT_CREATED notFoundErr := db.ErrNotFound{}
fakeError := db.ErrNotFound{}
// Create context with session ID
ctx := context.WithValue(context.Background(), "SessionId", sessionId) ctx := context.WithValue(context.Background(), "SessionId", sessionId)
// Define expected interactions with the mock tests := []struct {
mockDataStore.On("ReadEntry", ctx, sessionId, typ).Return([]byte("123"), fakeError) name string
expectedAccountResp := &models.AccountResponse{ serverResponse *server.OKResponse
Ok: true, expectedResult resource.Result
Result: struct {
CustodialId json.Number `json:"custodialId"`
PublicKey string `json:"publicKey"`
TrackingId string `json:"trackingId"`
}{ }{
CustodialId: "12", {
PublicKey: "0x8E0XSCSVA", name: "Test account creation success",
TrackingId: "d95a7e83-196c-4fd0-866fSGAGA", 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) for _, tt := range tests {
data := map[utils.DataTyp]string{ t.Run(tt.name, func(t *testing.T) {
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 := new(mocks.MockUserDataStore)
mockDataStore.On("WriteEntry", ctx, sessionId, key, []byte(value)).Return(nil) mockCreateAccountService := new(mocks.MockAccountService)
}
// Create a Handlers instance with the mock data store // Create a Handlers instance with the mock data store
h := &Handlers{ h := &Handlers{
@ -129,17 +123,31 @@ func TestCreateAccount(t *testing.T) {
flagManager: fm.parser, 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 // Call the method you want to test
res, err := h.CreateAccount(ctx, "create_account", []byte("some-input")) res, err := h.CreateAccount(ctx, "create_account", []byte("some-input"))
// Assert that no errors occurred // Assert that no errors occurred
assert.NoError(t, err) assert.NoError(t, err)
//Assert that the account created flag has been set to the result // 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 // Assert that expectations were met
mockDataStore.AssertExpectations(t) mockDataStore.AssertExpectations(t)
})
}
} }
func TestWithPersister(t *testing.T) { func TestWithPersister(t *testing.T) {
@ -1072,12 +1080,20 @@ func TestCheckAccountStatus(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input []byte input []byte
serverResponse *server.OKResponse
response *models.TrackStatusResponse response *models.TrackStatusResponse
expectedResult resource.Result expectedResult resource.Result
}{ }{
{ {
name: "Test when account status is Success", name: "Test when account is on the Sarafu network",
input: []byte("TrackingId1234"), input: []byte("TrackingId1234"),
serverResponse: &server.OKResponse{
Ok: true,
Description: "Account creation successed",
Result: map[string]any{
"active": true,
},
},
response: &models.TrackStatusResponse{ response: &models.TrackStatusResponse{
Ok: true, Ok: true,
Result: struct { Result: struct {
@ -1104,17 +1120,7 @@ func TestCheckAccountStatus(t *testing.T) {
}, },
}, },
{ {
name: "Test when fetching account status is not Success", name: "Test when the account is not yet on the sarafu network",
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",
input: []byte("TrackingId1234"), input: []byte("TrackingId1234"),
response: &models.TrackStatusResponse{ response: &models.TrackStatusResponse{
Ok: true, Ok: true,
@ -1129,13 +1135,20 @@ func TestCheckAccountStatus(t *testing.T) {
}{ }{
Transaction: Transaction{ Transaction: Transaction{
CreatedAt: time.Now(), CreatedAt: time.Now(),
Status: "IN_NETWORK", Status: "SUCCESS",
TransferValue: json.Number("0.5"), TransferValue: json.Number("0.5"),
TxHash: "0x123abc456def", TxHash: "0x123abc456def",
TxType: "transfer", TxType: "transfer",
}, },
}, },
}, },
serverResponse: &server.OKResponse{
Ok: true,
Description: "Account creation successed",
Result: map[string]any{
"active": false,
},
},
expectedResult: resource.Result{ expectedResult: resource.Result{
FlagSet: []uint32{flag_account_pending}, FlagSet: []uint32{flag_account_pending},
FlagReset: []uint32{flag_api_error, flag_account_success}, FlagReset: []uint32{flag_api_error, flag_account_success},
@ -1155,9 +1168,10 @@ func TestCheckAccountStatus(t *testing.T) {
status := tt.response.Result.Transaction.Status status := tt.response.Result.Transaction.Status
// Define expected interactions with the mock // 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("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() mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(status)).Return(nil).Maybe()
// Call the method under test // Call the method under test