mirror of
https://github.com/getAlby/hub.git
synced 2026-08-13 12:33:39 +02:00
81 lines
2.7 KiB
Go
81 lines
2.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/getAlby/go-nostr"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/getAlby/hub/db"
|
|
"github.com/getAlby/hub/nip47/models"
|
|
"github.com/getAlby/hub/tests"
|
|
)
|
|
|
|
var nip47LookupInvoiceJson = `
|
|
{
|
|
"method": "lookup_invoice",
|
|
"params": {
|
|
"payment_hash": "` + tests.MockLNClientTransaction.PaymentHash + `"
|
|
}
|
|
}
|
|
`
|
|
|
|
func TestHandleLookupInvoiceEvent(t *testing.T) {
|
|
ctx := context.TODO()
|
|
svc, err := tests.CreateTestService(t)
|
|
require.NoError(t, err)
|
|
defer svc.Remove()
|
|
|
|
nip47Request := &models.Request{}
|
|
err = json.Unmarshal([]byte(nip47LookupInvoiceJson), nip47Request)
|
|
assert.NoError(t, err)
|
|
|
|
app, _, err := tests.CreateApp(svc)
|
|
assert.NoError(t, err)
|
|
|
|
dbRequestEvent := &db.RequestEvent{
|
|
AppId: &app.ID,
|
|
}
|
|
err = svc.DB.Create(&dbRequestEvent).Error
|
|
assert.NoError(t, err)
|
|
|
|
settledAt := time.Unix(*tests.MockLNClientTransaction.SettledAt, 0)
|
|
err = svc.DB.Create(&db.Transaction{
|
|
Type: tests.MockLNClientTransaction.Type,
|
|
PaymentRequest: tests.MockLNClientTransaction.Invoice,
|
|
Description: tests.MockLNClientTransaction.Description,
|
|
DescriptionHash: tests.MockLNClientTransaction.DescriptionHash,
|
|
Preimage: &tests.MockLNClientTransaction.Preimage,
|
|
PaymentHash: tests.MockLNClientTransaction.PaymentHash,
|
|
AmountMsat: uint64(tests.MockLNClientTransaction.AmountMsat),
|
|
FeeMsat: uint64(tests.MockLNClientTransaction.FeesPaidMsat),
|
|
SettledAt: &settledAt,
|
|
AppId: &app.ID,
|
|
}).Error
|
|
assert.NoError(t, err)
|
|
|
|
var publishedResponse *models.Response
|
|
|
|
publishResponse := func(response *models.Response, tags nostr.Tags) {
|
|
publishedResponse = response
|
|
}
|
|
|
|
NewTestNip47Controller(svc).
|
|
HandleLookupInvoiceEvent(ctx, nip47Request, dbRequestEvent.ID, *dbRequestEvent.AppId, publishResponse)
|
|
|
|
assert.Nil(t, publishedResponse.Error)
|
|
transaction := publishedResponse.Result.(*lookupInvoiceResponse)
|
|
assert.Equal(t, tests.MockLNClientTransaction.Type, transaction.Type)
|
|
assert.Equal(t, tests.MockLNClientTransaction.Invoice, transaction.Invoice)
|
|
assert.Equal(t, tests.MockLNClientTransaction.Description, transaction.Description)
|
|
assert.Equal(t, tests.MockLNClientTransaction.DescriptionHash, transaction.DescriptionHash)
|
|
assert.Equal(t, tests.MockLNClientTransaction.Preimage, transaction.Preimage)
|
|
assert.Equal(t, tests.MockLNClientTransaction.PaymentHash, transaction.PaymentHash)
|
|
assert.Equal(t, tests.MockLNClientTransaction.AmountMsat, transaction.Amount)
|
|
assert.Equal(t, tests.MockLNClientTransaction.FeesPaidMsat, transaction.FeesPaid)
|
|
assert.Equal(t, tests.MockLNClientTransaction.SettledAt, transaction.SettledAt)
|
|
}
|