2021-04-29 09:53:59 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import { createConnection } from 'typeorm';
|
2021-08-29 16:45:46 +00:00
|
|
|
|
|
|
|
import { AuthToken } from './auth/auth-token.entity';
|
2021-05-19 19:09:15 +00:00
|
|
|
import { Author } from './authors/author.entity';
|
2021-04-29 09:53:59 +00:00
|
|
|
import { Group } from './groups/group.entity';
|
|
|
|
import { HistoryEntry } from './history/history-entry.entity';
|
2021-08-08 19:53:20 +00:00
|
|
|
import { Identity } from './identity/identity.entity';
|
2021-08-31 11:39:51 +00:00
|
|
|
import { ProviderType } from './identity/provider-type.enum';
|
2021-04-29 09:53:59 +00:00
|
|
|
import { MediaUpload } from './media/media-upload.entity';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { Note } from './notes/note.entity';
|
2021-04-29 09:53:59 +00:00
|
|
|
import { Tag } from './notes/tag.entity';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { NoteGroupPermission } from './permissions/note-group-permission.entity';
|
|
|
|
import { NoteUserPermission } from './permissions/note-user-permission.entity';
|
|
|
|
import { Edit } from './revisions/edit.entity';
|
|
|
|
import { Revision } from './revisions/revision.entity';
|
|
|
|
import { Session } from './users/session.entity';
|
|
|
|
import { User } from './users/user.entity';
|
2021-08-31 11:39:51 +00:00
|
|
|
import { hashPassword } from './utils/password';
|
2021-04-29 09:53:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function creates and populates a sqlite db for manual testing
|
|
|
|
*/
|
|
|
|
createConnection({
|
|
|
|
type: 'sqlite',
|
|
|
|
database: './hedgedoc.sqlite',
|
|
|
|
entities: [
|
|
|
|
User,
|
|
|
|
Note,
|
|
|
|
Revision,
|
2021-05-31 19:46:41 +00:00
|
|
|
Edit,
|
2021-04-29 09:53:59 +00:00
|
|
|
NoteGroupPermission,
|
|
|
|
NoteUserPermission,
|
|
|
|
Group,
|
|
|
|
HistoryEntry,
|
|
|
|
MediaUpload,
|
|
|
|
Tag,
|
|
|
|
AuthToken,
|
|
|
|
Identity,
|
2021-05-19 19:09:15 +00:00
|
|
|
Author,
|
|
|
|
Session,
|
2021-04-29 09:53:59 +00:00
|
|
|
],
|
|
|
|
synchronize: true,
|
|
|
|
logging: false,
|
2021-05-19 19:09:15 +00:00
|
|
|
dropSchema: true,
|
2021-04-29 09:53:59 +00:00
|
|
|
})
|
|
|
|
.then(async (connection) => {
|
2021-08-31 11:39:51 +00:00
|
|
|
const password = 'test_password';
|
2021-05-19 19:09:15 +00:00
|
|
|
const users = [];
|
|
|
|
users.push(User.create('hardcoded', 'Test User 1'));
|
|
|
|
users.push(User.create('hardcoded_2', 'Test User 2'));
|
|
|
|
users.push(User.create('hardcoded_3', 'Test User 3'));
|
|
|
|
const notes: Note[] = [];
|
|
|
|
notes.push(Note.create(undefined, 'test'));
|
|
|
|
notes.push(Note.create(undefined, 'test2'));
|
|
|
|
notes.push(Note.create(undefined, 'test3'));
|
|
|
|
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
const author = connection.manager.create(Author, Author.create(1));
|
|
|
|
const user = connection.manager.create(User, users[i]);
|
2021-08-31 11:39:51 +00:00
|
|
|
const identity = Identity.create(user, ProviderType.LOCAL);
|
|
|
|
identity.passwordHash = await hashPassword(password);
|
|
|
|
connection.manager.create(Identity, identity);
|
2021-05-19 19:09:15 +00:00
|
|
|
author.user = user;
|
|
|
|
const revision = Revision.create(
|
|
|
|
'This is a test note',
|
|
|
|
'This is a test note',
|
|
|
|
);
|
2021-05-31 19:46:41 +00:00
|
|
|
const edit = Edit.create(author, 1, 42);
|
|
|
|
revision.edits = [edit];
|
2021-05-19 19:09:15 +00:00
|
|
|
notes[i].revisions = Promise.all([revision]);
|
|
|
|
notes[i].userPermissions = [];
|
|
|
|
notes[i].groupPermissions = [];
|
|
|
|
user.ownedNotes = [notes[i]];
|
2021-08-31 11:39:51 +00:00
|
|
|
await connection.manager.save([
|
|
|
|
notes[i],
|
|
|
|
user,
|
|
|
|
revision,
|
|
|
|
edit,
|
|
|
|
author,
|
|
|
|
identity,
|
|
|
|
]);
|
2021-05-19 19:09:15 +00:00
|
|
|
}
|
2021-08-31 11:39:51 +00:00
|
|
|
const foundUsers = await connection.manager.find(User);
|
|
|
|
if (!foundUsers) {
|
|
|
|
throw new Error('Could not find freshly seeded users. Aborting.');
|
2021-05-02 16:35:38 +00:00
|
|
|
}
|
2021-08-31 11:39:51 +00:00
|
|
|
const foundNotes = await connection.manager.find(Note);
|
|
|
|
if (!foundNotes) {
|
|
|
|
throw new Error('Could not find freshly seeded notes. Aborting.');
|
2021-05-02 16:35:38 +00:00
|
|
|
}
|
2021-08-31 11:39:51 +00:00
|
|
|
for (const note of foundNotes) {
|
|
|
|
if (!note.alias) {
|
|
|
|
throw new Error(
|
|
|
|
'Could not find alias of freshly seeded notes. Aborting.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const user of foundUsers) {
|
|
|
|
console.log(
|
|
|
|
`Created User '${user.userName}' with password '${password}'`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (const note of foundNotes) {
|
|
|
|
console.log(`Created Note '${note.alias ?? ''}'`);
|
|
|
|
}
|
|
|
|
for (const user of foundUsers) {
|
|
|
|
for (const note of foundNotes) {
|
|
|
|
const historyEntry = HistoryEntry.create(user, note);
|
|
|
|
await connection.manager.save(historyEntry);
|
|
|
|
console.log(
|
|
|
|
`Created HistoryEntry for user '${user.userName}' and note '${
|
|
|
|
note.alias ?? ''
|
|
|
|
}'`,
|
|
|
|
);
|
|
|
|
}
|
2021-05-02 16:35:38 +00:00
|
|
|
}
|
2021-04-29 09:53:59 +00:00
|
|
|
})
|
|
|
|
.catch((error) => console.log(error));
|