From dace94b72b176e065762741869aada0adba5bc44 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Sat, 28 Dec 2019 22:11:56 +0100 Subject: [PATCH] Add user update methods --- js/src/user.js | 51 +++++++++++++++++++++++++++++ js/test/user.test.js | 78 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) diff --git a/js/src/user.js b/js/src/user.js index 4459837..5329b44 100644 --- a/js/src/user.js +++ b/js/src/user.js @@ -42,6 +42,14 @@ export default class uUser extends uListItem { this.listItem(id, login); } + /** + * @param {uUser} user + * @return {boolean} + */ + isEqualTo(user) { + return !!user && user.id === this.id; + } + /** * @return {Promise} */ @@ -62,4 +70,47 @@ export default class uUser extends uListItem { return users; }); } + + delete() { + return uUser.update({ + action: 'delete', + login: this.login + }); + } + + /** + * + * @param {string} login + * @param {string} password + * @return {Promise} + */ + static add(login, password) { + return uUser.update({ + action: 'add', + login: login, + pass: password + }).then((user) => new uUser(user.id, login)); + } + + /** + * @param {Object} data + * @return {Promise<*, Error>} + */ + static update(data) { + return uAjax.post('utils/handleuser.php', data); + } + + /** + * @param {string} password + * @param {string} oldPassword + * @return {Promise} + */ + setPassword(password, oldPassword) { + return uAjax.post('utils/changepass.php', + { + login: this.login, + pass: password, + oldpass: oldPassword + }); + } } diff --git a/js/test/user.test.js b/js/test/user.test.js index 804f6d2..fadef5c 100644 --- a/js/test/user.test.js +++ b/js/test/user.test.js @@ -56,6 +56,36 @@ describe('User tests', () => { // then expect(user.toString()).toBe(`[${id}, ${login}]`); }); + + it('should be equal to other user with same id', () => { + // given + const user = new uUser(1, 'testUser'); + const otherUser = new uUser(1, 'other'); + // when + const result = user.isEqualTo(otherUser); + // then + expect(result).toBe(true); + }); + + it('should not be equal to other track with other id', () => { + // given + const user = new uUser(1, 'testUser'); + const otherUser = new uUser(2, 'other'); + // when + const result = user.isEqualTo(otherUser); + // then + expect(result).toBe(false); + }); + + it('should not be equal to null track', () => { + // given + const user = new uUser(1, 'testUser'); + const otherUser = null; + // when + const result = user.isEqualTo(otherUser); + // then + expect(result).toBe(false); + }); }); describe('ajax tests', () => { @@ -98,5 +128,53 @@ describe('User tests', () => { }); }); + it('should delete user', (done) => { + // when + const user = new uUser(1, 'testUser'); + spyOnProperty(XMLHttpRequest.prototype, 'responseText').and.returnValue(JSON.stringify([])); + // then + user.delete() + .then(() => { + expect(XMLHttpRequest.prototype.open).toHaveBeenCalledWith('POST', 'utils/handleuser.php', true); + expect(XMLHttpRequest.prototype.send).toHaveBeenCalledWith(`action=delete&login=${user.login}`); + done(); + }) + .catch((e) => done.fail(`reject callback called (${e})`)); + }); + + it('should add user', (done) => { + // when + const id = 1; + const login = 'testUser'; + const password = 'password'; + const newUser = new uUser(id, login); + spyOnProperty(XMLHttpRequest.prototype, 'responseText').and.returnValue(JSON.stringify({ id: id })); + // then + uUser.add(login, password) + .then((user) => { + expect(XMLHttpRequest.prototype.open).toHaveBeenCalledWith('POST', 'utils/handleuser.php', true); + expect(XMLHttpRequest.prototype.send).toHaveBeenCalledWith(`action=add&login=${login}&pass=${password}`); + expect(user).toEqual(newUser); + done(); + }) + .catch((e) => done.fail(`reject callback called (${e})`)); + }); + + it('should change user password', (done) => { + // when + const user = new uUser(1, 'testUser'); + const password = 'password'; + const oldPassword = 'oldPassword'; + spyOnProperty(XMLHttpRequest.prototype, 'responseText').and.returnValue(JSON.stringify([])); + // then + user.setPassword(password, oldPassword) + .then(() => { + expect(XMLHttpRequest.prototype.open).toHaveBeenCalledWith('POST', 'utils/changepass.php', true); + expect(XMLHttpRequest.prototype.send).toHaveBeenCalledWith(`login=${user.login}&pass=${password}&oldpass=${oldPassword}`); + done(); + }) + .catch((e) => done.fail(`reject callback called (${e})`)); + }); + }); });