Skip to content

Commit 25c41f0

Browse files
committed
test: add test for CriptoDriver
1 parent 1b90b99 commit 25c41f0

1 file changed

Lines changed: 146 additions & 0 deletions

File tree

test/CriptoDriver.spec.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/**
2+
* @author Victor Giovanni Beltrán Rodríguez
3+
* @file This file contains the test for the `CryptoDriver` class.
4+
*/
5+
6+
// ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
7+
// » IMPORT NATIVE NODE MODULES
8+
const { describe, it } = require('node:test');
9+
const assert = require('node:assert');
10+
11+
// » IMPORT MODULES
12+
const CryptoDriver = require('..');
13+
14+
// ━━ CONSTANTS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
15+
/**
16+
* This constant defines the errors that can be thrown by
17+
* the `CryptoDriver` class.
18+
*
19+
* @private
20+
* @constant {object} THROWS
21+
*/
22+
const THROWS = {
23+
UNDEFINED_KEY: {
24+
name: 'ReferenceError',
25+
message: 'The "key" value must be provided and must be a string.',
26+
},
27+
LENGTH_KEY: {
28+
name: 'RangeError',
29+
message: 'The "Key" value must be 32 characters (256 bits).',
30+
},
31+
TYPE_KEY: {
32+
name: 'TypeError',
33+
message: 'The "key" value must be of type string',
34+
},
35+
UNDEFINED_DATA: {
36+
name: 'ReferenceError',
37+
message: 'The "data" value must be provided and must be a string.',
38+
},
39+
TYPE_DATA: {
40+
name: 'TypeError',
41+
message: 'The "data" value must be of type string',
42+
},
43+
UNDEFINED_ENCRYPTED: {
44+
name: 'ReferenceError',
45+
message: 'The "encrypted" value must be provided and must be a string.',
46+
},
47+
TYPE_ENCRYPTED: {
48+
name: 'TypeError',
49+
message: 'The "encrypted" value must be of type string',
50+
},
51+
};
52+
53+
// ━━ CONSTANTS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
54+
/**
55+
* Add description.
56+
*
57+
* @private
58+
* @function createInstance
59+
* @param {string} key - Add description.
60+
* @returns {CryptoDriver} Add description.
61+
* @example createInstance(d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq);
62+
*
63+
*/
64+
const createInstance = key => new CryptoDriver(key);
65+
66+
// ━━ TEST ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
67+
describe('CryptoDriver', () => {
68+
describe('constructor', () => {
69+
it('should create a new instance of CryptoDriver with a valid key', () => {
70+
const cryptoDriver = new CryptoDriver('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq');
71+
assert.ok(cryptoDriver instanceof CryptoDriver);
72+
});
73+
74+
it('should throw a ReferenceError if key is undefined', () => {
75+
assert.throws(() => {
76+
createInstance(undefined);
77+
}, THROWS.UNDEFINED_KEY);
78+
});
79+
it('should throw a RangeError when key length is not 32', () => {
80+
assert.throws(() => {
81+
createInstance('short key');
82+
}, THROWS.LENGTH_KEY);
83+
assert.throws(() => {
84+
createInstance('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeqd6F3EfeqX');
85+
}, THROWS.LENGTH_KEY);
86+
});
87+
it('should throw a TypeError if key is not a string', () => {
88+
assert.throws(() => {
89+
createInstance(true);
90+
}, THROWS.TYPE_KEY);
91+
assert.throws(() => {
92+
createInstance(100);
93+
}, THROWS.TYPE_KEY);
94+
});
95+
});
96+
97+
describe('#encrypt()', () => {
98+
it('should encrypt data and return the encrypted value', () => {
99+
const cryptoDriver = new CryptoDriver('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq');
100+
const encryptedData = cryptoDriver.encrypt('hello world');
101+
assert.notStrictEqual(encryptedData, 'hello world');
102+
});
103+
it('should throw a ReferenceError if data is undefined', () => {
104+
assert.throws(() => {
105+
const cryptoDriver = new CryptoDriver('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq');
106+
cryptoDriver.encrypt();
107+
}, THROWS.UNDEFINED_DATA);
108+
});
109+
it('should throw a TypeError if data is not a string', () => {
110+
assert.throws(() => {
111+
const cryptoDriver = new CryptoDriver('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq');
112+
cryptoDriver.encrypt(true);
113+
}, THROWS.TYPE_DATA);
114+
assert.throws(() => {
115+
const cryptoDriver = new CryptoDriver('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq');
116+
cryptoDriver.encrypt(100);
117+
}, THROWS.TYPE_DATA);
118+
});
119+
});
120+
121+
describe('#decrypt()', () => {
122+
it('should decrypt data and return the original value', () => {
123+
const cryptoDriver = new CryptoDriver('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq');
124+
const encryptedData = cryptoDriver.encrypt('hello world');
125+
const decryptedData = cryptoDriver.decrypt(encryptedData);
126+
assert.strictEqual(decryptedData, 'hello world');
127+
});
128+
129+
it('should throw a ReferenceError if encrypted is undefined', () => {
130+
assert.throws(() => {
131+
const cryptoDriver = new CryptoDriver('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq');
132+
cryptoDriver.decrypt();
133+
}, THROWS.UNDEFINED_ENCRYPTED);
134+
});
135+
it('should throw a TypeError if encrypted is not a string', () => {
136+
assert.throws(() => {
137+
const cryptoDriver = new CryptoDriver('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq');
138+
cryptoDriver.decrypt(true);
139+
}, THROWS.TYPE_ENCRYPTED);
140+
assert.throws(() => {
141+
const cryptoDriver = new CryptoDriver('d6F3Efeqd6F3Efeqd6F3Efeqd6F3Efeq');
142+
cryptoDriver.decrypt(100);
143+
}, THROWS.TYPE_ENCRYPTED);
144+
});
145+
});
146+
});

0 commit comments

Comments
 (0)