1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-24 19:48:17 +09:00
MisskeyIO/packages/backend/test/unit/RoleService.ts

279 lines
7.2 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
2023-02-02 10:26:59 +09:00
process.env.NODE_ENV = 'test';
import { jest } from '@jest/globals';
import { ModuleMocker } from 'jest-mock';
import { Test } from '@nestjs/testing';
import * as lolex from '@sinonjs/fake-timers';
2023-02-02 10:26:59 +09:00
import { GlobalModule } from '@/GlobalModule.js';
import { RoleService } from '@/core/RoleService.js';
import type { MiRole, RolesRepository, RoleAssignmentsRepository, UsersRepository, MiUser } from '@/models/index.js';
2023-02-02 10:26:59 +09:00
import { DI } from '@/di-symbols.js';
import { MetaService } from '@/core/MetaService.js';
import { genAidx } from '@/misc/id/aidx.js';
2023-04-04 17:32:09 +09:00
import { CacheService } from '@/core/CacheService.js';
import { IdService } from '@/core/IdService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { secureRndstr } from '@/misc/secure-rndstr.js';
import { sleep } from '../utils.js';
2023-02-02 10:26:59 +09:00
import type { TestingModule } from '@nestjs/testing';
import type { MockFunctionMetadata } from 'jest-mock';
const moduleMocker = new ModuleMocker(global);
describe('RoleService', () => {
let app: TestingModule;
let roleService: RoleService;
let usersRepository: UsersRepository;
let rolesRepository: RolesRepository;
let roleAssignmentsRepository: RoleAssignmentsRepository;
let metaService: jest.Mocked<MetaService>;
let clock: lolex.InstalledClock;
2023-02-02 10:26:59 +09:00
function createUser(data: Partial<MiUser> = {}) {
const un = secureRndstr(16);
2023-02-02 10:26:59 +09:00
return usersRepository.insert({
id: genAidx(new Date()),
2023-02-02 10:26:59 +09:00
createdAt: new Date(),
2023-02-02 14:28:29 +09:00
username: un,
usernameLower: un,
...data,
2023-02-02 10:26:59 +09:00
})
.then(x => usersRepository.findOneByOrFail(x.identifiers[0]));
}
function createRole(data: Partial<MiRole> = {}) {
2023-02-02 10:26:59 +09:00
return rolesRepository.insert({
id: genAidx(new Date()),
2023-02-02 10:26:59 +09:00
createdAt: new Date(),
updatedAt: new Date(),
lastUsedAt: new Date(),
description: '',
...data,
})
.then(x => rolesRepository.findOneByOrFail(x.identifiers[0]));
}
beforeEach(async () => {
clock = lolex.install({
now: new Date(),
shouldClearNativeTimers: true,
2023-02-02 18:08:34 +09:00
});
2023-02-02 10:26:59 +09:00
app = await Test.createTestingModule({
imports: [
GlobalModule,
],
providers: [
RoleService,
2023-04-04 17:32:09 +09:00
CacheService,
IdService,
GlobalEventService,
2023-02-02 10:26:59 +09:00
],
})
.useMocker((token) => {
if (token === MetaService) {
return { fetch: jest.fn() };
}
if (typeof token === 'function') {
const mockMetadata = moduleMocker.getMetadata(token) as MockFunctionMetadata<any, any>;
const Mock = moduleMocker.generateFromMetadata(mockMetadata);
return new Mock();
}
})
.compile();
app.enableShutdownHooks();
roleService = app.get<RoleService>(RoleService);
usersRepository = app.get<UsersRepository>(DI.usersRepository);
rolesRepository = app.get<RolesRepository>(DI.rolesRepository);
roleAssignmentsRepository = app.get<RoleAssignmentsRepository>(DI.roleAssignmentsRepository);
metaService = app.get<MetaService>(MetaService) as jest.Mocked<MetaService>;
});
afterEach(async () => {
clock.uninstall();
2023-02-02 10:26:59 +09:00
await Promise.all([
app.get(DI.metasRepository).delete({}),
usersRepository.delete({}),
rolesRepository.delete({}),
roleAssignmentsRepository.delete({}),
]);
2023-02-02 10:26:59 +09:00
await app.close();
});
describe('getUserPolicies', () => {
test('instance default policies', async () => {
2023-02-02 10:26:59 +09:00
const user = await createUser();
metaService.fetch.mockResolvedValue({
policies: {
canManageCustomEmojis: false,
},
2023-02-02 14:28:29 +09:00
} as any);
2023-02-02 10:26:59 +09:00
const result = await roleService.getUserPolicies(user.id);
2023-02-02 10:26:59 +09:00
expect(result.canManageCustomEmojis).toBe(false);
});
test('instance default policies 2', async () => {
2023-02-02 10:26:59 +09:00
const user = await createUser();
metaService.fetch.mockResolvedValue({
policies: {
canManageCustomEmojis: true,
},
2023-02-02 14:28:29 +09:00
} as any);
2023-02-02 10:26:59 +09:00
const result = await roleService.getUserPolicies(user.id);
2023-02-02 10:26:59 +09:00
expect(result.canManageCustomEmojis).toBe(true);
});
test('with role', async () => {
2023-02-02 10:26:59 +09:00
const user = await createUser();
const role = await createRole({
name: 'a',
policies: {
canManageCustomEmojis: {
useDefault: false,
priority: 0,
value: true,
},
},
});
await roleService.assign(user.id, role.id);
2023-02-02 10:26:59 +09:00
metaService.fetch.mockResolvedValue({
policies: {
canManageCustomEmojis: false,
},
2023-02-02 14:28:29 +09:00
} as any);
2023-02-02 10:26:59 +09:00
const result = await roleService.getUserPolicies(user.id);
2023-02-02 10:26:59 +09:00
expect(result.canManageCustomEmojis).toBe(true);
});
2023-02-02 14:28:29 +09:00
test('priority', async () => {
2023-02-02 18:06:23 +09:00
const user = await createUser();
const role1 = await createRole({
name: 'role1',
policies: {
driveCapacityMb: {
useDefault: false,
priority: 0,
value: 200,
},
},
});
const role2 = await createRole({
name: 'role2',
policies: {
driveCapacityMb: {
useDefault: false,
priority: 1,
value: 100,
},
},
});
await roleService.assign(user.id, role1.id);
await roleService.assign(user.id, role2.id);
2023-02-02 18:06:23 +09:00
metaService.fetch.mockResolvedValue({
policies: {
driveCapacityMb: 50,
},
} as any);
2023-02-02 18:06:23 +09:00
const result = await roleService.getUserPolicies(user.id);
2023-02-02 18:06:23 +09:00
expect(result.driveCapacityMb).toBe(100);
});
test('conditional role', async () => {
2023-02-02 14:28:29 +09:00
const user1 = await createUser({
createdAt: new Date(Date.now() - (1000 * 60 * 60 * 24 * 365)),
});
const user2 = await createUser({
createdAt: new Date(Date.now() - (1000 * 60 * 60 * 24 * 365)),
followersCount: 10,
});
await createRole({
2023-02-02 14:28:29 +09:00
name: 'a',
policies: {
canManageCustomEmojis: {
useDefault: false,
priority: 0,
value: true,
},
},
target: 'conditional',
condFormula: {
type: 'and',
values: [{
type: 'followersMoreThanOrEq',
value: 10,
}, {
type: 'createdMoreThan',
sec: 60 * 60 * 24 * 7,
}],
},
});
metaService.fetch.mockResolvedValue({
policies: {
canManageCustomEmojis: false,
},
} as any);
2023-02-02 14:28:29 +09:00
const user1Policies = await roleService.getUserPolicies(user1.id);
const user2Policies = await roleService.getUserPolicies(user2.id);
expect(user1Policies.canManageCustomEmojis).toBe(false);
expect(user2Policies.canManageCustomEmojis).toBe(true);
});
test('expired role', async () => {
const user = await createUser();
const role = await createRole({
name: 'a',
policies: {
canManageCustomEmojis: {
useDefault: false,
priority: 0,
value: true,
},
},
});
await roleService.assign(user.id, role.id, new Date(Date.now() + (1000 * 60 * 60 * 24)));
metaService.fetch.mockResolvedValue({
policies: {
canManageCustomEmojis: false,
},
} as any);
const result = await roleService.getUserPolicies(user.id);
expect(result.canManageCustomEmojis).toBe(true);
clock.tick('25:00:00');
const resultAfter25h = await roleService.getUserPolicies(user.id);
expect(resultAfter25h.canManageCustomEmojis).toBe(false);
await roleService.assign(user.id, role.id);
// ストリーミング経由で反映されるまでちょっと待つ
clock.uninstall();
await sleep(100);
const resultAfter25hAgain = await roleService.getUserPolicies(user.id);
expect(resultAfter25hAgain.canManageCustomEmojis).toBe(true);
});
2023-02-02 10:26:59 +09:00
});
});