factory for login user

This commit is contained in:
Moriz Wahl 2021-11-23 11:17:03 +01:00
parent 2af825b2ca
commit d4883928f9
5 changed files with 77 additions and 11 deletions

View File

@ -3,22 +3,28 @@ import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
// Moriz: I do not like the idea of having two user tables
@Entity('state_users')
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
@PrimaryGeneratedColumn('increment', { unsigned: true })
id: number
@Column({ default: 0, name: 'index_id' })
indexId: number
@Column({ default: 0, name: 'group_id', unsigned: true })
groupId: number
@Column({ type: 'binary', length: 32, name: 'public_key' })
pubkey: Buffer
@Column()
@Column({ length: 255, nullable: true, default: null })
email: string
@Column({ name: 'first_name' })
@Column({ name: 'first_name', length: 255, nullable: true, default: null })
firstName: string
@Column({ name: 'last_name' })
@Column({ name: 'last_name', length: 255, nullable: true, default: null })
lastName: string
@Column()
@Column({ length: 255, nullable: true, default: null })
username: string
@Column()

View File

@ -4,22 +4,28 @@ import { UserSetting } from './UserSetting'
// Moriz: I do not like the idea of having two user tables
@Entity('state_users')
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
@PrimaryGeneratedColumn('increment', { unsigned: true })
id: number
@Column({ default: 0, name: 'index_id' })
indexId: number
@Column({ default: 0, name: 'group_id', unsigned: true })
groupId: number
@Column({ type: 'binary', length: 32, name: 'public_key' })
pubkey: Buffer
@Column()
@Column({ length: 255, nullable: true, default: null })
email: string
@Column({ name: 'first_name' })
@Column({ name: 'first_name', length: 255, nullable: true, default: null })
firstName: string
@Column({ name: 'last_name' })
@Column({ name: 'last_name', length: 255, nullable: true, default: null })
lastName: string
@Column()
@Column({ length: 255, nullable: true, default: null })
username: string
@Column()

View File

@ -0,0 +1,47 @@
import Faker from 'faker'
import { define } from 'typeorm-seeding'
import { LoginUser } from '../../entity/LoginUser'
import { randomBytes } from 'crypto'
interface LoginUserContext {
email?: string
firstName?: string
lastName?: string
username?: string
description?: string
password?: BigInt
pubKey?: Buffer
privKey?: Buffer
emailHash?: Buffer
createdAt?: Date
emailChecked?: boolean
passphraseShown?: boolean
language?: string
disabled?: boolean
groupId?: number
publisherId?: number
}
define(LoginUser, (faker: typeof Faker, context?: LoginUserContext) => {
if (!context) context = {}
const user = new LoginUser()
user.email = context.email ? context.email : faker.internet.email()
user.firstName = context.firstName ? context.firstName : faker.name.firstName()
user.lastName = context.lastName ? context.lastName : faker.name.lastName()
user.username = context.username ? context.username : faker.internet.userName()
user.description = context.description ? context.description : faker.random.words(4)
user.password = context.password ? context.password : BigInt(0)
user.pubKey = context.pubKey ? context.pubKey : randomBytes(32)
user.privKey = context.privKey ? context.privKey : randomBytes(80)
user.emailHash = context.emailHash ? context.emailHash : randomBytes(32)
user.createdAt = context.createdAt ? context.createdAt : faker.date.recent()
user.emailChecked = context.emailChecked ? context.emailChecked : true
user.passphraseShown = context.passphraseShown ? context.passphraseShown : false
user.language = context.language ? context.language : 'en'
user.disabled = context.disabled ? context.disabled : false
user.groupId = context.groupId ? context.groupId : 1
user.publisherId = context.publisherId ? context.publisherId : 0
return user
})

View File

@ -22,6 +22,8 @@ define(User, (faker: typeof Faker, context?: UserContext) => {
user.lastName = context.lastName ? context.lastName : faker.name.lastName()
user.username = context.username ? context.username : faker.internet.userName()
user.disabled = context.disabled ? context.disabled : false
user.groupId = 0
user.indexId = 0
return user
})

View File

@ -3,6 +3,11 @@ import { User } from '../../entity/User'
export class CreateUserSeed implements Seeder {
public async run(factory: Factory): Promise<void> {
await factory(User)().create()
await factory(User)({
email: 'peter@lustig.de',
firstName: 'Peter',
lastName: 'Lustig',
username: 'peter',
}).create()
}
}