Skip to content

Commit

Permalink
add more options to unit helper generateReq and add tests for excludi…
Browse files Browse the repository at this point in the history
…ng fields in authWithHeaders
  • Loading branch information
paglias committed Apr 12, 2018
1 parent 21354e9 commit 066adff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
40 changes: 40 additions & 0 deletions test/api/v3/unit/middlewares/auth.test.js
@@ -0,0 +1,40 @@
import {
generateRes,
generateReq,
} from '../../../../helpers/api-unit.helper';
import { authWithHeaders as authWithHeadersFactory } from '../../../../../website/server/middlewares/auth';

describe('auth middleware', () => {
let res, req, user;

beforeEach(async () => {
res = generateRes();
req = generateReq();
user = await res.locals.user.save();
});

describe('auth with headers', () => {
it('allows to specify a list of user field that we do not want to load', (done) => {
const authWithHeaders = authWithHeadersFactory(false, {
userFieldsToExclude: ['items', 'flags', 'auth.timestamps'],
});

req.headers['x-api-user'] = user._id;
req.headers['x-api-key'] = user.apiToken;

authWithHeaders(req, res, (err) => {
if (err) return done(err);

const userToJSON = res.locals.user.toJSON();
expect(userToJSON.items).to.not.exist;
expect(userToJSON.flags).to.not.exist;
expect(userToJSON.auth.timestamps).to.not.exist;
expect(userToJSON.auth).to.exist;
expect(userToJSON.notifications).to.exist;
expect(userToJSON.preferences).to.exist;

done();
});
});
});
});
9 changes: 7 additions & 2 deletions test/helpers/api-unit.helper.js
Expand Up @@ -54,10 +54,15 @@ export function generateReq (options = {}) {
body: {},
query: {},
headers: {},
header: sandbox.stub().returns(null),
header (header) {
return this.headers[header];
},
session: {},
};

return defaultsDeep(options, defaultReq);
const req = defaultsDeep(options, defaultReq);

return req;
}

export function generateNext (func) {
Expand Down

0 comments on commit 066adff

Please sign in to comment.