make_hold_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/constants"
  13  	"github.com/getAlby/hub/db"
  14  	"github.com/getAlby/hub/nip47/models"
  15  	"github.com/getAlby/hub/tests"
  16  )
  17  
  18  const nip47MakeHoldInvoiceJson = `
  19  {
  20  	"method": "make_hold_invoice",
  21  	"params": {
  22  		"amount": 1000,
  23  		"description": "Hello, world",
  24  		"payment_hash": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
  25  		"expiry": 3600,
  26  		"min_cltv_expiry_delta": 144,
  27  		"metadata": {
  28  		  "a": 1,
  29  			"b": "2",
  30  			"c": {
  31  			  "d": 3,
  32  				"e": [{
  33  					"f": "g"
  34  				},{
  35  					"h": "i"
  36  				}]
  37  			}
  38  		}
  39  	}
  40  }
  41  `
  42  
  43  func TestHandleMakeHoldInvoiceEvent(t *testing.T) {
  44  	ctx := context.TODO()
  45  	svc, err := tests.CreateTestService(t)
  46  	require.NoError(t, err)
  47  	defer svc.Remove()
  48  
  49  	nip47Request := &models.Request{}
  50  	err = json.Unmarshal([]byte(nip47MakeHoldInvoiceJson), nip47Request)
  51  	assert.NoError(t, err)
  52  
  53  	app, _, err := tests.CreateApp(svc)
  54  	assert.NoError(t, err)
  55  
  56  	dbRequestEvent := &db.RequestEvent{
  57  		AppId: &app.ID,
  58  	}
  59  	err = svc.DB.Create(&dbRequestEvent).Error
  60  	assert.NoError(t, err)
  61  
  62  	var publishedResponse *models.Response
  63  
  64  	publishResponse := func(response *models.Response, tags nostr.Tags) {
  65  		publishedResponse = response
  66  	}
  67  
  68  	NewTestNip47Controller(svc).
  69  		HandleMakeHoldInvoiceEvent(ctx, nip47Request, dbRequestEvent.ID, *dbRequestEvent.AppId, publishResponse)
  70  
  71  	mockLn, ok := svc.LNClient.(*tests.MockLn)
  72  	require.True(t, ok)
  73  	require.NotNil(t, mockLn.LastMinCltvExpiryDelta)
  74  	assert.EqualValues(t, 144, *mockLn.LastMinCltvExpiryDelta)
  75  
  76  	expectedMetadata := map[string]interface{}{
  77  		"a": float64(1),
  78  		"b": "2",
  79  		"c": map[string]interface{}{
  80  			"d": float64(3),
  81  			"e": []interface{}{
  82  				map[string]interface{}{"f": "g"},
  83  				map[string]interface{}{"h": "i"},
  84  			},
  85  		},
  86  	}
  87  
  88  	assert.Nil(t, publishedResponse.Error)
  89  	assert.Equal(t, tests.MockLNClientHoldTransaction.Invoice, publishedResponse.Result.(*makeHoldInvoiceResponse).Invoice)
  90  	assert.Equal(t, tests.MockLNClientHoldTransaction.PaymentHash, publishedResponse.Result.(*makeHoldInvoiceResponse).PaymentHash)
  91  	assert.Equal(t, expectedMetadata, publishedResponse.Result.(*makeHoldInvoiceResponse).Metadata)
  92  }
  93  
  94  const nip47MakeHoldInvoiceMissingPaymentHashJson = `
  95  {
  96  "method": "make_hold_invoice",
  97  "params": {
  98  "amount": 1000,
  99  "description": "Hello, world",
 100  "expiry": 3600
 101  }
 102  }
 103  `
 104  
 105  func TestHandleMakeHoldInvoiceEvent_MissingPaymentHash(t *testing.T) {
 106  	ctx := context.TODO()
 107  	svc, err := tests.CreateTestService(t)
 108  	require.NoError(t, err)
 109  	defer svc.Remove()
 110  
 111  	nip47Request := &models.Request{}
 112  	err = json.Unmarshal([]byte(nip47MakeHoldInvoiceMissingPaymentHashJson), nip47Request)
 113  	assert.NoError(t, err)
 114  
 115  	app, _, err := tests.CreateApp(svc)
 116  	assert.NoError(t, err)
 117  
 118  	dbRequestEvent := &db.RequestEvent{
 119  		AppId: &app.ID,
 120  	}
 121  	err = svc.DB.Create(&dbRequestEvent).Error
 122  	assert.NoError(t, err)
 123  
 124  	var publishedResponse *models.Response
 125  
 126  	publishResponse := func(response *models.Response, tags nostr.Tags) {
 127  		publishedResponse = response
 128  	}
 129  
 130  	NewTestNip47Controller(svc).
 131  		HandleMakeHoldInvoiceEvent(ctx, nip47Request, dbRequestEvent.ID, *dbRequestEvent.AppId, publishResponse)
 132  
 133  	require.NotNil(t, publishedResponse.Error)
 134  	assert.Equal(t, constants.ERROR_BAD_REQUEST, publishedResponse.Error.Code)
 135  }
 136