Skip to content

Commit

Permalink
chore: update tests for dynamic budgets (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jul 17, 2024
1 parent 10b2a4e commit 9a8be5a
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 429 deletions.
58 changes: 16 additions & 42 deletions nip47/controllers/get_balance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const nip47GetBalanceJson = `
}
`

func TestHandleGetBalanceEvent_NoPermission(t *testing.T) {
func TestHandleGetBalanceEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
Expand All @@ -31,19 +31,13 @@ func TestHandleGetBalanceEvent_NoPermission(t *testing.T) {
err = json.Unmarshal([]byte(nip47GetBalanceJson), nip47Request)
assert.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)

dbRequestEvent := &db.RequestEvent{}
err = svc.DB.Create(&dbRequestEvent).Error
assert.NoError(t, err)

checkPermission := func(amountMsat uint64) *models.Response {
return &models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: models.ERROR_RESTRICTED,
},
}
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
Expand All @@ -53,13 +47,13 @@ func TestHandleGetBalanceEvent_NoPermission(t *testing.T) {
permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
transactionsSvc := transactions.NewTransactionsService(svc.DB)
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc).
HandleGetBalanceEvent(ctx, nip47Request, dbRequestEvent.ID, *dbRequestEvent.AppId, publishResponse)
HandleGetBalanceEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse)

assert.Nil(t, publishedResponse.Result)
assert.Equal(t, models.ERROR_RESTRICTED, publishedResponse.Error.Code)
assert.Equal(t, int64(21000), publishedResponse.Result.(*getBalanceResponse).Balance)
assert.Nil(t, publishedResponse.Error)
}

func TestHandleGetBalanceEvent_WithPermission(t *testing.T) {
func TestHandleGetBalanceEvent_IsolatedApp(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
Expand All @@ -69,14 +63,15 @@ func TestHandleGetBalanceEvent_WithPermission(t *testing.T) {
err = json.Unmarshal([]byte(nip47GetBalanceJson), nip47Request)
assert.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
app.Isolated = true
svc.DB.Save(&app)

dbRequestEvent := &db.RequestEvent{}
err = svc.DB.Create(&dbRequestEvent).Error
assert.NoError(t, err)

checkPermission := func(amountMsat uint64) *models.Response {
return nil
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
Expand All @@ -86,30 +81,9 @@ func TestHandleGetBalanceEvent_WithPermission(t *testing.T) {
permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
transactionsSvc := transactions.NewTransactionsService(svc.DB)
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc).
HandleGetBalanceEvent(ctx, nip47Request, dbRequestEvent.ID, *dbRequestEvent.AppId, publishResponse)
HandleGetBalanceEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse)

assert.Equal(t, int64(21000), publishedResponse.Result.(*getBalanceResponse).Balance)
// FIXME: implement
assert.Equal(t, int64(1234567), publishedResponse.Result.(*getBalanceResponse).Balance)
assert.Nil(t, publishedResponse.Error)
}

// create pay_invoice permission
// maxAmount := 1000
// budgetRenewal := "never"
// appPermission = &db.AppPermission{
// AppId: app.ID,
// App: *app,
// RequestMethod: models.PAY_INVOICE_METHOD,
// MaxAmount: maxAmount,
// BudgetRenewal: budgetRenewal,
// ExpiresAt: &expiresAt,
// }
// err = svc.DB.Create(appPermission).Error
// assert.NoError(t, err)

// reqEvent.ID = "test_get_balance_with_budget"
// responses = []*models.Response{}
// svc.nip47Svc.HandleGetBalanceEvent(ctx, nip47Request, dbRequestEvent, app, publishResponse)

// assert.Equal(t, int64(21000), responses[0].Result.(*getBalanceResponse).Balance)
// assert.Equal(t, 1000000, responses[0].Result.(*getBalanceResponse).MaxAmount)
// assert.Equal(t, "never", responses[0].Result.(*getBalanceResponse).BudgetRenewal)
28 changes: 5 additions & 23 deletions nip47/controllers/get_info_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/nbd-wtf/go-nostr"
"github.com/stretchr/testify/assert"

"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
"github.com/getAlby/hub/nip47/models"
"github.com/getAlby/hub/nip47/permissions"
Expand All @@ -21,7 +22,6 @@ const nip47GetInfoJson = `
}
`

// TODO: info event should always return something
func TestHandleGetInfoEvent_NoPermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
Expand All @@ -41,21 +41,12 @@ func TestHandleGetInfoEvent_NoPermission(t *testing.T) {

appPermission := &db.AppPermission{
AppId: app.ID,
Scope: permissions.GET_BALANCE_SCOPE,
Scope: constants.GET_BALANCE_SCOPE,
ExpiresAt: nil,
}
err = svc.DB.Create(appPermission).Error
assert.NoError(t, err)

checkPermission := func(amountMsat uint64) *models.Response {
return &models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: models.ERROR_RESTRICTED,
},
}
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
Expand Down Expand Up @@ -98,16 +89,12 @@ func TestHandleGetInfoEvent_WithPermission(t *testing.T) {

appPermission := &db.AppPermission{
AppId: app.ID,
Scope: permissions.GET_INFO_SCOPE,
Scope: constants.GET_INFO_SCOPE,
ExpiresAt: nil,
}
err = svc.DB.Create(appPermission).Error
assert.NoError(t, err)

checkPermission := func(amountMsat uint64) *models.Response {
return nil
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
Expand Down Expand Up @@ -150,25 +137,20 @@ func TestHandleGetInfoEvent_WithNotifications(t *testing.T) {

appPermission := &db.AppPermission{
AppId: app.ID,
Scope: permissions.GET_INFO_SCOPE,
Scope: constants.GET_INFO_SCOPE,
ExpiresAt: nil,
}
err = svc.DB.Create(appPermission).Error
assert.NoError(t, err)

// TODO: AppPermission RequestMethod needs to change to scope
appPermission = &db.AppPermission{
AppId: app.ID,
Scope: permissions.NOTIFICATIONS_SCOPE,
Scope: constants.NOTIFICATIONS_SCOPE,
ExpiresAt: nil,
}
err = svc.DB.Create(appPermission).Error
assert.NoError(t, err)

checkPermission := func(amountMsat uint64) *models.Response {
return nil
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
Expand Down
51 changes: 6 additions & 45 deletions nip47/controllers/list_transactions_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/nbd-wtf/go-nostr"
"github.com/stretchr/testify/assert"

"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
"github.com/getAlby/hub/nip47/models"
"github.com/getAlby/hub/nip47/permissions"
Expand All @@ -29,45 +30,7 @@ const nip47ListTransactionsJson = `
}
`

func TestHandleListTransactionsEvent_NoPermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47ListTransactionsJson), nip47Request)
assert.NoError(t, err)

dbRequestEvent := &db.RequestEvent{}
err = svc.DB.Create(&dbRequestEvent).Error
assert.NoError(t, err)

checkPermission := func(amountMsat uint64) *models.Response {
return &models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: models.ERROR_RESTRICTED,
},
}
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
publishedResponse = response
}

permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
transactionsSvc := transactions.NewTransactionsService(svc.DB)
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc).
HandleListTransactionsEvent(ctx, nip47Request, dbRequestEvent.ID, *dbRequestEvent.AppId, publishResponse)

assert.Nil(t, publishedResponse.Result)
assert.Equal(t, models.ERROR_RESTRICTED, publishedResponse.Error.Code)
}

