Skip to content

Commit

Permalink
chore: improve test assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Nov 1, 2024
1 parent 631aa3c commit 5bc4639
Show file tree
Hide file tree
Showing 25 changed files with 151 additions and 126 deletions.
27 changes: 14 additions & 13 deletions alby/alby_oauth_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,48 @@ import (
"github.com/getAlby/hub/events"
"github.com/getAlby/hub/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tyler-smith/go-bip32"
"github.com/tyler-smith/go-bip39"
)

func TestExistingEncryptedBackup(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

mnemonic := "limit reward expect search tissue call visa fit thank cream brave jump"
unlockPassword := "123"
svc.Cfg.SetUpdate("Mnemonic", mnemonic, unlockPassword)
err = svc.Keys.Init(svc.Cfg, unlockPassword)
assert.Nil(t, err)
assert.NoError(t, err)

encryptedBackup := "3fd21f9a393d8345ddbdd449-ba05c3dbafdfb7eea574373b7763d0c81c599b2cd1735e59a1c5571379498f4da8fe834c3403824ab02b61005abc1f563c638f425c65420e82941efe94794555c8b145a0603733ee115277f860011e6a17fd8c22f1d73a096ff7275582aac19b430940b40a2559c7ff59a063305290ef7c9ba46f9de17b0ddbac9030b0"

masterKey, err := bip32.NewMasterKey(bip39.NewSeed(mnemonic, ""))
assert.Nil(t, err)
assert.NoError(t, err)

appKey, err := masterKey.NewChildKey(bip32.FirstHardenedChild + 128029 /* 🐝 */)
assert.Nil(t, err)
assert.NoError(t, err)
encryptedChannelsBackupKey, err := appKey.NewChildKey(bip32.FirstHardenedChild)
assert.Nil(t, err)
assert.NoError(t, err)

decrypted, err := config.AesGcmDecryptWithKey(encryptedBackup, encryptedChannelsBackupKey.Key)
assert.Nil(t, err)
assert.NoError(t, err)

assert.Equal(t, "{\"node_id\":\"037e702144c4fa485d42f0f69864e943605823763866cf4bf619d2d2cf2eda420b\",\"channels\":[],\"monitors\":[]}\n", decrypted)
}

func TestEncryptedBackup(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

mnemonic := "limit reward expect search tissue call visa fit thank cream brave jump"
unlockPassword := "123"
svc.Cfg.SetUpdate("Mnemonic", mnemonic, unlockPassword)
err = svc.Keys.Init(svc.Cfg, unlockPassword)
assert.Nil(t, err)
assert.NoError(t, err)

albyOAuthSvc := NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
encryptedBackup, err := albyOAuthSvc.createEncryptedChannelBackup(&events.StaticChannelsBackupEvent{
Expand All @@ -56,19 +57,19 @@ func TestEncryptedBackup(t *testing.T) {
Monitors: []events.EncodedChannelMonitorBackup{},
})

assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, "channels_v2", encryptedBackup.Description)

masterKey, err := bip32.NewMasterKey(bip39.NewSeed(mnemonic, ""))
assert.Nil(t, err)
assert.NoError(t, err)

appKey, err := masterKey.NewChildKey(bip32.FirstHardenedChild + 128029 /* 🐝 */)
assert.Nil(t, err)
assert.NoError(t, err)
encryptedChannelsBackupKey, err := appKey.NewChildKey(bip32.FirstHardenedChild)
assert.Nil(t, err)
assert.NoError(t, err)

decrypted, err := config.AesGcmDecryptWithKey(encryptedBackup.Data, encryptedChannelsBackupKey.Key)
assert.Nil(t, err)
assert.NoError(t, err)

assert.Equal(t, "{\"node_id\":\"037e702144c4fa485d42f0f69864e943605823763866cf4bf619d2d2cf2eda420b\",\"channels\":[],\"monitors\":[]}\n", decrypted)
}
7 changes: 4 additions & 3 deletions nip47/controllers/get_balance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"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"
Expand All @@ -26,7 +27,7 @@ func TestHandleGetBalanceEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBalanceJson), nip47Request)
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestHandleGetBalanceEvent_IsolatedApp_NoTransactions(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBalanceJson), nip47Request)
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestHandleGetBalanceEvent_IsolatedApp_Transactions(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBalanceJson), nip47Request)
Expand Down
11 changes: 6 additions & 5 deletions nip47/controllers/get_budget_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/stretchr/testify/require"

