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

Backend [Integration Test] bounty.go handlers PollInvoice For Organization Budget #1598

Merged
merged 2 commits into from
Mar 7, 2024
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
37 changes: 37 additions & 0 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1549,4 +1549,41 @@ func TestPollInvoice(t *testing.T) {
assert.Equal(t, http.StatusOK, rr.Code)
mockHttpClient.AssertExpectations(t)
})

t.Run("If the invoice is settled and the invoice.Type is equal to BUDGET the invoice amount should be added to the organization budget and the payment status of the related invoice should be sent to true on the payment history table", func(t *testing.T) {
ctx := context.Background()
mockDb := &dbMocks.Database{}
mockHttpClient := &mocks.HttpClient{}
bHandler := NewBountyHandler(mockHttpClient, mockDb)
authorizedCtx := context.WithValue(ctx, auth.ContextKey, "valid-key")
expectedUrl := fmt.Sprintf("%s/invoice?payment_request=%s", config.RelayUrl, "1")

r := io.NopCloser(bytes.NewReader([]byte(`{"success": true, "response": { "settled": true, "payment_request": "1", "payment_hash": "payment_hash", "preimage": "preimage", "Amount": "1000"}}`)))
mockHttpClient.On("Do", mock.MatchedBy(func(req *http.Request) bool {
return req.Method == http.MethodGet && expectedUrl == req.URL.String() && req.Header.Get("x-user-token") == config.RelayAuthKey
})).Return(&http.Response{
StatusCode: 200,
Body: r,
}, nil).Once()

mockDb.On("GetInvoice", "1").Return(db.InvoiceList{Type: "BUDGET"})
mockDb.On("GetUserInvoiceData", "1").Return(db.UserInvoiceData{Amount: 1000, UserPubkey: "UserPubkey", RouteHint: "RouteHint", Created: 1234})
mockDb.On("GetInvoice", "1").Return(db.InvoiceList{Status: false})
mockDb.On("AddAndUpdateBudget", mock.Anything).Return(db.PaymentHistory{})
mockDb.On("UpdateInvoice", "1").Return(db.InvoiceList{}).Once()

ro := chi.NewRouter()
ro.Post("/poll/invoice/{paymentRequest}", bHandler.PollInvoice)

rr := httptest.NewRecorder()
req, err := http.NewRequestWithContext(authorizedCtx, http.MethodPost, "/poll/invoice/1", bytes.NewBufferString(`{}`))
if err != nil {
t.Fatal(err)
}

ro.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
mockHttpClient.AssertExpectations(t)
})
}
Loading