Skip to content

Commit 9fd46d2

Browse files
test: add NIP-22 created_at integration coverage
1 parent a89a95e commit 9fd46d2

4 files changed

Lines changed: 116 additions & 4 deletions

File tree

src/handlers/event-message-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class EventMessageHandler implements IMessageHandler {
165165
limits.createdAt.maxPositiveDelta > 0 &&
166166
event.created_at > now + limits.createdAt.maxPositiveDelta
167167
) {
168-
return `rejected: created_at is more than ${limits.createdAt.maxPositiveDelta} seconds in the future`
168+
return `invalid: created_at is more than ${limits.createdAt.maxPositiveDelta} seconds in the future`
169169
}
170170

171171
if (
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@nip-22
2+
Feature: NIP-22 created_at timestamp limits
3+
Scenario: Event with created_at at current time is accepted
4+
Given someone called Alice
5+
And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 0
6+
When Alice drafts a text_note event with content "test event" and created_at 0 seconds from now
7+
Then Alice sends their last draft event successfully
8+
When Alice subscribes to author Alice
9+
Then Alice receives a text_note event from Alice with content "test event"
10+
11+
Scenario: Event with created_at at positive delta limit is accepted
12+
Given someone called Alice
13+
And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 0
14+
When Alice drafts a text_note event with content "test event" and created_at 900 seconds from now
15+
Then Alice sends their last draft event successfully
16+
When Alice subscribes to author Alice
17+
Then Alice receives a text_note event from Alice with content "test event"
18+
19+
Scenario: Event with created_at above positive delta limit is rejected
20+
Given someone called Alice
21+
And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 0
22+
When Alice drafts a text_note event with content "test event" and created_at 901 seconds from now
23+
Then Alice sends their last draft event unsuccessfully with reason containing "invalid"
24+
25+
Scenario: Event older than configured negative delta limit is rejected
26+
Given someone called Alice
27+
And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 3600
28+
When Alice drafts a text_note event with content "test event" and created_at -3601 seconds from now
29+
Then Alice sends their last draft event unsuccessfully with reason containing "rejected"
30+
31+
Scenario: Event within configured negative delta limit is accepted
32+
Given someone called Alice
33+
And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 3600
34+
When Alice drafts a text_note event with content "test event" and created_at -3590 seconds from now
35+
Then Alice sends their last draft event successfully
36+
When Alice subscribes to author Alice
37+
Then Alice receives a text_note event from Alice with content "test event"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { After, Given, Then, When } from '@cucumber/cucumber'
2+
import { assocPath, pipe } from 'ramda'
3+
4+
import { CommandResult, MessageType } from '../../../../src/@types/messages'
5+
import { createEvent, sendEvent } from '../helpers'
6+
7+
import { Event } from '../../../../src/@types/event'
8+
import { expect } from 'chai'
9+
import { isDraft } from '../shared'
10+
import { SettingsStatic } from '../../../../src/utils/settings'
11+
import WebSocket from 'ws'
12+
13+
const setCreatedAtLimits = (maxPositiveDelta: number, maxNegativeDelta: number) => {
14+
const settings = SettingsStatic._settings ?? SettingsStatic.createSettings()
15+
16+
SettingsStatic._settings = pipe(
17+
assocPath(['limits', 'event', 'createdAt', 'maxPositiveDelta'], maxPositiveDelta),
18+
assocPath(['limits', 'event', 'createdAt', 'maxNegativeDelta'], maxNegativeDelta),
19+
)(settings) as any
20+
}
21+
22+
After({ tags: '@nip-22' }, function() {
23+
setCreatedAtLimits(900, 0)
24+
})
25+
26+
Given(/^created_at limits are set to maxPositiveDelta (\d+) and maxNegativeDelta (\d+)$/, function(
27+
maxPositiveDelta: string,
28+
maxNegativeDelta: string,
29+
) {
30+
setCreatedAtLimits(Number(maxPositiveDelta), Number(maxNegativeDelta))
31+
})
32+
33+
When(/^(\w+) drafts a text_note event with content "([^"]+)" and created_at (-?\d+) seconds from now$/, async function(
34+
name: string,
35+
content: string,
36+
offsetSeconds: string,
37+
) {
38+
const { pubkey, privkey } = this.parameters.identities[name]
39+
const createdAt = Math.floor(Date.now() / 1000) + Number(offsetSeconds)
40+
41+
const event: Event = await createEvent(
42+
{
43+
pubkey,
44+
kind: 1,
45+
content,
46+
created_at: createdAt,
47+
},
48+
privkey,
49+
)
50+
51+
const draftEvent = event as any
52+
draftEvent[isDraft] = true
53+
54+
this.parameters.events[name].push(event)
55+
})
56+
57+
Then(/^(\w+) sends their last draft event unsuccessfully with reason containing "([^"]+)"$/, async function(
58+
name: string,
59+
expectedReason: string,
60+
) {
61+
const ws = this.parameters.clients[name] as WebSocket
62+
63+
const event = this.parameters.events[name].findLast((lastEvent: Event) => (lastEvent as any)[isDraft])
64+
if (!event) {
65+
throw new Error(`No draft event found for ${name}`)
66+
}
67+
68+
delete (event as any)[isDraft]
69+
70+
const command = await sendEvent(ws, event, false) as CommandResult
71+
72+
expect(command[0]).to.equal(MessageType.OK)
73+
expect(command[2]).to.equal(false)
74+
expect(command[3].toLowerCase()).to.contain(expectedReason.toLowerCase())
75+
})

test/unit/handlers/event-message-handler.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ describe('EventMessageHandler', () => {
291291
eventLimits.createdAt.maxPositiveDelta = 100
292292
event.created_at += 101
293293

294-
expect((handler as any).canAcceptEvent(event)).to.equal(
295-
'rejected: created_at is more than 100 seconds in the future',
296-
)
294+
expect(
295+
(handler as any).canAcceptEvent(event)
296+
).to.equal('invalid: created_at is more than 100 seconds in the future')
297297
})
298298
})
299299

0 commit comments

Comments
 (0)