mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-29 06:15:29 -04:00
GroupsService: Create new GroupsService
This service is necessary as we plan to have functions to create and manipulate groups in the future. The GroupInfoDto was moved from the file note-permissions.dto.ts to mimic the UserInfoDto. Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
577811be29
commit
34087561e7
16 changed files with 220 additions and 38 deletions
30
src/groups/group-info.dto.ts
Normal file
30
src/groups/group-info.dto.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { IsBoolean, IsString } from 'class-validator';
|
||||
|
||||
export class GroupInfoDto {
|
||||
/**
|
||||
* Name of the group
|
||||
* @example "superheroes"
|
||||
*/
|
||||
@IsString()
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Display name of this group
|
||||
* @example "Superheroes"
|
||||
*/
|
||||
@IsString()
|
||||
displayName: string;
|
||||
|
||||
/**
|
||||
* True if this group must be specially handled
|
||||
* Used for e.g. "everybody", "all logged in users"
|
||||
* @example false
|
||||
*/
|
||||
@IsBoolean()
|
||||
special: boolean;
|
||||
}
|
|
@ -40,4 +40,15 @@ export class Group {
|
|||
})
|
||||
@JoinTable()
|
||||
members: User[];
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
private constructor() {}
|
||||
|
||||
public static create(name: string, displayName: string): Group {
|
||||
const newGroup = new Group();
|
||||
newGroup.special = false; // this attribute should only be true for the two special groups
|
||||
newGroup.name = name;
|
||||
newGroup.displayName = displayName;
|
||||
return newGroup;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,8 +7,12 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Group } from './group.entity';
|
||||
import { GroupsService } from './groups.service';
|
||||
import { LoggerModule } from '../logger/logger.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Group])],
|
||||
imports: [TypeOrmModule.forFeature([Group]), LoggerModule],
|
||||
providers: [GroupsService],
|
||||
exports: [GroupsService],
|
||||
})
|
||||
export class GroupsModule {}
|
||||
|
|
75
src/groups/groups.service.spec.ts
Normal file
75
src/groups/groups.service.spec.ts
Normal file
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { GroupsService } from './groups.service';
|
||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Group } from './group.entity';
|
||||
import { NotInDBError } from '../errors/errors';
|
||||
import { LoggerModule } from '../logger/logger.module';
|
||||
|
||||
describe('GroupsService', () => {
|
||||
let service: GroupsService;
|
||||
let groupRepo: Repository<Group>;
|
||||
let group: Group;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
GroupsService,
|
||||
{
|
||||
provide: getRepositoryToken(Group),
|
||||
useClass: Repository,
|
||||
},
|
||||
],
|
||||
imports: [LoggerModule],
|
||||
}).compile();
|
||||
|
||||
service = module.get<GroupsService>(GroupsService);
|
||||
groupRepo = module.get<Repository<Group>>(getRepositoryToken(Group));
|
||||
group = Group.create('testGroup', 'Superheros') as Group;
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe('getGroupByName', () => {
|
||||
it('works', async () => {
|
||||
jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(group);
|
||||
const foundGroup = await service.getGroupByName(group.name);
|
||||
expect(foundGroup.name).toEqual(group.name);
|
||||
expect(foundGroup.displayName).toEqual(group.displayName);
|
||||
expect(foundGroup.special).toEqual(group.special);
|
||||
});
|
||||
it('fails with non-existing group', async () => {
|
||||
jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(undefined);
|
||||
try {
|
||||
await service.getGroupByName('i_dont_exist');
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(NotInDBError);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('toGroupDto', () => {
|
||||
it('works', () => {
|
||||
const groupDto = service.toGroupDto(group);
|
||||
expect(groupDto.displayName).toEqual(group.displayName);
|
||||
expect(groupDto.name).toEqual(group.name);
|
||||
expect(groupDto.special).toBeFalsy();
|
||||
});
|
||||
it('fails with null parameter', () => {
|
||||
const groupDto = service.toGroupDto(null);
|
||||
expect(groupDto).toBeNull();
|
||||
});
|
||||
it('fails with undefined parameter', () => {
|
||||
const groupDto = service.toGroupDto(undefined);
|
||||
expect(groupDto).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
57
src/groups/groups.service.ts
Normal file
57
src/groups/groups.service.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConsoleLoggerService } from '../logger/console-logger.service';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Group } from './group.entity';
|
||||
import { NotInDBError } from '../errors/errors';
|
||||
import { GroupInfoDto } from './group-info.dto';
|
||||
|
||||
@Injectable()
|
||||
export class GroupsService {
|
||||
constructor(
|
||||
private readonly logger: ConsoleLoggerService,
|
||||
@InjectRepository(Group) private groupRepository: Repository<Group>,
|
||||
) {
|
||||
this.logger.setContext(GroupsService.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @async
|
||||
* Get a group by their name.
|
||||
* @param {string} name - the groups name
|
||||
* @return {Group} the group
|
||||
* @throws {NotInDBError} there is no group with this name
|
||||
*/
|
||||
async getGroupByName(name: string): Promise<Group> {
|
||||
const group = await this.groupRepository.findOne({
|
||||
where: { name: name },
|
||||
});
|
||||
if (group === undefined) {
|
||||
throw new NotInDBError(`Group with name '${name}' not found`);
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build GroupInfoDto from a group.
|
||||
* @param {Group} group - the group to use
|
||||
* @return {GroupInfoDto} the built GroupInfoDto
|
||||
*/
|
||||
toGroupDto(group: Group | null | undefined): GroupInfoDto | null {
|
||||
if (!group) {
|
||||
this.logger.warn(`Recieved ${group} argument!`, 'toGroupDto');
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
name: group.name,
|
||||
displayName: group.displayName,
|
||||
special: group.special,
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue