From 5b0a38351325bcf43eae700f5a06ac4e8bd8883b Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 21 Oct 2024 11:09:37 +0300 Subject: [PATCH] use importable api structs --- internal/handlers/server/accountservice.go | 29 ++++++++++++---------- internal/handlers/ussd/menuhandler_test.go | 14 +++++++---- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/internal/handlers/server/accountservice.go b/internal/handlers/server/accountservice.go index c284d63..39d1c66 100644 --- a/internal/handlers/server/accountservice.go +++ b/internal/handlers/server/accountservice.go @@ -9,18 +9,19 @@ import ( "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/internal/models" + "github.com/grassrootseconomics/eth-custodial/pkg/api" ) -var apiResponse struct { +type ApiResponse struct { Ok bool `json:"ok"` Description string `json:"description"` } type AccountServiceInterface interface { CheckBalance(publicKey string) (*models.BalanceResponse, error) - CreateAccount() (*OKResponse, *ErrResponse) + CreateAccount() (*api.OKResponse, *api.ErrResponse) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) - TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) + TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) } type AccountService struct { @@ -59,9 +60,9 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackSt } -func (as *AccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) { - var errResponse ErrResponse - var okResponse OKResponse +func (as *AccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) { + var errResponse api.ErrResponse + var okResponse api.OKResponse var err error // Construct the URL with the path parameter url := fmt.Sprintf("%s/%s", config.TrackURL, publicKey) @@ -90,6 +91,7 @@ func (as *AccountService) TrackAccountStatus(publicKey string) (*OKResponse, *Er } // Step 2: Unmarshal into the generic struct + var apiResponse ApiResponse err = json.Unmarshal([]byte(body), &apiResponse) if err != nil { errResponse.Description = err.Error() @@ -139,10 +141,10 @@ func (as *AccountService) CheckBalance(publicKey string) (*models.BalanceRespons // If there is an error during the request or processing, this will be nil. // - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data. // If no error occurs, this will be nil. -func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) { +func (as *AccountService) CreateAccount() (*api.OKResponse, *api.ErrResponse) { - var errResponse ErrResponse - var okResponse OKResponse + var errResponse api.ErrResponse + var okResponse api.OKResponse var err error // Create a new request @@ -166,6 +168,7 @@ func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) { errResponse.Description = err.Error() return nil, &errResponse } + var apiResponse ApiResponse err = json.Unmarshal([]byte(body), &apiResponse) if err != nil { return nil, &errResponse @@ -187,8 +190,8 @@ func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) { } } -func (tas *TestAccountService) CreateAccount() (*OKResponse, *ErrResponse) { - return &OKResponse{ +func (tas *TestAccountService) CreateAccount() (*api.OKResponse, *api.ErrResponse) { + return &api.OKResponse{ Ok: true, Description: "Account creation request received successfully", Result: map[string]any{"publicKey": "0x48ADca309b5085852207FAaf2816eD72B52F527C", "trackingId": "28ebe84d-b925-472c-87ae-bbdfa1fb97be"}, @@ -212,8 +215,8 @@ func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceRe return balanceResponse, nil } -func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) { - return &OKResponse{ +func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) { + return &api.OKResponse{ Ok: true, Description: "Account creation succeeded", Result: map[string]any{ diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index f84ca06..d78f526 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -20,6 +20,7 @@ import ( "git.grassecon.net/urdt/ussd/internal/models" "git.grassecon.net/urdt/ussd/internal/utils" "github.com/alecthomas/assert/v2" + "github.com/grassrootseconomics/eth-custodial/pkg/api" testdataloader "github.com/peteole/testdata-loader" "github.com/stretchr/testify/require" ) @@ -74,6 +75,8 @@ func TestCreateAccount(t *testing.T) { } // Create required mocks flag_account_created, err := fm.GetFlag("flag_account_created") + //flag_api_call_error, err := fm.GetFlag("flag_api_call_error,") + flag_api_call_error, _ := fm.GetFlag("flag_api_call_error") if err != nil { t.Logf(err.Error()) } @@ -85,12 +88,12 @@ func TestCreateAccount(t *testing.T) { tests := []struct { name string - serverResponse *server.OKResponse + serverResponse *api.OKResponse expectedResult resource.Result }{ { name: "Test account creation success", - serverResponse: &server.OKResponse{ + serverResponse: &api.OKResponse{ Ok: true, Description: "Account creation successed", Result: map[string]any{ @@ -100,6 +103,7 @@ func TestCreateAccount(t *testing.T) { }, expectedResult: resource.Result{ FlagSet: []uint32{flag_account_created}, + FlagReset: []uint32{flag_api_call_error}, }, }, } @@ -1073,14 +1077,14 @@ func TestCheckAccountStatus(t *testing.T) { tests := []struct { name string input []byte - serverResponse *server.OKResponse + serverResponse *api.OKResponse response *models.TrackStatusResponse expectedResult resource.Result }{ { name: "Test when account is on the Sarafu network", input: []byte("TrackingId1234"), - serverResponse: &server.OKResponse{ + serverResponse: &api.OKResponse{ Ok: true, Description: "Account creation succeeded", Result: map[string]any{ @@ -1135,7 +1139,7 @@ func TestCheckAccountStatus(t *testing.T) { }, }, }, - serverResponse: &server.OKResponse{ + serverResponse: &api.OKResponse{ Ok: true, Description: "Account creation succeeded", Result: map[string]any{