wip-account-creation #4
@ -21,158 +21,6 @@ type MockAccountCreator struct {
|
|||||||
mockError error
|
mockError error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountCreator) CreateAccount() (*models.AccountResponse, error) {
|
|
||||||
return m.mockResponse, m.mockError
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateAccount(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
tempDir, err := os.MkdirTemp("", "test_create_account")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create temp directory: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir) // Clean up after the test run
|
|
||||||
|
|
||||||
sessionID := "07xxxxxxxx"
|
|
||||||
|
|
||||||
// Set up the data file path using the session ID
|
|
||||||
accountFilePath := filepath.Join(tempDir, sessionID+"_data")
|
|
||||||
|
|
||||||
// Initialize account file handler
|
|
||||||
accountFileHandler := utils.NewAccountFileHandler(accountFilePath)
|
|
||||||
|
|
||||||
// Create a mock account creator
|
|
||||||
mockAccountCreator := &MockAccountCreator{
|
|
||||||
mockResponse: &models.AccountResponse{
|
|
||||||
Ok: true,
|
|
||||||
Result: struct {
|
|
||||||
CustodialId json.Number `json:"custodialId"`
|
|
||||||
PublicKey string `json:"publicKey"`
|
|
||||||
TrackingId string `json:"trackingId"`
|
|
||||||
}{
|
|
||||||
CustodialId: "test-custodial-id",
|
|
||||||
PublicKey: "test-public-key",
|
|
||||||
TrackingId: "test-tracking-id",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize Handlers with mock account creator
|
|
||||||
h := &Handlers{
|
|
||||||
accountFileHandler: accountFileHandler,
|
|
||||||
accountCreator: mockAccountCreator,
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
existingData map[string]string
|
|
||||||
expectedResult resource.Result
|
|
||||||
expectedData map[string]string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "New account creation",
|
|
||||||
existingData: nil,
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagSet: []uint32{models.USERFLAG_ACCOUNT_CREATED},
|
|
||||||
},
|
|
||||||
expectedData: map[string]string{
|
|
||||||
"TrackingId": "test-tracking-id",
|
|
||||||
"PublicKey": "test-public-key",
|
|
||||||
"CustodialId": "test-custodial-id",
|
|
||||||
"Status": "PENDING",
|
|
||||||
"Gender": "Not provided",
|
|
||||||
"YOB": "Not provided",
|
|
||||||
"Location": "Not provided",
|
|
||||||
"Offerings": "Not provided",
|
|
||||||
"FirstName": "Not provided",
|
|
||||||
"FamilyName": "Not provided",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Existing account",
|
|
||||||
existingData: map[string]string{
|
|
||||||
"TrackingId": "test-tracking-id",
|
|
||||||
"PublicKey": "test-public-key",
|
|
||||||
"CustodialId": "test-custodial-id",
|
|
||||||
"Status": "PENDING",
|
|
||||||
"Gender": "Not provided",
|
|
||||||
"YOB": "Not provided",
|
|
||||||
"Location": "Not provided",
|
|
||||||
"Offerings": "Not provided",
|
|
||||||
"FirstName": "Not provided",
|
|
||||||
"FamilyName": "Not provided",
|
|
||||||
},
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagSet: []uint32{models.USERFLAG_ACCOUNT_CREATED},
|
|
||||||
},
|
|
||||||
expectedData: map[string]string{
|
|
||||||
"TrackingId": "test-tracking-id",
|
|
||||||
"PublicKey": "test-public-key",
|
|
||||||
"CustodialId": "test-custodial-id",
|
|
||||||
"Status": "PENDING",
|
|
||||||
"Gender": "Not provided",
|
|
||||||
"YOB": "Not provided",
|
|
||||||
"Location": "Not provided",
|
|
||||||
"Offerings": "Not provided",
|
|
||||||
"FirstName": "Not provided",
|
|
||||||
"FamilyName": "Not provided",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
// Set up the data file path using the session ID
|
|
||||||
accountFilePath := filepath.Join(tempDir, sessionID+"_data")
|
|
||||||
|
|
||||||
// Setup existing data if any
|
|
||||||
if tt.existingData != nil {
|
|
||||||
data, _ := json.Marshal(tt.existingData)
|
|
||||||
err := os.WriteFile(accountFilePath, data, 0644)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to write existing data: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call the function
|
|
||||||
result, err := h.CreateAccount(context.Background(), "", nil)
|
|
||||||
|
|
||||||
// Check for errors
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("CreateAccount returned an error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the result
|
|
||||||
if len(result.FlagSet) != len(tt.expectedResult.FlagSet) {
|
|
||||||
t.Errorf("Expected %d flags, got %d", len(tt.expectedResult.FlagSet), len(result.FlagSet))
|
|
||||||
}
|
|
||||||
for i, flag := range tt.expectedResult.FlagSet {
|
|
||||||
if result.FlagSet[i] != flag {
|
|
||||||
t.Errorf("Expected flag %d, got %d", flag, result.FlagSet[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the stored data
|
|
||||||
data, err := os.ReadFile(accountFilePath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to read account data file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var storedData map[string]string
|
|
||||||
err = json.Unmarshal(data, &storedData)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to unmarshal stored data: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, expectedValue := range tt.expectedData {
|
|
||||||
if storedValue, ok := storedData[key]; !ok || storedValue != expectedValue {
|
|
||||||
t.Errorf("Expected %s to be %s, got %s", key, expectedValue, storedValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func TestCreateAccount_Success(t *testing.T) {
|
func TestCreateAccount_Success(t *testing.T) {
|
||||||
@ -327,3 +175,67 @@ func TestSavePin(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func TestSaveLocation(t *testing.T) {
|
||||||
|
// Create a new instance of MockAccountFileHandler
|
||||||
|
mockFileHandler := new(mocks.MockAccountFileHandler)
|
||||||
|
|
||||||
|
// Define test cases
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input []byte
|
||||||
|
existingData map[string]string
|
||||||
|
writeError error
|
||||||
|
expectedResult resource.Result
|
||||||
|
expectedError error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Successful Save",
|
||||||
|
input: []byte("Mombasa"),
|
||||||
|
existingData: map[string]string{"Location": "Mombasa"},
|
||||||
|
writeError: nil,
|
||||||
|
expectedResult: resource.Result{},
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Empty location input",
|
||||||
|
input: []byte{},
|
||||||
|
existingData: map[string]string{"OtherKey": "OtherValue"},
|
||||||
|
writeError: nil,
|
||||||
|
expectedResult: resource.Result{},
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// Set up the mock expectations
|
||||||
|
mockFileHandler.On("ReadAccountData").Return(tt.existingData, tt.expectedError)
|
||||||
|
if tt.expectedError == nil && len(tt.input) > 0 {
|
||||||
|
mockFileHandler.On("WriteAccountData", mock.MatchedBy(func(data map[string]string) bool {
|
||||||
|
return data["Location"] == string(tt.input)
|
||||||
|
})).Return(tt.writeError)
|
||||||
|
}else if len(tt.input) == 0 {
|
||||||
|
// For empty input, no WriteAccountData call should be made
|
||||||
|
mockFileHandler.On("WriteAccountData", mock.Anything).Maybe().Return(tt.writeError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the Handlers instance with the mock file handler
|
||||||
|
h := &Handlers{
|
||||||
|
accountFileHandler: mockFileHandler,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the method
|
||||||
|
result, err := h.SaveLocation(context.Background(), "save_location", tt.input)
|
||||||
|
|
||||||
|
// Assert the results
|
||||||
|
assert.Equal(t, tt.expectedResult, result)
|
||||||
|
assert.Equal(t, tt.expectedError, err)
|
||||||
|
|
||||||
|
// Assert all expectations were met
|
||||||
|
mockFileHandler.AssertExpectations(t)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user