-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplicationClass.test.js
More file actions
170 lines (149 loc) · 5.96 KB
/
applicationClass.test.js
File metadata and controls
170 lines (149 loc) · 5.96 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
import { assert } from './test-utils/deps-node.js';
import { createUserAndPermissions } from './test-utils/pryvService.js';
import HDSLib from '../ts/index.ts';
const Application = HDSLib.appTemplates.Application;
describe('[APAX] Application class', function () {
this.timeout(5000);
const baseStreamId = 'application-class';
const appName = 'application-app';
let user;
before(async () => {
await HDSLib.initHDSModel();
const initialStreams = [{ id: 'applications', name: 'Applications' }, { id: baseStreamId, name: appName, parentId: 'applications' }];
const permissionsManager = [{ streamId: baseStreamId, level: 'manage' }];
user = await createUserAndPermissions(null, permissionsManager, initialStreams, appName);
});
describe('[APIX] Application class internal', () => {
it('[APIS] Application custom settings', async () => {
class Dummy extends Application {
get appSettings () {
return { };
}
}
const appDummy = await Dummy.newFromApiEndpoint(baseStreamId, user.appApiEndpoint, appName);
const settings = await appDummy.getCustomSettings();
assert.deepEqual(settings, {});
const newSettings = { hello: 'Tom', value: 2 };
const newSettings1 = await appDummy.setCustomSettings(newSettings);
assert.deepEqual(newSettings, newSettings1);
const newSettings2 = await appDummy.getCustomSettings();
assert.deepEqual(newSettings, newSettings2);
assert.deepEqual(newSettings1, newSettings2);
assert.equal(newSettings1, newSettings2, 'should be the same object');
const newSettings3 = await appDummy.getCustomSettings(true);
assert.deepEqual(newSettings1, newSettings3);
assert.notEqual(newSettings1, newSettings3, 'should not be the same object');
});
});
describe('[APAE] Application class errors', () => {
it('[APAI] Application extension', async () => {
class Dummy extends Application { }
try {
await Dummy.newFromApiEndpoint(baseStreamId, user.appApiEndpoint, appName);
throw new Error('Should throw an error');
} catch (e) {
assert.equal(e.message, 'appSettings must be implemented');
}
try {
// eslint-disable-next-line no-new
new Dummy('u', user.appApiEndpoint, appName);
throw new Error('Should throw an error');
} catch (e) {
assert.equal(e.message, 'Missing or too short baseStreamId');
}
});
it('[APAA] Application name from accessInfo fails in not in settings', () => {
class Dummy extends Application {
get appSettings () {
return { };
}
}
try {
// eslint-disable-next-line no-new
new Dummy(baseStreamId, user.appApiEndpoint);
throw new Error('Should throw an error');
} catch (e) {
assert.equal(e.message, 'appName must be given unless appSettings.appNameFromAccessInfo = true');
}
});
it('[APAB] Application name from accessInfo', async () => {
class Dummy2 extends Application {
get appSettings () {
return {
appNameFromAccessInfo: true
};
}
}
const dummy2 = await Dummy2.newFromApiEndpoint(baseStreamId, user.appApiEndpoint);
assert.ok(dummy2, 'if appSettings.appNameFromAccessInfo = true appName is not required');
});
it('[APAC] Application should throw error if baseStream is not accessible', async () => {
class Dummy extends Application {
get appSettings () {
return { };
}
}
try {
await Dummy.newFromApiEndpoint('uuuu', user.appApiEndpoint, appName);
throw new Error('Should throw an error');
} catch (e) {
assert.equal(e.message, 'Application with "app" type of access requires (streamId = "uuuu", level = "manage") or master access');
}
});
it('[APAD] Application should throw error if personaToken are not explicity allowed', async () => {
class Dummy extends Application {
get appSettings () {
return { };
}
}
try {
await Dummy.newFromApiEndpoint('uuuu', user.personalApiEndpoint, appName);
throw new Error('Should throw an error');
} catch (e) {
assert.equal(e.message, 'Application should not use a personal token');
}
});
it('[APAE] Application should throw error if master token not provided and required', async () => {
class Dummy extends Application {
get appSettings () {
return {
mustBemaster: true
};
}
}
try {
await Dummy.newFromApiEndpoint('uuuu', user.appApiEndpoint, appName);
throw new Error('Should throw an error');
} catch (e) {
assert.equal(e.message, 'Application with "app" type of access requires "master" token (streamId = "*", level = "manage")');
}
});
});
describe('[ACSX] Application setCustomSetting tests', function () {
it('[ACSA] setCustomSetting adds a key', async () => {
class Dummy extends Application {
get appSettings () {
return { };
}
}
const appDummy = await Dummy.newFromApiEndpoint(baseStreamId, user.appApiEndpoint, appName);
await appDummy.setCustomSetting('newKey', 'newValue');
const settings = await appDummy.getCustomSettings();
assert.equal(settings.newKey, 'newValue');
});
it('[ACSB] setCustomSetting with null deletes key', async () => {
class Dummy extends Application {
get appSettings () {
return { };
}
}
const appDummy = await Dummy.newFromApiEndpoint(baseStreamId, user.appApiEndpoint, appName);
await appDummy.setCustomSetting('keyToDelete', 'value');
let settings = await appDummy.getCustomSettings();
assert.equal(settings.keyToDelete, 'value');
await appDummy.setCustomSetting('keyToDelete', null);
settings = await appDummy.getCustomSettings();
assert.equal(settings.keyToDelete, undefined);
});
});
});