I read about supertest. I managed to test two of my routes:
it('notAuthorized', function (done) {
request.agent(server)
.get('/notAuthorized')
.expect('Content-Type', /html/)
.expect(200)
.expect(/Not authorized!/)
.end(done)
})
it('redirect', function (done) {
request.agent(server)
.get('/no-valid-route')
.expect('Content-Type', /plain/)
.expect(302)
.expect('Location', '/notAuthorized')
.expect(/Moved Temporarily/)
.end(done)
})
However, the problem starts when I want to access my other pages, those I need to register for. I found this solution for a regular registration:
describe('When logged in', function () {
before(function (done) {
// login the user
agent
.post('/users/session')
.field('email', 'foobar@example.com')
.field('password', 'foobar')
.end(done)
})
// ...
})
In my application I register with a certificate. Can I somehow configure the test with my certificate? Changing my https options doesn't work either:
///////////////////
// https options
var options = {
// ...
requestCert: false,
rejectUnauthorized: false
};
I assume it is because of my middle ware I use in every of my routes:
exports.isAuthenticated = function(req, res, next){
if(req.client.authorized) {
// user authorized
if(!req.session.user) {
// set user session
var cert = req.connection.getPeerCertificate().subject;
// ..
// profile
app.get("/profile", mw.isAuthenticated, profileController.getProfile);
// user
app.get("/users", mw.isAuthenticated, userController.getUsers);
// chat
app.get("/chat", mw.isAuthenticated, chatController.getChat);
Question:
- is there anyway I can configure the agent with my certificate?
- should I maybe overthink the design of using the
isAuthenticated
middle ware in every route? - can I somehow change the cookie object of supertest's agent?
If I could set the req object like the following snippet, I'd probably have a solution.
req : {
client : {
authorized : true
},
connection :{
getPeerCertificate : function(){
this.subject = 1;
}
}
}