|
scenario('A Base model being created without a transaction', { |
|
dbFixtures: [], |
|
googleFixtures: null |
|
}, function () { |
|
it('receives an assertion error', function (done) { |
|
var user = User.build({email: '[email protected]'}); |
|
return user.save({_sourceType: 'server'}).asCallback(function handleSave (err) { |
|
expect(err).to.not.equal(null); |
|
expect(err.message).to.contain('All create/update/delete actions must be run in a transaction'); |
|
done(); |
|
}); |
|
}); |
|
}); |
|
|
|
scenario('A Base model being created with a transaction', { |
|
dbFixtures: [], |
|
googleFixtures: null |
|
}, function () { |
|
before(function createUser (done) { |
|
var user = User.build({email: '[email protected]'}); |
|
User.sequelize.transaction(function handleTransaction (t) { |
|
return user.save({_sourceType: 'server', transaction: t}); |
|
}).asCallback(done); |
|
}); |
|
|
|
it('is saved to an audit log', function (done) { |
|
// Assert source user, table, id, previous, and new data |
|
AuditLog.findAll().asCallback(function handleAuditLogs (err, auditLogs) { |
|
if (err) { return done(err); } |
|
expect(auditLogs).to.have.length(1); |
|
expect(auditLogs[0].get('source_type')).to.equal('server'); |
|
expect(auditLogs[0].get('source_id')).to.equal(null); |
|
expect(auditLogs[0].get('table_name')).to.equal('users'); |
|
expect(auditLogs[0].get('table_row_id')).to.be.a('String'); |
|
expect(auditLogs[0].get('action')).to.equal('create'); |
|
expect(auditLogs[0].get('timestamp')).to.be.a('Date'); |
|
expect(auditLogs[0].get('previous_values')).to.deep.equal({}); |
|
expect(auditLogs[0].get('current_values')).to.have.property('email', |
|
'[email protected]'); |
|
expect(auditLogs[0].get('transaction_id')).to.be.a('String'); |
|
done(); |
|
}); |
|
}); |
|
}); |
|
|
|
scenario('A Base model being updated without a transaction', { |
|
dbFixtures: ['user-default'], |
|
googleFixtures: null |
|
}, function () { |
|
it('receives an assertion error', function (done) { |
|
User.find().asCallback(function handleFind (err, user) { |
|
if (err) { return done(err); } |
|
user.update({ |
|
email: '[email protected]' |
|
}, { |
|
_sourceType: 'users', |
|
_sourceId: user.get('id'), |
|
transaction: t |
|
}).asCallback(function handleUpdate (err) { |
|
expect(err).to.not.equal(null); |
|
expect(err.message).to.contain('All create/update/delete actions must be run in a transaction'); |
|
done(); |
|
}); |
|
}); |
|
}); |
|
}); |
|
|
|
scenario('A Base model being updated with a transaction', { |
|
dbFixtures: ['user-default'], |
|
googleFixtures: null |
|
}, function () { |
|
before(function updateUser (done) { |
|
User.find().asCallback(function handleFind (err, user) { |
|
if (err) { return done(err); } |
|
User.sequelize.transaction(function handleTransaction (t) { |
|
return user.update({ |
|
email: '[email protected]' |
|
}, { |
|
_sourceType: 'users', |
|
_sourceId: user.get('id'), |
|
transaction: t |
|
}); |
|
}).asCallback(done); |
|
}); |
|
}); |
|
|
|
it('is saved to an audit log', function (done) { |
|
// Assert source user, table, id, previous, and new data |
|
AuditLog.findAll({where: {source_type: 'users'}}).asCallback(function handleAuditLogs (err, auditLogs) { |
|
if (err) { return done(err); } |
|
expect(auditLogs).to.have.length(1); |
|
expect(auditLogs[0].get('source_type')).to.equal('users'); |
|
expect(auditLogs[0].get('source_id')).to.be.a('String'); |
|
expect(auditLogs[0].get('table_name')).to.equal('users'); |
|
expect(auditLogs[0].get('table_row_id')).to.be.a('String'); |
|
expect(auditLogs[0].get('action')).to.equal('update'); |
|
expect(auditLogs[0].get('timestamp')).to.be.a('Date'); |
|
expect(auditLogs[0].get('previous_values')).to.have.property('email', |
|
'[email protected]'); |
|
expect(auditLogs[0].get('current_values')).to.have.property('email', |
|
'[email protected]'); |
|
expect(auditLogs[0].get('transaction_id')).to.be.a('String'); |
|
done(); |
|
}); |
|
}); |
|
}); |
|
|
|
scenario('A Base model being deleted without a transaction', { |
|
dbFixtures: ['user-default'], |
|
googleFixtures: null |
|
}, function () { |
|
it('receives an assertion error', function (done) { |
|
User.find().asCallback(function handleFind (err, user) { |
|
if (err) { return done(err); } |
|
user.destroy({ |
|
_sourceType: 'users', |
|
_sourceId: user.get('id'), |
|
}).asCallback(function handleSave (err) { |
|
expect(err).to.not.equal(null); |
|
expect(err.message).to.contain('All create/update/delete actions must be run in a transaction'); |
|
done(); |
|
}); |
|
}); |
|
}); |
|
}); |
|
|
|
scenario('A Base model being deleted with a transaction', { |
|
dbFixtures: ['user-default'], |
|
googleFixtures: null |
|
}, function () { |
|
before(function deleteUser (done) { |
|
User.find().asCallback(function handleFind (err, user) { |
|
if (err) { return done(err); } |
|
User.sequelize.transaction(function handleTransaction (t) { |
|
return user.destroy({ |
|
_sourceType: 'users', |
|
_sourceId: user.get('id'), |
|
transaction: t |
|
}); |
|
}).asCallback(done); |
|
}); |
|
}); |
|
|
|
it('is saved to an audit log', function (done) { |
|
// Assert source user, table, id, previous, and new data |
|
AuditLog.findAll({where: {source_type: 'users'}}).asCallback(function handleAuditLogs (err, auditLogs) { |
|
if (err) { return done(err); } |
|
expect(auditLogs).to.have.length(1); |
|
expect(auditLogs[0].get('source_type')).to.equal('users'); |
|
expect(auditLogs[0].get('source_id')).to.be.a('String'); |
|
expect(auditLogs[0].get('table_name')).to.equal('users'); |
|
expect(auditLogs[0].get('table_row_id')).to.be.a('String'); |
|
expect(auditLogs[0].get('action')).to.equal('delete'); |
|
expect(auditLogs[0].get('timestamp')).to.be.a('Date'); |
|
expect(auditLogs[0].get('previous_values')).to.have.property('email', |
|
'[email protected]'); |
|
expect(auditLogs[0].get('current_values')).to.have.property('email', |
|
'[email protected]'); |
|
expect(auditLogs[0].get('transaction_id')).to.be.a('String'); |
|
done(); |
|
}); |
|
}); |
|
}); |
|
|
|
// http://docs.sequelizejs.com/en/v3/docs/instances/#working-in-bulk-creating-updating-and-destroying-multiple-rows-at-once |
|
scenario('A Base model being bulk created', { |
|
dbFixtures: [], |
|
googleFixtures: null |
|
}, function () { |
|
it('is rejected due to lack of support', function (done) { |
|
User.bulkCreate([ |
|
{email: '[email protected]'} |
|
]).asCallback(function handleBulkCreate (err, users) { |
|
expect(err.message).to.contain('Audit logging not supported for bulk creation'); |
|
done(); |
|
}); |
|
}); |
|
}); |
|
scenario('A Base model being bulk updated', { |
|
dbFixtures: [], |
|
googleFixtures: null |
|
}, function () { |
|
it('is rejected due to lack of support', function (done) { |
|
User.update({ |
|
email: '[email protected]' |
|
}, { |
|
where: {email: '[email protected]'} |
|
}).asCallback(function handleBulkUpdate (err, users) { |
|
expect(err.message).to.contain('Audit logging not supported for bulk updates'); |
|
done(); |
|
}); |
|
}); |
|
}); |
|
scenario('A Base model being bulk deleted', { |
|
dbFixtures: [], |
|
googleFixtures: null |
|
}, function () { |
|
it('is rejected due to lack of support', function (done) { |
|
User.destroy({ |
|
where: {email: '[email protected]'} |
|
}).asCallback(function handleBulkDelete (err, users) { |
|
expect(err.message).to.contain('Audit logging not supported for bulk deletion'); |
|
done(); |
|
}); |
|
}); |
|
}); |
|
|
|
// DEV: Test is currently skipped due to no items in database |
|
scenario.skip('A Base model being created with a user source', { |
|
dbFixtures: [], |
|
googleFixtures: null |
|
}, function () { |
|
before(function createItem (done) { |
|
var item = Item.build({name: 'User source item'}); |
|
item._sourceType = 'users'; |
|
item._sourceId = 'mock-user-id'; |
|
item.save().asCallback(done); |
|
}); |
|
|
|
it('saves user source to its audit log', function (done) { |
|
AuditLog.findAll().asCallback(function handleAuditLogs (err, auditLogs) { |
|
if (err) { return done(err); } |
|
expect(auditLogs).to.have.length(1); |
|
expect(auditLogs[0].get('source_type')).to.equal('users'); |
|
expect(auditLogs[0].get('source_id')).to.equal('mock-user-id'); |
|
done(); |
|
}); |
|
}); |
|
}); |