Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-04-06 13:46:11 +02:00
parent 1092b49623
commit 3705d07b77
No known key found for this signature in database
GPG key ID: 42498463316F048B

View file

@ -7,6 +7,7 @@ import { ConfigModule } from '@nestjs/config';
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter'; import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm'; import { getRepositoryToken } from '@nestjs/typeorm';
import { Mock } from 'ts-mockery';
import { DataSource, EntityManager, Repository } from 'typeorm'; import { DataSource, EntityManager, Repository } from 'typeorm';
import { AuthToken } from '../auth/auth-token.entity'; import { AuthToken } from '../auth/auth-token.entity';
@ -48,11 +49,23 @@ import { PermissionsService } from './permissions.service';
describe('PermissionsService', () => { describe('PermissionsService', () => {
let service: PermissionsService; let service: PermissionsService;
let notes: Note[];
let noteRepo: Repository<Note>; let noteRepo: Repository<Note>;
let userRepo: Repository<User>; let userRepo: Repository<User>;
let groupRepo: Repository<Group>; let groupRepo: Repository<Group>;
let eventEmitter: EventEmitter2; let eventEmitter: EventEmitter2;
let noteNobodyRead: Note;
let noteUser1Read: Note;
let noteAllUsersRead1: Note;
let noteAllUsersRead2: Note;
let noteUser2Write: Note;
let noteAllWrite1: Note;
let noteAllWrite2: Note;
let noteUser2Read: Note;
let noteEverybodyNone: Note;
let noteEverybodyWrite: Note;
let noteEverybodyRead: Note;
const noteMockConfig: NoteConfig = createDefaultMockNoteConfig(); const noteMockConfig: NoteConfig = createDefaultMockNoteConfig();
beforeAll(async () => { beforeAll(async () => {
@ -143,7 +156,7 @@ describe('PermissionsService', () => {
.useValue({}) .useValue({})
.compile(); .compile();
service = module.get<PermissionsService>(PermissionsService); service = module.get<PermissionsService>(PermissionsService);
notes = await createNoteUserPermissionNotes(); await createNoteUserPermissionNotes();
groupRepo = module.get<Repository<Group>>(getRepositoryToken(Group)); groupRepo = module.get<Repository<Group>>(getRepositoryToken(Group));
noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note)); noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note));
eventEmitter = module.get<EventEmitter2>(EventEmitter2); eventEmitter = module.get<EventEmitter2>(EventEmitter2);
@ -154,119 +167,109 @@ describe('PermissionsService', () => {
}); });
// The two users we test with: // The two users we test with:
const user2 = {} as User; const user1 = Mock.of<User>({ id: 1 });
user2.id = 2; const user2 = Mock.of<User>({ id: 2 });
const user1 = {} as User;
user1.id = 1;
it('should be defined', () => { it('should be defined', () => {
expect(service).toBeDefined(); expect(service).toBeDefined();
}); });
function createNote(owner: User): Note { function createNote(owner: User): Note {
const note = {} as Note; return Mock.of<Note>({
note.userPermissions = Promise.resolve([]); userPermissions: Promise.resolve([]),
note.groupPermissions = Promise.resolve([]); groupPermissions: Promise.resolve([]),
note.owner = Promise.resolve(owner); owner: Promise.resolve(owner),
return note; });
}
function createUserPermission(
user: User,
canEdit: boolean,
): NoteUserPermission {
return Mock.of<NoteUserPermission>({
user: Promise.resolve(user),
canEdit: canEdit,
});
}
function createGroupPermission(
group: Group,
note: Note,
canEdit: boolean,
): NoteGroupPermission {
const groupPermission = Mock.of<NoteGroupPermission>({
group: Promise.resolve(group),
canEdit: canEdit,
note: Promise.resolve(note),
});
note.groupPermissions = Promise.resolve([groupPermission]);
return groupPermission;
} }
/* /*
* Creates the permission objects for UserPermission for two users with write and with out write permission * Creates the permission objects for UserPermission for two users with write and with out write permission
*/ */
async function createNoteUserPermissionNotes(): Promise<Note[]> { async function createNoteUserPermissionNotes(): Promise<void> {
const note0 = createNote(user1); const user1ReadPermission = createUserPermission(user1, false);
const note1 = createNote(user2); const user2ReadPermission = createUserPermission(user2, false);
const note2 = createNote(user2); const user1WritePermission = createUserPermission(user1, true);
const note3 = createNote(user2); const user2WritePermission = createUserPermission(user2, true);
const note4 = createNote(user2);
const note5 = createNote(user2);
const note6 = createNote(user2);
const note7 = createNote(user2);
const noteUserPermission1 = {} as NoteUserPermission;
noteUserPermission1.user = Promise.resolve(user1);
const noteUserPermission2 = {} as NoteUserPermission;
noteUserPermission2.user = Promise.resolve(user2);
const noteUserPermission3 = {} as NoteUserPermission;
noteUserPermission3.user = Promise.resolve(user1);
noteUserPermission3.canEdit = true;
const noteUserPermission4 = {} as NoteUserPermission;
noteUserPermission4.user = Promise.resolve(user2);
noteUserPermission4.canEdit = true;
(await note1.userPermissions).push(noteUserPermission1); noteNobodyRead = createNote(user1);
noteUser1Read = createNote(user2);
noteAllUsersRead1 = createNote(user2);
noteAllUsersRead2 = createNote(user2);
noteUser2Write = createNote(user2);
noteAllWrite1 = createNote(user2);
noteAllWrite2 = createNote(user2);
noteUser2Read = createNote(user2);
(await note2.userPermissions).push(noteUserPermission1); (await noteUser1Read.userPermissions).push(user1ReadPermission);
(await note2.userPermissions).push(noteUserPermission2); (await noteUser2Read.userPermissions).push(user2ReadPermission);
(await note3.userPermissions).push(noteUserPermission2); (await noteAllUsersRead1.userPermissions).push(user1ReadPermission);
(await note3.userPermissions).push(noteUserPermission1); (await noteAllUsersRead1.userPermissions).push(user2ReadPermission);
(await note4.userPermissions).push(noteUserPermission3); (await noteAllUsersRead2.userPermissions).push(user1ReadPermission);
(await noteAllUsersRead2.userPermissions).push(user2ReadPermission);
(await note5.userPermissions).push(noteUserPermission3); (await noteUser2Write.userPermissions).push(user1WritePermission);
(await note5.userPermissions).push(noteUserPermission4);
(await note6.userPermissions).push(noteUserPermission4); (await noteAllWrite1.userPermissions).push(user1WritePermission);
(await note6.userPermissions).push(noteUserPermission3); (await noteAllWrite1.userPermissions).push(user2WritePermission);
(await note7.userPermissions).push(noteUserPermission2); (await noteAllWrite2.userPermissions).push(user2WritePermission);
(await noteAllWrite2.userPermissions).push(user1WritePermission);
const everybody = {} as Group; const everybody = Mock.of<Group>({
everybody.name = SpecialGroup.EVERYONE; name: SpecialGroup.EVERYONE,
everybody.special = true; special: true,
});
const noteEverybodyNone = createNote(user1); noteEverybodyNone = createNote(user1);
noteEverybodyNone.groupPermissions = Promise.resolve([]);
const noteEverybodyRead = createNote(user1); noteEverybodyRead = createNote(user1);
const noteGroupPermissionRead = {} as NoteGroupPermission; createGroupPermission(everybody, noteEverybodyRead, false);
noteGroupPermissionRead.group = Promise.resolve(everybody);
noteGroupPermissionRead.canEdit = false;
noteGroupPermissionRead.note = Promise.resolve(noteEverybodyRead);
noteEverybodyRead.groupPermissions = Promise.resolve([
noteGroupPermissionRead,
]);
const noteEverybodyWrite = createNote(user1); noteEverybodyWrite = createNote(user1);
const noteGroupPermissionWrite = {} as NoteGroupPermission; createGroupPermission(everybody, noteEverybodyWrite, true);
noteGroupPermissionWrite.group = Promise.resolve(everybody);
noteGroupPermissionWrite.canEdit = true;
noteGroupPermissionWrite.note = Promise.resolve(noteEverybodyWrite);
noteEverybodyWrite.groupPermissions = Promise.resolve([
noteGroupPermissionWrite,
]);
return [
note0,
note1,
note2,
note3,
note4,
note5,
note6,
note7,
noteEverybodyRead,
noteEverybodyWrite,
noteEverybodyNone,
];
} }
describe('mayRead works with', () => { describe('mayRead works with', () => {
it('Owner', async () => { it('Owner', async () => {
expect(await service.mayRead(user1, notes[0])).toBeTruthy(); expect(await service.mayRead(user1, noteNobodyRead)).toBeTruthy();
expect(await service.mayRead(user1, notes[7])).toBeFalsy(); expect(await service.mayRead(user1, noteUser2Read)).toBeFalsy();
}); });
it('userPermission read', async () => { it('userPermission read', async () => {
expect(await service.mayRead(user1, notes[1])).toBeTruthy(); expect(await service.mayRead(user1, noteUser1Read)).toBeTruthy();
expect(await service.mayRead(user1, notes[2])).toBeTruthy(); expect(await service.mayRead(user1, noteAllUsersRead1)).toBeTruthy();
expect(await service.mayRead(user1, notes[3])).toBeTruthy(); expect(await service.mayRead(user1, noteAllUsersRead2)).toBeTruthy();
}); });
it('userPermission write', async () => { it('userPermission write', async () => {
expect(await service.mayRead(user1, notes[4])).toBeTruthy(); expect(await service.mayRead(user1, noteUser2Write)).toBeTruthy();
expect(await service.mayRead(user1, notes[5])).toBeTruthy(); expect(await service.mayRead(user1, noteAllWrite1)).toBeTruthy();
expect(await service.mayRead(user1, notes[6])).toBeTruthy(); expect(await service.mayRead(user1, noteAllWrite2)).toBeTruthy();
expect(await service.mayRead(user1, notes[7])).toBeFalsy(); expect(await service.mayRead(user1, noteUser2Read)).toBeFalsy();
}); });
describe('guest permission', () => { describe('guest permission', () => {
@ -279,13 +282,13 @@ describe('PermissionsService', () => {
noteMockConfig.guestAccess = GuestAccess.DENY; noteMockConfig.guestAccess = GuestAccess.DENY;
}); });
it('guest permission none', async () => { it('guest permission none', async () => {
expect(await service.mayRead(null, notes[10])).toBeFalsy(); expect(await service.mayRead(null, noteEverybodyNone)).toBeFalsy();
}); });
it('guest permission read', async () => { it('guest permission read', async () => {
expect(await service.mayRead(null, notes[8])).toBeFalsy(); expect(await service.mayRead(null, noteEverybodyRead)).toBeFalsy();
}); });
it('guest permission write', async () => { it('guest permission write', async () => {
expect(await service.mayRead(null, notes[9])).toBeFalsy(); expect(await service.mayRead(null, noteEverybodyWrite)).toBeFalsy();
}); });
}); });
describe('with guest access read', () => { describe('with guest access read', () => {
@ -293,13 +296,13 @@ describe('PermissionsService', () => {
noteMockConfig.guestAccess = GuestAccess.READ; noteMockConfig.guestAccess = GuestAccess.READ;
}); });
it('guest permission none', async () => { it('guest permission none', async () => {
expect(await service.mayRead(null, notes[10])).toBeFalsy(); expect(await service.mayRead(null, noteEverybodyNone)).toBeFalsy();
}); });
it('guest permission read', async () => { it('guest permission read', async () => {
expect(await service.mayRead(null, notes[8])).toBeTruthy(); expect(await service.mayRead(null, noteEverybodyRead)).toBeTruthy();
}); });
it('guest permission write', async () => { it('guest permission write', async () => {
expect(await service.mayRead(null, notes[9])).toBeTruthy(); expect(await service.mayRead(null, noteEverybodyWrite)).toBeTruthy();
}); });
}); });
describe('with guest access write', () => { describe('with guest access write', () => {
@ -307,13 +310,13 @@ describe('PermissionsService', () => {
noteMockConfig.guestAccess = GuestAccess.WRITE; noteMockConfig.guestAccess = GuestAccess.WRITE;
}); });
it('guest permission none', async () => { it('guest permission none', async () => {
expect(await service.mayRead(null, notes[10])).toBeFalsy(); expect(await service.mayRead(null, noteEverybodyNone)).toBeFalsy();
}); });
it('guest permission read', async () => { it('guest permission read', async () => {
expect(await service.mayRead(null, notes[8])).toBeTruthy(); expect(await service.mayRead(null, noteEverybodyRead)).toBeTruthy();
}); });
it('guest permission write', async () => { it('guest permission write', async () => {
expect(await service.mayRead(null, notes[9])).toBeTruthy(); expect(await service.mayRead(null, noteEverybodyWrite)).toBeTruthy();
}); });
}); });
describe('with guest access create', () => { describe('with guest access create', () => {
@ -321,13 +324,13 @@ describe('PermissionsService', () => {
noteMockConfig.guestAccess = GuestAccess.CREATE; noteMockConfig.guestAccess = GuestAccess.CREATE;
}); });
it('guest permission none', async () => { it('guest permission none', async () => {
expect(await service.mayRead(null, notes[10])).toBeFalsy(); expect(await service.mayRead(null, noteEverybodyNone)).toBeFalsy();
}); });
it('guest permission read', async () => { it('guest permission read', async () => {
expect(await service.mayRead(null, notes[8])).toBeTruthy(); expect(await service.mayRead(null, noteEverybodyRead)).toBeTruthy();
}); });
it('guest permission write', async () => { it('guest permission write', async () => {
expect(await service.mayRead(null, notes[9])).toBeTruthy(); expect(await service.mayRead(null, noteEverybodyWrite)).toBeTruthy();
}); });
}); });
}); });
@ -335,19 +338,19 @@ describe('PermissionsService', () => {
describe('mayWrite works with', () => { describe('mayWrite works with', () => {
it('Owner', async () => { it('Owner', async () => {
expect(await service.mayWrite(user1, notes[0])).toBeTruthy(); expect(await service.mayWrite(user1, noteNobodyRead)).toBeTruthy();
expect(await service.mayWrite(user1, notes[7])).toBeFalsy(); expect(await service.mayWrite(user1, noteUser2Read)).toBeFalsy();
}); });
it('userPermission read', async () => { it('userPermission read', async () => {
expect(await service.mayWrite(user1, notes[1])).toBeFalsy(); expect(await service.mayWrite(user1, noteUser1Read)).toBeFalsy();
expect(await service.mayWrite(user1, notes[2])).toBeFalsy(); expect(await service.mayWrite(user1, noteAllUsersRead1)).toBeFalsy();
expect(await service.mayWrite(user1, notes[3])).toBeFalsy(); expect(await service.mayWrite(user1, noteAllUsersRead2)).toBeFalsy();
}); });
it('userPermission write', async () => { it('userPermission write', async () => {
expect(await service.mayWrite(user1, notes[4])).toBeTruthy(); expect(await service.mayWrite(user1, noteUser2Write)).toBeTruthy();
expect(await service.mayWrite(user1, notes[5])).toBeTruthy(); expect(await service.mayWrite(user1, noteAllWrite1)).toBeTruthy();
expect(await service.mayWrite(user1, notes[6])).toBeTruthy(); expect(await service.mayWrite(user1, noteAllWrite2)).toBeTruthy();
expect(await service.mayWrite(user1, notes[7])).toBeFalsy(); expect(await service.mayWrite(user1, noteUser2Read)).toBeFalsy();
}); });
describe('guest permission', () => { describe('guest permission', () => {
beforeEach(() => { beforeEach(() => {
@ -360,13 +363,13 @@ describe('PermissionsService', () => {
noteMockConfig.guestAccess = GuestAccess.DENY; noteMockConfig.guestAccess = GuestAccess.DENY;
}); });
it('guest permission none', async () => { it('guest permission none', async () => {
expect(await service.mayWrite(null, notes[10])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyNone)).toBeFalsy();
}); });
it('guest permission read', async () => { it('guest permission read', async () => {
expect(await service.mayWrite(null, notes[8])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyRead)).toBeFalsy();
}); });
it('guest permission write', async () => { it('guest permission write', async () => {
expect(await service.mayWrite(null, notes[9])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyWrite)).toBeFalsy();
}); });
}); });
@ -375,13 +378,13 @@ describe('PermissionsService', () => {
noteMockConfig.guestAccess = GuestAccess.READ; noteMockConfig.guestAccess = GuestAccess.READ;
}); });
it('guest permission none', async () => { it('guest permission none', async () => {
expect(await service.mayWrite(null, notes[10])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyNone)).toBeFalsy();
}); });
it('guest permission read', async () => { it('guest permission read', async () => {
expect(await service.mayWrite(null, notes[8])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyRead)).toBeFalsy();
}); });
it('guest permission write', async () => { it('guest permission write', async () => {
expect(await service.mayWrite(null, notes[9])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyWrite)).toBeFalsy();
}); });
}); });
@ -390,13 +393,13 @@ describe('PermissionsService', () => {
noteMockConfig.guestAccess = GuestAccess.WRITE; noteMockConfig.guestAccess = GuestAccess.WRITE;
}); });
it('guest permission none', async () => { it('guest permission none', async () => {
expect(await service.mayWrite(null, notes[10])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyNone)).toBeFalsy();
}); });
it('guest permission read', async () => { it('guest permission read', async () => {
expect(await service.mayWrite(null, notes[8])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyRead)).toBeFalsy();
}); });
it('guest permission write', async () => { it('guest permission write', async () => {
expect(await service.mayWrite(null, notes[9])).toBeTruthy(); expect(await service.mayWrite(null, noteEverybodyWrite)).toBeTruthy();
}); });
}); });
@ -405,13 +408,13 @@ describe('PermissionsService', () => {
noteMockConfig.guestAccess = GuestAccess.CREATE; noteMockConfig.guestAccess = GuestAccess.CREATE;
}); });
it('guest permission none', async () => { it('guest permission none', async () => {
expect(await service.mayWrite(null, notes[10])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyNone)).toBeFalsy();
}); });
it('guest permission read', async () => { it('guest permission read', async () => {
expect(await service.mayWrite(null, notes[8])).toBeFalsy(); expect(await service.mayWrite(null, noteEverybodyRead)).toBeFalsy();
}); });
it('guest permission write', async () => { it('guest permission write', async () => {
expect(await service.mayWrite(null, notes[9])).toBeTruthy(); expect(await service.mayWrite(null, noteEverybodyWrite)).toBeTruthy();
}); });
}); });
}); });
@ -614,16 +617,11 @@ describe('PermissionsService', () => {
} }
// inspired by https://stackoverflow.com/questions/9960908/permutations-in-javascript // inspired by https://stackoverflow.com/questions/9960908/permutations-in-javascript
function permutator( function permutator<T>(inputArr: T[]): T[][] {
inputArr: NoteGroupPermission[], const results: T[][] = [];
): NoteGroupPermission[][] {
const results: NoteGroupPermission[][] = [];
function permute( function permute(arr: T[], memo: T[]): T[][] {
arr: NoteGroupPermission[], let cur: T[];
memo: NoteGroupPermission[],
): NoteGroupPermission[][] {
let cur: NoteGroupPermission[];
for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
cur = arr.splice(i, 1); cur = arr.splice(i, 1);
@ -705,10 +703,10 @@ describe('PermissionsService', () => {
describe('isOwner works', () => { describe('isOwner works', () => {
it('for positive case', async () => { it('for positive case', async () => {
expect(await service.isOwner(user1, notes[0])).toBeTruthy(); expect(await service.isOwner(user1, noteNobodyRead)).toBeTruthy();
}); });
it('for negative case', async () => { it('for negative case', async () => {
expect(await service.isOwner(user1, notes[1])).toBeFalsy(); expect(await service.isOwner(user1, noteUser1Read)).toBeFalsy();
}); });
}); });