Remove old JavaScript files

This commit is contained in:
Pig Fang 2018-08-12 17:56:00 +08:00
parent d32eb379ba
commit f71a9b3dd8
7 changed files with 0 additions and 1756 deletions

View File

@ -1,406 +0,0 @@
/* eslint no-unused-vars: "off" */
const $ = require('jquery');
window.$ = window.jQuery = $;
window.blessing = {
base_url: '/'
};
describe('tests for "captcha" module', () => {
it('refresh captcha', async () => {
const url = jest.fn(path => path);
window.url = url;
document.body.innerHTML = `
<img class="captcha" src="" />
<input id="captcha" value="old" />
`;
require('../auth/captcha')();
expect($('.captcha').attr('src')).toEqual(expect.stringContaining('auth/captcha?'));
expect($('#captcha').val()).toBe('');
});
});
describe('tests for "login" module', () => {
const modulePath = '../auth/login';
it('login', async () => {
const fetch = jest.fn()
.mockImplementationOnce(option => {
option.beforeSend();
return Promise.resolve({ errno: 0, msg: 'success' });
})
.mockImplementationOnce(() => Promise.resolve(
{ errno: 1, msg: 'warning1', login_fails: 1 }
))
.mockImplementationOnce(() => Promise.resolve(
{ errno: 1, msg: 'warning2', login_fails: 4 }
))
.mockImplementationOnce(() => Promise.reject());
const trans = jest.fn(key => key);
const url = jest.fn(path => path);
const swal = jest.fn();
const refreshCaptcha = jest.fn();
const showAjaxError = jest.fn();
window.fetch = fetch;
window.trans = trans;
window.url = url;
window.swal = swal;
window.showMsg = jest.fn();
window.refreshCaptcha = refreshCaptcha;
window.showAjaxError = showAjaxError;
document.body.innerHTML = `
<input id="identification" />
<input id="password" />
<div id="captcha-form"></div>
<input id="captcha" />
<input id="keep" checked />
<button id="login-button"></button>
`;
require(modulePath);
$('button').click();
expect(trans).toBeCalledWith('auth.emptyIdentification');
expect($('#identification').is(':focus')).toBe(true);
$('#identification').val('username');
$('button').click();
expect(trans).toBeCalledWith('auth.emptyPassword');
expect($('#password').is(':focus')).toBe(true);
$('#password').val('password');
$('button').click();
expect(trans).toBeCalledWith('auth.emptyCaptcha');
expect($('#captcha').is(':focus')).toBe(true);
$('#captcha').val('captcha');
await $('button').click();
expect(fetch).toBeCalledWith(expect.objectContaining({
type: 'POST',
url: 'auth/login',
dataType: 'json',
data: {
identification: 'username',
password: 'password',
keep: true,
captcha: 'captcha'
}
}));
expect($('button').html()).toBe(
'<i class="fa fa-spinner fa-spin"></i> auth.loggingIn'
);
expect($('button').prop('disabled')).toBe(true);
expect(swal).toBeCalledWith({ type: 'success', html: 'success' });
$('#captcha-form').css('display', 'none');
await $('button').click();
expect($('#captcha-form').css('display')).toBe('none');
expect(refreshCaptcha).toBeCalled();
expect(showMsg).toBeCalledWith('warning1', 'warning');
expect($('button').html()).toBe('auth.login');
expect($('button').prop('disabled')).toBe(false);
await $('button').click();
expect(swal).toBeCalledWith({ type: 'error', html: 'auth.tooManyFails' });
expect($('#captcha-form').css('display')).not.toBe('none');
expect(showMsg).toBeCalledWith('warning2', 'warning');
await $('button').click();
expect(showAjaxError).toBeCalled();
});
});
describe('tests for "register" module', () => {
const modulePath = '../auth/register';
it('register', async () => {
const fetch = jest.fn()
.mockImplementationOnce(option => {
option.beforeSend();
return Promise.resolve({ errno: 0, msg: 'success' });
})
.mockImplementationOnce(() => Promise.resolve(
{ errno: 1, msg: 'warning' }
))
.mockImplementationOnce(() => Promise.reject(new Error));
const trans = jest.fn(key => key);
const url = jest.fn(path => path);
const swal = jest.fn().mockImplementation(() => Promise.resolve());
const showMsg = jest.fn();
const refreshCaptcha = jest.fn();
const showAjaxError = jest.fn();
window.fetch = fetch;
window.trans = trans;
window.url = url;
window.swal = swal;
window.showMsg = showMsg;
window.refreshCaptcha = refreshCaptcha;
window.showAjaxError = showAjaxError;
document.body.innerHTML = `
<input id="email" />
<input id="nickname" />
<input id="password" />
<input id="confirm-pwd" />
<div id="captcha-form"></div>
<input id="captcha" />
<button id="register-button"></button>
`;
require(modulePath);
$('button').click();
expect(trans).toBeCalledWith('auth.emptyEmail');
expect($('#email').is(':focus')).toBe(true);
$('#email').val('email');
$('button').click();
expect(trans).toBeCalledWith('auth.invalidEmail');
expect(showMsg).toBeCalledWith('auth.invalidEmail', 'warning');
expect($('#email').is(':focus')).toBe(true);
$('#email').val('a@b.c');
$('button').click();
expect(trans).toBeCalledWith('auth.emptyPassword');
expect($('#password').is(':focus')).toBe(true);
$('#password').val('secret');
$('button').click();
expect(trans).toBeCalledWith('auth.invalidPassword');
expect(showMsg).toBeCalledWith('auth.invalidPassword', 'warning');
expect($('#password').is(':focus')).toBe(true);
$('#password').val('too_long_password_very_super_long');
$('#password').blur();
$('button').click();
expect(trans).toBeCalledWith('auth.invalidPassword');
expect(showMsg).toBeCalledWith('auth.invalidPassword', 'warning');
expect($('#password').is(':focus')).toBe(true);
$('#password').val('password');
$('button').click();
expect(trans).toBeCalledWith('auth.emptyConfirmPwd');
expect($('#confirm-pwd').is(':focus')).toBe(true);
$('#confirm-pwd').val('not_same');
$('button').click();
expect(trans).toBeCalledWith('auth.invalidConfirmPwd');
expect(showMsg).toBeCalledWith('auth.invalidConfirmPwd', 'warning');
expect($('#confirm-pwd').is(':focus')).toBe(true);
$('#confirm-pwd').val('password');
$('button').click();
expect(trans).toBeCalledWith('auth.emptyNickname');
expect($('#nickname').is(':focus')).toBe(true);
$('#nickname').val('nickname');
$('button').click();
expect(trans).toBeCalledWith('auth.emptyCaptcha');
expect($('#captcha').is(':focus')).toBe(true);
$('#captcha').val('captcha');
await $('button').click();
expect(fetch).toBeCalledWith(expect.objectContaining({
type: 'POST',
url: 'auth/register',
dataType: 'json',
data: {
email: 'a@b.c',
nickname: 'nickname',
password: 'password',
captcha: 'captcha'
}
}));
expect($('button').html()).toBe(
'<i class="fa fa-spinner fa-spin"></i> auth.registering'
);
expect($('button').prop('disabled')).toBe(true);
expect(swal).toBeCalledWith({ type: 'success', html: 'success' });
await $('button').click();
expect(refreshCaptcha).toBeCalled();
expect(showMsg).toBeCalledWith('warning', 'warning');
expect($('button').html()).toBe('auth.register');
await $('button').click();
expect(showAjaxError).toBeCalled();
});
});
describe('tests for "forgot" module', () => {
const modulePath = '../auth/forgot';
it('forgot password', async () => {
const fetch = jest.fn()
.mockImplementationOnce(option => {
option.beforeSend();
return Promise.resolve({ errno: 0, msg: 'success' });
})
.mockImplementationOnce(() => Promise.resolve(
{ errno: 1, msg: 'warning' }
))
.mockImplementationOnce(() => Promise.reject(new Error));
const trans = jest.fn(key => key);
const url = jest.fn(path => path);
const swal = jest.fn();
const showMsg = jest.fn();
const refreshCaptcha = jest.fn();
const showAjaxError = jest.fn();
window.fetch = fetch;
window.trans = trans;
window.url = url;
window.swal = swal;
window.showMsg = showMsg;
window.refreshCaptcha = refreshCaptcha;
window.showAjaxError = showAjaxError;
document.body.innerHTML = `
<input id="email" />
<div id="captcha-form"></div>
<input id="captcha" />
<button id="forgot-button"></button>
`;
require(modulePath);
$('button').click();
expect(trans).toBeCalledWith('auth.emptyEmail');
expect($('#email').is(':focus')).toBe(true);
$('#email').val('email');
$('button').click();
expect(trans).toBeCalledWith('auth.invalidEmail');
expect(showMsg).toBeCalledWith('auth.invalidEmail', 'warning');
expect($('#email').is(':focus')).toBe(true);
$('#email').val('a@b.c');
$('button').click();
expect(trans).toBeCalledWith('auth.emptyCaptcha');
expect($('#captcha').is(':focus')).toBe(true);
$('#captcha').val('captcha');
await $('button').click();
expect(fetch).toBeCalledWith(expect.objectContaining({
type: 'POST',
url: 'auth/forgot',
dataType: 'json',
data: {
email: 'a@b.c',
captcha: 'captcha'
}
}));
expect($('button').html()).toBe('auth.send');
expect($('button').prop('disabled')).toBe(true);
expect(showMsg).toBeCalledWith('success', 'success');
await $('button').click();
expect(refreshCaptcha).toBeCalled();
expect(showMsg).toBeCalledWith('warning', 'warning');
expect($('button').html()).toBe('auth.send');
await $('button').click();
expect($('button').html()).toBe('auth.send');
expect($('button').prop('disabled')).toBe(false);
expect(showAjaxError).toBeCalled();
});
});
describe('tests for "reset" module', () => {
const modulePath = '../auth/reset';
it('reset password', async () => {
const fetch = jest.fn()
.mockImplementationOnce(option => {
option.beforeSend();
return Promise.resolve({ errno: 0, msg: 'success' });
})
.mockImplementationOnce(() => Promise.resolve(
{ errno: 1, msg: 'warning' }
))
.mockImplementationOnce(() => Promise.reject(new Error));
const trans = jest.fn(key => key);
const url = jest.fn(path => path);
const swal = jest.fn().mockReturnValue(Promise.resolve());
const showMsg = jest.fn();
const getQueryString = jest.fn()
.mockReturnValueOnce('expires')
.mockReturnValueOnce('signature');
const showAjaxError = jest.fn();
window.fetch = fetch;
window.trans = trans;
window.url = url;
window.swal = swal;
window.showMsg = showMsg;
window.refreshCaptcha = jest.fn();
window.getQueryString = getQueryString;
window.showAjaxError = showAjaxError;
document.body.innerHTML = `
<input id="uid" value="1" />
<input id="password" />
<input id="confirm-pwd" />
<button id="reset-button"></button>
`;
require(modulePath);
$('button').click();
expect(trans).toBeCalledWith('auth.emptyPassword');
expect($('#password').is(':focus')).toBe(true);
$('#password').val('secret');
$('button').click();
expect(trans).toBeCalledWith('auth.invalidPassword');
expect(showMsg).toBeCalledWith('auth.invalidPassword', 'warning');
expect($('#password').is(':focus')).toBe(true);
$('#password').val('too_long_password_very_super_long');
$('#password').blur();
$('button').click();
expect(trans).toBeCalledWith('auth.invalidPassword');
expect(showMsg).toBeCalledWith('auth.invalidPassword', 'warning');
expect($('#password').is(':focus')).toBe(true);
$('#password').val('password');
$('button').click();
expect(trans).toBeCalledWith('auth.emptyConfirmPwd');
expect($('#confirm-pwd').is(':focus')).toBe(true);
$('#confirm-pwd').val('not_same');
$('button').click();
expect(trans).toBeCalledWith('auth.invalidConfirmPwd');
expect(showMsg).toBeCalledWith('auth.invalidConfirmPwd', 'warning');
expect($('#confirm-pwd').is(':focus')).toBe(true);
$('#confirm-pwd').val('password');
await $('button').click();
expect(getQueryString.mock.calls[0][0]).toBe('expires');
expect(getQueryString.mock.calls[1][0]).toBe('signature');
expect(fetch).toBeCalledWith(expect.objectContaining({
type: 'POST',
url: 'auth/reset/1?expires=expires&signature=signature',
dataType: 'json',
data: {
password: 'password'
}
}));
expect($('button').html()).toBe(
'<i class="fa fa-spinner fa-spin"></i> auth.resetting'
);
expect($('button').prop('disabled')).toBe(true);
expect(swal).toBeCalledWith({ type: 'success', html: 'success' });
await $('button').click();
expect(showMsg).toBeCalledWith('warning', 'warning');
expect($('button').html()).toBe('auth.reset');
await $('button').click();
expect($('button').html()).toBe('auth.reset');
expect(showAjaxError).toBeCalled();
});
});

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +0,0 @@
'use strict';
function refreshCaptcha() {
const timestamp = new Date().getTime();
// Refresh Captcha Image
$('.captcha').attr('src', url(`auth/captcha?${timestamp}`));
// Clear input
$('#captcha').val('');
}
$('.captcha').click(refreshCaptcha);
if (process.env.NODE_ENV === 'test') {
module.exports = refreshCaptcha;
}

