make_invoice_controller_test.go raw
1 package controllers
2
3 import (
4 "context"
5 "encoding/json"
6 "testing"
7
8 "github.com/getAlby/go-nostr"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11
12 "github.com/getAlby/hub/db"
13 "github.com/getAlby/hub/nip47/models"
14 "github.com/getAlby/hub/tests"
15 )
16
17 const nip47MakeInvoiceJson = `
18 {
19 "method": "make_invoice",
20 "params": {
21 "amount": 1000,
22 "description": "Hello, world",
23 "expiry": 3600,
24 "metadata": {
25 "a": 1,
26 "b": "2",
27 "c": {
28 "d": 3,
29 "e": [{
30 "f": "g"
31 },{
32 "h": "i"
33 }]
34 }
35 }
36 }
37 }
38 `
39
40 func TestHandleMakeInvoiceEvent(t *testing.T) {
41 ctx := context.TODO()
42 svc, err := tests.CreateTestService(t)
43 require.NoError(t, err)
44 defer svc.Remove()
45
46 nip47Request := &models.Request{}
47 err = json.Unmarshal([]byte(nip47MakeInvoiceJson), nip47Request)
48 assert.NoError(t, err)
49
50 app, _, err := tests.CreateApp(svc)
51 assert.NoError(t, err)
52
53 dbRequestEvent := &db.RequestEvent{
54 AppId: &app.ID,
55 }
56 err = svc.DB.Create(&dbRequestEvent).Error
57 assert.NoError(t, err)
58
59 var publishedResponse *models.Response
60
61 publishResponse := func(response *models.Response, tags nostr.Tags) {
62 publishedResponse = response
63 }
64
65 NewTestNip47Controller(svc).
66 HandleMakeInvoiceEvent(ctx, nip47Request, dbRequestEvent.ID, *dbRequestEvent.AppId, publishResponse)
67
68 expectedMetadata := map[string]interface{}{
69 "a": float64(1),
70 "b": "2",
71 "c": map[string]interface{}{
72 "d": float64(3),
73 "e": []interface{}{
74 map[string]interface{}{"f": "g"},
75 map[string]interface{}{"h": "i"},
76 },
77 },
78 }
79
80 assert.Nil(t, publishedResponse.Error)
81 assert.Equal(t, tests.MockLNClientTransaction.Invoice, publishedResponse.Result.(*makeInvoiceResponse).Invoice)
82 assert.Equal(t, expectedMetadata, publishedResponse.Result.(*makeInvoiceResponse).Metadata)
83 }
84