Add user update methods

This commit is contained in:
Bartek Fabiszewski 2019-12-28 22:11:56 +01:00
parent 869d34dae1
commit dace94b72b
2 changed files with 129 additions and 0 deletions

View File

@ -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<uTrack, Error>}
*/
@ -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<uUser>}
*/
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<void, Error>}
*/
setPassword(password, oldPassword) {
return uAjax.post('utils/changepass.php',
{
login: this.login,
pass: password,
oldpass: oldPassword
});
}
}

View File

@ -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})`));
});
});
});