Skip to content

Commit

Permalink
getTokens(): simplify to only fetch tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Oct 30, 2022
1 parent 232ca58 commit 81f4638
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,22 @@ export class mwn {
* @returns {Promise<void>}
*/
async getTokens(): Promise<void> {
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,
});
}
});
}

/**
Expand All @@ -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<void>}
*/
async getTokensAndSiteInfo(): Promise<void> {
Expand Down
8 changes: 8 additions & 0 deletions tests/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 81f4638

Please sign in to comment.