aboutsummaryrefslogtreecommitdiff
path: root/test/test-db.ts
blob: fa62ad7b6104343c1effb21f5673654f6874e689 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { default as Project } from '../src/db/model/Project';
import { default as User } from '../src/db/model/User';
import { default as UserGroup } from '../src/db/model/UserGroup';

import { Sequelize } from 'sequelize-typescript';
import * as sqlite from 'sqlite3';

export type Database = Sequelize;

export const initDB: () => Database = (): Database => {
	const dataFile: string = ':memory:';
	const db: sqlite.Database = new sqlite.Database(dataFile);
	const seq: Database = new Sequelize(`sqlite:${dataFile}`);
	seq.addModels([`${__dirname}/../src/db/model`]);
	seq.options.logging = false;
	return seq;
};

export const resetDB: (database: Database) => Promise<void> = async(
	database: Database
): Promise<void> => {
	await database.sync({
		force: true
	});
};

export interface Credentials {
	username: string;
	password: string;
	usergroups: UserGroup[];
}

export interface Projec {
	name: string;
	contributors: Credentials[];
	creationDate: Date;
	lastActivity: Date;
}

export const addUser: (database?: Database, admin?: boolean) =>
	Promise<Credentials> = async(
		database?: Database, admin?: boolean
): Promise<Credentials> => {
	const mockUser: Credentials = {
		username: 'mock_user',
		password: 'mock_password',
		usergroups: []
	};
	if (admin) {
		const priv: UserGroup = new UserGroup({
			name: 'admin',
			canCreateUsers: true,
			canDeleteUsers: true,
			canEditUsers: true,
			canCreateProjects: true,
			canDeleteProjects: true,
			canEditProjects: true,
			isInternal: false
		});
		await priv.save();
		mockUser.usergroups.push(priv);
	}
	await new User(mockUser).save();
	return mockUser;
};

export const addProjec: (database: Database, user: Credentials) =>
	Promise<Projec> = async(
		database: Database, user: Credentials
): Promise<Projec> => {
	const mockProject: Projec = {
		name: 'mocky',
		contributors: [user],
		creationDate: new Date(),
		lastActivity: new Date()
	};

	await new Project(mockProject).save();
	return mockProject;
};