View File

@ -1,51 +0,0 @@
/* global refreshCaptcha */
'use strict';
$('#forgot-button').click(e => {
e.preventDefault();
const data = {
email: $('#email').val(),
captcha: $('#captcha').val()
};
(function validate({ email, captcha }, callback) {
if (email === '') {
showMsg(trans('auth.emptyEmail'));
$('#email').focus();
} else if (!/\S+@\S+\.\S+/.test(email)) {
showMsg(trans('auth.invalidEmail'), 'warning');
} else if (captcha === '') {
showMsg(trans('auth.emptyCaptcha'));
$('#captcha').focus();
} else {
callback();
}
})(data, async () => {
try {
const { errno, msg } = await fetch({
type: 'POST',
url: url('auth/forgot'),
dataType: 'json',
data: data,
beforeSend: () => {
$('#forgot-button').html(
'<i class="fa fa-spinner fa-spin"></i> ' + trans('auth.sending')
).prop('disabled', 'disabled');
}
});
if (errno === 0) {
showMsg(msg, 'success');
$('#forgot-button').html(trans('auth.send')).prop('disabled', 'disabled');
} else {
showMsg(msg, 'warning');
refreshCaptcha();
$('#forgot-button').html(trans('auth.send')).prop('disabled', '');
}
} catch (error) {
showAjaxError(error);
$('#forgot-button').html(trans('auth.send')).prop('disabled', '');
}
});
});

