Skip to content

Commit

Permalink
Merge pull request stakwork#1786 from AbdulWahab3181/Refactor-TestGen…
Browse files Browse the repository at this point in the history
…erateBudgetInvoice-UT

Refactor TestGenerateBudgetInvoice To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty authored Jun 27, 2024
2 parents 7045f28 + d0ee28b commit 6ac4ccf
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions handlers/tribes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -774,9 +775,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{
Expand All @@ -801,8 +816,6 @@ 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"}
Expand All @@ -819,7 +832,12 @@ func TestGenerateBudgetInvoice(t *testing.T) {

config.RelayUrl = ts.URL

reqBody := map[string]interface{}{"amount": 0}
reqBody := map[string]interface{}{
"amount": uint(0),
"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))
Expand All @@ -836,8 +854,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)
Expand All @@ -852,7 +868,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))
Expand All @@ -866,7 +887,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)
Expand All @@ -876,7 +896,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)
Expand All @@ -892,7 +917,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"))
})
}

0 comments on commit 6ac4ccf

Please sign in to comment.