-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(plugin-common): fix wrong authorization
- Loading branch information
Showing
3 changed files
with
205 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,205 @@ | ||
import { MockedApp } from 'koishi-test-utils' | ||
import { App } from 'koishi-test-utils' | ||
import { sleep } from 'koishi-utils' | ||
import { createUser, Meta } from 'koishi-core' | ||
import { createUser } from 'koishi-core' | ||
import { authorize, AuthorizeOptions } from '../src' | ||
import 'koishi-database-memory' | ||
|
||
const app = new MockedApp({ database: { memory: {} } }) | ||
|
||
// make coverage happy | ||
app.plugin(authorize) | ||
app.plugin<AuthorizeOptions>(authorize, { | ||
authorizeUser: { | ||
123: 2, | ||
231: 2, | ||
312: 2, | ||
}, | ||
authorizeGroup: { | ||
123: 1, | ||
231: { | ||
admin: 2, | ||
owner: 3, | ||
let counter = 0 | ||
|
||
function createApp () { | ||
return new App({ database: { memory: { identifier: ++counter } } }) | ||
} | ||
|
||
test('authorize user', async () => { | ||
const app = createApp() | ||
|
||
// make coverage happy | ||
app.plugin(authorize) | ||
app.plugin<AuthorizeOptions>(authorize, { | ||
authorizeUser: { | ||
123: 2, | ||
231: 2, | ||
312: 2, | ||
}, | ||
312: 3, | ||
}, | ||
}) | ||
}) | ||
|
||
// mock group | ||
app.setResponse('get_group_member_list', (params) => { | ||
let data = [] | ||
if (params.group_id === 123) { | ||
data = [ | ||
{ user_id: 564, role: 'member' }, | ||
] | ||
} else if (params.group_id === 231) { | ||
data = [ | ||
{ user_id: 123, role: 'member' }, | ||
{ user_id: 564, role: 'admin' }, | ||
{ user_id: 645, role: 'owner' }, | ||
] | ||
} | ||
return { data } | ||
}) | ||
app.receiver.on('connect', () => { | ||
app.database.memory.store.user[231] = createUser(231, 3) | ||
app.database.memory.store.user[312] = createUser(312, 2) | ||
}) | ||
|
||
await app.start() | ||
await sleep(0) | ||
|
||
app.receiver.on('connect', () => { | ||
app.database.memory.store.user[231] = createUser(231, 3) | ||
app.database.memory.store.user[312] = createUser(312, 2) | ||
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) | ||
}) | ||
|
||
beforeAll(async () => { | ||
test('authorize group 1', async () => { | ||
const app = createApp() | ||
|
||
app.plugin<AuthorizeOptions>(authorize, { | ||
authorizeGroup: { | ||
456: 2, | ||
}, | ||
}) | ||
|
||
app.receiver.on('connect', () => { | ||
app.database.memory.store.user[231] = createUser(231, 3) | ||
}) | ||
|
||
app.setResponse('get_group_member_list', [ | ||
{ userId: 123, role: 'member' }, | ||
{ userId: 231, role: 'member' }, | ||
]) | ||
|
||
await app.start() | ||
await sleep(0) | ||
}) | ||
|
||
test('basic support', 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', 2) | ||
await expect(app.database.getUser(312)).resolves.toHaveProperty('authority', 2) | ||
await expect(app.database.getGroup(123)).resolves.toHaveProperty('assignee', app.selfId) | ||
await expect(app.database.getGroup(231)).resolves.toHaveProperty('assignee', app.selfId) | ||
await expect(app.database.getGroup(312)).resolves.toHaveProperty('assignee', app.selfId) | ||
await expect(app.database.getUser(564)).resolves.toHaveProperty('authority', 2) | ||
await expect(app.database.getUser(645)).resolves.toHaveProperty('authority', 3) | ||
await expect(app.database.getUser(231)).resolves.toHaveProperty('authority', 3) | ||
}) | ||
|
||
const createGroupIncrease = (userId: number, groupId: number): Meta => ({ | ||
postType: 'notice', | ||
noticeType: 'group_increase', | ||
subType: 'invite', | ||
userId, | ||
groupId, | ||
test('authorize group 2', async () => { | ||
const app = createApp() | ||
|
||
app.plugin<AuthorizeOptions>(authorize, { | ||
authorizeGroup: { | ||
456: { admin: 2, owner: 3 }, | ||
}, | ||
}) | ||
|
||
app.setResponse('get_group_member_list', [ | ||
{ userId: 123, role: 'member' }, | ||
{ userId: 231, role: 'admin' }, | ||
{ userId: 312, role: 'owner' }, | ||
]) | ||
|
||
await app.start() | ||
await sleep(0) | ||
|
||
await expect(app.database.getGroup(456)).resolves.toHaveProperty('assignee', app.selfId) | ||
await expect(app.database.getUser(123)).resolves.toHaveProperty('authority', 1) | ||
await expect(app.database.getUser(231)).resolves.toHaveProperty('authority', 2) | ||
await expect(app.database.getUser(312)).resolves.toHaveProperty('authority', 3) | ||
}) | ||
|
||
describe('handle group_increase', () => { | ||
describe('handle group increase', () => { | ||
const app = createApp() | ||
|
||
app.plugin<AuthorizeOptions>(authorize, { | ||
authorizeGroup: { | ||
456: 2, | ||
}, | ||
}) | ||
|
||
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', []) | ||
|
||
beforeAll(async () => { | ||
await app.start() | ||
await sleep(0) | ||
}) | ||
|
||
test('create new user', async () => { | ||
app.receive(createGroupIncrease(456, 231)) | ||
app.receiveGroupIncrease('approve', 123, 456) | ||
await sleep(0) | ||
await expect(app.database.getUser(456)).resolves.toHaveProperty('authority', 1) | ||
await expect(app.database.getUser(123)).resolves.toHaveProperty('authority', 2) | ||
}) | ||
|
||
test('not affect higher authority', async () => { | ||
app.receive(createGroupIncrease(312, 231)) | ||
app.receiveGroupIncrease('approve', 231, 456) | ||
await sleep(0) | ||
await expect(app.database.getUser(312)).resolves.toHaveProperty('authority', 2) | ||
await expect(app.database.getUser(231)).resolves.toHaveProperty('authority', 3) | ||
}) | ||
|
||
test('overwrite lower authority', async () => { | ||
app.receive(createGroupIncrease(564, 312)) | ||
app.receiveGroupIncrease('approve', 312, 456) | ||
await sleep(0) | ||
await expect(app.database.getUser(564)).resolves.toHaveProperty('authority', 3) | ||
await expect(app.database.getUser(312)).resolves.toHaveProperty('authority', 2) | ||
}) | ||
|
||
test('skip unregistered groups', async () => { | ||
app.receive(createGroupIncrease(789, 789)) | ||
app.receiveGroupIncrease('approve', 789, 789) | ||
await sleep(0) | ||
await expect(app.database.getUser(789)).resolves.toHaveProperty('authority', 0) | ||
}) | ||
}) | ||
|
||
test('handle group-admin/set', async () => { | ||
app.receive({ | ||
postType: 'notice', | ||
noticeType: 'group_admin', | ||
subType: 'set', | ||
userId: 456, | ||
groupId: 231, | ||
describe('handle group admin set', () => { | ||
const app = createApp() | ||
|
||
app.plugin<AuthorizeOptions>(authorize, { | ||
authorizeGroup: { | ||
456: { admin: 2 }, | ||
}, | ||
}) | ||
|
||
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' }, | ||
]) | ||
|
||
beforeAll(async () => { | ||
await app.start() | ||
await sleep(0) | ||
}) | ||
|
||
test('not affect higher authority', async () => { | ||
app.receiveGroupAdmin('set', 231, 456) | ||
await sleep(0) | ||
await expect(app.database.getUser(231)).resolves.toHaveProperty('authority', 3) | ||
}) | ||
|
||
test('overwrite lower authority', async () => { | ||
app.receiveGroupAdmin('set', 123, 456) | ||
await sleep(0) | ||
await expect(app.database.getUser(123)).resolves.toHaveProperty('authority', 2) | ||
}) | ||
|
||
test('skip unregistered groups', async () => { | ||
app.receiveGroupAdmin('set', 312, 789) | ||
await sleep(0) | ||
await expect(app.database.getUser(312)).resolves.toHaveProperty('authority', 1) | ||
}) | ||
}) | ||
|
||
test('mixed usage', async () => { | ||
const app = createApp() | ||
|
||
app.plugin<AuthorizeOptions>(authorize, { | ||
authorizeUser: { | ||
123: 2, | ||
}, | ||
authorizeGroup: { | ||
456: 1, | ||
564: 3, | ||
}, | ||
}) | ||
|
||
app.receiver.on('connect', () => { | ||
app.database.memory.store.user[231] = createUser(231, 2) | ||
}) | ||
|
||
app.setResponse('get_group_member_list', [ | ||
{ userId: 123, role: 'member' }, | ||
{ userId: 231, role: 'member' }, | ||
]) | ||
|
||
await app.start() | ||
await sleep(0) | ||
await expect(app.database.getUser(456)).resolves.toHaveProperty('authority', 2) | ||
|
||
await expect(app.database.getUser(123)).resolves.toHaveProperty('authority', 3) | ||
await expect(app.database.getUser(231)).resolves.toHaveProperty('authority', 3) | ||
}) |