Skip to content

Commit

Permalink
chore: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jan 8, 2025
1 parent d83fa64 commit 2cda910
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 27 deletions.
8 changes: 4 additions & 4 deletions nip47/cipher/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
)

type Nip47Cipher struct {
version string
Version string
pubkey string
privkey string
sharedSecret []byte
Expand Down Expand Up @@ -42,7 +42,7 @@ func NewNip47Cipher(version, pubkey, privkey string) (*Nip47Cipher, error) {
}

return &Nip47Cipher{
version: version,
Version: version,
pubkey: pubkey,
privkey: privkey,
sharedSecret: ss,
Expand All @@ -51,7 +51,7 @@ func NewNip47Cipher(version, pubkey, privkey string) (*Nip47Cipher, error) {
}

func (c *Nip47Cipher) Encrypt(message string) (msg string, err error) {
if c.version == "0.0" {
if c.Version == "0.0" {
msg, err = nip04.Encrypt(message, c.sharedSecret)
if err != nil {
return "", err
Expand All @@ -66,7 +66,7 @@ func (c *Nip47Cipher) Encrypt(message string) (msg string, err error) {
}

func (c *Nip47Cipher) Decrypt(content string) (payload string, err error) {
if c.version == "0.0" {
if c.Version == "0.0" {
payload, err = nip04.Decrypt(content, c.sharedSecret)
if err != nil {
return "", err
Expand Down
127 changes: 118 additions & 9 deletions nip47/event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,26 @@ func TestHandleResponse_WithPermission(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)

doTestHandleResponse_WithPermission(t, svc, "1.0")
}

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

doTestHandleResponse_WithPermission(t, svc, "0.0")
}

func doTestHandleResponse_WithPermission(t *testing.T, svc *tests.TestService, version string) {
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
assert.NoError(t, err)

app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey)
app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey, version)
assert.NoError(t, err)

appPermission := &db.AppPermission{
Expand All @@ -109,9 +122,14 @@ func TestHandleResponse_WithPermission(t *testing.T) {
Kind: models.REQUEST_KIND,
PubKey: reqPubkey,
CreatedAt: nostr.Now(),
Tags: nostr.Tags{[]string{"v", "1.0"}},
Tags: nostr.Tags{},
Content: msg,
}

if version != "0.0" {
reqEvent.Tags = append(reqEvent.Tags, []string{"v", version})
}

err = reqEvent.Sign(reqPrivateKey)
assert.NoError(t, err)

Expand Down Expand Up @@ -148,13 +166,29 @@ func TestHandleResponse_DuplicateRequest(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)

doTestHandleResponse_DuplicateRequest(t, svc, "1.0")
}

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

doTestHandleResponse_DuplicateRequest(t, svc, "0.0")
}

func doTestHandleResponse_DuplicateRequest(t *testing.T, svc *tests.TestService, version string) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
assert.NoError(t, err)

app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey)
app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey, version)
assert.NoError(t, err)

appPermission := &db.AppPermission{
Expand All @@ -179,9 +213,14 @@ func TestHandleResponse_DuplicateRequest(t *testing.T) {
Kind: models.REQUEST_KIND,
PubKey: reqPubkey,
CreatedAt: nostr.Now(),
Tags: nostr.Tags{[]string{"v", "1.0"}},
Tags: nostr.Tags{},
Content: msg,
}

if version != "0.0" {
reqEvent.Tags = append(reqEvent.Tags, []string{"v", version})
}

err = reqEvent.Sign(reqPrivateKey)
assert.NoError(t, err)

Expand All @@ -204,13 +243,28 @@ func TestHandleResponse_NoPermission(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)

doTestHandleResponse_NoPermission(t, svc, "1.0")
}

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

doTestHandleResponse_NoPermission(t, svc, "0.0")
}
func doTestHandleResponse_NoPermission(t *testing.T, svc *tests.TestService, version string) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
assert.NoError(t, err)

_, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey)
_, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey, version)
assert.NoError(t, err)

content := map[string]interface{}{
Expand All @@ -227,9 +281,14 @@ func TestHandleResponse_NoPermission(t *testing.T) {
Kind: models.REQUEST_KIND,
PubKey: reqPubkey,
CreatedAt: nostr.Now(),
Tags: nostr.Tags{[]string{"v", "1.0"}},
Tags: nostr.Tags{},
Content: msg,
}

if version != "0.0" {
reqEvent.Tags = append(reqEvent.Tags, []string{"v", version})
}

err = reqEvent.Sign(reqPrivateKey)
assert.NoError(t, err)

Expand Down Expand Up @@ -263,7 +322,7 @@ func TestHandleResponse_NoApp(t *testing.T) {
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
assert.NoError(t, err)

app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey)
app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey, "1.0")
assert.NoError(t, err)

// delete the app
Expand Down Expand Up @@ -298,6 +357,56 @@ func TestHandleResponse_NoApp(t *testing.T) {
assert.Nil(t, relay.PublishedEvents)
}

func TestHandleResponse_IncorrectVersion(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
assert.NoError(t, err)

app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey, "1.0")
assert.NoError(t, err)

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

content := map[string]interface{}{
"method": models.GET_INFO_METHOD,
}

payloadBytes, err := json.Marshal(content)
assert.NoError(t, err)

msg, err := cipher.Encrypt(string(payloadBytes))
assert.NoError(t, err)

// don't pass correct version
reqEvent := &nostr.Event{
Kind: models.REQUEST_KIND,
PubKey: reqPubkey,
CreatedAt: nostr.Now(),
Tags: nostr.Tags{},
Content: msg,
}
err = reqEvent.Sign(reqPrivateKey)
assert.NoError(t, err)

relay := tests.NewMockRelay()

nip47svc.HandleEvent(context.TODO(), relay, reqEvent, svc.LNClient)

// it shouldn't return anything for an invalid version
assert.Nil(t, relay.PublishedEvents)
}

