-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmocha-json-db.js
More file actions
176 lines (156 loc) · 6.01 KB
/
Copy pathmocha-json-db.js
File metadata and controls
176 lines (156 loc) · 6.01 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const JsonFileConnector = require('../../lib/JSONFileConnector.js');
const CONFIG_FILE = path.join(__dirname, 'db.json.temp');
const TEST_CONTENT = {
colsHeader: {
date: {
visible: true,
size: 'cell-m',
},
message: {
visible: true,
size: 'cell-s',
},
},
};
const NEW_CONTENT = {
colsHeader: {
date: {
visible: false,
size: 'cell-xl',
},
message: {
visible: false,
size: 'cell-xl',
},
},
};
let jsonConfig;
describe('JSON file custom database', () => {
before(() => {
// Drop previous DB if exists
try {
fs.unlinkSync(CONFIG_FILE);
} catch (error) { }
jsonConfig = new JsonFileConnector(CONFIG_FILE);
});
describe('Creating a new profile', () => {
it('should throw an error if username is undefined', async () => {
await assert.rejects(
jsonConfig.createNewProfile(undefined, TEST_CONTENT),
new Error('username for profile is mandatory'),
);
});
it('should throw an error if username is null', async () => {
await assert.rejects(
jsonConfig.createNewProfile(null, TEST_CONTENT),
new Error('username for profile is mandatory'),
);
});
it('should successfully create a new profile', async () => {
await assert.doesNotReject(jsonConfig.createNewProfile('anonymous', TEST_CONTENT));
const newProfile = await jsonConfig.getProfileByUsername('anonymous');
assert.ok(newProfile.createdTimestamp);
assert.ok(newProfile.lastModifiedTimestamp);
assert.deepStrictEqual(newProfile.content, TEST_CONTENT);
assert.strictEqual(newProfile.username, 'anonymous');
});
it('should throw an error when creating a new profile with the username as an existing profile', async () => {
await assert.rejects(
jsonConfig.createNewProfile('anonymous', TEST_CONTENT),
new Error('Profile with this username (anonymous) already exists'),
);
});
});
describe('Get a profile by username', () => {
it('should successfully get a profile by username', (done) => {
jsonConfig.getProfileByUsername('anonymous').then((profile) => {
assert.deepStrictEqual(profile.content, TEST_CONTENT);
assert.strictEqual(profile.username, 'anonymous');
done();
}).catch(done);
});
it('should successfully return undefined if their is no profile associated to requested username', (done) => {
jsonConfig.getProfileByUsername('no-user').then((profile) => {
assert.strictEqual(profile, undefined);
done();
}).catch(done);
});
});
describe('Update a profile by username', () => {
it('should successfully update the content and lastModifiedTimestamp of a profile by username', (done) => {
jsonConfig.getProfileByUsername('anonymous').then((profile) => {
const lastTimestamp = profile.lastModifiedTimestamp;
jsonConfig.updateProfile('anonymous', NEW_CONTENT).then((updatedProfile) => {
assert.deepStrictEqual(updatedProfile.content, NEW_CONTENT);
assert.strictEqual(updatedProfile.username, 'anonymous');
assert.ok(updatedProfile.lastModifiedTimestamp > lastTimestamp);
done();
}).catch(done);
});
});
it('should throw an error when trying to update a profile which does not exist', () => assert.rejects(async () => {
await jsonConfig.updateProfile('no-one', TEST_CONTENT);
}, new Error('Profile with this username (no-one) cannot be updated as it does not exist')));
});
describe('Testing read/write to fs', () => {
it('should reject when profiles are missing from data with error of bad data format ', async () => {
await assert.rejects(async () => {
jsonConfig.data = '{}';
await jsonConfig._writeToFile();
await jsonConfig._readFromFile();
}, new Error(`DB file should have an array of profiles ${CONFIG_FILE}`));
});
it('should reject when there is no data with error of bad data format ', async () => {
await assert.rejects(async () => {
jsonConfig.data = '';
await jsonConfig._writeToFile();
await jsonConfig._readFromFile();
}, new Error(`DB file should have an array of profiles ${CONFIG_FILE}`));
});
it('should reject when data.profiles is not an Array with error of bad data format ', async () => {
await assert.rejects(async () => {
jsonConfig.data = { profiles: 'test' };
await jsonConfig._writeToFile();
await jsonConfig._readFromFile();
}, new Error(`DB file should have an array of profiles ${CONFIG_FILE}`));
});
it('should successfully read profiles from data', async () => {
await assert.doesNotReject(async () => {
jsonConfig.data = { profiles: [] };
await jsonConfig._writeToFile();
await jsonConfig._readFromFile();
});
});
it('should reject when there is missing data with error of bad JSON format ', async () => {
const errorMessage = 'The "data" argument must be of type string or an instance of Buffer, '
+ 'TypedArray, or DataView. Received undefined';
await assert.rejects(
async () => {
jsonConfig.data = undefined;
await jsonConfig._writeToFile();
await jsonConfig._readFromFile();
},
new TypeError(errorMessage),
);
});
});
after(() => {
fs.unlinkSync(CONFIG_FILE);
});
});