func TestHandleListTransactionsEvent_WithPermission(t *testing.T) {
func TestHandleListTransactionsEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
Expand All @@ -80,7 +43,9 @@ func TestHandleListTransactionsEvent_WithPermission(t *testing.T) {
app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)

dbRequestEvent := &db.RequestEvent{}
dbRequestEvent := &db.RequestEvent{
AppId: &app.ID,
}
err = svc.DB.Create(&dbRequestEvent).Error
assert.NoError(t, err)

Expand All @@ -97,17 +62,13 @@ func TestHandleListTransactionsEvent_WithPermission(t *testing.T) {
AmountMsat: uint64(tests.MockLNClientTransactions[i].Amount),
FeeMsat: &feesPaid,
SettledAt: &settledAt,
State: transactions.TRANSACTION_STATE_SETTLED,
State: constants.TRANSACTION_STATE_SETTLED,
AppId: &app.ID,
CreatedAt: time.Now().Add(time.Duration(-i) * time.Hour),
}).Error
assert.NoError(t, err)
}

checkPermission := func(amountMsat uint64) *models.Response {
return nil
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
Expand Down
49 changes: 1 addition & 48 deletions nip47/controllers/lookup_invoice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,7 @@ var nip47LookupInvoiceJson = `
}
`

func TestHandleLookupInvoiceEvent_NoPermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)

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)

checkPermission := func(amountMsat uint64) *models.Response {
return &models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: models.ERROR_RESTRICTED,
},
}
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
publishedResponse = response
}

permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
transactionsSvc := transactions.NewTransactionsService(svc.DB)
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc).
HandleLookupInvoiceEvent(ctx, nip47Request, dbRequestEvent.ID, *dbRequestEvent.AppId, publishResponse)

assert.Nil(t, publishedResponse.Result)
assert.Equal(t, models.ERROR_RESTRICTED, publishedResponse.Error.Code)
}

func TestHandleLookupInvoiceEvent_WithPermission(t *testing.T) {
func TestHandleLookupInvoiceEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
Expand Down Expand Up @@ -103,10 +60,6 @@ func TestHandleLookupInvoiceEvent_WithPermission(t *testing.T) {
}).Error
assert.NoError(t, err)

checkPermission := func(amountMsat uint64) *models.Response {
return nil
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
Expand Down
49 changes: 1 addition & 48 deletions nip47/controllers/make_invoice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const nip47MakeInvoiceJson = `
}
`

func TestHandleMakeInvoiceEvent_NoPermission(t *testing.T) {
func TestHandleMakeInvoiceEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
Expand All @@ -45,53 +45,6 @@ func TestHandleMakeInvoiceEvent_NoPermission(t *testing.T) {
err = svc.DB.Create(&dbRequestEvent).Error
assert.NoError(t, err)

checkPermission := func(amountMsat uint64) *models.Response {
return &models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: models.ERROR_RESTRICTED,
},
}
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
publishedResponse = response
}

permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
transactionsSvc := transactions.NewTransactionsService(svc.DB)
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc).
HandleMakeInvoiceEvent(ctx, nip47Request, dbRequestEvent.ID, *dbRequestEvent.AppId, publishResponse)

assert.Nil(t, publishedResponse.Result)
assert.Equal(t, models.ERROR_RESTRICTED, publishedResponse.Error.Code)
}

func TestHandleMakeInvoiceEvent_WithPermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47MakeInvoiceJson), 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)

checkPermission := func(amountMsat uint64) *models.Response {
return nil
}

var publishedResponse *models.Response

publishResponse := func(response *models.Response, tags nostr.Tags) {
Expand Down
Loading

0 comments on commit 9a8be5a

Please sign in to comment.