Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TestGenerateBudgetInvoice To Use A Real Postgres DB For The Test #1786

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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{
Expand All @@ -778,8 +793,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 @@ -796,7 +809,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 @@ -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)
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"))
})
}
Loading