Skip to content

Commit

Permalink
tests: minor updates, run on weekly schedule as well
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Aug 21, 2022
1 parent f7007b0 commit 232ca58
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ on:
- 'src/**'
- 'tests/**'
- '.github/workflows/ci-push.yml'
schedule:
- '0 5 * * 1'
workflow_dispatch:

jobs:
Expand Down
2 changes: 1 addition & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions tests/core.test.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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();
});
});

Expand Down
3 changes: 2 additions & 1 deletion tests/edit.bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ 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) => {
return rev.content;
})
.then(() => {
expect(logger.log).to.have.been.calledOnce;
sinon.restore();
});
});

Expand Down
50 changes: 29 additions & 21 deletions tests/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 () {
Expand Down
5 changes: 3 additions & 2 deletions tests/login.bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});

Expand Down

0 comments on commit 232ca58

Please sign in to comment.