mirror of
https://github.com/getAlby/hub.git
synced 2026-08-13 12:33:39 +02:00
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 <roland.bewick@gmail.com>
This commit is contained in:
parent
e83caacef7
commit
201a93ce5e
2 changed files with 59 additions and 1 deletions
4
db/db.go
4
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue