mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
add route for QR codes (#7902)
* add route for QR codes * check that passed ID is a UUID * add test for qr code redirect * fix qr code test
This commit is contained in:
committed by
Matteo Pagliazzi
parent
e68c75c802
commit
7ef2c34177
33
test/api/v3/integration/qrcodes/GET-qrcodes_user.test.js
Normal file
33
test/api/v3/integration/qrcodes/GET-qrcodes_user.test.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import {
|
||||||
|
generateUser,
|
||||||
|
translate as t,
|
||||||
|
} from '../../../../helpers/api-v3-integration.helper';
|
||||||
|
import superagent from 'superagent';
|
||||||
|
import nconf from 'nconf';
|
||||||
|
|
||||||
|
const API_TEST_SERVER_PORT = nconf.get('PORT');
|
||||||
|
describe('GET /qr-code/user/:memberId', () => {
|
||||||
|
let user;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
user = await generateUser();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('validates req.params.memberId', async () => {
|
||||||
|
await expect(user.get('/qr-code/user/invalidUUID')).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 400,
|
||||||
|
error: 'BadRequest',
|
||||||
|
message: t('invalidReqParams'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('redirects to profile page', async () => {
|
||||||
|
let url = `http://localhost:${API_TEST_SERVER_PORT}/qr-code/user/${user._id}`;
|
||||||
|
let response = await superagent.get(url).end(function (err, res) {
|
||||||
|
expect(err).to.be(undefined);
|
||||||
|
return res;
|
||||||
|
});
|
||||||
|
expect(response.status).to.eql(200);
|
||||||
|
expect(response.request.url).to.eql(`http://localhost:${API_TEST_SERVER_PORT}/static/front/#?memberId=${user._id}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -33,7 +33,7 @@ function _requestMaker (user, method, additionalSets = {}) {
|
|||||||
let url = `http://localhost:${API_TEST_SERVER_PORT}`;
|
let url = `http://localhost:${API_TEST_SERVER_PORT}`;
|
||||||
|
|
||||||
// do not prefix with api/apiVersion requests to top level routes like dataexport, payments and emails
|
// do not prefix with api/apiVersion requests to top level routes like dataexport, payments and emails
|
||||||
if (route.indexOf('/email') === 0 || route.indexOf('/export') === 0 || route.indexOf('/paypal') === 0 || route.indexOf('/amazon') === 0 || route.indexOf('/stripe') === 0) {
|
if (route.indexOf('/email') === 0 || route.indexOf('/export') === 0 || route.indexOf('/paypal') === 0 || route.indexOf('/amazon') === 0 || route.indexOf('/stripe') === 0 || route.indexOf('/qr-code') === 0) {
|
||||||
url += `${route}`;
|
url += `${route}`;
|
||||||
} else {
|
} else {
|
||||||
url += `/api/${apiVersion}${route}`;
|
url += `/api/${apiVersion}${route}`;
|
||||||
|
|||||||
17
website/server/controllers/top-level/qrcodes.js
Normal file
17
website/server/controllers/top-level/qrcodes.js
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
let api = {};
|
||||||
|
|
||||||
|
api.redirectProfileQRCode = {
|
||||||
|
method: 'GET',
|
||||||
|
url: '/qr-code/user/:memberid',
|
||||||
|
runCron: false,
|
||||||
|
async handler (req, res) {
|
||||||
|
req.checkParams('memberid', res.t('memberIdRequired')).notEmpty().isUUID();
|
||||||
|
|
||||||
|
let validationErrors = req.validationErrors();
|
||||||
|
if (validationErrors) throw validationErrors;
|
||||||
|
|
||||||
|
res.redirect(301, `/static/front/#?memberId=${req.params.memberid}`);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = api;
|
||||||
Reference in New Issue
Block a user