mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-18 09:04:44 -04:00
Fix tests with using sessions in e2e tests of private api
Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
parent
28266bca0b
commit
276d423ee2
6 changed files with 132 additions and 88 deletions
|
@ -17,6 +17,7 @@ import request from 'supertest';
|
|||
|
||||
import { PrivateApiModule } from '../../src/api/private/private-api.module';
|
||||
import { AuthModule } from '../../src/auth/auth.module';
|
||||
import { AuthConfig } from '../../src/config/auth.config';
|
||||
import appConfigMock from '../../src/config/mock/app.config.mock';
|
||||
import authConfigMock from '../../src/config/mock/auth.config.mock';
|
||||
import customizationConfigMock from '../../src/config/mock/customization.config.mock';
|
||||
|
@ -25,6 +26,7 @@ import mediaConfigMock from '../../src/config/mock/media.config.mock';
|
|||
import { NotInDBError } from '../../src/errors/errors';
|
||||
import { GroupsModule } from '../../src/groups/groups.module';
|
||||
import { HistoryModule } from '../../src/history/history.module';
|
||||
import { IdentityService } from '../../src/identity/identity.service';
|
||||
import { LoggerModule } from '../../src/logger/logger.module';
|
||||
import { MediaModule } from '../../src/media/media.module';
|
||||
import { MediaService } from '../../src/media/media.service';
|
||||
|
@ -36,17 +38,20 @@ import { UserInfoDto } from '../../src/users/user-info.dto';
|
|||
import { User } from '../../src/users/user.entity';
|
||||
import { UsersModule } from '../../src/users/users.module';
|
||||
import { UsersService } from '../../src/users/users.service';
|
||||
import { setupSessionMiddleware } from '../../src/utils/session';
|
||||
|
||||
describe('Me', () => {
|
||||
let app: INestApplication;
|
||||
let userService: UsersService;
|
||||
let mediaService: MediaService;
|
||||
let identityService: IdentityService;
|
||||
let uploadPath: string;
|
||||
let user: User;
|
||||
let content: string;
|
||||
let note1: Note;
|
||||
let alias2: string;
|
||||
let note2: Note;
|
||||
let agent: request.SuperAgentTest;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
|
@ -82,21 +87,31 @@ describe('Me', () => {
|
|||
const config = moduleRef.get<ConfigService>(ConfigService);
|
||||
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
|
||||
app = moduleRef.createNestApplication();
|
||||
const authConfig = config.get('authConfig') as AuthConfig;
|
||||
setupSessionMiddleware(app, authConfig);
|
||||
await app.init();
|
||||
//historyService = moduleRef.get();
|
||||
userService = moduleRef.get(UsersService);
|
||||
mediaService = moduleRef.get(MediaService);
|
||||
identityService = moduleRef.get(IdentityService);
|
||||
user = await userService.createUser('hardcoded', 'Testy');
|
||||
await identityService.createLocalIdentity(user, 'test');
|
||||
|
||||
const notesService = moduleRef.get(NotesService);
|
||||
content = 'This is a test note.';
|
||||
alias2 = 'note2';
|
||||
note1 = await notesService.createNote(content, undefined, user);
|
||||
note2 = await notesService.createNote(content, alias2, user);
|
||||
agent = request.agent(app.getHttpServer());
|
||||
await agent
|
||||
.post('/auth/local/login')
|
||||
.send({ username: 'hardcoded', password: 'test' })
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
it('GET /me', async () => {
|
||||
const userInfo = userService.toUserDto(user);
|
||||
const response = await request(app.getHttpServer())
|
||||
const response = await agent
|
||||
.get('/me')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200);
|
||||
|
@ -105,8 +120,7 @@ describe('Me', () => {
|
|||
});
|
||||
|
||||
it('GET /me/media', async () => {
|
||||
const httpServer = app.getHttpServer();
|
||||
const responseBefore = await request(httpServer)
|
||||
const responseBefore = await agent
|
||||
.get('/me/media/')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200);
|
||||
|
@ -118,7 +132,7 @@ describe('Me', () => {
|
|||
const url2 = await mediaService.saveFile(testImage, user, note2);
|
||||
const url3 = await mediaService.saveFile(testImage, user, note2);
|
||||
|
||||
const response = await request(httpServer)
|
||||
const response = await agent
|
||||
.get('/me/media/')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200);
|
||||
|
@ -137,7 +151,7 @@ describe('Me', () => {
|
|||
it('POST /me/profile', async () => {
|
||||
const newDisplayName = 'Another name';
|
||||
expect(user.displayName).not.toEqual(newDisplayName);
|
||||
await request(app.getHttpServer())
|
||||
await agent
|
||||
.post('/me/profile')
|
||||
.send({
|
||||
name: newDisplayName,
|
||||
|
@ -155,7 +169,7 @@ describe('Me', () => {
|
|||
const mediaUploads = await mediaService.listUploadsByUser(dbUser);
|
||||
expect(mediaUploads).toHaveLength(1);
|
||||
expect(mediaUploads[0].fileUrl).toEqual(url0);
|
||||
await request(app.getHttpServer()).delete('/me').expect(204);
|
||||
await agent.delete('/me').expect(204);
|
||||
await expect(userService.getUserByUsername('hardcoded')).rejects.toThrow(
|
||||
NotInDBError,
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue