apps_test.go raw

   1  package api
   2  
   3  import (
   4  	"testing"
   5  
   6  	"github.com/getAlby/hub/constants"
   7  	"github.com/getAlby/hub/tests/mocks"
   8  	"github.com/stretchr/testify/assert"
   9  	"github.com/stretchr/testify/require"
  10  )
  11  
  12  func TestBuildReturnToUrl(t *testing.T) {
  13  	relayUrls := []string{"wss://relay.getalby.com/v1"}
  14  	walletPubkey := "6f8bf1b7d58ac41b2c793837ba528c1d0a4a1cd2e3f5b7c9d0e1f2a3b4c5d6e7"
  15  
  16  	assert.Equal(t,
  17  		"https://example.com?pubkey=6f8bf1b7d58ac41b2c793837ba528c1d0a4a1cd2e3f5b7c9d0e1f2a3b4c5d6e7&relay=wss%3A%2F%2Frelay.getalby.com%2Fv1",
  18  		buildReturnToUrl("https://example.com", relayUrls, walletPubkey, "", false))
  19  
  20  	// existing query parameters are preserved and lud16 is added
  21  	assert.Equal(t,
  22  		"https://example.com/path?foo=bar&lud16=user%40getalby.com&pubkey=6f8bf1b7d58ac41b2c793837ba528c1d0a4a1cd2e3f5b7c9d0e1f2a3b4c5d6e7&relay=wss%3A%2F%2Frelay.getalby.com%2Fv1",
  23  		buildReturnToUrl("https://example.com/path?foo=bar", relayUrls, walletPubkey, "user@getalby.com", false))
  24  
  25  	// isolated apps do not receive a lightning address
  26  	assert.Equal(t,
  27  		"http://example.com?pubkey=6f8bf1b7d58ac41b2c793837ba528c1d0a4a1cd2e3f5b7c9d0e1f2a3b4c5d6e7&relay=wss%3A%2F%2Frelay.getalby.com%2Fv1",
  28  		buildReturnToUrl("http://example.com", relayUrls, walletPubkey, "user@getalby.com", true))
  29  
  30  	// only http and https URLs are accepted
  31  	assert.Equal(t, "", buildReturnToUrl("", relayUrls, walletPubkey, "", false))
  32  	assert.Equal(t, "", buildReturnToUrl("example.com/path", relayUrls, walletPubkey, "", false))
  33  	assert.Equal(t, "", buildReturnToUrl("example://app", relayUrls, walletPubkey, "", false))
  34  	assert.Equal(t, "", buildReturnToUrl("javascript:void(0)", relayUrls, walletPubkey, "", false))
  35  	assert.Equal(t, "", buildReturnToUrl("::invalid::", relayUrls, walletPubkey, "", false))
  36  }
  37  
  38  func TestCreateApp_SuperuserScopeIncorrectPassword(t *testing.T) {
  39  	cfg := mocks.NewMockConfig(t)
  40  	cfg.On("CheckUnlockPassword", "").Return(false)
  41  	theAPI := &api{svc: mocks.NewMockService(t), cfg: cfg}
  42  	response, err := theAPI.CreateApp(&CreateAppRequest{
  43  		Scopes: []string{constants.SUPERUSER_SCOPE},
  44  	})
  45  
  46  	assert.Nil(t, response)
  47  	require.Error(t, err)
  48  	assert.Equal(t, "incorrect unlock password to create app with superuser permission", err.Error())
  49  }
  50