wip-account-creation #4

Merged
lash merged 143 commits from wip-account-creation into master 2024-08-30 14:37:58 +02:00
Showing only changes of commit 76185c3aa1 - Show all commits

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")