perf(salix): refs #6427 imrpove
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2024-05-24 11:11:22 +02:00
parent 8201d057f3
commit 9953df2f4f
3 changed files with 31 additions and 52 deletions

View File

@ -7,7 +7,7 @@ module.exports = Self => {
accepts: [ accepts: [
{ {
arg: 'userId', arg: 'user',
type: 'string', type: 'string',
description: 'The recoveryPhone user\'s', description: 'The recoveryPhone user\'s',
required: true required: true
@ -28,44 +28,36 @@ module.exports = Self => {
} }
}); });
Self.recoverPasswordSMS = async function(userId, verificationCode, options) { Self.recoverPasswordSMS = async function(user, verificationCode, options) {
const models = Self.app.models;
const myOptions = {}; const myOptions = {};
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
const user = await Self.findOne({ const usesEmail = user.indexOf('@') !== -1;
fields: ['id', 'name', 'email', 'name', 'recoveryPhone'], const filter = usesEmail ? {email: user} : {name: user};
where: { // if (!usesEmail) {
or: [ const account = await models.VnUser.findOne({
{id: userId}, fields: ['id', 'name', 'recoveryPhone'],
{nickname: userId}, where: filter
{name: userId},
{email: userId},
]
}
}); });
if (!user) throw new UserError('Credentials not valid'); if (!account) return;
user = account;
// }
try { if (verificationCode) {
if (verificationCode) { await Self.validateCode(user.name, verificationCode);
await Self.validateCode(user.name, verificationCode);
return { return {
token: await user.accessTokens.create({}) token: await user.accessTokens.create({})
}; };
}
const code = await authCode(user, myOptions);
if (isProduction(true))
return {code};
await Self.app.models.Sms.send({req: {accessToken: {userId}}}, +userId, code);
} catch (err) {
if (err.code === 'EMAIL_NOT_FOUND')
return;
else
throw err;
} }
const code = await authCode(user, myOptions);
if (isProduction(true))
return {code};
await Self.app.models.Sms.send({req: {accessToken: {user: user.id}}}, +user.recoveryPhone, code);
}; };
}; };

View File

@ -8,6 +8,10 @@ fdescribe('RecoverPassword path', async() => {
beforeAll(async() => { beforeAll(async() => {
browser = await getBrowser(); browser = await getBrowser();
page = browser.page; page = browser.page;
});
beforeEach(async() => {
await page.waitForState('login');
await page.waitToClick(selectors.recoverPassword.recoverPasswordButton); await page.waitToClick(selectors.recoverPassword.recoverPasswordButton);
await page.waitForState('recover-password'); await page.waitForState('recover-password');
@ -33,47 +37,33 @@ fdescribe('RecoverPassword path', async() => {
}); });
it('should send email using email', async() => { it('should send email using email', async() => {
await page.waitForState('login');
await page.waitToClick(selectors.recoverPassword.recoverPasswordButton);
await page.write(selectors.recoverPassword.email, 'BruceWayne@mydomain.com'); await page.write(selectors.recoverPassword.email, 'BruceWayne@mydomain.com');
await page.waitToClick(selectors.recoverPassword.emailOption); await page.waitToClick(selectors.recoverPassword.emailOption);
await page.waitToClick(selectors.recoverPassword.sendEmailButton); await page.waitToClick(selectors.recoverPassword.sendEmailButton);
const message = await page.waitForSnackbar(); const message = await page.waitForSnackbar();
await page.waitForState('login');
expect(message.text).toContain('Notification sent!'); expect(message.text).toContain('Notification sent!');
}); });
it('should send email using username', async() => { it('should send email using username', async() => {
await page.waitForState('login');
await page.waitToClick(selectors.recoverPassword.recoverPasswordButton);
await page.write(selectors.recoverPassword.email, 'BruceWayne'); await page.write(selectors.recoverPassword.email, 'BruceWayne');
await page.waitToClick(selectors.recoverPassword.emailOption); await page.waitToClick(selectors.recoverPassword.emailOption);
await page.waitToClick(selectors.recoverPassword.sendEmailButton); await page.waitToClick(selectors.recoverPassword.sendEmailButton);
const message = await page.waitForSnackbar(); const message = await page.waitForSnackbar();
await page.waitForState('login');
expect(message.text).toContain('Notification sent!'); expect(message.text).toContain('Notification sent!');
}); });
it('should not throw error if not exist user when select sms option', async() => { it('should not throw error if not exist user when select sms option', async() => {
await page.waitForState('login');
await page.waitToClick(selectors.recoverPassword.recoverPasswordButton);
await page.write(selectors.recoverPassword.email, 'fakeEmail@mydomain.com'); await page.write(selectors.recoverPassword.email, 'fakeEmail@mydomain.com');
await page.waitToClick(selectors.recoverPassword.smsOption); await page.waitToClick(selectors.recoverPassword.smsOption);
await page.waitToClick(selectors.recoverPassword.sendEmailButton); await page.waitToClick(selectors.recoverPassword.sendEmailButton);
const message = await page.waitForSnackbar(); const message = await page.waitForSnackbar();
expect(message.text).toContain('Credentials not valid'); expect(message.text).toContain('Notification sent!');
expect(await page.getState()).toContain('recover-password');
}); });
it('should send sms using username', async() => { it('should send sms using username', async() => {
await page.clearInput(selectors.recoverPassword.email);
await page.write(selectors.recoverPassword.email, 'BruceWayne'); await page.write(selectors.recoverPassword.email, 'BruceWayne');
await page.waitToClick(selectors.recoverPassword.smsOption); await page.waitToClick(selectors.recoverPassword.smsOption);
await page.waitToClick(selectors.recoverPassword.sendEmailButton); await page.waitToClick(selectors.recoverPassword.sendEmailButton);

View File

@ -33,19 +33,16 @@ export default class Controller {
'email': { 'email': {
url: 'VnUsers/recoverPassword', url: 'VnUsers/recoverPassword',
data: {user: this.user}, data: {user: this.user},
cb: data => {
if (data === '') this.goToLogin();
}
}, },
'sms': { 'sms': {
url: 'VnUsers/recoverPasswordSMS', url: 'VnUsers/recoverPasswordSMS',
data: {userId: this.user, verificationCode: this.verificationCode}, data: {user: this.user, verificationCode: this.verificationCode},
cb: data => { cb: data => {
if (this.method && this.code) { if (this.method && this.code) {
data.token && this.goToChangePassword(data); data.token && this.goToChangePassword(data);
!data.token && this.goToLogin(); !data.token && this.goToLogin();
} else } else
data.code && this.handleCode(data.code); data.code && this.handleCode(data.code);
} }
}, },
@ -57,7 +54,7 @@ export default class Controller {
throw new UserError(`Credentials not valid`); throw new UserError(`Credentials not valid`);
const method = this.methodsAvailables()[this.method]; const method = this.methodsAvailables()[this.method];
this.$http.post(method.url, method.data) this.$http.post(method.url, method.data)
.then(({data}) => method.cb(data)); .then(({data}) => data === '' ? this.goToLogin() : method.cb(data));
} }
} }
Controller.$inject = ['$scope', '$element', '$http', 'vnApp', '$translate', '$state', '$location']; Controller.$inject = ['$scope', '$element', '$http', 'vnApp', '$translate', '$state', '$location'];