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
8 changed files with 30 additions and 24 deletions

View File

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

View File

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

View File

@ -1,9 +1,11 @@
import { import {
CommandResult,
EndOfStoredEventsNotice, EndOfStoredEventsNotice,
IncomingEventMessage, IncomingEventMessage,
IncomingRelayedEventMessage, IncomingRelayedEventMessage,
MessageType, MessageType,
NoticeMessage, NoticeMessage,
OutgoingAuthMessage,
OutgoingMessage, OutgoingMessage,
SubscribeMessage, SubscribeMessage,
} from '../@types/messages' } from '../@types/messages'
@ -30,12 +32,12 @@ export const createEndOfStoredEventsNoticeMessage = (
} }
// NIP-20 // 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] return [MessageType.OK, eventId, successful, message]
} }
// NIP-42 // NIP-42
export const createAuthEventMessage = (challenge) => { export const createAuthMessage = (challenge: string): OutgoingAuthMessage => {
return [MessageType.AUTH, challenge] 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 And Alice subscribes to text_note events from Bob and set_metadata events from Charlie
Then Alice receives 2 events from Bob and Charlie Then Alice receives 2 events from Bob and Charlie
@test
Scenario: Alice is interested in Bob's events from back in November Scenario: Alice is interested in Bob's events from back in November
Given someone called Alice Given someone called Alice
And someone called Bob 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) 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 ws = this.parameters.clients[name] as WebSocket
const { pubkey, privkey } = this.parameters.identities[name] const { pubkey, privkey } = this.parameters.identities[name]
const event: Event = await createEvent({ pubkey, kind: 1, content }, privkey) 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) this.parameters.events[name].push(event)
}) })

View File

@ -2,5 +2,5 @@ Feature: NIP-42
Scenario: Alice gets an event by ID Scenario: Alice gets an event by ID
Given someone called Alice Given someone called Alice
And the relay requires the client to authenticate 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 Then Alice receives an authentication challenge

View File

@ -5,7 +5,6 @@ import {
} from '@cucumber/cucumber' } from '@cucumber/cucumber'
import chai from 'chai' import chai from 'chai'
import { EventKinds } from '../../../../src/constants/base'
import { SettingsStatic } from '../../../../src/utils/settings' import { SettingsStatic } from '../../../../src/utils/settings'
import sinonChai from 'sinon-chai' import sinonChai from 'sinon-chai'
import { waitForAuth } from '../helpers' import { waitForAuth } from '../helpers'
@ -19,10 +18,11 @@ Given(/the relay requires the client to authenticate/, async function (this: Wor
settings.authentication.enabled = true 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 ws = this.parameters.clients[name] as WebSocket
const outgoingAuthMessage = await waitForAuth(ws) const outgoingAuthMessage = await waitForAuth(ws)
const event = outgoingAuthMessage[1] const challenge = outgoingAuthMessage[1]
expect(event.kind).to.equal(EventKinds.AUTH) 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() cacheClient = getCacheClient()
dbClient = getMasterDbClient() dbClient = getMasterDbClient()
rrDbClient = getReadReplicaDbClient() 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') Sinon.stub(SettingsStatic, 'watchSettings')
const settings = SettingsStatic.createSettings() const settings = SettingsStatic.createSettings()
@ -66,6 +68,9 @@ Before(function () {
this.parameters.subscriptions = {} this.parameters.subscriptions = {}
this.parameters.clients = {} this.parameters.clients = {}
this.parameters.events = {} this.parameters.events = {}
this.parameters.challenges = {}
const settings = SettingsStatic.createSettings()
settings.authentication.enabled = false
}) })
After(async function () { After(async function () {
@ -87,6 +92,7 @@ After(async function () {
.map(({ pubkey }) => Buffer.from(pubkey, 'hex')), .map(({ pubkey }) => Buffer.from(pubkey, 'hex')),
}).del() }).del()
this.parameters.identities = {} this.parameters.identities = {}
this.parameters.challenges = {}
}) })
Given(/someone called (\w+)/, async function(name: string) { 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.clients[name] = connection
this.parameters.subscriptions[name] = [] this.parameters.subscriptions[name] = []
this.parameters.events[name] = [] this.parameters.events[name] = []
this.parameters.challenges[name] = []
const subject = new Subject() const subject = new Subject()
connection.once('close', subject.next.bind(subject)) connection.once('close', subject.next.bind(subject))