Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add more options to unit helper generateReq and add tests for excludi…
…ng fields in authWithHeaders
- Loading branch information
Showing
2 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters