1 |
|
2 |
|
3 | const test = require('tape')
|
4 |
|
5 | const aes256 = require('./fb-jwt-aes256')
|
6 |
|
7 |
|
8 | const userToken = 'testUserToken'
|
9 | const data = '{"foo":"bar"}'
|
10 | const expectedEncryptedData = 'RRqDeJRQlZULKx1NYql/imRmDsy9AZshKozgLuY='
|
11 | const expectedEncryptedDataWithIV = 'lGqo0ndPhzip43NWVjCRbcVPzdGsNzDwkzJy7Ck='
|
12 |
|
13 |
|
14 | test('When decrypting data and failing to pass a key', async t => {
|
15 | try {
|
16 | aes256.decrypt(undefined, data)
|
17 | t.notOk(true, 'it should throw an error')
|
18 | } catch (e) {
|
19 | t.equal(e.name, 'FBJWTAES256Error', 'it should return an error object of the correct type')
|
20 | t.equal(e.code, 'ENODECRYPTKEY', 'it should return correct error code')
|
21 | t.equal(e.message, 'Key must be a non-empty string', 'it should return the correct error message')
|
22 | }
|
23 |
|
24 | t.end()
|
25 | })
|
26 |
|
27 | test('When decrypting data and failing to pass a value to decrypt', async t => {
|
28 | try {
|
29 | aes256.decrypt(userToken, undefined)
|
30 | t.notOk(true, 'it should throw an error')
|
31 | } catch (e) {
|
32 | t.equal(e.name, 'FBJWTAES256Error', 'it should return an error object of the correct type')
|
33 | t.equal(e.code, 'ENODECRYPTVALUE', 'it should return correct error code')
|
34 | t.equal(e.message, 'Encrypted value must be a non-empty string', 'it should return the correct error message')
|
35 | }
|
36 |
|
37 | t.end()
|
38 | })
|
39 |
|
40 | test('When decrypting data', async t => {
|
41 | const decryptedData = aes256.decrypt(userToken, expectedEncryptedData)
|
42 | t.deepEqual(data, decryptedData, 'it should return the correct data from valid encrypted input')
|
43 |
|
44 | t.end()
|
45 | })
|
46 |
|
47 |
|
48 | test('When encrypting data and failing to pass a key', async t => {
|
49 | try {
|
50 | aes256.encrypt(undefined, data)
|
51 | t.notOk(true, 'it should throw an error')
|
52 | } catch (e) {
|
53 | t.equal(e.name, 'FBJWTAES256Error', 'it should return an error object of the correct type')
|
54 | t.equal(e.code, 'ENOENCRYPTKEY', 'it should return correct error code')
|
55 | t.equal(e.message, 'Key must be a non-empty string', 'it should return the correct error message')
|
56 | }
|
57 |
|
58 | t.end()
|
59 | })
|
60 |
|
61 | test('When encrypting data and failing to pass a value to encrypt', async t => {
|
62 | try {
|
63 | aes256.encrypt(userToken, undefined)
|
64 | t.notOk(true, 'it should throw an error')
|
65 | } catch (e) {
|
66 | t.equal(e.name, 'FBJWTAES256Error', 'it should return an error object of the correct type')
|
67 | t.equal(e.code, 'ENOENCRYPTVALUE', 'it should return correct error code')
|
68 | t.equal(e.message, 'Plaintext value must be a non-empty string', 'it should return the correct error message')
|
69 | }
|
70 |
|
71 | t.end()
|
72 | })
|
73 |
|
74 | test('When encrypting data', async t => {
|
75 | const encryptedData = aes256.encrypt(userToken, data)
|
76 | const decryptedData = aes256.decrypt(userToken, encryptedData)
|
77 | t.equal(data, decryptedData, 'it should encrypt the data correctly')
|
78 |
|
79 |
|
80 |
|
81 | const encryptedDataAgain = aes256.encrypt(userToken, data)
|
82 | t.notEqual(encryptedDataAgain, encryptedData, 'it should not return the same value for the same input')
|
83 |
|
84 | t.end()
|
85 | })
|
86 |
|
87 | test('When encrypting data with a provided IV seed', async t => {
|
88 | const encryptedData = aes256.encrypt(userToken, data, 'ivSeed')
|
89 | t.equal(encryptedData, expectedEncryptedDataWithIV, 'it should encrypt the data correctly')
|
90 |
|
91 | const encryptedDataAgain = aes256.encrypt(userToken, data, 'ivSeed')
|
92 | t.equal(encryptedDataAgain, encryptedData, 'it should return the same value for the same input')
|
93 |
|
94 | t.end()
|
95 | })
|