Skip to content

Commit

Permalink
tests: added tests for api method deactivation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Clement committed Apr 28, 2017
1 parent 54dc9eb commit 711f4db
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/errors/aggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ function _collect(transaction, exception, customParameters, timestamp) {

// add error event
if (this.config.error_collector.capture_events === true) {
this.events.add(createEvent(transaction, error, timestamp))
this.events.add(createEvent(transaction, error, timestamp, this.config))
}
return true
}
Expand Down
26 changes: 18 additions & 8 deletions lib/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ function createError(transaction, exception, customParameters, config) {
// user, but it is a common pattern.
if (typeof exception === 'string') {
message = exception
} else if (exception !== null && typeof exception === 'object' && exception.message) {
} else if (
exception !== null &&
typeof exception === 'object' &&
exception.message &&
!config.high_security
) {
message = exception.message

if (exception.name) {
Expand Down Expand Up @@ -82,8 +87,8 @@ function createError(transaction, exception, customParameters, config) {
var stack = exception && exception.stack
if (stack) {
params.stack_trace = ('' + stack).split(/[\n\r]/g)
if (transaction.agent.config.high_security) {
params.stack_trace[0] = 'Error: <redacted>'
if (config.high_security) {
params.stack_trace[0] = exception.name + ': <redacted>'
}
}

Expand All @@ -100,13 +105,18 @@ function createError(transaction, exception, customParameters, config) {
* Creates a structure for error event that is sent to the collector.
* The error parameter is an output of the createError() function for a given exception.
*/
function createEvent(transaction, error, timestamp) {
function createEvent(transaction, error, timestamp, config) {
var message = error[2]
var errorClass = error[3]
var paramsFromError = error[4]

var intrinsicAttributes = _getErrorEventIntrinsicAttrs(transaction, errorClass, message,
timestamp)
var intrinsicAttributes = _getErrorEventIntrinsicAttrs(
transaction,
errorClass,
message,
timestamp,
config
)

// the error structure created by createError() already performs filtering of custom
// and agent attributes, so it is ok to just copy them
Expand All @@ -122,14 +132,14 @@ function createEvent(transaction, error, timestamp) {
return errorEvent
}

function _getErrorEventIntrinsicAttrs(transaction, errorClass, message, timestamp) {
function _getErrorEventIntrinsicAttrs(transaction, errorClass, message, timestamp, conf) {
// the server expects seconds instead of milliseconds
if (timestamp) timestamp = timestamp / 1000

var attributes = {
type: "TransactionError",
"error.class": errorClass,
"error.message": transaction.agent.config.high_security ? null : message,
"error.message": conf.high_security ? '' : message,
timestamp: timestamp
}

Expand Down
86 changes: 86 additions & 0 deletions test/unit/api/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,76 @@ describe("the New Relic agent API", function () {
expect(api.addCustomParameters).a('function')
})

describe("when adding custom parameters", function () {
it("should properly add custom parameters", function () {
helper.runInTransaction(agent, function (transaction) {
api.addCustomParameter('test', 1)
expect(transaction.trace.custom['test']).to.equal(1)
transaction.end()
})
})

it("should properly add mutliple custom parameters", function () {
helper.runInTransaction(agent, function (transaction) {
api.addCustomParameters({
'test': 1,
'second': 2
})
expect(transaction.trace.custom['test']).to.equal(1)
expect(transaction.trace.custom['second']).to.equal(2)
transaction.end()
})
})

it("should not add custom parameters when disabled", function () {
helper.runInTransaction(agent, function (transaction) {
agent.config.api.custom_parameters_enabled = false
api.addCustomParameter('test', 1)
expect(transaction.trace.custom['test']).to.equal(undefined)
agent.config.api.custom_parameters_enabled = true
transaction.end()
})
})

it("should not add mutliple custom parameters when disabled", function () {
helper.runInTransaction(agent, function (transaction) {
agent.config.api.custom_parameters_enabled = false
api.addCustomParameters({
'test': 1,
'second': 2
})
expect(transaction.trace.custom['test']).to.equal(undefined)
expect(transaction.trace.custom['second']).to.equal(undefined)
agent.config.api.custom_parameters_enabled = true
transaction.end()
})
})

it("should not add custom parameters when in high security mode", function () {
helper.runInTransaction(agent, function (transaction) {
agent.config.high_security = true
api.addCustomParameter('test', 1)
expect(transaction.trace.custom['test']).to.equal(undefined)
agent.config.high_security = false
transaction.end()
})
})

it("should not add mutliple custom parameters when in high security mode", function () {
helper.runInTransaction(agent, function (transaction) {
agent.config.high_security = true
api.addCustomParameters({
'test': 1,
'second': 2
})
expect(transaction.trace.custom['test']).to.equal(undefined)
expect(transaction.trace.custom['second']).to.equal(undefined)
agent.config.high_security = false
transaction.end()
})
})
})

describe("when explicitly naming transactions", function () {
describe("in the simplest case", function () {
var segment
Expand Down Expand Up @@ -561,6 +631,22 @@ describe("the New Relic agent API", function () {
expect(agent.errors.errors.length).equal(1)
})

it("should not add errors in high security mode", function () {
agent.config.high_security = true
expect(agent.errors.errors.length).equal(0)
api.noticeError(new TypeError('this test is bogus, man'))
expect(agent.errors.errors.length).equal(0)
agent.config.high_security = false
})

it("should not add errors when noticeErrors is disabled", function () {
agent.config.api.notice_error_enabled = false
expect(agent.errors.errors.length).equal(0)
api.noticeError(new TypeError('this test is bogus, man'))
expect(agent.errors.errors.length).equal(0)
agent.config.api.notice_error_enabled = true
})

it("should track custom parameters on error without a transaction", function () {
expect(agent.errors.errors.length).equal(0)
api.noticeError(new TypeError('this test is bogus, man'), {present : 'yep'})
Expand Down
12 changes: 12 additions & 0 deletions test/unit/api/custom-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ describe('The custom events API', function () {
expect(myEvent).to.exist()
})

it('does not collect events when high security mode is on', function () {
agent.config.high_security = true
api.recordCustomEvent('EventName', {key: 'value'})
expect(agent.customEvents.toArray().length).to.equal(0)
})

it('does not collect events when the endpoint is disabled in the config', function () {
agent.config.api.custom_events_enabled = false
api.recordCustomEvent('EventName', {key: 'value'})
expect(agent.customEvents.toArray().length).to.equal(0)
})

it('creates the proper intrinsic values when recorded', function () {
var when = Date.now()
api.recordCustomEvent('EventName', {key: 'value'})
Expand Down
18 changes: 18 additions & 0 deletions test/unit/errors/aggregator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ describe('agent attribute format', function () {

expect(params).deep.equals({})
})

it('redacts the error message in high security mode', function () {
agent.config.high_security = true
error.add(trans, new Error('this should not be here'), {a: 'AA'})
agent.errors.onTransactionFinished(trans, agent.metrics)

expect(error.errors[0][2]).to.equal('')
expect(error.errors[0][4].stack_trace[0]).to.equal('Error: <redacted>')
})
})

describe('display name', function () {
Expand Down Expand Up @@ -1577,6 +1586,15 @@ describe('error events', function() {
})
})

it('should omit the error message when in high security mode', function() {
agent.config.high_security = true
agent.errors.add(null, new Error('some error'))
var events = agent.errors.getEvents()
expect(events[0][0]['error.message']).to.equal('')
agent.config.high_security = false

})

it('not spill over reservoir size', function() {
if (agent) helper.unloadAgent(agent)
agent = helper.loadMockedAgent(null,
Expand Down
15 changes: 2 additions & 13 deletions test/unit/high-security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,8 @@ describe('high security mode', function () {
var config = new Config({'high_security': true})
config.transaction_tracer.record_sql = 'raw'
config.on('transaction_tracer.record_sql', function(value) {
value.should.equal('off')
config.transaction_tracer.record_sql.should.equal('off')
done()
})
config._applyHighSecurity()
})

it('should detect that record_sql is obfuscated', function (done) {
var config = new Config({'high_security': true})
config.transaction_tracer.record_sql = 'obfuscated'
config.on('transaction_tracer.record_sql', function(value) {
value.should.equal('off')
config.transaction_tracer.record_sql.should.equal('off')
value.should.equal('obfuscated')
config.transaction_tracer.record_sql.should.equal('obfuscated')
done()
})
config._applyHighSecurity()
Expand Down

0 comments on commit 711f4db

Please sign in to comment.