Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ package api
import (
"context"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/getAlby/hub/lnclient"
"github.com/getAlby/hub/logger"
"github.com/getAlby/hub/service"
"github.com/getAlby/hub/tests/mocks"
)

func TestMain(m *testing.M) {
logger.Init("")
os.Exit(m.Run())
}

func TestGetCustomNodeCommandDefinitions(t *testing.T) {
lnClient := mocks.NewMockLNClient(t)
svc := mocks.NewMockService(t)
Expand Down Expand Up @@ -221,6 +228,42 @@ func TestExecuteCustomNodeCommand(t *testing.T) {
}
}

func TestSetNodeAlias(t *testing.T) {
t.Run("success", func(t *testing.T) {
cfg := mocks.NewMockConfig(t)
cfg.On("SetUpdate", "NodeAlias", "SatoshiSquirrel", "").Return(nil)

theAPI := &api{cfg: cfg}

err := theAPI.SetNodeAlias("SatoshiSquirrel")
require.NoError(t, err)
cfg.AssertExpectations(t)
})

t.Run("empty alias", func(t *testing.T) {
cfg := mocks.NewMockConfig(t)
cfg.On("SetUpdate", "NodeAlias", "", "").Return(nil)

theAPI := &api{cfg: cfg}

err := theAPI.SetNodeAlias("")
require.NoError(t, err)
cfg.AssertExpectations(t)
})

t.Run("config error", func(t *testing.T) {
cfg := mocks.NewMockConfig(t)
cfg.On("SetUpdate", "NodeAlias", "test", "").Return(fmt.Errorf("database error"))

theAPI := &api{cfg: cfg}

err := theAPI.SetNodeAlias("test")
require.Error(t, err)
require.ErrorContains(t, err, "database error")
cfg.AssertExpectations(t)
})
}

// instantiateAPIWithService is a helper function that returns a partially
// constructed API instance. It is only suitable for the simplest of test cases.
func instantiateAPIWithService(s service.Service) *api {
Expand Down
Loading