From 46d03571c1dcc8a5a7be23403a4ebc2b53c024fe Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Tue, 31 Aug 2021 13:39:51 +0200 Subject: [PATCH] fix: update seed.ts Signed-off-by: Philip Molares --- src/seed.ts | 59 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/seed.ts b/src/seed.ts index cd8b7ce98..3275640e1 100644 --- a/src/seed.ts +++ b/src/seed.ts @@ -10,6 +10,7 @@ import { Author } from './authors/author.entity'; import { Group } from './groups/group.entity'; import { HistoryEntry } from './history/history-entry.entity'; import { Identity } from './identity/identity.entity'; +import { ProviderType } from './identity/provider-type.enum'; import { MediaUpload } from './media/media-upload.entity'; import { Note } from './notes/note.entity'; import { Tag } from './notes/tag.entity'; @@ -19,6 +20,7 @@ import { Edit } from './revisions/edit.entity'; import { Revision } from './revisions/revision.entity'; import { Session } from './users/session.entity'; import { User } from './users/user.entity'; +import { hashPassword } from './utils/password'; /** * This function creates and populates a sqlite db for manual testing @@ -47,6 +49,7 @@ createConnection({ dropSchema: true, }) .then(async (connection) => { + const password = 'test_password'; const users = []; users.push(User.create('hardcoded', 'Test User 1')); users.push(User.create('hardcoded_2', 'Test User 2')); @@ -59,6 +62,9 @@ createConnection({ for (let i = 0; i < 3; i++) { const author = connection.manager.create(Author, Author.create(1)); const user = connection.manager.create(User, users[i]); + const identity = Identity.create(user, ProviderType.LOCAL); + identity.passwordHash = await hashPassword(password); + connection.manager.create(Identity, identity); author.user = user; const revision = Revision.create( 'This is a test note', @@ -70,23 +76,48 @@ createConnection({ notes[i].userPermissions = []; notes[i].groupPermissions = []; user.ownedNotes = [notes[i]]; - await connection.manager.save([notes[i], user, revision, edit, author]); + await connection.manager.save([ + notes[i], + user, + revision, + edit, + author, + identity, + ]); } - const foundUser = await connection.manager.findOne(User); - if (!foundUser) { - throw new Error('Could not find freshly seeded user. Aborting.'); + const foundUsers = await connection.manager.find(User); + if (!foundUsers) { + throw new Error('Could not find freshly seeded users. Aborting.'); } - const foundNote = await connection.manager.findOne(Note); - if (!foundNote) { - throw new Error('Could not find freshly seeded note. Aborting.'); + const foundNotes = await connection.manager.find(Note); + if (!foundNotes) { + throw new Error('Could not find freshly seeded notes. Aborting.'); } - if (!foundNote.alias) { - throw new Error('Could not find alias of freshly seeded note. Aborting.'); + 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 ?? '' + }'`, + ); + } } - const historyEntry = HistoryEntry.create(foundUser, foundNote); - await connection.manager.save(historyEntry); - console.log(`Created User '${foundUser.userName}'`); - console.log(`Created Note '${foundNote.alias}'`); - console.log(`Created HistoryEntry`); }) .catch((error) => console.log(error));