Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit bc64e3f

Browse files
committed
refactor: Convert integration/Encryption test to typescript.
1 parent cc23d7d commit bc64e3f

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

test/integration/Encryption.test.js renamed to test/integration/Encryption.test.ts

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { describeRepeats, fakePrivateKey, uid, Msg, getPublishTestMessages } fro
55
import { Defer } from '../../src/utils'
66
import { StreamrClient } from '../../src/StreamrClient'
77
import { GroupKey } from '../../src/stream/encryption/Encryption'
8+
import { Stream, StreamOperation } from '../../src/stream'
9+
import { Subscription } from '../../src'
810
import Connection from '../../src/Connection'
911
import { StorageNode } from '../../src/stream/StorageNode'
1012

@@ -15,17 +17,17 @@ const TIMEOUT = 10 * 1000
1517
const { StreamMessage } = MessageLayer
1618

1719
describeRepeats('decryption', () => {
18-
let publishTestMessages
20+
let publishTestMessages: ReturnType<typeof getPublishTestMessages>
1921
let expectErrors = 0 // check no errors by default
20-
let errors = []
22+
let errors: Error[] = []
2123

22-
const getOnError = (errs) => jest.fn((err) => {
24+
const getOnError = (errs: Error[]) => jest.fn((err) => {
2325
errs.push(err)
2426
})
2527

2628
let onError = jest.fn()
27-
let client
28-
let stream
29+
let client: StreamrClient
30+
let stream: Stream
2931

3032
const createClient = (opts = {}) => {
3133
const c = new StreamrClient({
@@ -35,6 +37,7 @@ describeRepeats('decryption', () => {
3537
},
3638
autoConnect: false,
3739
autoDisconnect: false,
40+
// @ts-expect-error
3841
disconnectDelay: 1,
3942
publishAutoDisconnectDelay: 50,
4043
maxRetries: 2,
@@ -46,7 +49,7 @@ describeRepeats('decryption', () => {
4649
return c
4750
}
4851

49-
function checkEncryptionMessages(testClient) {
52+
function checkEncryptionMessages(testClient: StreamrClient) {
5053
const onSendTest = Defer()
5154
testClient.connection.on('_send', onSendTest.wrapError((sendingMsg) => {
5255
// check encryption is as expected
@@ -73,7 +76,7 @@ describeRepeats('decryption', () => {
7376
})
7477

7578
afterEach(async () => {
76-
await wait()
79+
await wait(0)
7780
// ensure no unexpected errors
7881
expect(errors).toHaveLength(expectErrors)
7982
if (client) {
@@ -82,7 +85,7 @@ describeRepeats('decryption', () => {
8285
})
8386

8487
afterEach(async () => {
85-
await wait()
88+
await wait(0)
8689
if (client) {
8790
client.debug('disconnecting after test')
8891
await client.disconnect()
@@ -95,7 +98,7 @@ describeRepeats('decryption', () => {
9598
}
9699
})
97100

98-
async function setupClient(opts) {
101+
async function setupClient(opts?: any) {
99102
client = createClient(opts)
100103
await Promise.all([
101104
client.session.getSessionToken(),
@@ -130,6 +133,7 @@ describeRepeats('decryption', () => {
130133
const done = Defer()
131134
const sub = await client.subscribe({
132135
stream: stream.id,
136+
// @ts-expect-error
133137
groupKeys: keys,
134138
}, done.wrap((parsedContent, streamMessage) => {
135139
expect(parsedContent).toEqual(msg)
@@ -174,7 +178,7 @@ describeRepeats('decryption', () => {
174178
client.publish(stream.id, msg),
175179
done,
176180
])
177-
onEncryptionMessageErr.resolve() // will be ignored if errored
181+
onEncryptionMessageErr.resolve(undefined) // will be ignored if errored
178182
await onEncryptionMessageErr
179183
// All good, unsubscribe
180184
await client.unsubscribe(sub)
@@ -191,7 +195,7 @@ describeRepeats('decryption', () => {
191195
// Check signature stuff
192196
received.push(streamMessage)
193197
if (received.length === msgs.length) {
194-
done.resolve()
198+
done.resolve(undefined)
195199
}
196200
}))
197201

@@ -238,14 +242,15 @@ describeRepeats('decryption', () => {
238242
}
239243
})
240244

241-
onEncryptionMessageErr.resolve() // will be ignored if errored
245+
onEncryptionMessageErr.resolve(undefined) // will be ignored if errored
242246
await onEncryptionMessageErr
243247
// All good, unsubscribe
244248
await client.unsubscribe(sub)
245249
}, TIMEOUT)
246250

247251
it('errors if rotating group key for no stream', async () => {
248252
expect(async () => (
253+
// @ts-expect-error
249254
client.rotateGroupKey()
250255
)).rejects.toThrow('streamId')
251256
})
@@ -257,8 +262,8 @@ describeRepeats('decryption', () => {
257262
})
258263

259264
it('allows other users to get group key', async () => {
260-
let otherClient
261-
let sub
265+
let otherClient: StreamrClient
266+
let sub: Subscription
262267
try {
263268
otherClient = createClient({
264269
autoConnect: true,
@@ -268,8 +273,8 @@ describeRepeats('decryption', () => {
268273
const onEncryptionMessageErr = checkEncryptionMessages(client)
269274
const onEncryptionMessageErr2 = checkEncryptionMessages(otherClient)
270275
const otherUser = await otherClient.getUserInfo()
271-
await stream.grantPermission('stream_get', otherUser.username)
272-
await stream.grantPermission('stream_subscribe', otherUser.username)
276+
await stream.grantPermission(StreamOperation.STREAM_GET, otherUser.username)
277+
await stream.grantPermission(StreamOperation.STREAM_SUBSCRIBE, otherUser.username)
273278

274279
const done = Defer()
275280
const msg = Msg()
@@ -293,9 +298,9 @@ describeRepeats('decryption', () => {
293298
client.publish(stream.id, msg),
294299
done,
295300
])
296-
onEncryptionMessageErr.resolve() // will be ignored if errored
301+
onEncryptionMessageErr.resolve(undefined) // will be ignored if errored
297302
await onEncryptionMessageErr
298-
onEncryptionMessageErr2.resolve() // will be ignored if errored
303+
onEncryptionMessageErr2.resolve(undefined) // will be ignored if errored
299304
await onEncryptionMessageErr2
300305
} finally {
301306
if (otherClient) {
@@ -317,7 +322,7 @@ describeRepeats('decryption', () => {
317322

318323
let didFindStream2 = false
319324

320-
function checkEncryptionMessagesPerStream(testClient) {
325+
function checkEncryptionMessagesPerStream(testClient: StreamrClient) {
321326
const onSendTest = Defer()
322327
testClient.connection.on('_send', onSendTest.wrapError((sendingMsg) => {
323328
// check encryption is as expected
@@ -344,7 +349,7 @@ describeRepeats('decryption', () => {
344349
return onSendTest
345350
}
346351

347-
async function testSub(testStream) {
352+
async function testSub(testStream: Stream) {
348353
const NUM_MESSAGES = 5
349354
const done = Defer()
350355
const received = []
@@ -353,7 +358,7 @@ describeRepeats('decryption', () => {
353358
}, done.wrapError((parsedContent) => {
354359
received.push(parsedContent)
355360
if (received.length === NUM_MESSAGES) {
356-
done.resolve()
361+
done.resolve(undefined)
357362
}
358363
}))
359364
sub.once('error', done.reject)
@@ -376,7 +381,7 @@ describeRepeats('decryption', () => {
376381
testSub(stream),
377382
testSub(stream2),
378383
])
379-
onEncryptionMessageErr.resolve() // will be ignored if errored
384+
onEncryptionMessageErr.resolve(undefined) // will be ignored if errored
380385
await onEncryptionMessageErr
381386
expect(didFindStream2).toBeTruthy()
382387
}, TIMEOUT)
@@ -393,7 +398,7 @@ describeRepeats('decryption', () => {
393398
const groupKey2 = GroupKey.generate()
394399
await client.setNextGroupKey(stream2.id, groupKey2)
395400

396-
function checkEncryptionMessagesPerStream(testClient) {
401+
function checkEncryptionMessagesPerStream(testClient: StreamrClient) {
397402
const onSendTest = Defer()
398403
testClient.connection.on('_send', onSendTest.wrapError((sendingMsg) => {
399404
// check encryption is as expected
@@ -421,7 +426,7 @@ describeRepeats('decryption', () => {
421426
return onSendTest
422427
}
423428

424-
async function testSub(testStream) {
429+
async function testSub(testStream: Stream) {
425430
const NUM_MESSAGES = 5
426431
const done = Defer()
427432
const received = []
@@ -430,7 +435,7 @@ describeRepeats('decryption', () => {
430435
}, done.wrapError((parsedContent) => {
431436
received.push(parsedContent)
432437
if (received.length === NUM_MESSAGES) {
433-
done.resolve()
438+
done.resolve(undefined)
434439
}
435440
}))
436441
sub.once('error', done.reject)
@@ -450,7 +455,7 @@ describeRepeats('decryption', () => {
450455
testSub(stream),
451456
testSub(stream2),
452457
])
453-
onEncryptionMessageErr.resolve() // will be ignored if errored
458+
onEncryptionMessageErr.resolve(undefined) // will be ignored if errored
454459
await onEncryptionMessageErr
455460
}, TIMEOUT)
456461

@@ -586,6 +591,7 @@ describeRepeats('decryption', () => {
586591

587592
const sub = await client.subscribe({
588593
stream: stream.id,
594+
// @ts-expect-error
589595
groupKeys: keys,
590596
})
591597

@@ -618,7 +624,7 @@ describeRepeats('decryption', () => {
618624
})
619625

620626
describe('revoking permissions', () => {
621-
let client2
627+
let client2: StreamrClient
622628

623629
beforeEach(async () => {
624630
client2 = createClient({ id: 'subscriber' })
@@ -643,8 +649,8 @@ describeRepeats('decryption', () => {
643649
const MAX_MESSAGES = 6
644650
await client.rotateGroupKey(stream.id)
645651

646-
const p1 = await stream.grantPermission('stream_get', client2.getPublisherId())
647-
const p2 = await stream.grantPermission('stream_subscribe', client2.getPublisherId())
652+
const p1 = await stream.grantPermission(StreamOperation.STREAM_GET, await client2.getPublisherId())
653+
const p2 = await stream.grantPermission(StreamOperation.STREAM_SUBSCRIBE, await client2.getPublisherId())
648654

649655
const sub = await client2.subscribe({
650656
stream: stream.id,
@@ -676,12 +682,13 @@ describeRepeats('decryption', () => {
676682
}
677683
}
678684
})
679-
let t
685+
686+
let t!: ReturnType<typeof setTimeout>
680687
await expect(async () => {
681688
for await (const m of sub) {
682689
received.push(m.getParsedContent())
683690
if (received.length === REVOKE_AFTER) {
684-
gotMessages.resolve()
691+
gotMessages.resolve(undefined)
685692
clearTimeout(t)
686693
t = setTimeout(() => {
687694
sub.cancel()
@@ -704,7 +711,7 @@ describeRepeats('decryption', () => {
704711
expect(onSubError).toHaveBeenCalledTimes(1)
705712
})
706713

707-
it('fails gracefully if permission revoked with low cache maxAge fail first message', async () => {
714+
it.only('fails gracefully if permission revoked with low cache maxAge fail first message', async () => {
708715
await client.disconnect()
709716
await setupClient({
710717
id: 'publisher',
@@ -715,8 +722,8 @@ describeRepeats('decryption', () => {
715722
const MAX_MESSAGES = 3
716723
await client.rotateGroupKey(stream.id)
717724

718-
const p1 = await stream.grantPermission('stream_get', client2.getPublisherId())
719-
const p2 = await stream.grantPermission('stream_subscribe', client2.getPublisherId())
725+
const p1 = await stream.grantPermission(StreamOperation.STREAM_GET, await client2.getPublisherId())
726+
const p2 = await stream.grantPermission(StreamOperation.STREAM_SUBSCRIBE, await client2.getPublisherId())
720727

721728
const sub = await client2.subscribe({
722729
stream: stream.id,
@@ -749,12 +756,12 @@ describeRepeats('decryption', () => {
749756
}
750757
}
751758
})
752-
let t
759+
let t!: ReturnType<typeof setTimeout>
753760
await expect(async () => {
754761
for await (const m of sub) {
755762
received.push(m.getParsedContent())
756763
if (received.length === REVOKE_AFTER) {
757-
gotMessages.resolve()
764+
gotMessages.resolve(undefined)
758765
clearTimeout(t)
759766
t = setTimeout(() => {
760767
sub.cancel()
@@ -788,8 +795,8 @@ describeRepeats('decryption', () => {
788795
const MAX_MESSAGES = 10
789796
await client.rotateGroupKey(stream.id)
790797

791-
const p1 = await stream.grantPermission('stream_get', client2.getPublisherId())
792-
const p2 = await stream.grantPermission('stream_subscribe', client2.getPublisherId())
798+
const p1 = await stream.grantPermission(StreamOperation.STREAM_GET, await client2.getPublisherId())
799+
const p2 = await stream.grantPermission(StreamOperation.STREAM_SUBSCRIBE, await client2.getPublisherId())
793800

794801
const sub = await client2.subscribe({
795802
stream: stream.id,
@@ -822,12 +829,12 @@ describeRepeats('decryption', () => {
822829
}
823830
})
824831

825-
let t
832+
let t!: ReturnType<typeof setTimeout>
826833
await expect(async () => {
827834
for await (const m of sub) {
828835
received.push(m.getParsedContent())
829836
if (received.length === REVOKE_AFTER) {
830-
gotMessages.resolve()
837+
gotMessages.resolve(undefined)
831838
clearTimeout(t)
832839
t = setTimeout(() => {
833840
sub.cancel()

0 commit comments

Comments
 (0)