@codefresh-io/audit
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -20,3 +20,3 @@ /* eslint global-require: 0 */ | ||
userEmail: 'testUserEmail', | ||
userName: 'testUserName' | ||
userName: 'testUserName', | ||
}, | ||
@@ -27,7 +27,7 @@ request: { | ||
query: { query: 1 }, | ||
params: { params: 1 } | ||
params: { params: 1 }, | ||
}, | ||
response: { | ||
status: 200 | ||
} | ||
status: 200, | ||
}, | ||
}; | ||
@@ -37,24 +37,32 @@ | ||
return { | ||
Audit: { | ||
create(data) { | ||
delete data.createdAt; | ||
expect(data).toEqual( | ||
{ | ||
'accountId': 'testAccountID', | ||
'action': 'testAction', | ||
'entity': 'testEntity', | ||
'entityId': 'testEntityId', | ||
'entityName': 'testEntityName', | ||
'req_body': '{"test":1}', | ||
'req_params': '{"params":1}', | ||
'req_query': '{"query":1}', | ||
'req_url': 'testUrl', | ||
'rs_status': 200, | ||
'userEmail': 'testUserEmail', | ||
'userId': 'testUserID', | ||
'userName': 'testUserName' | ||
} | ||
); | ||
} | ||
} | ||
createDatabaseIfNotExists() { | ||
return Promise.resolve(); | ||
}, | ||
getInstance() { | ||
return { | ||
Audit: { | ||
create(data) { | ||
delete data.createdAt; | ||
expect(data).toEqual( | ||
{ | ||
'accountId': 'testAccountID', | ||
'action': 'testAction', | ||
'entity': 'testEntity', | ||
'entityId': 'testEntityId', | ||
'entityName': 'testEntityName', | ||
'req_body': '{"test":1}', | ||
'req_params': '{"params":1}', | ||
'req_query': '{"query":1}', | ||
'req_url': 'testUrl', | ||
'rs_status': 200, | ||
'userEmail': 'testUserEmail', | ||
'userId': 'testUserID', | ||
'userName': 'testUserName', | ||
}, | ||
); | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; | ||
@@ -64,2 +72,3 @@ }); | ||
const audit = require('../../index'); | ||
await audit.init(); | ||
await audit.store(storeData); | ||
@@ -73,10 +82,17 @@ }); | ||
return { | ||
Audit: { | ||
findAndCountAll() { | ||
return Promise.resolve({ | ||
count: 1, | ||
rows: [{ _id: 'testID', action: 'testAction' }] | ||
}); | ||
} | ||
} | ||
createDatabaseIfNotExists() { | ||
return Promise.resolve(); | ||
}, | ||
getInstance() { | ||
return { | ||
Audit: { | ||
findAndCountAll() { | ||
return Promise.resolve({ | ||
count: 1, | ||
rows: [{ _id: 'testID', action: 'testAction' }], | ||
}); | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; | ||
@@ -86,2 +102,3 @@ }); | ||
const audit = require('../../index'); | ||
await audit.init(); | ||
const result = await audit.getAudit({}); | ||
@@ -91,3 +108,3 @@ | ||
rows: [{ _id: 'testID', action: 'testAction' }], | ||
count: 1 | ||
count: 1, | ||
}); | ||
@@ -101,7 +118,14 @@ }); | ||
return { | ||
Audit: { | ||
aggregate() { | ||
return [{ DISTINCT: 'userName1' }, { DISTINCT: 'userName2' }]; | ||
} | ||
} | ||
createDatabaseIfNotExists() { | ||
return Promise.resolve(); | ||
}, | ||
getInstance() { | ||
return { | ||
Audit: { | ||
aggregate() { | ||
return [{ DISTINCT: 'userName1' }, { DISTINCT: 'userName2' }]; | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; | ||
@@ -111,2 +135,3 @@ }); | ||
const audit = require('../../index'); | ||
await audit.init(); | ||
const result = await audit.getUserNames(); | ||
@@ -122,19 +147,27 @@ | ||
return { | ||
Audit: { | ||
findAll(opts) { | ||
expect(opts).toEqual({ | ||
where: { | ||
accountId: 'testAccId', | ||
action: { [require('sequelize').Op.ne]: 'receiveWebhook' } | ||
createDatabaseIfNotExists() { | ||
return Promise.resolve(); | ||
}, | ||
getInstance() { | ||
return { | ||
Audit: { | ||
findAll(opts) { | ||
expect(opts).toEqual({ | ||
where: { | ||
accountId: 'testAccId', | ||
action: { [require('sequelize').Op.ne]: 'receiveWebhook' }, | ||
}, | ||
attributes: ['entity', 'action'], | ||
distinct: true, | ||
raw: true, | ||
}); | ||
return Promise.resolve([ | ||
{ entity: 'entity1', action: 'action1' }, | ||
{ entity: 'entity2', action: 'action2' }, | ||
]); | ||
}, | ||
attributes: ['entity', 'action'], | ||
distinct: true, | ||
raw: true | ||
}); | ||
return Promise.resolve([ | ||
{ entity: 'entity1', action: 'action1' }, | ||
{ entity: 'entity2', action: 'action2' }, | ||
]); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
@@ -144,2 +177,3 @@ }); | ||
const audit = require('../../index'); | ||
await audit.init(); | ||
const result = await audit.getEntitiesList('testAccId', {}); | ||
@@ -149,3 +183,3 @@ | ||
entity1: ['action1'], | ||
entity2: ['action2'] | ||
entity2: ['action2'], | ||
}); | ||
@@ -157,16 +191,24 @@ }); | ||
return { | ||
Audit: { | ||
findAll(opts) { | ||
expect(opts).toEqual({ | ||
where: { accountId: 'testAccId', action: 'receiveWebhook' }, | ||
attributes: ['entity', 'action'], | ||
distinct: true, | ||
raw: true | ||
}); | ||
return Promise.resolve([ | ||
{ entity: 'entity1', action: 'action1' }, | ||
{ entity: 'entity2', action: 'action2' }, | ||
]); | ||
} | ||
} | ||
createDatabaseIfNotExists() { | ||
return Promise.resolve(); | ||
}, | ||
getInstance() { | ||
return { | ||
Audit: { | ||
findAll(opts) { | ||
expect(opts).toEqual({ | ||
where: { accountId: 'testAccId', action: 'receiveWebhook' }, | ||
attributes: ['entity', 'action'], | ||
distinct: true, | ||
raw: true, | ||
}); | ||
return Promise.resolve([ | ||
{ entity: 'entity1', action: 'action1' }, | ||
{ entity: 'entity2', action: 'action2' }, | ||
]); | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; | ||
@@ -176,2 +218,3 @@ }); | ||
const audit = require('../../index'); | ||
await audit.init(); | ||
const result = await audit.getEntitiesList('testAccId', { showTriggers: 'true' }); | ||
@@ -181,3 +224,3 @@ | ||
entity1: ['action1'], | ||
entity2: ['action2'] | ||
entity2: ['action2'], | ||
}); | ||
@@ -184,0 +227,0 @@ }); |
@@ -5,6 +5,16 @@ /* eslint class-methods-use-this: 0 */ | ||
const { Audit } = require('../models'); | ||
const models = require('../models'); | ||
let Audit = {}; | ||
class AuditDal { | ||
async init(needCreateDb) { | ||
if (needCreateDb) { | ||
await models.createDatabaseIfNotExists(); | ||
} | ||
({ Audit } = models.getInstance().Audit); | ||
} | ||
store(audit) { | ||
@@ -19,3 +29,6 @@ return Audit.create(audit); | ||
async getUserNames(accountId) { | ||
const data = await Audit.aggregate('userName', 'DISTINCT', { plain: false, where: { accountId } }); | ||
const data = await Audit.aggregate('userName', 'DISTINCT', { | ||
plain: false, | ||
where: { accountId }, | ||
}); | ||
return data.map(item => item.DISTINCT); | ||
@@ -37,3 +50,3 @@ } | ||
distinct: true, | ||
raw: true | ||
raw: true, | ||
}); | ||
@@ -40,0 +53,0 @@ } |
@@ -30,2 +30,6 @@ /* eslint class-methods-use-this: 0 */ | ||
init(needCreateDb = false) { | ||
return auditDal.init(needCreateDb); | ||
} | ||
getAudit({ accountId, qs }) { | ||
@@ -32,0 +36,0 @@ return auditDal.getAudit(queryBuilder.buildQuery(qs, accountId)); |
@@ -10,44 +10,67 @@ const fs = require('fs'); | ||
const db = {}; | ||
const createDatabaseIfNotExists = async () => { | ||
const sequelize = new Sequelize('postgres', pgConfig.user, pgConfig.password, { | ||
dialect: 'postgres', | ||
host: pgConfig.host, | ||
}); | ||
const sequelize = new Sequelize(pgConfig.database, pgConfig.user, pgConfig.password, { | ||
host: pgConfig.host, | ||
dialect: 'postgres', | ||
logging: false, | ||
pool: { | ||
max: pgConfig.max, | ||
min: 0, | ||
acquire: 30000, | ||
idle: pgConfig.idleTimeoutMillis, | ||
}, | ||
try { | ||
await sequelize.query(`CREATE DATABASE ${pgConfig.database};`); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}); | ||
return sequelize.close(); | ||
}; | ||
fs | ||
.readdirSync(__dirname) | ||
.filter((file) => { | ||
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); | ||
}) | ||
.forEach((file) => { | ||
const model = sequelize.import(path.join(__dirname, file)); | ||
db[model.name] = model; | ||
}); | ||
const getInstance = () => { | ||
const db = {}; | ||
Object.keys(db) | ||
.forEach((modelName) => { | ||
if (db[modelName].associate) { | ||
db[modelName].associate(db); | ||
} | ||
const sequelize = new Sequelize(pgConfig.database, pgConfig.user, pgConfig.password, { | ||
host: pgConfig.host, | ||
dialect: 'postgres', | ||
logging: false, | ||
pool: { | ||
max: pgConfig.max, | ||
min: 0, | ||
acquire: 30000, | ||
idle: pgConfig.idleTimeoutMillis, | ||
}, | ||
}); | ||
fs | ||
.readdirSync(__dirname) | ||
.filter((file) => { | ||
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); | ||
}) | ||
.forEach((file) => { | ||
const model = sequelize.import(path.join(__dirname, file)); | ||
db[model.name] = model; | ||
}); | ||
Object.keys(db) | ||
.forEach((modelName) => { | ||
db[modelName].sync({ alter: true }); | ||
}); | ||
Object.keys(db) | ||
.forEach((modelName) => { | ||
if (db[modelName].associate) { | ||
db[modelName].associate(db); | ||
} | ||
}); | ||
db.sequelize = sequelize; | ||
db.Sequelize = Sequelize; | ||
module.exports = db; | ||
Object.keys(db) | ||
.forEach((modelName) => { | ||
db[modelName].sync({ alter: true }); | ||
}); | ||
db.sequelize = sequelize; | ||
db.Sequelize = Sequelize; | ||
return db; | ||
}; | ||
module.exports = { | ||
getInstance, | ||
createDatabaseIfNotExists, | ||
}; |
{ | ||
"name": "@codefresh-io/audit", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "Receive and collect audit logs", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
202552
572