From 0be8e4ea554ca7e598e4b72988a70647f8f91f1c Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 27 Feb 2022 19:02:28 +0100 Subject: [PATCH] feat(api/private): add GroupsController The GroupsController can be used to fetch information about groups. Signed-off-by: David Mehren --- src/api/private/groups/groups.controller.ts | 34 ++++++++++++ src/api/private/private-api.module.ts | 5 +- test/private-api/groups.e2e-spec.ts | 60 +++++++++++++++++++++ test/test-setup.ts | 4 ++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/api/private/groups/groups.controller.ts create mode 100644 test/private-api/groups.e2e-spec.ts diff --git a/src/api/private/groups/groups.controller.ts b/src/api/private/groups/groups.controller.ts new file mode 100644 index 000000000..c9d43edf5 --- /dev/null +++ b/src/api/private/groups/groups.controller.ts @@ -0,0 +1,34 @@ +/* + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { Controller, Get, Param, UseGuards } from '@nestjs/common'; +import { ApiTags } from '@nestjs/swagger'; + +import { GroupInfoDto } from '../../../groups/group-info.dto'; +import { GroupsService } from '../../../groups/groups.service'; +import { SessionGuard } from '../../../identity/session.guard'; +import { ConsoleLoggerService } from '../../../logger/console-logger.service'; +import { OpenApi } from '../../utils/openapi.decorator'; + +@UseGuards(SessionGuard) +@OpenApi(401, 403) +@ApiTags('groups') +@Controller('groups') +export class GroupsController { + constructor( + private readonly logger: ConsoleLoggerService, + private groupService: GroupsService, + ) { + this.logger.setContext(GroupsController.name); + } + + @Get(':groupName') + @OpenApi(200) + async getGroup(@Param('groupName') groupName: string): Promise { + return this.groupService.toGroupDto( + await this.groupService.getGroupByName(groupName), + ); + } +} diff --git a/src/api/private/private-api.module.ts b/src/api/private/private-api.module.ts index 3c8c83db5..448d41713 100644 --- a/src/api/private/private-api.module.ts +++ b/src/api/private/private-api.module.ts @@ -7,6 +7,7 @@ import { Module } from '@nestjs/common'; import { AuthModule } from '../../auth/auth.module'; import { FrontendConfigModule } from '../../frontend-config/frontend-config.module'; +import { GroupsModule } from '../../groups/groups.module'; import { HistoryModule } from '../../history/history.module'; import { IdentityModule } from '../../identity/identity.module'; import { LoggerModule } from '../../logger/logger.module'; @@ -18,6 +19,7 @@ import { UsersModule } from '../../users/users.module'; import { AliasController } from './alias/alias.controller'; import { AuthController } from './auth/auth.controller'; import { ConfigController } from './config/config.controller'; +import { GroupsController } from './groups/groups.controller'; import { HistoryController } from './me/history/history.controller'; import { MeController } from './me/me.controller'; import { MediaController } from './media/media.controller'; @@ -37,6 +39,7 @@ import { UsersController } from './users/users.controller'; MediaModule, RevisionsModule, IdentityModule, + GroupsModule, ], controllers: [ TokensController, @@ -48,7 +51,7 @@ import { UsersController } from './users/users.controller'; AliasController, AuthController, UsersController, - GroupController, + GroupsController, ], }) export class PrivateApiModule {} diff --git a/test/private-api/groups.e2e-spec.ts b/test/private-api/groups.e2e-spec.ts new file mode 100644 index 000000000..5b8f888dc --- /dev/null +++ b/test/private-api/groups.e2e-spec.ts @@ -0,0 +1,60 @@ +/* + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import request from 'supertest'; + +import { LoginDto } from '../../src/identity/local/login.dto'; +import { TestSetup, TestSetupBuilder } from '../test-setup'; + +describe('Groups', () => { + let testSetup: TestSetup; + let testuser1Session: request.SuperAgentTest; + + beforeEach(async () => { + testSetup = await TestSetupBuilder.create().withUsers().build(); + await testSetup.app.init(); + + // create a test group + await testSetup.groupService.createGroup('testgroup1', 'testgroup1', false); + + // log in to create a session + const loginDto: LoginDto = { + password: 'testuser1', + username: 'testuser1', + }; + testuser1Session = request.agent(testSetup.app.getHttpServer()); + await testuser1Session + .post('/api/private/auth/local/login') + .set('Content-Type', 'application/json') + .send(JSON.stringify(loginDto)) + .expect(201); + }); + + afterEach(async () => { + await testSetup.app.close(); + }); + + test('details for existing groups can be retrieved', async () => { + const response = await testuser1Session.get( + '/api/private/groups/testgroup1', + ); + expect(response.status).toBe(200); + expect(response.body.name).toBe('testgroup1'); + }); + + test('details for non-existing groups cannot be retrieved', async () => { + const response = await testuser1Session.get( + '/api/private/groups/i_dont_exist', + ); + expect(response.status).toBe(404); + }); + + test('API requires authentication', async () => { + const response = await request(testSetup.app.getHttpServer()).get( + '/api/private/groups/testgroup1', + ); + expect(response.status).toBe(401); + }); +}); diff --git a/test/test-setup.ts b/test/test-setup.ts index 81ca8dc8a..d2b929c33 100644 --- a/test/test-setup.ts +++ b/test/test-setup.ts @@ -27,6 +27,7 @@ import noteConfigMock from '../src/config/mock/note.config.mock'; import { ErrorExceptionMapping } from '../src/errors/error-mapping'; import { FrontendConfigModule } from '../src/frontend-config/frontend-config.module'; import { GroupsModule } from '../src/groups/groups.module'; +import { GroupsService } from '../src/groups/groups.service'; import { HistoryModule } from '../src/history/history.module'; import { HistoryService } from '../src/history/history.service'; import { IdentityModule } from '../src/identity/identity.module'; @@ -51,6 +52,7 @@ export class TestSetup { app: NestExpressApplication; userService: UsersService; + groupService: GroupsService; configService: ConfigService; identityService: IdentityService; notesService: NotesService; @@ -152,6 +154,8 @@ export class TestSetupBuilder { this.testSetup.userService = this.testSetup.moduleRef.get(UsersService); + this.testSetup.groupService = + this.testSetup.moduleRef.get(GroupsService); this.testSetup.configService = this.testSetup.moduleRef.get(ConfigService); this.testSetup.identityService =