From 201a93ce5e75989c701816aaa6fa7ebd632bf37c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Aaron?= <100827540+reneaaron@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:24:46 +0200 Subject: [PATCH] fix: enable translate error to have typed errors for e.g. unique key 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 --- db/db.go | 4 ++- nip47/event_handler_test.go | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/db/db.go b/db/db.go index d0fea02c..364bb843 100644 --- a/db/db.go +++ b/db/db.go @@ -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) } diff --git a/nip47/event_handler_test.go b/nip47/event_handler_test.go index b9b7caf2..114439d4 100644 --- a/nip47/event_handler_test.go +++ b/nip47/event_handler_test.go @@ -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()