View File

@ -1,69 +0,0 @@
/* global refreshCaptcha */
'use strict';
$('#login-button').click(async e => {
e.preventDefault();
const data = {
identification: $('#identification').val(),
password: $('#password').val(),
keep: $('#keep').prop('checked') ? true : false
};
if (data.identification === '') {
showMsg(trans('auth.emptyIdentification'));
$('#identification').focus();
} else if (data.password === '') {
showMsg(trans('auth.emptyPassword'));
$('#password').focus();
} else {
// Verify it when captcha form is shown
if ($('#captcha-form').css('display') === 'block') {
data.captcha = $('#captcha').val();
if (data.captcha === '') {
showMsg(trans('auth.emptyCaptcha'));
$('#captcha').focus();
return false;
}
}
try {
const { errno, msg, login_fails } = await fetch({
type: 'POST',
url: url('auth/login'),
dataType: 'json',
data: data,
beforeSend: () => {
$('#login-button').html(
'<i class="fa fa-spinner fa-spin"></i> ' + trans('auth.loggingIn')
).prop('disabled', 'disabled');
}
});
if (errno === 0) {
swal({ type: 'success', html: msg });
setTimeout(() => {
window.location = url(blessing.redirect_to || 'user');
}, 1000);
} else {
if (login_fails > 3) {
if ($('#captcha-form').css('display') === 'none') {
swal({ type: 'error', html: trans('auth.tooManyFails') });
$('#captcha-form').show();
}
}
refreshCaptcha();
showMsg(msg, 'warning');
$('#login-button').html(trans('auth.login')).prop('disabled', '');
}
} catch (error) {
showAjaxError(error);
$('#login-button').html(trans('auth.login')).prop('disabled', '');
}
}
});

