From fad8f04e8dcf66b641ef024da04b779e75354936 Mon Sep 17 00:00:00 2001 From: AbdulWahab3181 Date: Thu, 27 Jun 2024 18:30:48 +0500 Subject: [PATCH 1/2] Refactored TestGenerateBudgetInvoice UT --- handlers/tribes_test.go | 53 +++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/handlers/tribes_test.go b/handlers/tribes_test.go index 797017cfb..d17b0329e 100644 --- a/handlers/tribes_test.go +++ b/handlers/tribes_test.go @@ -4,12 +4,13 @@ import ( "bytes" "context" "encoding/json" - "github.com/google/uuid" "net/http" "net/http/httptest" "strings" "testing" + "github.com/google/uuid" + "github.com/stakwork/sphinx-tribes/config" "github.com/go-chi/chi" @@ -751,9 +752,23 @@ func TestGetListedTribes(t *testing.T) { func TestGenerateBudgetInvoice(t *testing.T) { ctx := context.Background() - mockDb := mocks.NewDatabase(t) - tHandler := NewTribeHandler(mockDb) - authorizedCtx := context.WithValue(ctx, auth.ContextKey, "valid-key") + + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + + person := db.Person{ + ID: 104, + Uuid: "perosn_104_uuid", + OwnerAlias: "person104", + UniqueName: "person104", + OwnerPubKey: "person_104_pubkey", + PriceToMeet: 0, + Description: "This is test user 104", + } + db.TestDB.CreateOrEditPerson(person) + + tHandler := NewTribeHandler(db.TestDB) + authorizedCtx := context.WithValue(ctx, auth.ContextKey, person.OwnerPubKey) userAmount := uint(1000) invoiceResponse := db.InvoiceResponse{ @@ -778,11 +793,9 @@ func TestGenerateBudgetInvoice(t *testing.T) { }) t.Run("Should mock a call to relay /invoices with the correct body", func(t *testing.T) { - mockDb.On("ProcessBudgetInvoice", mock.AnythingOfType("db.NewPaymentHistory"), mock.AnythingOfType("db.NewInvoiceList")).Return(nil) - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - expectedBody := map[string]interface{}{"amount": float64(0), "memo": "Budget Invoice"} + expectedBody := map[string]interface{}{"amount": float64(1000), "memo": "Budget Invoice"} var body map[string]interface{} err := json.NewDecoder(r.Body).Decode(&body) assert.NoError(t, err) @@ -796,7 +809,12 @@ func TestGenerateBudgetInvoice(t *testing.T) { config.RelayUrl = ts.URL - reqBody := map[string]interface{}{"amount": 0} + reqBody := map[string]interface{}{ + "amount": userAmount, + "sender_pubkey": person.OwnerPubKey, + "payment_type": "deposit", + "workspace_uuid": "workspaceuuid", + } bodyBytes, _ := json.Marshal(reqBody) req, err := http.NewRequestWithContext(authorizedCtx, http.MethodPost, "/budgetinvoices", bytes.NewBuffer(bodyBytes)) @@ -813,8 +831,6 @@ func TestGenerateBudgetInvoice(t *testing.T) { userAmount := float64(1000) - mockDb.On("ProcessBudgetInvoice", mock.AnythingOfType("db.NewPaymentHistory"), mock.AnythingOfType("db.NewInvoiceList")).Return(nil) - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var body map[string]interface{} err := json.NewDecoder(r.Body).Decode(&body) @@ -829,7 +845,12 @@ func TestGenerateBudgetInvoice(t *testing.T) { config.RelayUrl = ts.URL - reqBody := map[string]interface{}{"amount": userAmount} + reqBody := map[string]interface{}{ + "amount": userAmount, + "sender_pubkey": person.OwnerPubKey, + "payment_type": "deposit", + "workspace_uuid": "workspaceuuid", + } bodyBytes, _ := json.Marshal(reqBody) req, err := http.NewRequestWithContext(authorizedCtx, http.MethodPost, "/budgetinvoices", bytes.NewBuffer(bodyBytes)) @@ -843,7 +864,6 @@ func TestGenerateBudgetInvoice(t *testing.T) { }) t.Run("Should add payments to the payment history and invoice to the invoice list upon successful relay call", func(t *testing.T) { - mockDb.On("ProcessBudgetInvoice", mock.AnythingOfType("db.NewPaymentHistory"), mock.AnythingOfType("db.NewInvoiceList")).Return(nil) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) @@ -853,7 +873,12 @@ func TestGenerateBudgetInvoice(t *testing.T) { config.RelayUrl = ts.URL - reqBody := map[string]interface{}{"amount": userAmount} + reqBody := map[string]interface{}{ + "amount": userAmount, + "sender_pubkey": person.OwnerPubKey, + "payment_type": "deposit", + "workspace_uuid": "workspaceuuid", + } bodyBytes, _ := json.Marshal(reqBody) req, err := http.NewRequestWithContext(authorizedCtx, http.MethodPost, "/budgetinvoices", bytes.NewBuffer(bodyBytes)) assert.NoError(t, err) @@ -869,7 +894,5 @@ func TestGenerateBudgetInvoice(t *testing.T) { assert.NoError(t, err) assert.True(t, response.Succcess, "Invoice generation should be successful") assert.Equal(t, "example_invoice", response.Response.Invoice, "The invoice in the response should match the mock") - - mockDb.AssertCalled(t, "ProcessBudgetInvoice", mock.AnythingOfType("db.NewPaymentHistory"), mock.AnythingOfType("db.NewInvoiceList")) }) } From d0ee28b870e246ac80b66f27a312eed4206aa5b8 Mon Sep 17 00:00:00 2001 From: AbdulWahab3181 Date: Thu, 27 Jun 2024 18:35:43 +0500 Subject: [PATCH 2/2] Minor fixes --- handlers/tribes_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers/tribes_test.go b/handlers/tribes_test.go index d17b0329e..e28ff92ca 100644 --- a/handlers/tribes_test.go +++ b/handlers/tribes_test.go @@ -795,7 +795,7 @@ func TestGenerateBudgetInvoice(t *testing.T) { t.Run("Should mock a call to relay /invoices with the correct body", func(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - expectedBody := map[string]interface{}{"amount": float64(1000), "memo": "Budget Invoice"} + expectedBody := map[string]interface{}{"amount": float64(0), "memo": "Budget Invoice"} var body map[string]interface{} err := json.NewDecoder(r.Body).Decode(&body) assert.NoError(t, err) @@ -810,7 +810,7 @@ func TestGenerateBudgetInvoice(t *testing.T) { config.RelayUrl = ts.URL reqBody := map[string]interface{}{ - "amount": userAmount, + "amount": uint(0), "sender_pubkey": person.OwnerPubKey, "payment_type": "deposit", "workspace_uuid": "workspaceuuid",