Refactoring, better tests

This commit is contained in:
Spitap
2022-12-28 18:42:58 +01:00
parent 9ad9bf9ce4
commit 0146175153
9 changed files with 270 additions and 185 deletions
@@ -104,11 +104,13 @@ export default {
this.loading = true
auth.changePassword(payload).then(resp => {
document.getElementById('password_validation').innerHTML = ''
this.loading = false
this.returnLogin()
if (resp.status === 200) {
this.loading = false
this.returnLogin()
}
}).catch(err => {
this.loading = false
if (err.response.status === 401) {
if (err.response.status === 403) {
this.$refs.observer.setErrors({
password_confirmed: this.$gettext('Invalid reset token.')
})
@@ -116,9 +118,9 @@ export default {
this.$refs.observer.setErrors({
password_confirmed: this.$gettext('User unknown.')
})
} else if (err.response.status === 455) {
} else if (err.response.status === 400 && err.response.data.type === 'password_requirement') {
let message = ''
err.response.data.forEach(element => {
err.response.data.errors.forEach(element => {
message += this.$gettext(element) + '<br>'
})
document.getElementById('password_validation').innerHTML = message
@@ -62,30 +62,18 @@ export default {
returnLogin () {
this.$router.push({ name: 'Login' })
},
async showDialogEmail () {
const confirm = await this.$refs.dialog_response.open(
this.$gettext('Info'),
this.$gettext(
'Email sent, please check your inbox'
),
{
color: 'info',
cancelLabel: this.$gettext('Ok'),
agreeLabel: this.$gettext('Return to login')
})
if (!confirm) {
return
async showDialog (title, body, error = false) {
let color = 'info'
if (error) {
color = 'error'
}
this.returnLogin()
},
async emailFailedToSend () {
const confirm = await this.$refs.dialog_response.open(
this.$gettext('Error'),
this.$gettext(title),
this.$gettext(
'Failed to send email, please check with you administrator'
body
),
{
color: 'error',
color: color,
cancelLabel: this.$gettext('Ok'),
agreeLabel: this.$gettext('Return to login')
})
@@ -105,20 +93,23 @@ export default {
this.loading = true
auth.recoverPassword(payload).then(resp => {
this.loading = false
if (resp.status === 233) {
this.$router.push({ name: 'PasswordRecoverySmsTotpForm' })
} else if (resp.status === 210) {
this.showDialogEmail()
if (resp.status === 200) {
if (resp.data.type === 'sms') {
this.$router.push({ name: 'PasswordRecoverySmsTotpForm' })
} else if (resp.data.type === 'email') {
this.showDialog('Info', 'Email sent, please check your inbox')
}
}
}).catch(err => {
this.loading = false
if (err.response.status === 404) {
// User not found
this.$refs.observer.setErrors({
email: this.$gettext('Invalid email')
})
} else if (err.response.status === 502) {
this.emailFailedToSend()
if (err.response.data.type === 'sms' || err.response.data.type === 'email') {
this.$refs.observer.setErrors({
email: this.$gettext(err.response.data.reason)
})
}
} else if (err.response.status === 503 && err.response.data.type === 'email') {
this.showDialog('Error', err.response.data.reason, true)
}
})
}
@@ -10,7 +10,7 @@
<v-col cols="12" sm="6" class="d-flex flex-column justify-center pa-10" @keyup.enter="changePassword">
<confirm-dialog ref="confirm_wrong_user">
</confirm-dialog>
<span class="primary--text mb-6 text-h4"><translate>Password recovert</translate></span>
<span class="primary--text mb-6 text-h4"><translate>Password recovery</translate></span>
<validation-observer ref="observer">
<validation-provider
vid="sms_totp"
@@ -31,7 +31,7 @@
class="flex-grow-1"
color="primary"
large
@click="changePassword"
@click="checkSmsTotp"
:loading="loading"
>
<translate>Submit</translate>
@@ -69,9 +69,9 @@ export default {
},
async userPkError () {
const confirm = await this.$refs.confirm.open(
this.$gettext('Warning'),
this.$gettext('Error'),
this.$gettext(
'User seems wrong, return to login or restart reset process?'
'User seems wrong, return to login or restart reset the process?'
),
{
color: 'error',
@@ -86,13 +86,18 @@ export default {
}
},
async resendSms () {
auth.resendSmsTotp().then(resp => {
auth.checkSmsTotp({ type: 'resend' }).then(resp => {
this.loading = false
// Reset done !
if (resp.status === 200) {
this.$refs.observer.setErrors({
password_confirmed: this.$gettext('TOTP resent.')
})
}
}).catch(err => {
console.error(err)
this.loading = false
this.userPkError()
if (err.response.status === 400) {
this.loading = false
this.userPkError()
}
})
},
async checkSmsTotp () {
@@ -101,12 +106,13 @@ export default {
return
}
const payload = {
type: 'confirm',
sms_totp: this.sms_totp
}
this.loading = true
auth.checkSmsTotp(payload).then(resp => {
this.loading = false
if (resp.response.status === 200) {
if (resp.status === 200) {
// Code is good, redirecting to change password form.
this.$router.push({ name: 'PasswordRecoveryChangeForm', data: resp.response.data })
}
@@ -114,7 +120,7 @@ export default {
this.loading = false
if (err.response.status === 500) {
this.$refs.observer.setErrors({
password_confirmed: this.$gettext('Invalid totp, try resend')
password_confirmed: this.$gettext('Invalid totp.')
})
}
})