Node.js based forum software built for the modern web
test: test fixes
Barış Soner Uşaklı9d92f04ae312
diff --git a/src/controllers/accounts/edit.js b/src/controllers/accounts/edit.jsindex c02bb7563d..6e9ed3b2f3 100644--- a/src/controllers/accounts/edit.js+++ b/src/controllers/accounts/edit.js@@ -86,15 +86,17 @@ editController.password = async function (req, res, next) {};editController.username = async function (req, res, next) {+ console.log('username controller');await renderRoute('username', req, res, next);};editController.email = async function (req, res, next) {+ console.log('editController.email', { req: req.path, uid: req.uid, slug: req.params.slug });const targetUid = await user.getUidByUserslug(req.params.userslug);if (!targetUid || req.uid !== parseInt(targetUid, 10)) {return next();}-+ console.log('MADE IT');req.session.returnTo = `/uid/${targetUid}`;req.session.registration = req.session.registration || {};req.session.registration.updateEmail = true;diff --git a/src/controllers/authentication.js b/src/controllers/authentication.jsindex 2ec606b947..214a81f5f7 100644--- a/src/controllers/authentication.js+++ b/src/controllers/authentication.js@@ -364,6 +364,7 @@ authenticationController.onSuccessfulLogin = async function (req, uid, trackSessreq.session.meta = {};const now = Date.now();if (req.session.forceLogin) {+ console.log('forced login', { returnTo: req.session.returnTo });req.session.meta.reAuthAt = now;} else {delete req.session.meta.reAuthAt;diff --git a/src/middleware/index.js b/src/middleware/index.jsindex 9b1cbad720..3bd06ea425 100644--- a/src/middleware/index.js+++ b/src/middleware/index.js@@ -372,11 +372,13 @@ middleware.requirePasswordAuth = helpers.try(async function (req, res, next) {// ajaxify /api/foo/baz => returnTo /foo/bazmiddleware.requirePageReAuth = function ({ reauthWindowMinutes = 2 } = {}) {async function redirect(req, res) {+ console.log('REDIRECT TO LOGIN', { req: req.path, uid: req.uid, slug: req.params.slug, returnTo: req.session.returnTo });if (res.locals.isAPI) {req.session.returnTo = req.url.replace(/^\/api/, '');await controllers.helpers.formatApiResponse(401, res);} else {req.session.returnTo = req.url;+ console.log({ returnTo: req.session.returnTo });const isAdminPath = req.path === '/admin' || req.path.startsWith('/admin/');res.redirect(`${relative_path}/login${isAdminPath ? '?local=1' : ''}`);}@@ -430,6 +432,7 @@ function isReAuthValid(req, reauthWindowMinutes) {async function triggerReLoginHook(req, res) {req.session.forceLogin = 1;+ console.log({ req: req.path, headers: req.headers, forceLogin: req.session.forceLogin });await plugins.hooks.fire('response:auth.relogin', { req, res });return res.headersSent;}diff --git a/test/controllers.js b/test/controllers.jsindex 613815bf26..60a7a42948 100644--- a/test/controllers.js+++ b/test/controllers.js@@ -648,12 +648,11 @@ describe('Controllers', () => {describe('abort behaviour', () => {let jar;let token;+ const username = utils.generateUUID().slice(0, 10);+ const password = utils.generateUUID();beforeEach(async () => {- jar = (await helpers.registerUser({- username: utils.generateUUID().slice(0, 10),- password: utils.generateUUID(),- })).jar;+ jar = (await helpers.registerUser({ username, password })).jar;token = await helpers.getCsrfToken(jar);});@@ -687,7 +686,21 @@ describe('Controllers', () => {},});- // Start email change flow+ // this gets blocked by requirePageReAuth+ await request.get(`${nconf.get('url')}/me/edit/email`, { jar });++ // reautheticate to get past requirePageReAuth+ await request.post(`${nconf.get('url')}/login`, {+ jar,+ headers: {+ 'x-csrf-token': token,+ },+ body: {+ username: username,+ password: password,+ },+ });+ // Start email change flow, thisawait request.get(`${nconf.get('url')}/me/edit/email`, { jar });const { response } = await request.post(`${nconf.get('url')}/register/abort`, {@@ -1010,6 +1023,11 @@ describe('Controllers', () => {});describe('/me/*', () => {+ let jar;+ before(async () => {+ jar = (await helpers.loginUser('foo', 'barbar')).jar;+ });+it('should redirect to user profile', async () => {const { response, body } = await request.get(`${nconf.get('url')}/me`, { jar });assert.equal(response.statusCode, 200);@@ -1145,12 +1163,14 @@ describe('Controllers', () => {});describe('user data export routes', () => {+ let jar;before(async () => {const types = ['profile', 'uploads', 'posts'];await Promise.all(types.map(async (type) => {await api.users.generateExport({ uid: fooUid, ip: '127.0.0.1' }, { uid: fooUid, type });}));await sleep(10000);+ jar = (await helpers.loginUser('foo', 'barbar')).jar;});it('should export users posts', async () => {@@ -1358,16 +1378,15 @@ describe('Controllers', () => {await groups.leave('administrators', fooUid);});- it('should render edit/password', async () => {+ it('should render edit/password and get 401', async () => {const { response } = await request.get(`${nconf.get('url')}/api/user/foo/edit/password`, { jar });- assert.equal(response.statusCode, 200);+ assert.equal(response.statusCode, 401);});it('should render edit/email', async () => {const { response, body } = await request.get(`${nconf.get('url')}/api/user/foo/edit/email`, { jar });-- assert.strictEqual(response.statusCode, 200);- assert.strictEqual(body, '/register/complete');+ assert.strictEqual(response.statusCode, 401);+ assert.strictEqual(body.status.code, 'not-authorised');await request.post(`${nconf.get('url')}/register/abort`, {jar,@@ -1378,7 +1397,8 @@ describe('Controllers', () => {});it('should render edit/username', async () => {- const { response } = await request.get(`${nconf.get('url')}/api/user/foo/edit/username`, { jar });+ const { jar } = await helpers.loginUser('foo', 'barbar');+ const { response, body } = await request.get(`${nconf.get('url')}/api/user/foo/edit/username`, { jar });assert.equal(response.statusCode, 200);});});diff --git a/test/user.js b/test/user.jsindex 534a49d6dd..10c2daf70c 100644--- a/test/user.js+++ b/test/user.js@@ -1271,20 +1271,6 @@ describe('User', () => {assert(body);});- it('should load edit/email page', async () => {- const { response, body } = await request.get(`${nconf.get('url')}/api/user/updatedagain/edit/email`, { jar });- assert.strictEqual(response.statusCode, 200);- assert(body);-- // Accessing this page will mark the user's account as needing an updated email, below code undo's.- await request.post(`${nconf.get('url')}/register/abort`, {- jar,- headers: {- 'x-csrf-token': csrf_token,- },- });- });-it('should load user\'s groups page', async () => {await groups.create({name: 'Test',