"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
Expand All @@ -27,7 +28,7 @@ func TestHandleGetBudgetEvent_NoRenewal(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestHandleGetBudgetEvent_NoneUsed(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down Expand Up @@ -119,7 +120,7 @@ func TestHandleGetBudgetEvent_HalfUsed(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down Expand Up @@ -173,7 +174,7 @@ func TestHandleGetBudgetEvent_NoBudget(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down Expand Up @@ -220,7 +221,7 @@ func TestHandleGetBudgetEvent_NoPayInvoicePermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down
7 changes: 4 additions & 3 deletions nip47/controllers/get_info_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"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"
Expand All @@ -26,7 +27,7 @@ func TestHandleGetInfoEvent_NoPermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestHandleGetInfoEvent_WithPermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down Expand Up @@ -123,7 +124,7 @@ func TestHandleGetInfoEvent_WithNotifications(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion nip47/controllers/list_transactions_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/stretchr/testify/require"

"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
Expand All @@ -34,7 +35,7 @@ func TestHandleListTransactionsEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47ListTransactionsJson), nip47Request)
Expand Down
3 changes: 2 additions & 1 deletion nip47/controllers/lookup_invoice_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/stretchr/testify/require"

"github.com/getAlby/hub/db"
"github.com/getAlby/hub/nip47/models"
Expand All @@ -29,7 +30,7 @@ func TestHandleLookupInvoiceEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47LookupInvoiceJson), nip47Request)
Expand Down
3 changes: 2 additions & 1 deletion nip47/controllers/make_invoice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/nbd-wtf/go-nostr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/getAlby/hub/db"
"github.com/getAlby/hub/nip47/models"
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestHandleMakeInvoiceEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47MakeInvoiceJson), nip47Request)
Expand Down
9 changes: 5 additions & 4 deletions nip47/controllers/multi_pay_invoice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/nbd-wtf/go-nostr"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestHandleMultiPayInvoiceEvent_Success(t *testing.T) {

defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

var preimages = []string{"123preimage", "123preimage2"}

Expand Down Expand Up @@ -135,7 +136,7 @@ func TestHandleMultiPayInvoiceEvent_OneMalformedInvoice(t *testing.T) {

defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down Expand Up @@ -197,7 +198,7 @@ func TestHandleMultiPayInvoiceEvent_IsolatedApp_OneBudgetExceeded(t *testing.T)

defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down Expand Up @@ -279,7 +280,7 @@ func TestHandleMultiPayInvoiceEvent_LNClient_OnePaymentFailed(t *testing.T) {

defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)
svc.LNClient.(*tests.MockLn).PayInvoiceResponses = []*lnclient.PayInvoiceResponse{{
Preimage: "123preimage",
}, nil}
Expand Down
5 changes: 3 additions & 2 deletions nip47/controllers/multi_pay_keysend_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/stretchr/testify/require"

"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestHandleMultiPayKeysendEvent_Success(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down Expand Up @@ -123,7 +124,7 @@ func TestHandleMultiPayKeysendEvent_OneBudgetExceeded(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions nip47/controllers/pay_invoice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"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"
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestHandlePayInvoiceEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down Expand Up @@ -77,7 +78,7 @@ func TestHandlePayInvoiceEvent_MalformedInvoice(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions nip47/controllers/pay_keysend_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"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"
Expand Down Expand Up @@ -49,7 +50,7 @@ func TestHandlePayKeysendEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down Expand Up @@ -90,7 +91,7 @@ func TestHandlePayKeysendEvent_WithPreimage(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)
require.NoError(t, err)

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

0 comments on commit 5bc4639

Please sign in to comment.