wechaty-puppet-service
Version:
Puppet Service for Wechaty
63 lines • 1.95 kB
JavaScript
import { test, sinon, } from 'tstest';
import { GrpcStatus, Metadata, } from './grpc-js.js';
import { authImplToken } from './auth-impl-token.js';
test('authImplToken()', async (t) => {
const sandbox = sinon.createSandbox();
const spy = sandbox.spy();
const TOKEN = 'UUIDv4';
const IMPL = {
spy,
};
const implWithAuth = authImplToken(TOKEN)(IMPL);
const validMetadata = new Metadata();
validMetadata.add('Authorization', 'Wechaty ' + TOKEN);
const TEST_LIST = [
{
call: {
metadata: validMetadata,
},
callback: sandbox.spy(),
ok: true,
},
{
call: {
metadata: new Metadata(),
},
callback: sandbox.spy(),
ok: false,
},
{
call: {
emit: sandbox.spy(),
metadata: new Metadata(),
},
callback: undefined,
ok: false,
},
];
const method = implWithAuth['spy'];
for (const { call, callback, ok } of TEST_LIST) {
spy.resetHistory();
method(call, callback);
if (ok) {
t.ok(spy.calledOnce, 'should call IMPL handler');
continue;
}
/**
* not ok
*/
t.ok(spy.notCalled, 'should not call IMPL handler');
if (callback) {
t.equal(callback.args[0][0].code, GrpcStatus.UNAUTHENTICATED, 'should return UNAUTHENTICATED');
}
else {
t.equal(call.emit.args[0][0], 'error', 'should emit error');
t.equal(call.emit.args[0][1].code, GrpcStatus.UNAUTHENTICATED, 'should emit UNAUTHENTICATED');
}
// console.info(spy.args)
// console.info(callback?.args)
}
sandbox.restore();
});
//# sourceMappingURL=auth-impl-token.spec.js.map