test: update integration tests

Signed-off-by: Ricardo Arturo Cabral Mejía <me@ricardocabral.io>
This commit is contained in:
Ricardo Arturo Cabral Mejía 2023-02-10 11:17:19 -05:00 committed by antonleviathan
parent c2fc571adc
commit 70ace4f7f1
No known key found for this signature in database
GPG Key ID: 44A86CFF1FDF0E85
8 changed files with 30 additions and 24 deletions

View File

@ -16,6 +16,7 @@ export enum MessageType {
export type IncomingMessage = (
| SubscribeMessage
| IncomingEventMessage
| IncomingAuthMessage
| UnsubscribeMessage
) & {
[ContextMetadataKey]?: ContextMetadata
@ -53,6 +54,11 @@ export interface OutgoingEventMessage {
}
export interface OutgoingAuthMessage {
0: MessageType.AUTH
1: string
}
export interface IncomingAuthMessage {
0: MessageType.AUTH
1: Event
}

View File

@ -6,7 +6,7 @@ import { randomBytes } from 'crypto'
import { WebSocket } from 'ws'
import { ContextMetadata, Factory } from '../@types/base'
import { createAuthEventMessage, createCommandResult, createNoticeMessage, createOutgoingEventMessage } from '../utils/messages'
import { createAuthMessage, createCommandResult, createNoticeMessage, createOutgoingEventMessage } from '../utils/messages'
import { IAbortable, IMessageHandler } from '../@types/message-handlers'
import { IncomingMessage, MessageType, OutgoingMessage } from '../@types/messages'
import { IWebSocketAdapter, IWebSocketServerAdapter } from '../@types/adapters'
@ -188,10 +188,7 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
// Need to emit event?
const challenge = this.setNewAuthChallenge()
this.webSocketServer.emit(
WebSocketServerAdapterEvent.Broadcast,
createAuthEventMessage(challenge)
)
this.sendMessage(createAuthMessage(challenge))
return
}
@ -201,15 +198,9 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
const challenge = this.setNewAuthChallenge()
this.webSocketServer.emit(
WebSocketServerAdapterEvent.Broadcast,
createCommandResult(message[1].id, false, 'rejected: unauthorized')
)
this.sendMessage(createCommandResult(message[1].id, false, 'rejected: unauthorized'))
this.webSocketServer.emit(
WebSocketServerAdapterEvent.Broadcast,
createAuthEventMessage(challenge)
)
this.sendMessage(createAuthMessage(challenge))
return
}

View File

@ -1,9 +1,11 @@
import {
CommandResult,
EndOfStoredEventsNotice,
IncomingEventMessage,
IncomingRelayedEventMessage,
MessageType,
NoticeMessage,
OutgoingAuthMessage,
OutgoingMessage,
SubscribeMessage,
} from '../@types/messages'
@ -30,12 +32,12 @@ export const createEndOfStoredEventsNoticeMessage = (
}
// NIP-20
export const createCommandResult = (eventId: EventId, successful: boolean, message: string) => {
export const createCommandResult = (eventId: EventId, successful: boolean, message: string): CommandResult => {
return [MessageType.OK, eventId, successful, message]
}
// NIP-42
export const createAuthEventMessage = (challenge) => {
export const createAuthMessage = (challenge: string): OutgoingAuthMessage => {
return [MessageType.AUTH, challenge]
}

View File

@ -72,7 +72,6 @@ Feature: NIP-01
And Alice subscribes to text_note events from Bob and set_metadata events from Charlie
Then Alice receives 2 events from Bob and Charlie
@test
Scenario: Alice is interested in Bob's events from back in November
Given someone called Alice
And someone called Bob

View File

@ -101,13 +101,13 @@ When(/(\w+) sends a set_metadata event/, async function(name: string) {
this.parameters.events[name].push(event)
})
When(/^(\w+) sends a text_note event with content "([^"]+)"$/, async function(name: string, content: string) {
When(/^(\w+) sends a text_note event with content "([^"]+)"(?:\s+(successfully|unsuccessfully))?$/, async function(name: string, content: string, outcome: string) {
const ws = this.parameters.clients[name] as WebSocket
const { pubkey, privkey } = this.parameters.identities[name]
const event: Event = await createEvent({ pubkey, kind: 1, content }, privkey)
await sendEvent(ws, event)
await sendEvent(ws, event, outcome !== 'unsuccessfully')
this.parameters.events[name].push(event)
})

View File

@ -2,5 +2,5 @@ Feature: NIP-42
Scenario: Alice gets an event by ID
Given someone called Alice
And the relay requires the client to authenticate
When Alice sends a text_note event with content "hello nostr"
When Alice sends a text_note event with content "hello nostr" unsuccessfully
Then Alice receives an authentication challenge

View File

@ -5,7 +5,6 @@ import {
} from '@cucumber/cucumber'
import chai from 'chai'
import { EventKinds } from '../../../../src/constants/base'
import { SettingsStatic } from '../../../../src/utils/settings'
import sinonChai from 'sinon-chai'
import { waitForAuth } from '../helpers'
@ -19,10 +18,11 @@ Given(/the relay requires the client to authenticate/, async function (this: Wor
settings.authentication.enabled = true
})
Then(/(\w+) receives an authentication challenge "([^"]+?)"/, async function (name: string) {
Then(/(\w+) receives an authentication challenge/, async function (name: string) {
const ws = this.parameters.clients[name] as WebSocket
const outgoingAuthMessage = await waitForAuth(ws)
const event = outgoingAuthMessage[1]
expect(event.kind).to.equal(EventKinds.AUTH)
const challenge = outgoingAuthMessage[1]
expect(challenge).to.be.a.string
this.parameters.challenges[name].push(challenge)
})

View File

@ -38,7 +38,9 @@ BeforeAll({ timeout: 1000 }, async function () {
cacheClient = getCacheClient()
dbClient = getMasterDbClient()
rrDbClient = getReadReplicaDbClient()
await dbClient.raw('SELECT 1=1')
await dbClient.raw('DELETE FROM events')
await dbClient.raw('DELETE FROM invoices')
await dbClient.raw('DELETE FROM users')
Sinon.stub(SettingsStatic, 'watchSettings')
const settings = SettingsStatic.createSettings()
@ -66,6 +68,9 @@ Before(function () {
this.parameters.subscriptions = {}
this.parameters.clients = {}
this.parameters.events = {}
this.parameters.challenges = {}
const settings = SettingsStatic.createSettings()
settings.authentication.enabled = false
})
After(async function () {
@ -87,6 +92,7 @@ After(async function () {
.map(({ pubkey }) => Buffer.from(pubkey, 'hex')),
}).del()
this.parameters.identities = {}
this.parameters.challenges = {}
})
Given(/someone called (\w+)/, async function(name: string) {
@ -95,6 +101,8 @@ Given(/someone called (\w+)/, async function(name: string) {
this.parameters.clients[name] = connection
this.parameters.subscriptions[name] = []
this.parameters.events[name] = []
this.parameters.challenges[name] = []
const subject = new Subject()
connection.once('close', subject.next.bind(subject))