wechaty-token
Version:
Wechaty Token Based Authentication Manager
142 lines • 5.19 kB
JavaScript
;
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 nock_1 = __importDefault(require("nock"));
const wechaty_token_js_1 = require("./wechaty-token.js");
(0, tstest_1.test)('WechatyToken sni() & toString()', async (t) => {
/**
* [token, sni]
*/
const FIXTURES = [
[
'TOKEN',
undefined,
],
[
'INSECURE_TOKEN',
'insecure', // should normalized to lowercase
],
[
'wxwork_ID',
'wxwork',
],
[
'puppet_wxwork_ID',
'puppet_wxwork',
],
];
for (const [token, sni] of FIXTURES) {
const wechatyToken = new wechaty_token_js_1.WechatyToken(token);
t.equal(wechatyToken.sni, sni, `token.sni() => ${sni}`);
t.equal(wechatyToken.toString(), token, `token.toString() => ${sni}`);
}
});
(0, tstest_1.test)('WechatyToken.discover() resolved', async (t) => {
const TOKEN = '__token__';
const EXPECTED_ADDRESS = {
host: '1.2.3.4',
port: 5678,
};
const scope = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(200, EXPECTED_ADDRESS);
const token = new wechaty_token_js_1.WechatyToken(TOKEN);
const address = await token.discover();
// console.info(address)
t.same(address, EXPECTED_ADDRESS, 'should get address');
scope.done();
});
(0, tstest_1.test)('WechatyToken.discover() with custom authority', async (t) => {
const AUTHORITY = 'authority.com';
const TOKEN = '__token__';
const EXPECTED_ADDRESS = {
host: '1.2.3.4',
port: 5678,
};
const scope = (0, nock_1.default)(`https://${AUTHORITY}`)
.get(`/v0/hosties/${TOKEN}`)
.reply(200, EXPECTED_ADDRESS);
const wechatyToken = new wechaty_token_js_1.WechatyToken({
authority: AUTHORITY,
token: TOKEN,
});
const address = await wechatyToken.discover();
// console.info(address)
t.same(address, EXPECTED_ADDRESS, `should get address from authority ${AUTHORITY}`);
scope.done();
});
(0, tstest_1.test)('WechatyToken.discover() retry succeed: HTTP/5XX <= 3', async (t) => {
const TOKEN = '__token__';
const EXPECTED_ADDRESS = {
host: '1.2.3.4',
port: 5678,
};
const scope500 = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(500);
const scope501 = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(501);
const scope502 = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(502);
const scope200 = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(200, EXPECTED_ADDRESS);
const wechatyToken = new wechaty_token_js_1.WechatyToken(TOKEN);
const address = await wechatyToken.discover();
// console.info(address)
t.same(address, EXPECTED_ADDRESS, 'should get address');
scope200.done();
scope500.done();
scope501.done();
scope502.done();
});
(0, tstest_1.test)('WechatyToken.discover() retry failed: too many HTTP/500 (>3)', async (t) => {
const TOKEN = '__token__';
const EXPECTED_ADDRESS = {
host: '1.2.3.4',
port: 5678,
};
const scope500 = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(500);
const scope501 = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(501);
const scope502 = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(502);
const scope503 = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(503);
const scope200 = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(200, EXPECTED_ADDRESS);
const wechatyToken = new wechaty_token_js_1.WechatyToken(TOKEN);
const address = await wechatyToken.discover();
// console.info(address)
t.equal(address, undefined, 'should not get address');
t.equal(scope200.isDone(), false, 'should not call the 4th times');
scope200.interceptors.forEach(nock_1.default.removeInterceptor);
scope500.done();
scope501.done();
scope502.done();
scope503.done();
});
(0, tstest_1.test)('WechatyToken.discover() 404', async (t) => {
const TOKEN = '__token__';
const scope = (0, nock_1.default)('https://api.chatie.io')
.get(`/v0/hosties/${TOKEN}`)
.reply(404);
const wechatyToken = new wechaty_token_js_1.WechatyToken(TOKEN);
const address = await wechatyToken.discover();
// console.info(address)
t.equal(address, undefined, 'should get undefined for 404 NotFound');
scope.done();
});
//# sourceMappingURL=wechaty-token.spec.js.map