mirror of
https://github.com/getAlby/hub.git
synced 2026-08-13 12:33:39 +02:00
* feat: nwc create_connection command (WIP) * feat: allow creating superuser apps from the ui * fix: pass methods rather than scopes in create_connection method * chore: add extra tests * fix: use browser router in http mode * fix: update links to not use hash router * fix: add redirect from hash router url * fix: return nostrWalletConnectUrl in nwc connection success event and message * feat: publish nwa event * chore: use nwc info event instead of nwa event * feat: create custom alby go detail page * chore: allow creating/editing apps with the same name * fix: convert budget from msats to sats (#1111) * chore: address NWA feedback * chore: address feedback - adjust button copy - make create_connection methods consistent with http deeplink flow - remove unused constant - add empty string check before adding lud16 tag - fix test * feat: add support for notification_types in create_connection method * fix: shorter button copy * fix: do not include lud16 tag in published info event * Feat: add lud16 to get_info response (#1128) feat: add lud16 to get_info response * chore: minor alby go screen improvements * fix: incorrect unlock password error message to create app with superuser access * chore: minor ui improvements on alby go detail page * chore: avoid duplicate app names by adding a suffix * chore: move scopes check to apps service, add new tests, DRY controller test setup * fix: scopes component full access scopes and isolated scope group check * fix: use supported capabilities for Alby Go * fix: return correct app name * chore: address minor comments --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com>
225 lines
6.7 KiB
Go
225 lines
6.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/getAlby/hub/constants"
|
|
"github.com/getAlby/hub/db"
|
|
"github.com/getAlby/hub/nip47/models"
|
|
"github.com/getAlby/hub/tests"
|
|
)
|
|
|
|
const nip47GetInfoJson = `
|
|
{
|
|
"method": "get_info"
|
|
}
|
|
`
|
|
|
|
func TestHandleGetInfoEvent_NoPermission(t *testing.T) {
|
|
ctx := context.TODO()
|
|
svc, err := tests.CreateTestService(t)
|
|
require.NoError(t, err)
|
|
defer svc.Remove()
|
|
|
|
app, _, err := tests.CreateApp(svc)
|
|
assert.NoError(t, err)
|
|
|
|
nip47Request := &models.Request{}
|
|
err = json.Unmarshal([]byte(nip47GetInfoJson), nip47Request)
|
|
assert.NoError(t, err)
|
|
|
|
dbRequestEvent := &db.RequestEvent{}
|
|
err = svc.DB.Create(&dbRequestEvent).Error
|
|
assert.NoError(t, err)
|
|
|
|
// delete the existing app permissions (the app was created with get_info scope)
|
|
svc.DB.Exec("delete from app_permissions")
|
|
|
|
appPermission := &db.AppPermission{
|
|
AppId: app.ID,
|
|
Scope: constants.GET_BALANCE_SCOPE,
|
|
ExpiresAt: nil,
|
|
}
|
|
err = svc.DB.Create(appPermission).Error
|
|
assert.NoError(t, err)
|
|
|
|
var publishedResponse *models.Response
|
|
|
|
publishResponse := func(response *models.Response, tags nostr.Tags) {
|
|
publishedResponse = response
|
|
}
|
|
|
|
NewTestNip47Controller(svc).
|
|
HandleGetInfoEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse)
|
|
|
|
assert.Nil(t, publishedResponse.Error)
|
|
nodeInfo := publishedResponse.Result.(*getInfoResponse)
|
|
assert.Empty(t, nodeInfo.Alias)
|
|
assert.Empty(t, nodeInfo.Color)
|
|
assert.Empty(t, nodeInfo.Pubkey)
|
|
assert.Empty(t, nodeInfo.Network)
|
|
assert.Empty(t, nodeInfo.BlockHeight)
|
|
assert.Empty(t, nodeInfo.BlockHash)
|
|
// get_info method is always granted, but does not return pubkey
|
|
assert.Contains(t, nodeInfo.Methods, models.GET_INFO_METHOD)
|
|
assert.Equal(t, []string{}, nodeInfo.Notifications)
|
|
}
|
|
|
|
func TestHandleGetInfoEvent_WithPermission(t *testing.T) {
|
|
ctx := context.TODO()
|
|
svc, err := tests.CreateTestService(t)
|
|
require.NoError(t, err)
|
|
defer svc.Remove()
|
|
|
|
app, _, err := tests.CreateApp(svc)
|
|
assert.NoError(t, err)
|
|
|
|
nip47Request := &models.Request{}
|
|
err = json.Unmarshal([]byte(nip47GetInfoJson), nip47Request)
|
|
assert.NoError(t, err)
|
|
|
|
dbRequestEvent := &db.RequestEvent{}
|
|
err = svc.DB.Create(&dbRequestEvent).Error
|
|
assert.NoError(t, err)
|
|
|
|
appPermission := &db.AppPermission{
|
|
AppId: app.ID,
|
|
Scope: constants.GET_INFO_SCOPE,
|
|
ExpiresAt: nil,
|
|
}
|
|
err = svc.DB.Create(appPermission).Error
|
|
assert.NoError(t, err)
|
|
|
|
var publishedResponse *models.Response
|
|
|
|
publishResponse := func(response *models.Response, tags nostr.Tags) {
|
|
publishedResponse = response
|
|
}
|
|
|
|
NewTestNip47Controller(svc).
|
|
HandleGetInfoEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse)
|
|
|
|
assert.Nil(t, publishedResponse.Error)
|
|
nodeInfo := publishedResponse.Result.(*getInfoResponse)
|
|
assert.Equal(t, tests.MockNodeInfo.Alias, nodeInfo.Alias)
|
|
assert.Equal(t, tests.MockNodeInfo.Color, nodeInfo.Color)
|
|
assert.Equal(t, tests.MockNodeInfo.Pubkey, nodeInfo.Pubkey)
|
|
assert.Equal(t, tests.MockNodeInfo.Network, nodeInfo.Network)
|
|
assert.Equal(t, tests.MockNodeInfo.BlockHeight, nodeInfo.BlockHeight)
|
|
assert.Equal(t, tests.MockNodeInfo.BlockHash, nodeInfo.BlockHash)
|
|
assert.Contains(t, nodeInfo.Methods, "get_info")
|
|
assert.Equal(t, []string{}, nodeInfo.Notifications)
|
|
}
|
|
|
|
func TestHandleGetInfoEvent_WithMetadata(t *testing.T) {
|
|
ctx := context.TODO()
|
|
svc, err := tests.CreateTestService(t)
|
|
require.NoError(t, err)
|
|
defer svc.Remove()
|
|
|
|
metadata := map[string]interface{}{
|
|
"a": 123,
|
|
}
|
|
|
|
app, _, err := svc.AppsService.CreateApp("test", "", 0, "monthly", nil, []string{constants.GET_INFO_SCOPE}, false, metadata)
|
|
assert.NoError(t, err)
|
|
|
|
nip47Request := &models.Request{}
|
|
err = json.Unmarshal([]byte(nip47GetInfoJson), nip47Request)
|
|
assert.NoError(t, err)
|
|
|
|
dbRequestEvent := &db.RequestEvent{}
|
|
err = svc.DB.Create(&dbRequestEvent).Error
|
|
assert.NoError(t, err)
|
|
|
|
appPermission := &db.AppPermission{
|
|
AppId: app.ID,
|
|
Scope: constants.GET_INFO_SCOPE,
|
|
ExpiresAt: nil,
|
|
}
|
|
err = svc.DB.Create(appPermission).Error
|
|
assert.NoError(t, err)
|
|
|
|
var publishedResponse *models.Response
|
|
|
|
publishResponse := func(response *models.Response, tags nostr.Tags) {
|
|
publishedResponse = response
|
|
}
|
|
|
|
NewTestNip47Controller(svc).
|
|
HandleGetInfoEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse)
|
|
|
|
assert.Nil(t, publishedResponse.Error)
|
|
nodeInfo := publishedResponse.Result.(*getInfoResponse)
|
|
assert.Equal(t, tests.MockNodeInfo.Alias, nodeInfo.Alias)
|
|
assert.Equal(t, tests.MockNodeInfo.Color, nodeInfo.Color)
|
|
assert.Equal(t, tests.MockNodeInfo.Pubkey, nodeInfo.Pubkey)
|
|
assert.Equal(t, tests.MockNodeInfo.Network, nodeInfo.Network)
|
|
assert.Equal(t, tests.MockNodeInfo.BlockHeight, nodeInfo.BlockHeight)
|
|
assert.Equal(t, tests.MockNodeInfo.BlockHash, nodeInfo.BlockHash)
|
|
assert.Contains(t, nodeInfo.Methods, "get_info")
|
|
assert.Equal(t, []string{}, nodeInfo.Notifications)
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, float64(123), nodeInfo.Metadata.(map[string]interface{})["a"])
|
|
}
|
|
|
|
func TestHandleGetInfoEvent_WithNotifications(t *testing.T) {
|
|
ctx := context.TODO()
|
|
svc, err := tests.CreateTestService(t)
|
|
require.NoError(t, err)
|
|
defer svc.Remove()
|
|
|
|
app, _, err := tests.CreateApp(svc)
|
|
assert.NoError(t, err)
|
|
|
|
nip47Request := &models.Request{}
|
|
err = json.Unmarshal([]byte(nip47GetInfoJson), nip47Request)
|
|
assert.NoError(t, err)
|
|
|
|
dbRequestEvent := &db.RequestEvent{}
|
|
err = svc.DB.Create(&dbRequestEvent).Error
|
|
assert.NoError(t, err)
|
|
|
|
appPermission := &db.AppPermission{
|
|
AppId: app.ID,
|
|
Scope: constants.GET_INFO_SCOPE,
|
|
ExpiresAt: nil,
|
|
}
|
|
err = svc.DB.Create(appPermission).Error
|
|
assert.NoError(t, err)
|
|
|
|
appPermission = &db.AppPermission{
|
|
AppId: app.ID,
|
|
Scope: constants.NOTIFICATIONS_SCOPE,
|
|
ExpiresAt: nil,
|
|
}
|
|
err = svc.DB.Create(appPermission).Error
|
|
assert.NoError(t, err)
|
|
|
|
var publishedResponse *models.Response
|
|
|
|
publishResponse := func(response *models.Response, tags nostr.Tags) {
|
|
publishedResponse = response
|
|
}
|
|
|
|
NewTestNip47Controller(svc).
|
|
HandleGetInfoEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse)
|
|
|
|
assert.Nil(t, publishedResponse.Error)
|
|
nodeInfo := publishedResponse.Result.(*getInfoResponse)
|
|
assert.Equal(t, tests.MockNodeInfo.Alias, nodeInfo.Alias)
|
|
assert.Equal(t, tests.MockNodeInfo.Color, nodeInfo.Color)
|
|
assert.Equal(t, tests.MockNodeInfo.Pubkey, nodeInfo.Pubkey)
|
|
assert.Equal(t, tests.MockNodeInfo.Network, nodeInfo.Network)
|
|
assert.Equal(t, tests.MockNodeInfo.BlockHeight, nodeInfo.BlockHeight)
|
|
assert.Equal(t, tests.MockNodeInfo.BlockHash, nodeInfo.BlockHash)
|
|
assert.Contains(t, nodeInfo.Methods, "get_info")
|
|
assert.Equal(t, []string{"payment_received", "payment_sent"}, nodeInfo.Notifications)
|
|
}
|