From 232ca5867fb8ca19305fd4993cd6d97975a30fa2 Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Mon, 22 Aug 2022 00:53:52 +0530 Subject: [PATCH] tests: minor updates, run on weekly schedule as well --- .github/workflows/ci-push.yml | 2 ++ src/bot.ts | 2 +- tests/core.test.js | 6 +++-- tests/edit.bot.test.js | 3 ++- tests/errors.test.js | 50 ++++++++++++++++++++--------------- tests/login.bot.test.js | 5 ++-- 6 files changed, 41 insertions(+), 27 deletions(-) diff --git a/.github/workflows/ci-push.yml b/.github/workflows/ci-push.yml index aa9d704..964d5b2 100644 --- a/.github/workflows/ci-push.yml +++ b/.github/workflows/ci-push.yml @@ -9,6 +9,8 @@ on: - 'src/**' - 'tests/**' - '.github/workflows/ci-push.yml' + schedule: + - '0 5 * * 1' workflow_dispatch: jobs: diff --git a/src/bot.ts b/src/bot.ts index eccf527..492e5cc 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -593,7 +593,7 @@ export class mwn { ) { reason = `Already logged in as ${this.options.username}, logout first to re-login`; } else if ( - data.reason === 'Cannot log in when using MediaWiki\\Extensions\\OAuth\\SessionProvider sessions.' + data.reason === 'Cannot log in when using MediaWiki\\Extension\\OAuth\\SessionProvider sessions.' ) { reason = `Cannot use login/logout while using OAuth`; } else if (data.reason) { diff --git a/tests/core.test.js b/tests/core.test.js index c0280fb..e02b7ee 100644 --- a/tests/core.test.js +++ b/tests/core.test.js @@ -1,4 +1,4 @@ -const { Request } = require('../build/core'); +const { Request, Response } = require('../build/core'); const logger = require('../build/log'); const { MwnError } = require('../build/error'); @@ -35,11 +35,13 @@ describe('core', function () { it("doesn't retry on ENOTFOUND rejection", async function () { const bot2 = new mwn({ apiUrl: 'https://somewebsite2342978653424.org/w/api.php' }); - // this test relies on no retry taking place since they won't take place within the 2 second timeout + sinon.spy(Response.prototype, 'handleRequestFailure'); await expect(bot2.getSiteInfo()) .to.be.eventually.rejectedWith(Error) .that.has.property('code') .which.equals('ENOTFOUND'); + expect(Response.prototype.handleRequestFailure).to.have.been.calledOnce; + sinon.restore(); }); }); diff --git a/tests/edit.bot.test.js b/tests/edit.bot.test.js index 5730ca0..32e7894 100644 --- a/tests/edit.bot.test.js +++ b/tests/edit.bot.test.js @@ -43,7 +43,7 @@ describe('methods which modify the wiki', function () { }); }); - it('shows warning (see above) on a no-op edit', function () { + it('shows warning on a no-op edit', function () { sinon.spy(logger, 'log'); return bot .edit(randPage, (rev) => { @@ -51,6 +51,7 @@ describe('methods which modify the wiki', function () { }) .then(() => { expect(logger.log).to.have.been.calledOnce; + sinon.restore(); }); }); diff --git a/tests/errors.test.js b/tests/errors.test.js index 31e40b4..842eb4c 100644 --- a/tests/errors.test.js +++ b/tests/errors.test.js @@ -4,6 +4,8 @@ const { sinon, bot, bot2, expect, setup, teardown } = require('./local_wiki'); const nock = require('nock'); const { Request, Response } = require('../build/core'); const utils = require('../build/utils'); +const { mwn } = require('../build/bot'); +const logger = require('../build/log'); describe('testing for error recoveries', function () { this.timeout(10000); @@ -21,33 +23,39 @@ describe('testing for error recoveries', function () { after('logs out', teardown); - it('recovers from badtoken errors', function () { + it('recovers from badtoken errors', async function () { bot.csrfToken = 'invalid-value'; - return bot - .edit(testPage, function (rev) { - return { - text: rev.content + '\n\nappended text', - summary: 'testing mwn', - }; - }) - .then((edit) => { - expect(edit.result).to.equal('Success'); - }); + sinon.spy(mwn.prototype, 'getTokens'); + sinon.spy(logger, 'log'); + let edit = await bot.edit(testPage, function (rev) { + return { + text: rev.content + '\n\nappended text', + summary: 'testing mwn', + }; + }); + expect(mwn.prototype.getTokens).to.have.been.calledOnce; + expect(logger.log).to.have.been.calledOnceWith( + '[W] Encountered badtoken error, fetching new token and retrying' + ); + expect(edit.result).to.equal('Success'); + sinon.restore(); }); it('recovers from session loss failure', async function () { bot2.setDefaultParams({ assert: 'user' }); await bot2.logout(); - return bot2 - .edit(testPage, function (rev) { - return { - text: rev.content + '\n\nappended text', - summary: 'Test edit after session loss', - }; - }) - .then((edit) => { - expect(edit.result).to.equal('Success'); - }); + sinon.spy(mwn.prototype, 'login'); + sinon.spy(logger, 'log'); + let edit = await bot2.edit(testPage, function (rev) { + return { + text: rev.content + '\n\nappended text', + summary: 'Test edit after session loss', + }; + }); + expect(mwn.prototype.login).to.be.calledOnce; + expect(logger.log).to.have.been.calledOnceWith('[W] Received assertuserfailed, attempting to log in and retry'); + expect(edit.result).to.equal('Success'); + sinon.restore(); }); it('makes large edits (multipart/form-data) after session loss', async function () { diff --git a/tests/login.bot.test.js b/tests/login.bot.test.js index 320c4f1..0ad3a94 100644 --- a/tests/login.bot.test.js +++ b/tests/login.bot.test.js @@ -30,11 +30,12 @@ describe('login', async function () { }); }); - it('raises correct error on trying to login while using OAuth', async function () { + it('raises correct error on trying to login while using OAuth', function (done) { let client = new mwn({ ...testwiki.account1, ...testwiki.account1_oauth }); client.initOAuth(); - return client.login().catch((err) => { + client.login().catch((err) => { expect(err.info).to.eq(`Cannot use login/logout while using OAuth`); + done(); // using done() here to test that catch callback gets called }); });