From 81f4638138c85ef7fb1b7cdf8b5cf58320a93a10 Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Sun, 30 Oct 2022 13:27:49 +0530 Subject: [PATCH] getTokens(): simplify to only fetch tokens --- src/bot.ts | 19 +++++++++++++++++-- tests/bot.test.js | 8 ++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 492e5cc..5445f06 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -699,7 +699,22 @@ export class mwn { * @returns {Promise} */ async getTokens(): Promise { - return this.getTokensAndSiteInfo(); + return this.request({ + action: 'query', + meta: 'tokens', + type: 'csrf|createaccount|login|patrol|rollback|userrights|watch', + }).then((response: ApiResponse) => { + if (response.query && response.query.tokens) { + this.csrfToken = response.query.tokens.csrftoken; + this.state = merge(this.state, response.query.tokens); + } else { + return rejectWithError({ + code: 'mwn_notoken', + info: 'Could not get token', + response, + }); + } + }); } /** @@ -713,7 +728,7 @@ export class mwn { } /** - * Get the tokens and siteinfo in one request + * Get tokens and siteinfo (using a single API request) and save them in the bot state. * @returns {Promise} */ async getTokensAndSiteInfo(): Promise { diff --git a/tests/bot.test.js b/tests/bot.test.js index c6e2137..1487b58 100644 --- a/tests/bot.test.js +++ b/tests/bot.test.js @@ -77,6 +77,14 @@ describe('mwn', async function () { }); }); + it('gets tokens', function () { + bot.state = {}; + return bot.getTokens().then(() => { + expect(bot.csrfToken.endsWith('+\\')).to.be.true; + expect(bot.csrfToken.length).to.be.greaterThan(5); + }); + }); + it('correctly sets logging config', function () { const { logConfig } = require('../build/log'); mwn.setLoggingConfig({