Skip to content

Commit

Permalink
tests for user class methods, and createAccount()
Browse files Browse the repository at this point in the history
Also add better error handling for user#email()
  • Loading branch information
siddharthvp committed Feb 14, 2021
1 parent 0dfba93 commit da7036c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint src tests",
"test:testwiki": "cd tests && npx mocha bot.test.js category.test.js file.test.js login.bot.test.js oauth.test.js page.test.js suppl.bot.test.js user.test.js wikitext.test.js",
"setuplocalwiki": "cd tests/docker && bash main.sh",
"test:localwiki": "cd tests && npx mocha edit.bot.test.js errors.test.js shutoff.test.js",
"test:localwiki": "cd tests && npx mocha edit.bot.test.js user.edit.test.js errors.test.js shutoff.test.js",
"test:nowiki": "cd tests && npx mocha batchOperations.bot.test.js date.test.js log.test.js static_utils.test.js title.test.js",
"test": "nyc --reporter=lcov --reporter=text mocha tests/",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
Expand Down
9 changes: 8 additions & 1 deletion src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,14 @@ export default function(bot: mwn) {
text: message,
token: bot.csrfToken,
...options
}).then(data => data.emailuser);
}).then(response => {
let data = response.emailuser;
if (data.result === 'Success') {
return data;
} else {
return Promise.reject(data);
}
});
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/edit.bot.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { bot, sinon, crypto, expect, setup, teardown} = require('./local_wiki');
const {bot, sinon, crypto, expect, setup, teardown} = require('./local_wiki');
const logger = require('../build/log');

describe('methods which modify the wiki', function() {
Expand Down Expand Up @@ -99,6 +99,12 @@ describe('methods which modify the wiki', function() {
});
});

it('successfully creates an account', function () {
return bot.createAccount('testAcct', 'testPassword').then(data => {
expect(data.status).to.equal('PASS');
});
});

describe('image uploads', function() {

it('successfully upload image from URL', function() {
Expand Down
36 changes: 36 additions & 0 deletions tests/user.edit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const {mwn, bot, expect, setup, teardown} = require('./local_wiki');

describe('write methods', async function () {

before('setup', setup);
after('teardown', teardown);

let u = new bot.user('Wikiuser2');

it('sends message', function () {
return u.sendMessage('Hi', 'Testing from mwn. ~~~~').then(data => {
expect(data).to.be.an('object').which.has.property('result').that.equals('Success');
});
});

it('sends email', function () {
return expect(u.email('Subject', 'body')).to.be.eventually.rejectedWith(mwn.Error)
.with.property('code').that.equals('noemail');
});

it('block', function () {
return u.block().then(data => {
expect(data).to.be.an('object').that.includes.keys('user', 'userID', 'reason',
'anononly', 'nocreate', 'partial');
expect(data.user).to.equal('Wikiuser2');
});
});

it('unblock', function () {
return u.unblock().then(data => {
expect(data).to.be.an('object').that.includes.keys('user', 'id', 'userid', 'reason');
expect(data.user).to.equal('Wikiuser2');
});
});

});
3 changes: 2 additions & 1 deletion tests/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ describe('User', async function() {
expect(u.talkpage.namespace).to.equal(3);
});

it('info', async function () {
it('info and globalinfo', async function () {
let u = new bot.user('SD0001');
let info = await u.info();
expect(info).to.include.keys('userid', 'editcount', 'groups', 'rights');
let globalinfo = await u.globalinfo();
expect(globalinfo).to.include.keys('home', 'id', 'name');
});

});

0 comments on commit da7036c

Please sign in to comment.