View File

@ -1,74 +0,0 @@
/* global refreshCaptcha */
'use strict';
$('#register-button').click(e => {
e.preventDefault();
const data = {
email: $('#email').val(),
password: $('#password').val(),
nickname: $('#nickname').val(),
captcha: $('#captcha').val(),
};
(function validate({ email, password, nickname, captcha }, callback) {
// Massive form validation
if (email === '') {
showMsg(trans('auth.emptyEmail'));
$('#email').focus();
} else if (!/\S+@\S+\.\S+/.test(email)) {
showMsg(trans('auth.invalidEmail'), 'warning');
} else if (password === '') {
showMsg(trans('auth.emptyPassword'));
$('#password').focus();
} else if (password.length < 8 || password.length > 32) {
showMsg(trans('auth.invalidPassword'), 'warning');
$('#password').focus();
} else if ($('#confirm-pwd').val() === '') {
showMsg(trans('auth.emptyConfirmPwd'));
$('#confirm-pwd').focus();
} else if (password !== $('#confirm-pwd').val()) {
showMsg(trans('auth.invalidConfirmPwd'), 'warning');
$('#confirm-pwd').focus();
} else if (nickname === '') {
showMsg(trans('auth.emptyNickname'));
$('#nickname').focus();
} else if (captcha === '') {
showMsg(trans('auth.emptyCaptcha'));
$('#captcha').focus();
} else {
callback();
}
return;
})(data, async () => {
try {
const { errno, msg } = await fetch({
type: 'POST',
url: url('auth/register'),
dataType: 'json',
data: data,
beforeSend: function () {
$('#register-button').html(
'<i class="fa fa-spinner fa-spin"></i> ' + trans('auth.registering')
).prop('disabled', 'disabled');
}
});
if (errno === 0) {
swal({ type: 'success', html: msg });
setTimeout(() => {
window.location = url('user');
}, 1000);
} else {
showMsg(msg, 'warning');
refreshCaptcha();
$('#register-button').html(trans('auth.register')).prop('disabled', '');
}
} catch (error) {
showAjaxError(error);
$('#register-button').html(trans('auth.register')).prop('disabled', '');
}
});
});

