aboutsummaryrefslogtreecommitdiff
path: root/test/test-db.ts
blob: ef980c568ed954e2d2e1963dfeb8809a72eaf26f (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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 { default as File } from '../src/db/model/File';
import { FileTypeName } from '../src/files';

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 interface Filee {
	name: string,
	path: string,
	uuid: string,
	creator: Credentials,
	type: FileTypeName
}

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: []
	};

	const priv: UserGroup = new UserGroup({
		name: 'admin',
		canCreateUsers: true,
		canDeleteUsers: true,
		canEditUsers: true,
		canCreateProjects: true,
		canDeleteProjects: true,
		canEditProjects: true,
		canAccessLogs: true,
		isInternal: false
	});
	await priv.save();
	mockUser.usergroups.push(priv);

	const logging: UserGroup = new UserGroup({
		name: 'logging',
		canAccessLogs: true,
		isInternal: false,
		description: 'Allows Post to Log'
	});
	mockUser.usergroups.push(logging);

	await logging.save();

	const root: User = await new User(mockUser).save();

	await root.$set('userGroups', [priv, logging]);

	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;
};

export const addFilee: (database: Database, user: Credentials) =>
	Promise<Filee> = async(
		database: Database, user: Credentials
): Promise<Filee> => {
	const mockFile: Filee = {
		name: 'mockyFile',
		path: 'example/path',
		uuid: 'file1',
		creator: user,
		type: 'generic'
	};

	await new File(mockFile).save();
	return mockFile;
};