Use AccountService in the TestCreateAccount

This commit is contained in:
Alfred Kamanda 2024-08-30 10:45:04 +03:00
parent 1ded359b59
commit 76185c3aa1
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703

View File

@ -15,14 +15,24 @@ import (
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
) )
// MockAccountCreator implements AccountCreator for testing // MockAccountService implements AccountServiceInterface for testing
type MockAccountCreator struct { type MockAccountService struct {
mockResponse *models.AccountResponse mock.Mock
mockError error
} }
func (m *MockAccountCreator) CreateAccount() (*models.AccountResponse, error) { func (m *MockAccountService) CreateAccount() (*models.AccountResponse, error) {
return m.mockResponse, m.mockError args := m.Called()
return args.Get(0).(*models.AccountResponse), args.Error(1)
}
func (m *MockAccountService) CheckBalance(publicKey string) (string, error) {
args := m.Called(publicKey)
return args.String(0), args.Error(1)
}
func (m *MockAccountService) CheckAccountStatus(trackingId string) (string, error) {
args := m.Called(trackingId)
return args.String(0), args.Error(1)
} }
func TestCreateAccount(t *testing.T) { func TestCreateAccount(t *testing.T) {
@ -41,9 +51,9 @@ func TestCreateAccount(t *testing.T) {
// Initialize account file handler // Initialize account file handler
accountFileHandler := utils.NewAccountFileHandler(accountFilePath) accountFileHandler := utils.NewAccountFileHandler(accountFilePath)
// Create a mock account creator // Create a mock account service
mockAccountCreator := &MockAccountCreator{ mockAccountService := &MockAccountService{}
mockResponse: &models.AccountResponse{ mockAccountResponse := &models.AccountResponse{
Ok: true, Ok: true,
Result: struct { Result: struct {
CustodialId json.Number `json:"custodialId"` CustodialId json.Number `json:"custodialId"`
@ -54,13 +64,16 @@ func TestCreateAccount(t *testing.T) {
PublicKey: "test-public-key", PublicKey: "test-public-key",
TrackingId: "test-tracking-id", TrackingId: "test-tracking-id",
}, },
},
} }
// Initialize Handlers with mock account creator // Set up expectations for the mock account service
mockAccountService.On("CreateAccount").Return(mockAccountResponse, nil)
// Initialize Handlers with mock account service
h := &Handlers{ h := &Handlers{
fs: &FSData{Path: accountFilePath},
accountFileHandler: accountFileHandler, accountFileHandler: accountFileHandler,
accountCreator: mockAccountCreator, accountService: mockAccountService,
} }
tests := []struct { tests := []struct {
@ -102,9 +115,7 @@ func TestCreateAccount(t *testing.T) {
"FirstName": "Not provided", "FirstName": "Not provided",
"FamilyName": "Not provided", "FamilyName": "Not provided",
}, },
expectedResult: resource.Result{ expectedResult: resource.Result{},
FlagSet: []uint32{models.USERFLAG_ACCOUNT_CREATED},
},
expectedData: map[string]string{ expectedData: map[string]string{
"TrackingId": "test-tracking-id", "TrackingId": "test-tracking-id",
"PublicKey": "test-public-key", "PublicKey": "test-public-key",
@ -173,8 +184,6 @@ func TestCreateAccount(t *testing.T) {
} }
} }
func TestCreateAccount_Success(t *testing.T) { func TestCreateAccount_Success(t *testing.T) {
mockAccountFileHandler := new(mocks.MockAccountFileHandler) mockAccountFileHandler := new(mocks.MockAccountFileHandler)
mockCreateAccountService := new(mocks.MockAccountService) mockCreateAccountService := new(mocks.MockAccountService)
@ -206,18 +215,14 @@ func TestCreateAccount_Success(t *testing.T) {
accountService: mockCreateAccountService, accountService: mockCreateAccountService,
} }
actualResponse, err := handlers.accountService.CreateAccount() actualResponse, err := handlers.accountService.CreateAccount()
// Assert results // Assert results
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t,expectedAccountResp.Ok,true) assert.Equal(t, expectedAccountResp.Ok, true)
assert.Equal(t,expectedAccountResp,actualResponse) assert.Equal(t, expectedAccountResp, actualResponse)
} }
func TestSavePin(t *testing.T) { func TestSavePin(t *testing.T) {
// Setup // Setup
tempDir, err := os.MkdirTemp("", "test_save_pin") tempDir, err := os.MkdirTemp("", "test_save_pin")