2021-03-20 14:01:04 -04:00
|
|
|
/*
|
2022-02-19 14:47:28 -05:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-03-20 14:01:04 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2021-08-29 12:45:46 -04:00
|
|
|
import { promises as fs } from 'fs';
|
2021-04-29 16:47:18 -04:00
|
|
|
import request from 'supertest';
|
2021-08-29 12:45:46 -04:00
|
|
|
|
|
|
|
import { NotInDBError } from '../../src/errors/errors';
|
|
|
|
import { Note } from '../../src/notes/note.entity';
|
2022-01-30 15:54:30 -05:00
|
|
|
import { UserLoginInfoDto } from '../../src/users/user-info.dto';
|
2021-03-20 14:01:04 -04:00
|
|
|
import { User } from '../../src/users/user.entity';
|
2022-01-06 15:41:36 -05:00
|
|
|
import { TestSetup, TestSetupBuilder } from '../test-setup';
|
2021-03-20 14:01:04 -04:00
|
|
|
|
|
|
|
describe('Me', () => {
|
2021-10-14 16:01:18 -04:00
|
|
|
let testSetup: TestSetup;
|
|
|
|
|
2021-03-20 14:01:04 -04:00
|
|
|
let uploadPath: string;
|
|
|
|
let user: User;
|
|
|
|
let content: string;
|
|
|
|
let note1: Note;
|
2021-06-06 11:55:41 -04:00
|
|
|
let alias2: string;
|
2021-03-20 14:01:04 -04:00
|
|
|
let note2: Note;
|
2021-10-13 14:43:56 -04:00
|
|
|
let agent: request.SuperAgentTest;
|
2021-03-20 14:01:04 -04:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-01-06 15:41:36 -05:00
|
|
|
testSetup = await TestSetupBuilder.create().build();
|
2021-10-14 16:01:18 -04:00
|
|
|
|
|
|
|
uploadPath =
|
|
|
|
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
|
2022-09-24 20:05:30 -04:00
|
|
|
const username = 'hardcoded';
|
|
|
|
const password = 'AHardcodedStrongP@ssword123';
|
2021-10-14 16:01:18 -04:00
|
|
|
await testSetup.app.init();
|
|
|
|
|
2022-09-24 20:05:30 -04:00
|
|
|
user = await testSetup.userService.createUser(username, 'Testy');
|
|
|
|
await testSetup.identityService.createLocalIdentity(user, password);
|
2021-10-13 14:43:56 -04:00
|
|
|
|
2021-03-20 14:01:04 -04:00
|
|
|
content = 'This is a test note.';
|
2021-06-06 11:55:41 -04:00
|
|
|
alias2 = 'note2';
|
2021-11-14 15:44:59 -05:00
|
|
|
note1 = await testSetup.notesService.createNote(content, user);
|
|
|
|
note2 = await testSetup.notesService.createNote(content, user, alias2);
|
2021-10-14 16:01:18 -04:00
|
|
|
agent = request.agent(testSetup.app.getHttpServer());
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.post('/api/private/auth/local/login')
|
2022-09-24 20:05:30 -04:00
|
|
|
.send({ username: username, password: password })
|
2021-10-13 14:43:56 -04:00
|
|
|
.expect(201);
|
2021-03-20 14:01:04 -04:00
|
|
|
});
|
|
|
|
|
2022-03-04 07:13:46 -05:00
|
|
|
afterAll(async () => {
|
|
|
|
await testSetup.cleanup();
|
|
|
|
});
|
|
|
|
|
2021-03-20 14:01:04 -04:00
|
|
|
it('GET /me', async () => {
|
2022-01-30 15:54:30 -05:00
|
|
|
const userInfo = testSetup.userService.toUserLoginInfoDto(user, 'local');
|
2021-10-13 14:43:56 -04:00
|
|
|
const response = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/me')
|
2021-03-20 14:01:04 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2022-01-30 15:54:30 -05:00
|
|
|
const gotUser = response.body as UserLoginInfoDto;
|
2021-03-20 14:01:04 -04:00
|
|
|
expect(gotUser).toEqual(userInfo);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('GET /me/media', async () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
const responseBefore = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/me/media/')
|
2021-03-20 14:01:04 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(responseBefore.body).toHaveLength(0);
|
|
|
|
|
|
|
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2022-03-05 12:47:45 -05:00
|
|
|
const imageUrls = [];
|
|
|
|
imageUrls.push(
|
2022-01-29 12:57:44 -05:00
|
|
|
(await testSetup.mediaService.saveFile(testImage, user, note1)).fileUrl,
|
2022-03-05 12:47:45 -05:00
|
|
|
);
|
|
|
|
imageUrls.push(
|
2022-01-29 12:57:44 -05:00
|
|
|
(await testSetup.mediaService.saveFile(testImage, user, note1)).fileUrl,
|
2022-03-05 12:47:45 -05:00
|
|
|
);
|
|
|
|
imageUrls.push(
|
2022-01-29 12:57:44 -05:00
|
|
|
(await testSetup.mediaService.saveFile(testImage, user, note2)).fileUrl,
|
2022-03-05 12:47:45 -05:00
|
|
|
);
|
|
|
|
imageUrls.push(
|
2022-01-29 12:57:44 -05:00
|
|
|
(await testSetup.mediaService.saveFile(testImage, user, note2)).fileUrl,
|
2022-03-05 12:47:45 -05:00
|
|
|
);
|
2021-03-20 14:01:04 -04:00
|
|
|
|
2021-10-13 14:43:56 -04:00
|
|
|
const response = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/me/media/')
|
2021-03-20 14:01:04 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveLength(4);
|
2022-03-05 12:47:45 -05:00
|
|
|
expect(imageUrls).toContain(response.body[0].url);
|
|
|
|
expect(imageUrls).toContain(response.body[1].url);
|
|
|
|
expect(imageUrls).toContain(response.body[2].url);
|
|
|
|
expect(imageUrls).toContain(response.body[3].url);
|
2021-10-14 16:01:18 -04:00
|
|
|
const mediaUploads = await testSetup.mediaService.listUploadsByUser(user);
|
2021-03-20 14:01:04 -04:00
|
|
|
for (const upload of mediaUploads) {
|
2021-10-14 16:01:18 -04:00
|
|
|
await testSetup.mediaService.deleteFile(upload);
|
2021-03-20 14:01:04 -04:00
|
|
|
}
|
|
|
|
await fs.rmdir(uploadPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('POST /me/profile', async () => {
|
|
|
|
const newDisplayName = 'Another name';
|
|
|
|
expect(user.displayName).not.toEqual(newDisplayName);
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.post('/api/private/me/profile')
|
2021-03-20 14:01:04 -04:00
|
|
|
.send({
|
2022-02-05 10:21:13 -05:00
|
|
|
displayName: newDisplayName,
|
2021-03-20 14:01:04 -04:00
|
|
|
})
|
2022-02-19 14:47:28 -05:00
|
|
|
.expect(201);
|
2021-10-14 16:01:18 -04:00
|
|
|
const dbUser = await testSetup.userService.getUserByUsername('hardcoded');
|
2021-03-20 14:01:04 -04:00
|
|
|
expect(dbUser.displayName).toEqual(newDisplayName);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('DELETE /me', async () => {
|
|
|
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2022-01-29 12:57:44 -05:00
|
|
|
const upload = await testSetup.mediaService.saveFile(
|
|
|
|
testImage,
|
|
|
|
user,
|
|
|
|
note1,
|
|
|
|
);
|
2021-10-14 16:01:18 -04:00
|
|
|
const dbUser = await testSetup.userService.getUserByUsername('hardcoded');
|
2021-03-20 14:01:04 -04:00
|
|
|
expect(dbUser).toBeInstanceOf(User);
|
2021-10-14 16:01:18 -04:00
|
|
|
const mediaUploads = await testSetup.mediaService.listUploadsByUser(dbUser);
|
2021-03-20 14:01:04 -04:00
|
|
|
expect(mediaUploads).toHaveLength(1);
|
2022-01-29 12:57:44 -05:00
|
|
|
expect(mediaUploads[0].fileUrl).toEqual(upload.fileUrl);
|
2021-10-15 11:06:56 -04:00
|
|
|
await agent.delete('/api/private/me').expect(204);
|
2021-10-14 16:01:18 -04:00
|
|
|
await expect(
|
|
|
|
testSetup.userService.getUserByUsername('hardcoded'),
|
|
|
|
).rejects.toThrow(NotInDBError);
|
|
|
|
const mediaUploadsAfter = await testSetup.mediaService.listUploadsByNote(
|
|
|
|
note1,
|
2021-03-20 14:01:04 -04:00
|
|
|
);
|
|
|
|
expect(mediaUploadsAfter).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|