2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-14 00:59:27 +09:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-02-02 10:26:59 +09:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2024-07-02 14:47:07 +09:00
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2023-02-02 10:26:59 +09:00
|
|
|
import { jest } from '@jest/globals';
|
|
|
|
import { ModuleMocker } from 'jest-mock';
|
|
|
|
import { Test } from '@nestjs/testing';
|
2023-03-01 10:20:03 +09:00
|
|
|
import * as lolex from '@sinonjs/fake-timers';
|
2024-10-11 20:59:36 +09:00
|
|
|
import type { TestingModule } from '@nestjs/testing';
|
|
|
|
import type { MockFunctionMetadata } from 'jest-mock';
|
2023-02-02 10:26:59 +09:00
|
|
|
import { GlobalModule } from '@/GlobalModule.js';
|
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2024-06-08 15:34:19 +09:00
|
|
|
import {
|
2024-09-22 12:53:13 +09:00
|
|
|
MiMeta,
|
2024-06-08 15:34:19 +09:00
|
|
|
MiRole,
|
|
|
|
MiRoleAssignment,
|
|
|
|
MiUser,
|
|
|
|
RoleAssignmentsRepository,
|
|
|
|
RolesRepository,
|
|
|
|
UsersRepository,
|
|
|
|
} from '@/models/_.js';
|
2023-02-02 10:26:59 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2023-09-06 18:33:51 +09:00
|
|
|
import { genAidx } from '@/misc/id/aidx.js';
|
2023-04-04 17:32:09 +09:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2023-03-01 10:20:03 +09:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2023-06-25 11:04:33 +09:00
|
|
|
import { secureRndstr } from '@/misc/secure-rndstr.js';
|
2023-12-21 10:39:11 +09:00
|
|
|
import { NotificationService } from '@/core/NotificationService.js';
|
2024-04-19 15:22:23 +09:00
|
|
|
import { RoleCondFormulaValue } from '@/models/Role.js';
|
2024-06-08 15:34:19 +09:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2023-02-02 10:26:59 +09:00
|
|
|
|
|
|
|
const moduleMocker = new ModuleMocker(global);
|
|
|
|
|
|
|
|
describe('RoleService', () => {
|
|
|
|
let app: TestingModule;
|
|
|
|
let roleService: RoleService;
|
|
|
|
let usersRepository: UsersRepository;
|
|
|
|
let rolesRepository: RolesRepository;
|
|
|
|
let roleAssignmentsRepository: RoleAssignmentsRepository;
|
2024-09-22 12:53:13 +09:00
|
|
|
let meta: jest.Mocked<MiMeta>;
|
2023-12-21 10:39:11 +09:00
|
|
|
let notificationService: jest.Mocked<NotificationService>;
|
2023-03-01 10:20:03 +09:00
|
|
|
let clock: lolex.InstalledClock;
|
2023-02-02 10:26:59 +09:00
|
|
|
|
2024-06-08 15:34:19 +09:00
|
|
|
async function createUser(data: Partial<MiUser> = {}) {
|
2023-06-25 11:04:33 +09:00
|
|
|
const un = secureRndstr(16);
|
2024-06-08 15:34:19 +09:00
|
|
|
const x = await usersRepository.insert({
|
2023-10-16 12:58:17 +09:00
|
|
|
id: genAidx(Date.now()),
|
2023-02-02 14:28:29 +09:00
|
|
|
username: un,
|
|
|
|
usernameLower: un,
|
|
|
|
...data,
|
2024-06-08 15:34:19 +09:00
|
|
|
});
|
|
|
|
return await usersRepository.findOneByOrFail(x.identifiers[0]);
|
2023-02-02 10:26:59 +09:00
|
|
|
}
|
|
|
|
|
2024-06-08 15:34:19 +09:00
|
|
|
async function createRole(data: Partial<MiRole> = {}) {
|
|
|
|
const x = await rolesRepository.insert({
|
2023-10-16 12:58:17 +09:00
|
|
|
id: genAidx(Date.now()),
|
2023-02-02 10:26:59 +09:00
|
|
|
updatedAt: new Date(),
|
|
|
|
lastUsedAt: new Date(),
|
2024-04-19 15:22:23 +09:00
|
|
|
name: '',
|
2023-02-02 10:26:59 +09:00
|
|
|
description: '',
|
|
|
|
...data,
|
2024-06-08 15:34:19 +09:00
|
|
|
});
|
|
|
|
return await rolesRepository.findOneByOrFail(x.identifiers[0]);
|
2023-02-02 10:26:59 +09:00
|
|
|
}
|
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
function createConditionalRole(condFormula: RoleCondFormulaValue, data: Partial<MiRole> = {}) {
|
|
|
|
return createRole({
|
|
|
|
name: `[conditional] ${condFormula.type}`,
|
|
|
|
target: 'conditional',
|
|
|
|
condFormula: condFormula,
|
|
|
|
...data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-08 15:34:19 +09:00
|
|
|
async function assignRole(args: Partial<MiRoleAssignment>) {
|
|
|
|
const id = genAidx(Date.now());
|
|
|
|
const expiresAt = new Date();
|
|
|
|
expiresAt.setDate(expiresAt.getDate() + 1);
|
|
|
|
|
|
|
|
await roleAssignmentsRepository.insert({
|
|
|
|
id,
|
|
|
|
expiresAt,
|
|
|
|
...args,
|
|
|
|
});
|
|
|
|
|
|
|
|
return await roleAssignmentsRepository.findOneByOrFail({ id });
|
|
|
|
}
|
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
function aidx() {
|
|
|
|
return genAidx(Date.now());
|
|
|
|
}
|
|
|
|
|
2023-03-01 10:20:03 +09:00
|
|
|
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,
|
2023-03-01 10:20:03 +09:00
|
|
|
IdService,
|
|
|
|
GlobalEventService,
|
2024-04-19 15:22:23 +09:00
|
|
|
UserEntityService,
|
2023-12-22 09:06:13 +09:00
|
|
|
{
|
|
|
|
provide: NotificationService,
|
|
|
|
useFactory: () => ({
|
|
|
|
createNotification: jest.fn(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: NotificationService.name,
|
|
|
|
useExisting: NotificationService,
|
|
|
|
},
|
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);
|
|
|
|
|
2024-09-22 12:53:13 +09:00
|
|
|
meta = app.get<MiMeta>(DI.meta) as jest.Mocked<MiMeta>;
|
2023-12-21 10:39:11 +09:00
|
|
|
notificationService = app.get<NotificationService>(NotificationService) as jest.Mocked<NotificationService>;
|
2023-12-22 09:06:13 +09:00
|
|
|
|
|
|
|
await roleService.onModuleInit();
|
2023-02-02 10:26:59 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2023-03-01 10:20:03 +09:00
|
|
|
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-03-01 10:20:03 +09:00
|
|
|
|
2023-02-02 10:26:59 +09:00
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getUserPolicies', () => {
|
2023-06-25 11:04:33 +09:00
|
|
|
test('instance default policies', async () => {
|
2023-02-02 10:26:59 +09:00
|
|
|
const user = await createUser();
|
2024-09-22 12:53:13 +09:00
|
|
|
meta.policies = {
|
|
|
|
canManageCustomEmojis: false,
|
|
|
|
};
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-02-02 10:26:59 +09:00
|
|
|
const result = await roleService.getUserPolicies(user.id);
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-02-02 10:26:59 +09:00
|
|
|
expect(result.canManageCustomEmojis).toBe(false);
|
|
|
|
});
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-03-01 10:20:03 +09:00
|
|
|
test('instance default policies 2', async () => {
|
2023-02-02 10:26:59 +09:00
|
|
|
const user = await createUser();
|
2024-09-22 12:53:13 +09:00
|
|
|
meta.policies = {
|
|
|
|
canManageCustomEmojis: true,
|
|
|
|
};
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-02-02 10:26:59 +09:00
|
|
|
const result = await roleService.getUserPolicies(user.id);
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-02-02 10:26:59 +09:00
|
|
|
expect(result.canManageCustomEmojis).toBe(true);
|
|
|
|
});
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-03-01 10:20:03 +09:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-03-01 10:20:03 +09:00
|
|
|
await roleService.assign(user.id, role.id);
|
2024-09-22 12:53:13 +09:00
|
|
|
meta.policies = {
|
|
|
|
canManageCustomEmojis: false,
|
|
|
|
};
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-02-02 10:26:59 +09:00
|
|
|
const result = await roleService.getUserPolicies(user.id);
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-02-02 10:26:59 +09:00
|
|
|
expect(result.canManageCustomEmojis).toBe(true);
|
|
|
|
});
|
2023-02-02 14:28:29 +09:00
|
|
|
|
2023-03-01 10:20:03 +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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-03-01 10:20:03 +09:00
|
|
|
await roleService.assign(user.id, role1.id);
|
|
|
|
await roleService.assign(user.id, role2.id);
|
2024-09-22 12:53:13 +09:00
|
|
|
meta.policies = {
|
|
|
|
driveCapacityMb: 50,
|
|
|
|
};
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-02-02 18:06:23 +09:00
|
|
|
const result = await roleService.getUserPolicies(user.id);
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2023-02-02 18:06:23 +09:00
|
|
|
expect(result.driveCapacityMb).toBe(100);
|
|
|
|
});
|
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
test('expired role', async () => {
|
|
|
|
const user = await createUser();
|
|
|
|
const role = await createRole({
|
2023-02-02 14:28:29 +09:00
|
|
|
name: 'a',
|
|
|
|
policies: {
|
|
|
|
canManageCustomEmojis: {
|
|
|
|
useDefault: false,
|
|
|
|
priority: 0,
|
|
|
|
value: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-04-19 15:22:23 +09:00
|
|
|
await roleService.assign(user.id, role.id, new Date(Date.now() + (1000 * 60 * 60 * 24)));
|
2024-09-22 12:53:13 +09:00
|
|
|
meta.policies = {
|
|
|
|
canManageCustomEmojis: false,
|
|
|
|
};
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
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();
|
2024-07-02 14:47:07 +09:00
|
|
|
await setTimeout(100);
|
2024-04-19 15:22:23 +09:00
|
|
|
|
|
|
|
const resultAfter25hAgain = await roleService.getUserPolicies(user.id);
|
|
|
|
expect(resultAfter25hAgain.canManageCustomEmojis).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-06-08 15:34:19 +09:00
|
|
|
describe('getModeratorIds', () => {
|
2024-10-11 20:59:36 +09:00
|
|
|
test('includeAdmins = false, includeRoot = false, excludeExpire = false', async () => {
|
|
|
|
const [adminUser1, adminUser2, modeUser1, modeUser2, normalUser1, normalUser2, rootUser] = await Promise.all([
|
|
|
|
createUser(), createUser(), createUser(), createUser(), createUser(), createUser(), createUser({ isRoot: true }),
|
2024-06-08 15:34:19 +09:00
|
|
|
]);
|
|
|
|
|
|
|
|
const role1 = await createRole({ name: 'admin', isAdministrator: true });
|
|
|
|
const role2 = await createRole({ name: 'moderator', isModerator: true });
|
|
|
|
const role3 = await createRole({ name: 'normal' });
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
assignRole({ userId: adminUser1.id, roleId: role1.id }),
|
|
|
|
assignRole({ userId: adminUser2.id, roleId: role1.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: modeUser1.id, roleId: role2.id }),
|
|
|
|
assignRole({ userId: modeUser2.id, roleId: role2.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: normalUser1.id, roleId: role3.id }),
|
|
|
|
assignRole({ userId: normalUser2.id, roleId: role3.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
]);
|
|
|
|
|
2024-10-11 20:59:36 +09:00
|
|
|
const result = await roleService.getModeratorIds({
|
|
|
|
includeAdmins: false,
|
|
|
|
includeRoot: false,
|
|
|
|
excludeExpire: false,
|
|
|
|
});
|
2024-06-08 15:34:19 +09:00
|
|
|
expect(result).toEqual([modeUser1.id, modeUser2.id]);
|
|
|
|
});
|
|
|
|
|
2024-10-11 20:59:36 +09:00
|
|
|
test('includeAdmins = false, includeRoot = false, excludeExpire = true', async () => {
|
|
|
|
const [adminUser1, adminUser2, modeUser1, modeUser2, normalUser1, normalUser2, rootUser] = await Promise.all([
|
|
|
|
createUser(), createUser(), createUser(), createUser(), createUser(), createUser(), createUser({ isRoot: true }),
|
2024-06-08 15:34:19 +09:00
|
|
|
]);
|
|
|
|
|
|
|
|
const role1 = await createRole({ name: 'admin', isAdministrator: true });
|
|
|
|
const role2 = await createRole({ name: 'moderator', isModerator: true });
|
|
|
|
const role3 = await createRole({ name: 'normal' });
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
assignRole({ userId: adminUser1.id, roleId: role1.id }),
|
|
|
|
assignRole({ userId: adminUser2.id, roleId: role1.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: modeUser1.id, roleId: role2.id }),
|
|
|
|
assignRole({ userId: modeUser2.id, roleId: role2.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: normalUser1.id, roleId: role3.id }),
|
|
|
|
assignRole({ userId: normalUser2.id, roleId: role3.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
]);
|
|
|
|
|
2024-10-11 20:59:36 +09:00
|
|
|
const result = await roleService.getModeratorIds({
|
|
|
|
includeAdmins: false,
|
|
|
|
includeRoot: false,
|
|
|
|
excludeExpire: true,
|
|
|
|
});
|
2024-06-08 15:34:19 +09:00
|
|
|
expect(result).toEqual([modeUser1.id]);
|
|
|
|
});
|
|
|
|
|
2024-10-11 20:59:36 +09:00
|
|
|
test('includeAdmins = true, includeRoot = false, excludeExpire = false', async () => {
|
|
|
|
const [adminUser1, adminUser2, modeUser1, modeUser2, normalUser1, normalUser2, rootUser] = await Promise.all([
|
|
|
|
createUser(), createUser(), createUser(), createUser(), createUser(), createUser(), createUser({ isRoot: true }),
|
2024-06-08 15:34:19 +09:00
|
|
|
]);
|
|
|
|
|
|
|
|
const role1 = await createRole({ name: 'admin', isAdministrator: true });
|
|
|
|
const role2 = await createRole({ name: 'moderator', isModerator: true });
|
|
|
|
const role3 = await createRole({ name: 'normal' });
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
assignRole({ userId: adminUser1.id, roleId: role1.id }),
|
|
|
|
assignRole({ userId: adminUser2.id, roleId: role1.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: modeUser1.id, roleId: role2.id }),
|
|
|
|
assignRole({ userId: modeUser2.id, roleId: role2.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: normalUser1.id, roleId: role3.id }),
|
|
|
|
assignRole({ userId: normalUser2.id, roleId: role3.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
]);
|
|
|
|
|
2024-10-11 20:59:36 +09:00
|
|
|
const result = await roleService.getModeratorIds({
|
|
|
|
includeAdmins: true,
|
|
|
|
includeRoot: false,
|
|
|
|
excludeExpire: false,
|
|
|
|
});
|
2024-06-08 15:34:19 +09:00
|
|
|
expect(result).toEqual([adminUser1.id, adminUser2.id, modeUser1.id, modeUser2.id]);
|
|
|
|
});
|
|
|
|
|
2024-10-11 20:59:36 +09:00
|
|
|
test('includeAdmins = true, includeRoot = false, excludeExpire = true', async () => {
|
|
|
|
const [adminUser1, adminUser2, modeUser1, modeUser2, normalUser1, normalUser2, rootUser] = await Promise.all([
|
|
|
|
createUser(), createUser(), createUser(), createUser(), createUser(), createUser(), createUser({ isRoot: true }),
|
2024-06-08 15:34:19 +09:00
|
|
|
]);
|
|
|
|
|
|
|
|
const role1 = await createRole({ name: 'admin', isAdministrator: true });
|
|
|
|
const role2 = await createRole({ name: 'moderator', isModerator: true });
|
|
|
|
const role3 = await createRole({ name: 'normal' });
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
assignRole({ userId: adminUser1.id, roleId: role1.id }),
|
|
|
|
assignRole({ userId: adminUser2.id, roleId: role1.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: modeUser1.id, roleId: role2.id }),
|
|
|
|
assignRole({ userId: modeUser2.id, roleId: role2.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: normalUser1.id, roleId: role3.id }),
|
|
|
|
assignRole({ userId: normalUser2.id, roleId: role3.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
]);
|
|
|
|
|
2024-10-11 20:59:36 +09:00
|
|
|
const result = await roleService.getModeratorIds({
|
|
|
|
includeAdmins: true,
|
|
|
|
includeRoot: false,
|
|
|
|
excludeExpire: true,
|
|
|
|
});
|
2024-06-08 15:34:19 +09:00
|
|
|
expect(result).toEqual([adminUser1.id, modeUser1.id]);
|
|
|
|
});
|
2024-10-11 20:59:36 +09:00
|
|
|
|
|
|
|
test('includeAdmins = false, includeRoot = true, excludeExpire = false', async () => {
|
|
|
|
const [adminUser1, adminUser2, modeUser1, modeUser2, normalUser1, normalUser2, rootUser] = await Promise.all([
|
|
|
|
createUser(), createUser(), createUser(), createUser(), createUser(), createUser(), createUser({ isRoot: true }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const role1 = await createRole({ name: 'admin', isAdministrator: true });
|
|
|
|
const role2 = await createRole({ name: 'moderator', isModerator: true });
|
|
|
|
const role3 = await createRole({ name: 'normal' });
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
assignRole({ userId: adminUser1.id, roleId: role1.id }),
|
|
|
|
assignRole({ userId: adminUser2.id, roleId: role1.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: modeUser1.id, roleId: role2.id }),
|
|
|
|
assignRole({ userId: modeUser2.id, roleId: role2.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: normalUser1.id, roleId: role3.id }),
|
|
|
|
assignRole({ userId: normalUser2.id, roleId: role3.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const result = await roleService.getModeratorIds({
|
|
|
|
includeAdmins: false,
|
|
|
|
includeRoot: true,
|
|
|
|
excludeExpire: false,
|
|
|
|
});
|
|
|
|
expect(result).toEqual([modeUser1.id, modeUser2.id, rootUser.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('root has moderator role', async () => {
|
|
|
|
const [adminUser1, modeUser1, normalUser1, rootUser] = await Promise.all([
|
|
|
|
createUser(), createUser(), createUser(), createUser({ isRoot: true }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const role1 = await createRole({ name: 'admin', isAdministrator: true });
|
|
|
|
const role2 = await createRole({ name: 'moderator', isModerator: true });
|
|
|
|
const role3 = await createRole({ name: 'normal' });
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
assignRole({ userId: adminUser1.id, roleId: role1.id }),
|
|
|
|
assignRole({ userId: modeUser1.id, roleId: role2.id }),
|
|
|
|
assignRole({ userId: rootUser.id, roleId: role2.id }),
|
|
|
|
assignRole({ userId: normalUser1.id, roleId: role3.id }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const result = await roleService.getModeratorIds({
|
|
|
|
includeAdmins: false,
|
|
|
|
includeRoot: true,
|
|
|
|
excludeExpire: false,
|
|
|
|
});
|
|
|
|
expect(result).toEqual([modeUser1.id, rootUser.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('root has administrator role', async () => {
|
|
|
|
const [adminUser1, modeUser1, normalUser1, rootUser] = await Promise.all([
|
|
|
|
createUser(), createUser(), createUser(), createUser({ isRoot: true }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const role1 = await createRole({ name: 'admin', isAdministrator: true });
|
|
|
|
const role2 = await createRole({ name: 'moderator', isModerator: true });
|
|
|
|
const role3 = await createRole({ name: 'normal' });
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
assignRole({ userId: adminUser1.id, roleId: role1.id }),
|
|
|
|
assignRole({ userId: rootUser.id, roleId: role1.id }),
|
|
|
|
assignRole({ userId: modeUser1.id, roleId: role2.id }),
|
|
|
|
assignRole({ userId: normalUser1.id, roleId: role3.id }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const result = await roleService.getModeratorIds({
|
|
|
|
includeAdmins: true,
|
|
|
|
includeRoot: true,
|
|
|
|
excludeExpire: false,
|
|
|
|
});
|
|
|
|
expect(result).toEqual([adminUser1.id, modeUser1.id, rootUser.id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('root has moderator role(expire)', async () => {
|
|
|
|
const [adminUser1, modeUser1, normalUser1, rootUser] = await Promise.all([
|
|
|
|
createUser(), createUser(), createUser(), createUser({ isRoot: true }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const role1 = await createRole({ name: 'admin', isAdministrator: true });
|
|
|
|
const role2 = await createRole({ name: 'moderator', isModerator: true });
|
|
|
|
const role3 = await createRole({ name: 'normal' });
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
assignRole({ userId: adminUser1.id, roleId: role1.id }),
|
|
|
|
assignRole({ userId: modeUser1.id, roleId: role2.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: rootUser.id, roleId: role2.id, expiresAt: new Date(Date.now() - 1000) }),
|
|
|
|
assignRole({ userId: normalUser1.id, roleId: role3.id }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const result = await roleService.getModeratorIds({
|
|
|
|
includeAdmins: false,
|
|
|
|
includeRoot: true,
|
|
|
|
excludeExpire: true,
|
|
|
|
});
|
|
|
|
expect(result).toEqual([rootUser.id]);
|
|
|
|
});
|
2024-06-08 15:34:19 +09:00
|
|
|
});
|
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
describe('conditional role', () => {
|
|
|
|
test('~かつ~', async () => {
|
|
|
|
const [user1, user2, user3, user4] = await Promise.all([
|
|
|
|
createUser({ isBot: true, isCat: false, isSuspended: false }),
|
|
|
|
createUser({ isBot: false, isCat: true, isSuspended: false }),
|
|
|
|
createUser({ isBot: true, isCat: true, isSuspended: false }),
|
|
|
|
createUser({ isBot: false, isCat: false, isSuspended: true }),
|
|
|
|
]);
|
|
|
|
const role1 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isBot',
|
|
|
|
});
|
|
|
|
const role2 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isCat',
|
|
|
|
});
|
|
|
|
const role3 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isSuspended',
|
|
|
|
});
|
|
|
|
const role4 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'and',
|
|
|
|
values: [role1.condFormula, role2.condFormula],
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
const actual4 = await roleService.getUserRoles(user4.id);
|
|
|
|
expect(actual1.some(r => r.id === role4.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role4.id)).toBe(false);
|
|
|
|
expect(actual3.some(r => r.id === role4.id)).toBe(true);
|
|
|
|
expect(actual4.some(r => r.id === role4.id)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('~または~', async () => {
|
|
|
|
const [user1, user2, user3, user4] = await Promise.all([
|
|
|
|
createUser({ isBot: true, isCat: false, isSuspended: false }),
|
|
|
|
createUser({ isBot: false, isCat: true, isSuspended: false }),
|
|
|
|
createUser({ isBot: true, isCat: true, isSuspended: false }),
|
|
|
|
createUser({ isBot: false, isCat: false, isSuspended: true }),
|
|
|
|
]);
|
|
|
|
const role1 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isBot',
|
|
|
|
});
|
|
|
|
const role2 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isCat',
|
|
|
|
});
|
|
|
|
const role3 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isSuspended',
|
|
|
|
});
|
|
|
|
const role4 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'or',
|
|
|
|
values: [role1.condFormula, role2.condFormula],
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
const actual4 = await roleService.getUserRoles(user4.id);
|
|
|
|
expect(actual1.some(r => r.id === role4.id)).toBe(true);
|
|
|
|
expect(actual2.some(r => r.id === role4.id)).toBe(true);
|
|
|
|
expect(actual3.some(r => r.id === role4.id)).toBe(true);
|
|
|
|
expect(actual4.some(r => r.id === role4.id)).toBe(false);
|
2023-02-02 14:28:29 +09:00
|
|
|
});
|
2023-03-01 10:20:03 +09:00
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
test('~ではない', async () => {
|
|
|
|
const [user1, user2, user3] = await Promise.all([
|
|
|
|
createUser({ isBot: true, isCat: false, isSuspended: false }),
|
|
|
|
createUser({ isBot: false, isCat: true, isSuspended: false }),
|
|
|
|
createUser({ isBot: true, isCat: true, isSuspended: false }),
|
|
|
|
]);
|
|
|
|
const role1 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isBot',
|
|
|
|
});
|
|
|
|
const role2 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isCat',
|
|
|
|
});
|
|
|
|
const role4 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'not',
|
|
|
|
value: role1.condFormula,
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
expect(actual1.some(r => r.id === role4.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role4.id)).toBe(true);
|
|
|
|
expect(actual3.some(r => r.id === role4.id)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('マニュアルロールにアサイン済み', async () => {
|
2024-02-27 18:45:46 +09:00
|
|
|
const [user1, user2, role1] = await Promise.all([
|
|
|
|
createUser(),
|
|
|
|
createUser(),
|
|
|
|
createRole({
|
|
|
|
name: 'manual role',
|
|
|
|
}),
|
|
|
|
]);
|
2024-04-19 15:22:23 +09:00
|
|
|
const role2 = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'roleAssignedTo',
|
|
|
|
roleId: role1.id,
|
2024-02-27 18:45:46 +09:00
|
|
|
});
|
|
|
|
await roleService.assign(user2.id, role1.id);
|
|
|
|
|
|
|
|
const [u1role, u2role] = await Promise.all([
|
|
|
|
roleService.getUserRoles(user1.id),
|
|
|
|
roleService.getUserRoles(user2.id),
|
|
|
|
]);
|
|
|
|
expect(u1role.some(r => r.id === role2.id)).toBe(false);
|
|
|
|
expect(u2role.some(r => r.id === role2.id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
test('ローカルユーザのみ', async () => {
|
|
|
|
const [user1, user2] = await Promise.all([
|
|
|
|
createUser({ host: null }),
|
|
|
|
createUser({ host: 'example.com' }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isLocal',
|
2023-03-01 10:20:03 +09:00
|
|
|
});
|
2023-06-25 11:04:33 +09:00
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(false);
|
|
|
|
});
|
2023-03-01 10:20:03 +09:00
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
test('リモートユーザのみ', async () => {
|
|
|
|
const [user1, user2] = await Promise.all([
|
|
|
|
createUser({ host: null }),
|
|
|
|
createUser({ host: 'example.com' }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isRemote',
|
|
|
|
});
|
2023-03-01 10:20:03 +09:00
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
});
|
2023-03-01 10:20:03 +09:00
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
test('サスペンド済みユーザである', async () => {
|
|
|
|
const [user1, user2] = await Promise.all([
|
|
|
|
createUser({ isSuspended: false }),
|
|
|
|
createUser({ isSuspended: true }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isSuspended',
|
|
|
|
});
|
2023-03-01 10:20:03 +09:00
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
});
|
2023-03-01 10:20:03 +09:00
|
|
|
|
2024-04-19 15:22:23 +09:00
|
|
|
test('鍵アカウントユーザである', async () => {
|
|
|
|
const [user1, user2] = await Promise.all([
|
|
|
|
createUser({ isLocked: false }),
|
|
|
|
createUser({ isLocked: true }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isLocked',
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('botユーザである', async () => {
|
|
|
|
const [user1, user2] = await Promise.all([
|
|
|
|
createUser({ isBot: false }),
|
|
|
|
createUser({ isBot: true }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isBot',
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('猫である', async () => {
|
|
|
|
const [user1, user2] = await Promise.all([
|
|
|
|
createUser({ isCat: false }),
|
|
|
|
createUser({ isCat: true }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isCat',
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('「ユーザを見つけやすくする」が有効なアカウント', async () => {
|
|
|
|
const [user1, user2] = await Promise.all([
|
|
|
|
createUser({ isExplorable: false }),
|
|
|
|
createUser({ isExplorable: true }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'isExplorable',
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('ユーザが作成されてから指定期間経過した', async () => {
|
|
|
|
const base = new Date();
|
|
|
|
base.setMinutes(base.getMinutes() - 5);
|
|
|
|
|
|
|
|
const d1 = new Date(base);
|
|
|
|
const d2 = new Date(base);
|
|
|
|
const d3 = new Date(base);
|
|
|
|
d1.setSeconds(d1.getSeconds() - 1);
|
|
|
|
d3.setSeconds(d3.getSeconds() + 1);
|
|
|
|
|
|
|
|
const [user1, user2, user3] = await Promise.all([
|
|
|
|
// 4:59
|
|
|
|
createUser({ id: genAidx(d1.getTime()) }),
|
|
|
|
// 5:00
|
|
|
|
createUser({ id: genAidx(d2.getTime()) }),
|
|
|
|
// 5:01
|
|
|
|
createUser({ id: genAidx(d3.getTime()) }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'createdLessThan',
|
|
|
|
// 5 minutes
|
|
|
|
sec: 300,
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual3.some(r => r.id === role.id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('ユーザが作成されてから指定期間経っていない', async () => {
|
|
|
|
const base = new Date();
|
|
|
|
base.setMinutes(base.getMinutes() - 5);
|
|
|
|
|
|
|
|
const d1 = new Date(base);
|
|
|
|
const d2 = new Date(base);
|
|
|
|
const d3 = new Date(base);
|
|
|
|
d1.setSeconds(d1.getSeconds() - 1);
|
|
|
|
d3.setSeconds(d3.getSeconds() + 1);
|
|
|
|
|
|
|
|
const [user1, user2, user3] = await Promise.all([
|
|
|
|
// 4:59
|
|
|
|
createUser({ id: genAidx(d1.getTime()) }),
|
|
|
|
// 5:00
|
|
|
|
createUser({ id: genAidx(d2.getTime()) }),
|
|
|
|
// 5:01
|
|
|
|
createUser({ id: genAidx(d3.getTime()) }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'createdMoreThan',
|
|
|
|
// 5 minutes
|
|
|
|
sec: 300,
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual3.some(r => r.id === role.id)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('フォロワー数が指定値以下', async () => {
|
|
|
|
const [user1, user2, user3] = await Promise.all([
|
|
|
|
createUser({ followersCount: 99 }),
|
|
|
|
createUser({ followersCount: 100 }),
|
|
|
|
createUser({ followersCount: 101 }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'followersLessThanOrEq',
|
|
|
|
value: 100,
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual3.some(r => r.id === role.id)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('フォロワー数が指定値以下', async () => {
|
|
|
|
const [user1, user2, user3] = await Promise.all([
|
|
|
|
createUser({ followersCount: 99 }),
|
|
|
|
createUser({ followersCount: 100 }),
|
|
|
|
createUser({ followersCount: 101 }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'followersMoreThanOrEq',
|
|
|
|
value: 100,
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual3.some(r => r.id === role.id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('フォロー数が指定値以下', async () => {
|
|
|
|
const [user1, user2, user3] = await Promise.all([
|
|
|
|
createUser({ followingCount: 99 }),
|
|
|
|
createUser({ followingCount: 100 }),
|
|
|
|
createUser({ followingCount: 101 }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'followingLessThanOrEq',
|
|
|
|
value: 100,
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual3.some(r => r.id === role.id)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('フォロー数が指定値以上', async () => {
|
|
|
|
const [user1, user2, user3] = await Promise.all([
|
|
|
|
createUser({ followingCount: 99 }),
|
|
|
|
createUser({ followingCount: 100 }),
|
|
|
|
createUser({ followingCount: 101 }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'followingMoreThanOrEq',
|
|
|
|
value: 100,
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual3.some(r => r.id === role.id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('ノート数が指定値以下', async () => {
|
|
|
|
const [user1, user2, user3] = await Promise.all([
|
|
|
|
createUser({ notesCount: 9 }),
|
|
|
|
createUser({ notesCount: 10 }),
|
|
|
|
createUser({ notesCount: 11 }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'notesLessThanOrEq',
|
|
|
|
value: 10,
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual3.some(r => r.id === role.id)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('ノート数が指定値以上', async () => {
|
|
|
|
const [user1, user2, user3] = await Promise.all([
|
|
|
|
createUser({ notesCount: 9 }),
|
|
|
|
createUser({ notesCount: 10 }),
|
|
|
|
createUser({ notesCount: 11 }),
|
|
|
|
]);
|
|
|
|
const role = await createConditionalRole({
|
|
|
|
id: aidx(),
|
|
|
|
type: 'notesMoreThanOrEq',
|
|
|
|
value: 10,
|
|
|
|
});
|
|
|
|
|
|
|
|
const actual1 = await roleService.getUserRoles(user1.id);
|
|
|
|
const actual2 = await roleService.getUserRoles(user2.id);
|
|
|
|
const actual3 = await roleService.getUserRoles(user3.id);
|
|
|
|
expect(actual1.some(r => r.id === role.id)).toBe(false);
|
|
|
|
expect(actual2.some(r => r.id === role.id)).toBe(true);
|
|
|
|
expect(actual3.some(r => r.id === role.id)).toBe(true);
|
2023-03-01 10:20:03 +09:00
|
|
|
});
|
2023-02-02 10:26:59 +09:00
|
|
|
});
|
2023-12-21 10:39:11 +09:00
|
|
|
|
|
|
|
describe('assign', () => {
|
|
|
|
test('公開ロールの場合は通知される', async () => {
|
|
|
|
const user = await createUser();
|
|
|
|
const role = await createRole({
|
|
|
|
isPublic: true,
|
2023-12-22 09:06:13 +09:00
|
|
|
name: 'a',
|
2023-12-21 10:39:11 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
await roleService.assign(user.id, role.id);
|
|
|
|
|
2023-12-22 09:06:13 +09:00
|
|
|
clock.uninstall();
|
2024-07-02 14:47:07 +09:00
|
|
|
await setTimeout(100);
|
2023-12-21 10:39:11 +09:00
|
|
|
|
|
|
|
const assignments = await roleAssignmentsRepository.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
roleId: role.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(assignments).toHaveLength(1);
|
|
|
|
|
|
|
|
expect(notificationService.createNotification).toHaveBeenCalled();
|
|
|
|
expect(notificationService.createNotification.mock.lastCall![0]).toBe(user.id);
|
|
|
|
expect(notificationService.createNotification.mock.lastCall![1]).toBe('roleAssigned');
|
2023-12-22 09:06:13 +09:00
|
|
|
expect(notificationService.createNotification.mock.lastCall![2]).toEqual({
|
2023-12-21 10:39:11 +09:00
|
|
|
roleId: role.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('非公開ロールの場合は通知されない', async () => {
|
|
|
|
const user = await createUser();
|
|
|
|
const role = await createRole({
|
|
|
|
isPublic: false,
|
2023-12-22 09:06:13 +09:00
|
|
|
name: 'a',
|
2023-12-21 10:39:11 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
await roleService.assign(user.id, role.id);
|
|
|
|
|
2023-12-22 09:06:13 +09:00
|
|
|
clock.uninstall();
|
2024-07-02 14:47:07 +09:00
|
|
|
await setTimeout(100);
|
2023-12-21 10:39:11 +09:00
|
|
|
|
|
|
|
const assignments = await roleAssignmentsRepository.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
roleId: role.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(assignments).toHaveLength(1);
|
|
|
|
|
|
|
|
expect(notificationService.createNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2023-02-02 10:26:59 +09:00
|
|
|
});
|