Skip to content

Commit

Permalink
fix: enable translate error to have typed errors for e.g. unique key …
Browse files Browse the repository at this point in the history
…constraints (#700)

* fix: enable translate error to have typed errors for e.g. unique key constraints

* chore: add test for event handler duplicate request

---------

Co-authored-by: Roland Bewick <roland.bewick@gmail.com>
  • Loading branch information
reneaaron and rolznz authored Sep 24, 2024
1 parent e83caac commit 201a93c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
4 changes: 3 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (

func NewDB(uri string, logDBQueries bool) (*gorm.DB, error) {

config := &gorm.Config{}
config := &gorm.Config{
TranslateError: true,
}
if logDBQueries {
config.Logger = gorm_logger.Default.LogMode(gorm_logger.Info)
}
Expand Down
56 changes: 56 additions & 0 deletions nip47/event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,62 @@ func TestHandleResponse_WithPermission(t *testing.T) {
assert.Equal(t, []interface{}{"get_balance"}, unmarshalledResponse.Result.(map[string]interface{})["methods"])
}

func TestHandleResponse_DuplicateRequest(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.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, ss, err := tests.CreateAppWithPrivateKey(svc, reqPrivateKey)
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 := nip04.Encrypt(string(payloadBytes), ss)
assert.NoError(t, err)

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)

assert.NotNil(t, relay.PublishedEvent)
assert.NotEmpty(t, relay.PublishedEvent.Content)

relay.PublishedEvent = nil

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

// second time it should not publish
assert.Nil(t, relay.PublishedEvent)
}

func TestHandleResponse_NoPermission(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
Expand Down

0 comments on commit 201a93c

Please sign in to comment.