From da7036c24f8081d2f45049e83c872cb4fa9bc981 Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Sun, 14 Feb 2021 13:07:44 +0530 Subject: [PATCH] tests for user class methods, and createAccount() Also add better error handling for user#email() --- package.json | 2 +- src/user.ts | 9 ++++++++- tests/edit.bot.test.js | 8 +++++++- tests/user.edit.test.js | 36 ++++++++++++++++++++++++++++++++++++ tests/user.test.js | 3 ++- 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 tests/user.edit.test.js diff --git a/package.json b/package.json index b4c2e20..027d6de 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/user.ts b/src/user.ts index 935832c..f04faf7 100644 --- a/src/user.ts +++ b/src/user.ts @@ -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); + } + }); } /** diff --git a/tests/edit.bot.test.js b/tests/edit.bot.test.js index 0e27a6b..447a8d8 100644 --- a/tests/edit.bot.test.js +++ b/tests/edit.bot.test.js @@ -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() { @@ -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() { diff --git a/tests/user.edit.test.js b/tests/user.edit.test.js new file mode 100644 index 0000000..8b5e41f --- /dev/null +++ b/tests/user.edit.test.js @@ -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'); + }); + }); + +}); diff --git a/tests/user.test.js b/tests/user.test.js index 6cf00bb..7712037 100644 --- a/tests/user.test.js +++ b/tests/user.test.js @@ -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'); }); + });