-
Notifications
You must be signed in to change notification settings - Fork 229
test: add NIP-22 created_at integration coverage #547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9fd46d2
37669d2
cbec93c
901b589
0380c63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| --- | ||
|
|
||
| Add empty changeset for NIP-22 created_at integration test coverage (issue #505). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| @nip-22 | ||
| Feature: NIP-22 created_at timestamp limits | ||
| Scenario: Event with created_at at current time is accepted | ||
| Given someone called Alice | ||
| And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 0 | ||
| When Alice drafts a text_note event with content "test event" and created_at 0 seconds from now | ||
| Then Alice sends their last draft event successfully | ||
| When Alice subscribes to author Alice | ||
| Then Alice receives a text_note event from Alice with content "test event" | ||
|
|
||
| Scenario: Event with created_at above positive delta limit is rejected | ||
| Given someone called Alice | ||
| And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 0 | ||
| When Alice drafts a text_note event with content "test event" and created_at 910 seconds from now | ||
| Then Alice sends their last draft event unsuccessfully with reason containing "rejected" | ||
|
|
||
| Scenario: Event older than configured negative delta limit is rejected | ||
| Given someone called Alice | ||
| And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 3600 | ||
| When Alice drafts a text_note event with content "test event" and created_at -3601 seconds from now | ||
| Then Alice sends their last draft event unsuccessfully with reason containing "rejected" | ||
|
|
||
| Scenario: Event within configured negative delta limit is accepted | ||
| Given someone called Alice | ||
| And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 3600 | ||
| When Alice drafts a text_note event with content "test event" and created_at -3590 seconds from now | ||
| Then Alice sends their last draft event successfully | ||
| When Alice subscribes to author Alice | ||
| Then Alice receives a text_note event from Alice with content "test event" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { After, Before, Given, Then, When } from '@cucumber/cucumber' | ||
| import { assocPath, pipe } from 'ramda' | ||
|
|
||
| import { CommandResult, MessageType } from '../../../../src/@types/messages' | ||
| import { createEvent, sendEvent } from '../helpers' | ||
|
|
||
| import { Event } from '../../../../src/@types/event' | ||
| import { expect } from 'chai' | ||
| import { isDraft } from '../shared' | ||
| import { SettingsStatic } from '../../../../src/utils/settings' | ||
| import WebSocket from 'ws' | ||
|
|
||
| const previousSettingsSnapshot = Symbol('nip22PreviousSettingsSnapshot') | ||
|
|
||
| const setCreatedAtLimits = (maxPositiveDelta: number, maxNegativeDelta: number) => { | ||
| const settings = SettingsStatic._settings ?? SettingsStatic.createSettings() | ||
|
|
||
| SettingsStatic._settings = pipe( | ||
| assocPath(['limits', 'event', 'createdAt', 'maxPositiveDelta'], maxPositiveDelta), | ||
| assocPath(['limits', 'event', 'createdAt', 'maxNegativeDelta'], maxNegativeDelta), | ||
| )(settings) as any | ||
| } | ||
|
|
||
| Before({ tags: '@nip-22' }, function(this: any) { | ||
| this[previousSettingsSnapshot] = SettingsStatic._settings | ||
| }) | ||
|
|
||
| After({ tags: '@nip-22' }, function(this: any) { | ||
| SettingsStatic._settings = this[previousSettingsSnapshot] | ||
| delete this[previousSettingsSnapshot] | ||
| }) | ||
|
Comment on lines
+15
to
+31
|
||
|
|
||
| Given(/^created_at limits are set to maxPositiveDelta (\d+) and maxNegativeDelta (\d+)$/, function( | ||
| maxPositiveDelta: string, | ||
| maxNegativeDelta: string, | ||
| ) { | ||
| setCreatedAtLimits(Number(maxPositiveDelta), Number(maxNegativeDelta)) | ||
| }) | ||
|
|
||
| When(/^(\w+) drafts a text_note event with content "([^"]+)" and created_at (-?\d+) seconds from now$/, async function( | ||
| name: string, | ||
| content: string, | ||
| offsetSeconds: string, | ||
| ) { | ||
| const { pubkey, privkey } = this.parameters.identities[name] | ||
| const createdAt = Math.floor(Date.now() / 1000) + Number(offsetSeconds) | ||
|
|
||
| const event: Event = await createEvent( | ||
| { | ||
| pubkey, | ||
| kind: 1, | ||
| content, | ||
| created_at: createdAt, | ||
|
Comment on lines
+40
to
+53
|
||
| }, | ||
| privkey, | ||
| ) | ||
|
|
||
| const draftEvent = event as any | ||
| draftEvent[isDraft] = true | ||
|
|
||
| this.parameters.events[name].push(event) | ||
| }) | ||
|
|
||
| Then(/^(\w+) sends their last draft event unsuccessfully with reason containing "([^"]+)"$/, async function( | ||
| name: string, | ||
| expectedReason: string, | ||
| ) { | ||
| const ws = this.parameters.clients[name] as WebSocket | ||
|
|
||
| const event = this.parameters.events[name].findLast((lastEvent: Event) => (lastEvent as any)[isDraft]) | ||
| if (!event) { | ||
| throw new Error(`No draft event found for ${name}`) | ||
| } | ||
|
|
||
| delete (event as any)[isDraft] | ||
|
|
||
| const command = await sendEvent(ws, event, false) as CommandResult | ||
|
|
||
| expect(command[0]).to.equal(MessageType.OK) | ||
| expect(command[2]).to.equal(false) | ||
| expect(command[3].toLowerCase()).to.contain(expectedReason.toLowerCase()) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changeset is declared as empty/tests-only, but this PR also changes runtime behavior in
EventMessageHandler(switching a rejection reason prefix toinvalid:). If that behavior change is kept, the changeset should include the appropriate package release entry (or the code change should be moved/reverted so the changeset matches the PR scope).