UNPKG

wechaty-puppet-service

Version:
137 lines 4.3 kB
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const tstest_1 = require("tstest"); const wechaty_puppet_1 = require("wechaty-puppet"); const wechaty_puppet_mock_1 = __importDefault(require("wechaty-puppet-mock")); const get_port_1 = __importDefault(require("get-port")); const grpc_manager_js_1 = require("../src/client/grpc-manager.js"); const mod_js_1 = require("../src/mod.js"); const NIL_UUID_V4 = '00000000-0000-0000-0000-000000000000'; (0, tstest_1.test)('GrpcClient with TLS and valid token', async (t) => { const PORT = await (0, get_port_1.default)(); const TOKEN = `insecure_${NIL_UUID_V4}`; const ENDPOINT = `0.0.0.0:${PORT}`; /** * Puppet Server */ const serverOptions = { endpoint: ENDPOINT, puppet: new wechaty_puppet_mock_1.default(), token: TOKEN, }; const puppetServer = new mod_js_1.PuppetServer(serverOptions); await puppetServer.start(); /** * Puppet Service Client */ const puppetOptions = { endpoint: ENDPOINT, token: TOKEN, }; const validTokenPuppet = new grpc_manager_js_1.GrpcManager(puppetOptions); try { await validTokenPuppet.start(); t.pass('should work with TLS and valid token'); } catch (e) { console.error(e); t.fail('should not reject for a valid token & tls'); } finally { try { await validTokenPuppet.stop(); } catch (_) { } } await puppetServer.stop(); }); (0, tstest_1.test)('GrpcClient with invalid TLS options', async (t) => { const PORT = await (0, get_port_1.default)(); const TOKEN = `uuid_${NIL_UUID_V4}`; const ENDPOINT = `0.0.0.0:${PORT}`; /** * Puppet Server */ const serverOptions = { endpoint: ENDPOINT, puppet: new wechaty_puppet_mock_1.default(), token: TOKEN, }; const puppetServer = new mod_js_1.PuppetServer(serverOptions); await puppetServer.start(); /** * Grpc Client */ const puppetOptions = { endpoint: ENDPOINT, tls: { disable: true, }, token: TOKEN, }; const grpcClient = new grpc_manager_js_1.GrpcManager(puppetOptions); grpcClient.on('error', e => console.info('###noTlsPuppet.on(error):', e)); // Disable error log const level = wechaty_puppet_1.log.level(); wechaty_puppet_1.log.level('silent'); try { await grpcClient.start(); t.fail('should throw for no-tls client to tls-server instead of not running to here'); } catch (e) { t.pass('should throw for non-tls client to tls-server with noTlsInsecure: true'); } finally { wechaty_puppet_1.log.level(level); try { await grpcClient.stop(); } catch (_) { } } await puppetServer.stop(); }); (0, tstest_1.test)('GrpcClient with invalid token', async (t) => { const PORT = await (0, get_port_1.default)(); const endpoint = `0.0.0.0:${PORT}`; /** * Puppet Server */ const serverOptions = { endpoint, puppet: new wechaty_puppet_mock_1.default(), token: 'insecure_UUIDv4', }; const puppetServer = new mod_js_1.PuppetServer(serverOptions); await puppetServer.start(); /** * Puppet Service Client */ const puppetOptions = { endpoint, /** * Put a random token for invalid the client token * https://stackoverflow.com/a/8084248/1123955 */ token: 'insecure_' + Math.random().toString(36), }; const invalidTokenPuppet = new grpc_manager_js_1.GrpcManager(puppetOptions); try { await invalidTokenPuppet.start(); t.fail('should throw for invalid token instead of not running to here'); } catch (e) { t.pass('should throw for invalid random token'); } finally { try { await invalidTokenPuppet.stop(); } catch (_) { } } await puppetServer.stop(); }); //# sourceMappingURL=grpc-client.spec.js.map