forked from hedgedoc/hedgedoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api/private): add GroupsController
The GroupsController can be used to fetch information about groups. Signed-off-by: David Mehren <[email protected]>
- Loading branch information
1 parent
0394679
commit 0be8e4e
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GroupInfoDto> { | ||
return this.groupService.toGroupDto( | ||
await this.groupService.getGroupByName(groupName), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters