Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Copy req.user to user interface if present (fixes #160) (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar authored Jul 29, 2016
1 parent 242efbf commit c1035b5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
21 changes: 21 additions & 0 deletions lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,26 @@ module.exports.parseRequest = function parseRequest(req, kwargs) {
// expose http interface
kwargs.request = http;

// user
//
// typically found on req.user according to Express and Passport

var user = {};
if (req.user && !kwargs.user) {
// shallow copy is okay because we are only modifying top-level
// object (req.user)
for (var key in req.user) {
if ({}.hasOwnProperty.call(req.user, key)) {
user[key] = req.user[key];
}
}

if (ip) {
user.ip_address = ip;
}

kwargs.user = user;
}

return kwargs;
};
67 changes: 61 additions & 6 deletions test/raven.parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ describe('raven.parsers', function() {
encrypted: true
},
connection: {
remoteAddress: '69.69.69.69'
remoteAddress: '127.0.0.1'
}
};
var parsed = raven.parsers.parseRequest(mockReq);
parsed.should.have.property('request');
parsed.request.url.should.equal('https://mattrobenolt.com/some/path?key=value');
parsed.request.env.NODE_ENV.should.equal(process.env.NODE_ENV);
parsed.request.env.REMOTE_ADDR.should.equal('69.69.69.69');
parsed.request.env.REMOTE_ADDR.should.equal('127.0.0.1');
});

describe('`headers` detection', function() {
Expand Down Expand Up @@ -303,12 +303,12 @@ describe('raven.parsers', function() {
headers: {
hostname: 'mattrobenolt.com',
},
ip: '69.69.69.69'
ip: '127.0.0.1'
};

var parsed = raven.parsers.parseRequest(mockReq);

parsed.request.env.REMOTE_ADDR.should.equal('69.69.69.69');
parsed.request.env.REMOTE_ADDR.should.equal('127.0.0.1');
});

it('should detect ip via `req.connection.remoteAddress`', function() {
Expand All @@ -319,13 +319,13 @@ describe('raven.parsers', function() {
hostname: 'mattrobenolt.com',
},
connection: {
remoteAddress: '69.69.69.69'
remoteAddress: '127.0.0.1'
}
};

var parsed = raven.parsers.parseRequest(mockReq);

parsed.request.env.REMOTE_ADDR.should.equal('69.69.69.69');
parsed.request.env.REMOTE_ADDR.should.equal('127.0.0.1');
});
});

Expand Down Expand Up @@ -409,6 +409,61 @@ describe('raven.parsers', function() {
parsed.request.data.should.equal('{\"foo\":true}');
});
});

describe('`user` detection', function () {
it('should assign req.user to kwargs', function () {
var mockReq = {
method: 'POST',
hostname: 'example.org',
url: '/some/path?key=value',
user: {
username: 'janedoe',
email: 'hello@janedoe.com'
}
};

var parsed = raven.parsers.parseRequest(mockReq);
parsed.should.have.property('user', {
username: 'janedoe',
email: 'hello@janedoe.com'
});
});

it('should NOT assign req.user if already present in kwargs', function () {
var mockReq = {
method: 'POST',
hostname: 'example.org',
url: '/some/path?key=value',
user: {
username: 'janedoe',
email: 'hello@janedoe.com'
}
};

var parsed = raven.parsers.parseRequest(mockReq, { user: {} });
parsed.should.have.property('user', {});
});

it('should add ip address to user if available', function () {
var mockReq = {
method: 'POST',
hostname: 'example.org',
url: '/some/path?key=value',
ip: '127.0.0.1',
user: {
username: 'janedoe',
email: 'hello@janedoe.com'
}
};

var parsed = raven.parsers.parseRequest(mockReq);
parsed.should.have.property('user', {
username: 'janedoe',
email: 'hello@janedoe.com',
ip_address: '127.0.0.1'
});
});
});
});

describe('#parseError()', function() {
Expand Down

0 comments on commit c1035b5

Please sign in to comment.