From 981cd55f7783cfabfe00e37404b9f853f66d0428 Mon Sep 17 00:00:00 2001 From: Ariel Boukris Date: Tue, 17 Sep 2024 21:45:14 +0300 Subject: [PATCH] Add jest test --- tests/end_to_end.test.js | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/end_to_end.test.js b/tests/end_to_end.test.js index 1a551def..dbdf8bd6 100644 --- a/tests/end_to_end.test.js +++ b/tests/end_to_end.test.js @@ -1284,5 +1284,49 @@ const Pulsar = require('../index'); await consumer.close(); await client.close(); }); + + test('AuthenticationToken token supplier', async () => { + const mockTokenSupplier = jest.fn().mockReturnValue('token'); + const auth = new Pulsar.AuthenticationToken({ + token: mockTokenSupplier, + }); + const client = new Pulsar.Client({ + serviceUrl: 'pulsar://localhost:6650', + authentication: auth, + }); + + // A producer/consumer is needed to triger the callback function + const topic = 'persistent://public/default/token-auth'; + const producer = await client.createProducer({ + topic, + }); + expect(producer).not.toBeNull(); + expect(mockTokenSupplier).toHaveBeenCalledTimes(1); + + await producer.close(); + await client.close(); + }); + + test('AuthenticationToken async token supplier', async () => { + const mockTokenSupplier = jest.fn().mockResolvedValue('token'); + const auth = new Pulsar.AuthenticationToken({ + token: mockTokenSupplier, + }); + const client = new Pulsar.Client({ + serviceUrl: 'pulsar://localhost:6650', + authentication: auth, + }); + + // A producer/consumer is needed to triger the callback function + const topic = 'persistent://public/default/token-auth'; + const producer = await client.createProducer({ + topic, + }); + expect(producer).not.toBeNull(); + expect(mockTokenSupplier).toHaveBeenCalledTimes(1); + + await producer.close(); + await client.close(); + }); }); })();