Skip to content

Commit

Permalink
feat(plugin-common): add authority logger
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 29, 2020
1 parent 2f36aca commit fb7dbe6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/plugin-common/src/authorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ export default function apply (ctx: Context, config: AuthorizeOptions = {}) {

await Promise.all(tasks.map(task => task.catch(logger.warn)))

let totalInsert = 0, totalUpdate = 0
let insertTotal = 0, updateTotal = 0
for (const key in authorizeInfoList) {
const authority = +key
const { insert, update } = authorizeInfoList[key]
totalInsert += insert.size
totalUpdate += update.size
insertTotal += insert.size
updateTotal += update.size
for (const id of insert) {
await database.getUser(id, authority)
logger.debug(`inserted ${id} with authority ${authority}`)
Expand All @@ -133,6 +133,13 @@ export default function apply (ctx: Context, config: AuthorizeOptions = {}) {
}
}

logger.info(`inserted ${totalInsert} user(s) and updated ${totalUpdate} user(s)`)
const output: string[] = []
if (insertTotal) output.push(`inserted ${insertTotal} user${insertTotal > 1 ? 's' : ''}`)
if (updateTotal) output.push(`updated ${updateTotal} user${updateTotal > 1 ? 's' : ''}`)
if (!output.length) {
logger.info('all users are up to date')
} else {
logger.info(output.join(' and '))
}
})
}
8 changes: 7 additions & 1 deletion packages/plugin-common/tests/authorize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('authorize user', async () => {

app.receiver.on('connect', () => {
app.database.memory.store.user[231] = createUser(231, 3)
app.database.memory.store.user[312] = createUser(312, 2)
app.database.memory.store.user[312] = createUser(312, 1)
})

await app.start()
Expand All @@ -47,11 +47,13 @@ test('authorize group 1', async () => {

app.receiver.on('connect', () => {
app.database.memory.store.user[231] = createUser(231, 3)
app.database.memory.store.user[312] = createUser(312, 1)
})

app.setResponse('get_group_member_list', [
{ userId: 123, role: 'member' },
{ userId: 231, role: 'member' },
{ userId: 312, role: 'member' },
])

await app.start()
Expand All @@ -60,6 +62,7 @@ test('authorize group 1', async () => {
await expect(app.database.getGroup(456)).resolves.toHaveProperty('assignee', app.selfId)
await expect(app.database.getUser(123)).resolves.toHaveProperty('authority', 2)
await expect(app.database.getUser(231)).resolves.toHaveProperty('authority', 3)
await expect(app.database.getUser(312)).resolves.toHaveProperty('authority', 2)
})

test('authorize group 2', async () => {
Expand Down Expand Up @@ -190,16 +193,19 @@ test('mixed usage', async () => {

app.receiver.on('connect', () => {
app.database.memory.store.user[231] = createUser(231, 2)
app.database.memory.store.user[312] = createUser(312, 1)
})

app.setResponse('get_group_member_list', [
{ userId: 123, role: 'member' },
{ userId: 231, role: 'member' },
{ userId: 312, role: 'member' },
])

await app.start()
await sleep(0)

await expect(app.database.getUser(123)).resolves.toHaveProperty('authority', 3)
await expect(app.database.getUser(231)).resolves.toHaveProperty('authority', 3)
await expect(app.database.getUser(312)).resolves.toHaveProperty('authority', 3)
})

0 comments on commit fb7dbe6

Please sign in to comment.