Skip to content

Commit d7d4f39

Browse files
committed
completing test suite
1 parent 654fc28 commit d7d4f39

File tree

4 files changed

+332
-1
lines changed

4 files changed

+332
-1
lines changed

components/pryv-monitor/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = function (pryv) {
1515
console.log('Pryv version', pryv.version);
1616
// check version here
1717
if (pryv.Monitor) {
18-
throw new Error('Monitor already loaded');
18+
return; // already loaded
1919
}
2020
// sharing cross references
2121
pryv.Monitor = Monitor;
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* @license
3+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4+
*/
5+
/* global describe, it, before, after, expect, pryv, testData */
6+
7+
const { createId: cuid } = require('@paralleldrive/cuid2');
8+
9+
let conn = null;
10+
const testStreamId = 'acc-' + cuid().slice(0, 8);
11+
let createdAccessId = null;
12+
let createdAccessToken = null;
13+
14+
describe('[ACSX] Accesses', () => {
15+
before(async function () {
16+
this.timeout(15000);
17+
await testData.prepare();
18+
conn = new pryv.Connection(testData.apiEndpointWithToken);
19+
20+
// Create a test stream for permissions
21+
await conn.api([{
22+
method: 'streams.create',
23+
params: { id: testStreamId, name: 'Access Test Stream' }
24+
}]);
25+
});
26+
27+
describe('[ACRX] accesses.create', function () {
28+
it('[ACRA] create a shared access', async () => {
29+
const res = await conn.api([{
30+
method: 'accesses.create',
31+
params: {
32+
name: 'test-shared-' + cuid().slice(0, 8),
33+
type: 'shared',
34+
permissions: [{ streamId: testStreamId, level: 'read' }]
35+
}
36+
}]);
37+
expect(res[0]).to.exist;
38+
expect(res[0].access).to.exist;
39+
expect(res[0].access.token).to.exist;
40+
expect(res[0].access.type).to.equal('shared');
41+
createdAccessId = res[0].access.id;
42+
createdAccessToken = res[0].access.token;
43+
});
44+
45+
it('[ACRB] create an app access', async () => {
46+
const res = await conn.api([{
47+
method: 'accesses.create',
48+
params: {
49+
name: 'test-app-' + cuid().slice(0, 8),
50+
type: 'app',
51+
permissions: [{ streamId: testStreamId, level: 'contribute' }]
52+
}
53+
}]);
54+
expect(res[0]).to.exist;
55+
expect(res[0].access).to.exist;
56+
expect(res[0].access.type).to.equal('app');
57+
});
58+
59+
it('[ACRC] reject access with invalid permission level', async () => {
60+
const res = await conn.api([{
61+
method: 'accesses.create',
62+
params: {
63+
name: 'bad-access',
64+
type: 'shared',
65+
permissions: [{ streamId: testStreamId, level: 'bogus' }]
66+
}
67+
}]);
68+
expect(res[0]).to.exist;
69+
expect(res[0].error).to.exist;
70+
});
71+
});
72+
73+
describe('[AGTX] accesses.get', function () {
74+
it('[AGTA] list accesses', async () => {
75+
const res = await conn.api([{
76+
method: 'accesses.get',
77+
params: {}
78+
}]);
79+
expect(res[0]).to.exist;
80+
expect(res[0].accesses).to.exist;
81+
expect(Array.isArray(res[0].accesses)).to.equal(true);
82+
expect(res[0].accesses.length).to.be.gt(0);
83+
});
84+
85+
it('[AGTB] created access is in the list', async () => {
86+
const res = await conn.api([{
87+
method: 'accesses.get',
88+
params: {}
89+
}]);
90+
const found = res[0].accesses.find(a => a.id === createdAccessId);
91+
expect(found).to.exist;
92+
expect(found.token).to.equal(createdAccessToken);
93+
});
94+
});
95+
96+
describe('[ADLX] accesses.delete', function () {
97+
it('[ADLA] delete (revoke) an access', async () => {
98+
expect(createdAccessId).to.exist;
99+
const res = await conn.api([{
100+
method: 'accesses.delete',
101+
params: { id: createdAccessId }
102+
}]);
103+
expect(res[0]).to.exist;
104+
expect(res[0].accessDeletion).to.exist;
105+
expect(res[0].accessDeletion.id).to.equal(createdAccessId);
106+
});
107+
108+
it('[ADLB] deleted access is no longer in the list', async () => {
109+
const res = await conn.api([{
110+
method: 'accesses.get',
111+
params: {}
112+
}]);
113+
const found = res[0].accesses.find(a => a.id === createdAccessId);
114+
expect(found).to.not.exist;
115+
});
116+
});
117+
118+
// Cleanup
119+
after(async () => {
120+
if (!conn) return;
121+
await conn.api([
122+
{ method: 'streams.delete', params: { id: testStreamId } },
123+
{ method: 'streams.delete', params: { id: testStreamId } }
124+
]);
125+
});
126+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @license
3+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4+
*/
5+
/* global describe, it, before, expect, pryv, testData */
6+
7+
let conn = null;
8+
9+
describe('[ACTX] Account', () => {
10+
before(async function () {
11+
this.timeout(15000);
12+
await testData.prepare();
13+
conn = new pryv.Connection(testData.apiEndpointWithToken);
14+
});
15+
16+
describe('[AINX] account.get', function () {
17+
it('[AINA] get account info', async () => {
18+
const res = await conn.api([{
19+
method: 'account.get',
20+
params: {}
21+
}]);
22+
expect(res[0]).to.exist;
23+
expect(res[0].account).to.exist;
24+
expect(res[0].account.username).to.equal(testData.username);
25+
expect(res[0].account.email).to.exist;
26+
});
27+
});
28+
29+
describe('[ASUX] account.get storageUsed', function () {
30+
it('[ASUA] storageUsed has expected fields', async () => {
31+
const res = await conn.api([{
32+
method: 'account.get',
33+
params: {}
34+
}]);
35+
const account = res[0].account;
36+
expect(account.storageUsed).to.exist;
37+
expect(account.storageUsed.dbDocuments).to.be.a('number');
38+
expect(account.storageUsed.attachedFiles).to.be.a('number');
39+
});
40+
});
41+
42+
describe('[ACPX] account.changePassword', function () {
43+
it('[ACPA] change password and change back', async () => {
44+
const oldPassword = testData.password;
45+
const newPassword = oldPassword + '-new';
46+
47+
// Change to new password
48+
const res1 = await conn.api([{
49+
method: 'account.changePassword',
50+
params: { oldPassword, newPassword }
51+
}]);
52+
expect(res1[0]).to.exist;
53+
expect(res1[0].error).to.not.exist;
54+
55+
// Change back to original
56+
const res2 = await conn.api([{
57+
method: 'account.changePassword',
58+
params: { oldPassword: newPassword, newPassword: oldPassword }
59+
}]);
60+
expect(res2[0]).to.exist;
61+
expect(res2[0].error).to.not.exist;
62+
});
63+
64+
it('[ACPB] reject wrong old password', async () => {
65+
const res = await conn.api([{
66+
method: 'account.changePassword',
67+
params: { oldPassword: 'wrong-password', newPassword: 'new-password' }
68+
}]);
69+
expect(res[0]).to.exist;
70+
expect(res[0].error).to.exist;
71+
});
72+
});
73+
});
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* @license
3+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4+
*/
5+
/* global describe, it, before, after, expect, pryv, testData */
6+
7+
const { createId: cuid } = require('@paralleldrive/cuid2');
8+
9+
let conn = null;
10+
const testStreamId = 'str-' + cuid().slice(0, 8);
11+
const childStreamId = 'str-child-' + cuid().slice(0, 8);
12+
13+
describe('[STRX] Streams', () => {
14+
before(async function () {
15+
this.timeout(15000);
16+
await testData.prepare();
17+
conn = new pryv.Connection(testData.apiEndpointWithToken);
18+
});
19+
20+
describe('[SCRX] streams.create', function () {
21+
it('[SCRA] create a root stream', async () => {
22+
const res = await conn.api([{
23+
method: 'streams.create',
24+
params: { id: testStreamId, name: 'Test Stream' }
25+
}]);
26+
expect(res[0]).to.exist;
27+
expect(res[0].stream).to.exist;
28+
expect(res[0].stream.id).to.equal(testStreamId);
29+
expect(res[0].stream.name).to.equal('Test Stream');
30+
});
31+
32+
it('[SCRB] create a child stream', async () => {
33+
const res = await conn.api([{
34+
method: 'streams.create',
35+
params: { id: childStreamId, name: 'Child Stream', parentId: testStreamId }
36+
}]);
37+
expect(res[0]).to.exist;
38+
expect(res[0].stream).to.exist;
39+
expect(res[0].stream.parentId).to.equal(testStreamId);
40+
});
41+
42+
it('[SCRC] reject duplicate stream id', async () => {
43+
const res = await conn.api([{
44+
method: 'streams.create',
45+
params: { id: testStreamId, name: 'Duplicate' }
46+
}]);
47+
expect(res[0]).to.exist;
48+
expect(res[0].error).to.exist;
49+
expect(res[0].error.id).to.equal('item-already-exists');
50+
});
51+
});
52+
53+
describe('[SGTX] streams.get', function () {
54+
it('[SGTA] get all streams', async () => {
55+
const res = await conn.api([{
56+
method: 'streams.get',
57+
params: {}
58+
}]);
59+
expect(res[0]).to.exist;
60+
expect(res[0].streams).to.exist;
61+
expect(Array.isArray(res[0].streams)).to.equal(true);
62+
expect(res[0].streams.length).to.be.gt(0);
63+
});
64+
65+
it('[SGTB] get stream tree includes parent and child', async () => {
66+
const res = await conn.api([{
67+
method: 'streams.get',
68+
params: {}
69+
}]);
70+
const streams = res[0].streams;
71+
const parent = findStream(streams, testStreamId);
72+
expect(parent).to.exist;
73+
expect(parent.children).to.exist;
74+
const child = parent.children.find(s => s.id === childStreamId);
75+
expect(child).to.exist;
76+
});
77+
});
78+
79+
describe('[SUPX] streams.update', function () {
80+
it('[SUPA] rename a stream', async () => {
81+
const res = await conn.api([{
82+
method: 'streams.update',
83+
params: { id: childStreamId, update: { name: 'Renamed Child' } }
84+
}]);
85+
expect(res[0]).to.exist;
86+
expect(res[0].stream).to.exist;
87+
expect(res[0].stream.name).to.equal('Renamed Child');
88+
});
89+
});
90+
91+
describe('[SDLX] streams.delete', function () {
92+
it('[SDLA] trash a stream (first delete)', async () => {
93+
const res = await conn.api([{
94+
method: 'streams.delete',
95+
params: { id: childStreamId }
96+
}]);
97+
expect(res[0]).to.exist;
98+
expect(res[0].stream).to.exist;
99+
expect(res[0].stream.trashed).to.equal(true);
100+
});
101+
102+
it('[SDLB] delete a trashed stream (second delete)', async () => {
103+
const res = await conn.api([{
104+
method: 'streams.delete',
105+
params: { id: childStreamId }
106+
}]);
107+
expect(res[0]).to.exist;
108+
// After second delete, stream is gone
109+
expect(res[0].streamDeletion).to.exist;
110+
});
111+
});
112+
113+
// Cleanup parent stream
114+
after(async () => {
115+
if (!conn) return;
116+
await conn.api([
117+
{ method: 'streams.delete', params: { id: testStreamId } },
118+
{ method: 'streams.delete', params: { id: testStreamId } }
119+
]);
120+
});
121+
});
122+
123+
function findStream (streams, id) {
124+
for (const s of streams) {
125+
if (s.id === id) return s;
126+
if (s.children) {
127+
const found = findStream(s.children, id);
128+
if (found) return found;
129+
}
130+
}
131+
return null;
132+
}

0 commit comments

Comments
 (0)