View File

@ -1,53 +0,0 @@
'use strict';
$('#reset-button').click(e => {
e.preventDefault();
const data = {
password: $('#password').val(),
};
(function validate({ password }, callback) {
if (password === '') {
showMsg(trans('auth.emptyPassword'));
$('#password').focus();
} else if (password.length < 8 || password.length > 16) {
showMsg(trans('auth.invalidPassword'), 'warning');
$('#password').focus();
} else if ($('#confirm-pwd').val() === '') {
showMsg(trans('auth.emptyConfirmPwd'));
$('#confirm-pwd').focus();
} else if (password !== $('#confirm-pwd').val()) {
showMsg(trans('auth.invalidConfirmPwd'), 'warning');
$('#confirm-pwd').focus();
} else {
callback();
}
})(data, async () => {
try {
const { errno, msg } = await fetch({
type: 'POST',
url: `${url('auth/reset')}/${$('#uid').val()}?expires=${getQueryString('expires')}&signature=${getQueryString('signature')}`,
dataType: 'json',
data,
beforeSend: () => {
$('#reset-button').html(
'<i class="fa fa-spinner fa-spin"></i> ' + trans('auth.resetting')
).prop('disabled', 'disabled');
}
});
if (errno === 0) {
swal({
type: 'success',
html: msg
}).then(() => (window.location = url('auth/login')));
} else {
showMsg(msg, 'warning');
$('#reset-button').html(trans('auth.reset')).prop('disabled', '');
}
} catch (error) {
showAjaxError(error);
$('#reset-button').html(trans('auth.reset')).prop('disabled', '');
}
});
});