mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 11:16:31 -05:00
feat(api/private): add UsersController
The UsersController can be used to fetch information about users. Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
908f418620
commit
a7edf00ebc
3 changed files with 76 additions and 0 deletions
|
@ -23,6 +23,7 @@ import { MeController } from './me/me.controller';
|
|||
import { MediaController } from './media/media.controller';
|
||||
import { NotesController } from './notes/notes.controller';
|
||||
import { TokensController } from './tokens/tokens.controller';
|
||||
import { UsersController } from './users/users.controller';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
@ -46,6 +47,8 @@ import { TokensController } from './tokens/tokens.controller';
|
|||
NotesController,
|
||||
AliasController,
|
||||
AuthController,
|
||||
UsersController,
|
||||
GroupController,
|
||||
],
|
||||
})
|
||||
export class PrivateApiModule {}
|
||||
|
|
31
src/api/private/users/users.controller.ts
Normal file
31
src/api/private/users/users.controller.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { Controller, Get, Param } from '@nestjs/common';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
||||
import { UserInfoDto } from '../../../users/user-info.dto';
|
||||
import { UsersService } from '../../../users/users.service';
|
||||
import { OpenApi } from '../../utils/openapi.decorator';
|
||||
|
||||
@ApiTags('users')
|
||||
@Controller('users')
|
||||
export class UsersController {
|
||||
constructor(
|
||||
private readonly logger: ConsoleLoggerService,
|
||||
private userService: UsersService,
|
||||
) {
|
||||
this.logger.setContext(UsersController.name);
|
||||
}
|
||||
|
||||
@Get(':username')
|
||||
@OpenApi(200)
|
||||
async getUser(@Param('username') username: string): Promise<UserInfoDto> {
|
||||
return this.userService.toUserDto(
|
||||
await this.userService.getUserByUsername(username),
|
||||
);
|
||||
}
|
||||
}
|
42
test/private-api/users.e2e-spec.ts
Normal file
42
test/private-api/users.e2e-spec.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import request from 'supertest';
|
||||
|
||||
import { TestSetup, TestSetupBuilder } from '../test-setup';
|
||||
|
||||
describe('Users', () => {
|
||||
let testSetup: TestSetup;
|
||||
|
||||
beforeEach(async () => {
|
||||
testSetup = await TestSetupBuilder.create().withUsers().build();
|
||||
await testSetup.app.init();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await testSetup.app.close();
|
||||
});
|
||||
|
||||
test('details for existing users can be retrieved', async () => {
|
||||
let response = await request
|
||||
.agent(testSetup.app.getHttpServer())
|
||||
.get('/api/private/users/testuser1');
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.username).toBe('testuser1');
|
||||
|
||||
response = await request
|
||||
.agent(testSetup.app.getHttpServer())
|
||||
.get('/api/private/users/testuser2');
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.username).toBe('testuser2');
|
||||
});
|
||||
|
||||
test('details for non-existing users cannot be retrieved', async () => {
|
||||
const response = await request
|
||||
.agent(testSetup.app.getHttpServer())
|
||||
.get('/api/private/users/i_dont_exist');
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue