Repository navigation
Expand file tree
/
Copy pathcommon.test.js
More file actions
64 lines (50 loc) · 1.99 KB
/
Copy pathcommon.test.js
File metadata and controls
64 lines (50 loc) · 1.99 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
/* global describe, beforeEach, it, before, afterEach, after */
import assert from 'assert';
import sinon from 'sinon';
import uuidGenerator from 'uuid';
import PubNub from '../../src/node/index';
import CryptoJS from '../../src/core/components/cryptography/hmac-sha256';
describe('#core / mounting point', () => {
beforeEach(() => {
sinon.stub(uuidGenerator, 'v4').returns('uuidCustom');
});
afterEach(() => {
uuidGenerator.v4.restore();
});
it('supports UUID generation', () => {
assert.ok(!PubNub.generateUUID().startsWith('pn-'));
});
it('supports encryption', () => {
let pn = new PubNub({ cipherKey: 'customKey' });
assert.equal(pn.encrypt(JSON.stringify({ hi: 'there' })), 'TejX6F2JNqH/gIiGHWN4Cw==');
});
it('supports encryption with custom key', () => {
let pn = new PubNub({});
assert.equal(pn.encrypt(JSON.stringify({ hi: 'there' }), 'customKey'), 'TejX6F2JNqH/gIiGHWN4Cw==');
});
it('supports decryption', () => {
let pn = new PubNub({ cipherKey: 'customKey' });
assert.deepEqual(pn.decrypt('TejX6F2JNqH/gIiGHWN4Cw=='), { hi: 'there' });
});
it('supports decryption with custom key', () => {
let pn = new PubNub({});
assert.deepEqual(pn.decrypt('TejX6F2JNqH/gIiGHWN4Cw==', 'customKey'), { hi: 'there' });
});
it('supports decryption with custom key', () => {
let pn = new PubNub({});
assert.deepEqual(pn.decrypt('TejX6F2JNqH/gIiGHWN4Cw==', 'customKey'), { hi: 'there' });
});
it('supports custom encryption/decryption', () => {
let customEncrypt = (data) => {
let cipher = CryptoJS.AES.encrypt(JSON.stringify(data), 'customKey');
return cipher.toString();
};
let customDecrypt = (data) => {
let bytes = CryptoJS.AES.decrypt(data, 'customKey');
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
};
let pn = new PubNub({ customEncrypt, customDecrypt });
let ciphertext = pn.encrypt({ hi: 'there' });
assert.deepEqual(pn.decrypt(ciphertext), { hi: 'there' });
});
});