func TestHandleResponse_OldRequestForPayment(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
Expand All @@ -308,7 +417,7 @@ func TestHandleResponse_OldRequestForPayment(t *testing.T) {
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
assert.NoError(t, err)

app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey)
app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey, "1.0")
assert.NoError(t, err)

content := map[string]interface{}{
Expand Down Expand Up @@ -367,7 +476,7 @@ func TestHandleResponse_IncorrectPubkey(t *testing.T) {

reqPrivateKey2 := nostr.GeneratePrivateKey()

app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey)
app, cipher, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey, "1.0")
assert.NoError(t, err)

appPermission := &db.AppPermission{
Expand Down
56 changes: 45 additions & 11 deletions nip47/notifications/nip47_notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ func doTestSendNotificationPaymentReceived(t *testing.T, svc *tests.TestService,
notifier := NewNip47Notifier(relay, svc.DB, svc.Cfg, svc.Keys, permissionsSvc, transactionsSvc, svc.LNClient)
notifier.ConsumeEvent(ctx, receivedEvent)

assert.NotNil(t, relay.PublishedEvents[1])
assert.NotEmpty(t, relay.PublishedEvents[1].Content)
var publishedEvent *nostr.Event
if cipher.Version == "0.0" {
publishedEvent = relay.PublishedEvents[0]
} else {
publishedEvent = relay.PublishedEvents[1]
}

assert.NotNil(t, publishedEvent)
assert.NotEmpty(t, publishedEvent.Content)

decrypted, err := cipher.Decrypt(relay.PublishedEvents[1].Content)
decrypted, err := cipher.Decrypt(publishedEvent.Content)
assert.NoError(t, err)
unmarshalledResponse := Notification{
Notification: &PaymentReceivedNotification{},
Expand All @@ -113,12 +120,22 @@ func TestSendNotification_PaymentReceived(t *testing.T) {
svc, err := tests.CreateTestService()
require.NoError(t, err)

app, cipher, err := tests.CreateApp(svc)
app, cipher, err := tests.CreateAppWithPrivateKey(svc, "", "1.0")
assert.NoError(t, err)
doTestSendNotificationPaymentReceived(t, svc, app, cipher)
}

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

app, cipher, err := tests.CreateAppWithPrivateKey(svc, "", "0.0")
assert.NoError(t, err)
doTestSendNotificationPaymentReceived(t, svc, app, cipher)
}

func TestSendNotification_LegacyApp_PaymentReceived(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
Expand Down Expand Up @@ -176,10 +193,17 @@ func doTestSendNotificationPaymentSent(t *testing.T, svc *tests.TestService, app
notifier := NewNip47Notifier(relay, svc.DB, svc.Cfg, svc.Keys, permissionsSvc, transactionsSvc, svc.LNClient)
notifier.ConsumeEvent(ctx, receivedEvent)

assert.NotNil(t, relay.PublishedEvents[1])
assert.NotEmpty(t, relay.PublishedEvents[1].Content)
var publishedEvent *nostr.Event
if cipher.Version == "0.0" {
publishedEvent = relay.PublishedEvents[0]
} else {
publishedEvent = relay.PublishedEvents[1]
}

decrypted, err := cipher.Decrypt(relay.PublishedEvents[1].Content)
assert.NotNil(t, publishedEvent)
assert.NotEmpty(t, publishedEvent.Content)

decrypted, err := cipher.Decrypt(publishedEvent.Content)
assert.NoError(t, err)
unmarshalledResponse := Notification{
Notification: &PaymentReceivedNotification{},
Expand All @@ -206,12 +230,22 @@ func TestSendNotification_PaymentSent(t *testing.T) {
svc, err := tests.CreateTestService()
require.NoError(t, err)

app, ss, err := tests.CreateApp(svc)
app, cipher, err := tests.CreateAppWithPrivateKey(svc, "", "1.0")
assert.NoError(t, err)
doTestSendNotificationPaymentSent(t, svc, app, cipher)
}

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

app, cipher, err := tests.CreateAppWithPrivateKey(svc, "", "0.0")
assert.NoError(t, err)
doTestSendNotificationPaymentSent(t, svc, app, ss)
doTestSendNotificationPaymentSent(t, svc, app, cipher)
}

func TestSendNotification_Legacy_PaymentSent(t *testing.T) {
func TestSendNotification_LegacyApp_PaymentSent(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions tests/create_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

func CreateApp(svc *TestService) (app *db.App, cipher *cipher.Nip47Cipher, err error) {
return CreateAppWithPrivateKey(svc, "")
return CreateAppWithPrivateKey(svc, "", "1.0")
}
func CreateAppWithPrivateKey(svc *TestService, senderPrivkey string) (app *db.App, nip47Cipher *cipher.Nip47Cipher, err error) {

func CreateAppWithPrivateKey(svc *TestService, senderPrivkey, version string) (app *db.App, nip47Cipher *cipher.Nip47Cipher, err error) {
senderPubkey := ""
if senderPrivkey != "" {
var err error
Expand All @@ -29,7 +30,7 @@ func CreateAppWithPrivateKey(svc *TestService, senderPrivkey string) (app *db.Ap
pairingSecretKey = senderPrivkey
}

nip47Cipher, err = cipher.NewNip47Cipher("1.0", *app.WalletPubkey, pairingSecretKey)
nip47Cipher, err = cipher.NewNip47Cipher(version, *app.WalletPubkey, pairingSecretKey)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 2cda910

Please sign in to comment.