Skip to content

Commit

Permalink
remove relay test for poll invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Oct 18, 2024
1 parent 9770732 commit 3d7aa60
Showing 1 changed file with 0 additions and 79 deletions.
79 changes: 0 additions & 79 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2140,85 +2140,6 @@ func TestPollInvoice(t *testing.T) {
mockHttpClient.AssertExpectations(t)
})

t.Run("Should mock relay payment is successful update the bounty associated with the invoice and set the paid as true", func(t *testing.T) {
expectedUrl := fmt.Sprintf("%s/invoice?payment_request=%s", config.RelayUrl, invoice.PaymentRequest)
expectedBody := fmt.Sprintf(`{"success": true, "response": { "settled": true, "payment_request": "%s", "payment_hash": "payment_hash", "preimage": "preimage", "Amount": %d}}`, invoice.OwnerPubkey, bountyAmount)

expectedV2Url := fmt.Sprintf("%s/check_invoice", botURL)
expectedV2InvoiceBody := `{"status": "paid", "amt_msat": "", "timestamp": ""}`

r := io.NopCloser(bytes.NewReader([]byte(expectedBody)))
rv2 := io.NopCloser(bytes.NewReader([]byte(expectedV2InvoiceBody)))

if botURL != "" && botToken != "" {
mockHttpClient.On("Do", mock.MatchedBy(func(req *http.Request) bool {
return req.Method == http.MethodPost && expectedV2Url == req.URL.String() && req.Header.Get("x-admin-token") == botToken
})).Return(&http.Response{
StatusCode: 200,
Body: rv2,
}, nil).Once()
} else {
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()
}

expectedPaymentUrl := fmt.Sprintf("%s/payment", config.RelayUrl)
expectedV2PaymentUrl := fmt.Sprintf("%s/pay", botURL)

expectedPaymentBody := fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "text": "memotext added for notification", "data": ""}`, bountyAmount, invoice.OwnerPubkey)

expectedV2PaymentBody :=
fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "data": "", "wait": true}`, bountyAmount*1000, invoice.OwnerPubkey, invoiceData.RouteHint)

r2 := io.NopCloser(bytes.NewReader([]byte(`{"success": true, "response": { "sumAmount": "1"}}`)))
r3 := io.NopCloser(bytes.NewReader([]byte(`{"status": "COMPLETE", "amt_msat": "", "timestamp": "" }`)))

if botURL != "" && botToken != "" {
mockHttpClient.On("Do", mock.MatchedBy(func(req *http.Request) bool {
bodyByt, _ := io.ReadAll(req.Body)
return req.Method == http.MethodPost && expectedV2PaymentUrl == req.URL.String() && req.Header.Get("x-admin-token") == botToken && expectedV2PaymentBody == string(bodyByt)
})).Return(&http.Response{
StatusCode: 200,
Body: r3,
}, nil).Once()
} else {
mockHttpClient.On("Do", mock.MatchedBy(func(req *http.Request) bool {
bodyByt, _ := io.ReadAll(req.Body)
return req.Method == http.MethodPost && expectedPaymentUrl == req.URL.String() && req.Header.Get("x-user-token") == config.RelayAuthKey && expectedPaymentBody == string(bodyByt)
})).Return(&http.Response{
StatusCode: 200,
Body: r2,
}, nil).Once()
}

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

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

ro.ServeHTTP(rr, req)

invData := db.TestDB.GetUserInvoiceData(invoice.PaymentRequest)
updatedBounty, err := db.TestDB.GetBountyByCreated(uint(invData.Created))
if err != nil {
t.Fatal(err)
}
updatedInvoice := db.TestDB.GetInvoice(invoice.PaymentRequest)

assert.True(t, updatedBounty.Paid, "Expected bounty to be marked as paid")
assert.True(t, updatedInvoice.Status, "Expected invoice status to be true")
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 workspace budget and the payment status of the related invoice should be sent to true on the payment history table", func(t *testing.T) {
db.TestDB.DeleteInvoice(paymentRequest)

Expand Down

0 comments on commit 3d7aa60

Please sign in to comment.