adds tests and handles more scenarios
This commit is contained in:
@@ -7,6 +7,8 @@ import jwtStrategy from './auth/jwt';
|
||||
import User from '../demo/User/User.model';
|
||||
import fileUpload from 'express-fileupload';
|
||||
import assetRoutes from './routes/uploads.routes'
|
||||
import config from '../demo/payload.config';
|
||||
import language from './middleware/language';
|
||||
|
||||
module.exports = {
|
||||
init: options => {
|
||||
@@ -34,14 +36,14 @@ module.exports = {
|
||||
res.header('Access-Control-Allow-Headers',
|
||||
'Origin X-Requested-With, Content-Type, Accept, Authorization');
|
||||
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
|
||||
res.header('Content-Language', options.config.localization.language);
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
options.router.use('/upload', assetRoutes);
|
||||
|
||||
// TODO: This doesn't seem to work, had to add it to the model instead of having a global plugin
|
||||
mongoose.plugin(mongooseIntl, options.config.localization);
|
||||
options.app.use(language(config.localization));
|
||||
|
||||
options.app.use(express.json());
|
||||
options.app.use(methodOverride('X-HTTP-Method-Override'));
|
||||
|
||||
25
src/middleware/language.js
Normal file
25
src/middleware/language.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import languageParser from 'accept-language-parser';
|
||||
|
||||
/**
|
||||
* sets request language
|
||||
*
|
||||
* @param localization
|
||||
* @returns {Function}
|
||||
*/
|
||||
export default function language(localization) {
|
||||
return function (req, res, next) {
|
||||
let language;
|
||||
if (req.query.lang) {
|
||||
language = localization.languages.find(lang => lang === req.query.lang);
|
||||
if (language) {
|
||||
req.language = language;
|
||||
}
|
||||
}
|
||||
if (!req.language && req.headers['accept-language'])
|
||||
req.language = languageParser.pick(localization.languages, req.headers['accept-language']);
|
||||
if (!req.language)
|
||||
req.language = localization.defaultLanguage;
|
||||
|
||||
next()
|
||||
}
|
||||
}
|
||||
@@ -1,39 +1,114 @@
|
||||
import middleware from '../middleware';
|
||||
import mockExpress from 'jest-mock-express';
|
||||
import language from '../middleware/language';
|
||||
|
||||
let res = null;
|
||||
let next = null;
|
||||
|
||||
beforeEach(() => {
|
||||
res = mockExpress.response();
|
||||
next = jest.fn();
|
||||
});
|
||||
|
||||
describe('Payload Role Middleware', () => {
|
||||
it('Exact role - authorized', () => {
|
||||
const req = {
|
||||
user: {
|
||||
role: 'user'
|
||||
}
|
||||
};
|
||||
|
||||
middleware.role('user')(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(res.status).not.toHaveBeenCalled();
|
||||
describe('Payload Middleware', () => {
|
||||
beforeEach(() => {
|
||||
res = mockExpress.response();
|
||||
next = jest.fn();
|
||||
});
|
||||
|
||||
it('Exact role - unauthorized', () => {
|
||||
const req = {
|
||||
user: {
|
||||
role: 'user'
|
||||
}
|
||||
};
|
||||
|
||||
middleware.role('admin')(req, res, next);
|
||||
describe('Payload Role Middleware', () => {
|
||||
it('Exact role - authorized', () => {
|
||||
const req = {
|
||||
user: {
|
||||
role: 'user'
|
||||
}
|
||||
};
|
||||
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(res.status).toHaveBeenCalled();
|
||||
expect(res.send).toHaveBeenCalled();
|
||||
middleware.role('user')(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(res.status).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('Exact role - unauthorized', () => {
|
||||
const req = {
|
||||
user: {
|
||||
role: 'user'
|
||||
}
|
||||
};
|
||||
|
||||
middleware.role('admin')(req, res, next);
|
||||
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(res.status).toHaveBeenCalled();
|
||||
expect(res.send).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Payload Language Middleware', () => {
|
||||
|
||||
let req, localization;
|
||||
|
||||
beforeEach(() => {
|
||||
req = {
|
||||
query: {},
|
||||
headers: {}
|
||||
};
|
||||
localization = {
|
||||
languages: ['en', 'es'],
|
||||
defaultLanguage: 'en'
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
it('Supports query params', () => {
|
||||
req.query.lang = 'es';
|
||||
|
||||
language(localization)(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(req.language).toEqual(req.query.lang);
|
||||
});
|
||||
|
||||
it('Supports query param fallback to default', () => {
|
||||
req.query.lang = 'pt';
|
||||
|
||||
language(localization)(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(req.language).toEqual(localization.defaultLanguage);
|
||||
});
|
||||
|
||||
it('Supports accept language header', () => {
|
||||
req.headers['accept-language'] = 'es,fr;';
|
||||
|
||||
language(localization)(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(req.language).toEqual('es');
|
||||
});
|
||||
|
||||
it('Supports accept language header fallback', () => {
|
||||
req.query.lang = 'pt';
|
||||
req.headers['accept-language'] = 'fr;';
|
||||
|
||||
language(localization)(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(req.language).toEqual(localization.defaultLanguage);
|
||||
});
|
||||
|
||||
it('Query param takes precedence over header', () => {
|
||||
req.query.lang = 'es';
|
||||
req.headers['accept-language'] = 'en;';
|
||||
|
||||
language(localization)(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(req.language).toEqual('es');
|
||||
});
|
||||
|
||||
it('Supports default language', () => {
|
||||
language(localization)(req, res, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(req.language).toEqual(localization.defaultLanguage);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user