Skip to content

Commit a890424

Browse files
committed
Added tests for the cipher service
1 parent 461d09d commit a890424

File tree

7 files changed

+53
-36
lines changed

7 files changed

+53
-36
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: npm install
22-
- run: npm test
22+
- run: npm run test

package.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"node": ">=16"
2020
},
2121
"scripts": {
22-
"test": "xo --env=node && ava && pnpm run build && tsd --typings dist/index.d.ts",
22+
"test": "xo --env=node && pnpm run build && mocha -r ts-node/register 'tests/**/*.js'",
2323
"build": "del-cli dist && tsc",
2424
"prepack": "pnpm run build"
2525
},
@@ -31,25 +31,19 @@
3131
],
3232
"devDependencies": {
3333
"@sindresorhus/tsconfig": "^5.0.0",
34-
"ava": "^5.3.0",
34+
"@types/chai": "^4.3.11",
35+
"@types/mocha": "^10.0.6",
36+
"chai": "^5.0.0",
3537
"del-cli": "^5.1.0",
38+
"mocha": "^10.2.0",
3639
"ts-node": "^10.9.2",
3740
"tsd": "^0.30.4",
38-
"tsup": "^8.0.1",
3941
"xo": "^0.54.2"
4042
},
4143
"dependencies": {
4244
"@types/node": "^20.11.5",
4345
"axios": "^1.6.5"
4446
},
45-
"ava": {
46-
"extensions": {
47-
"ts": "module"
48-
},
49-
"nodeArguments": [
50-
"--loader=tsx"
51-
]
52-
},
5347
"prettier": {
5448
"trailingComma": "all",
5549
"tabWidth": 4,

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ log.Println(message.Code) // 202
7979
You can run the unit tests for this client from the root directory using the command below:
8080

8181
```bash
82-
go test -v
82+
go tests -v
8383
```
8484

8585
## License

src/cipher-service.test.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/cipher-service.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,31 @@ import {Buffer} from 'node:buffer';
99
class CipherService {
1010
public encrypt(key: string, message: string): string {
1111
const iv = randomBytes(16);
12-
const cipher = createCipheriv('aes-256-cfb', this.hash(key), iv);
13-
return Buffer.from(
14-
Buffer.concat([iv, cipher.update(message, 'utf8'), cipher.final()]),
15-
).toString('base64');
12+
const cipher = createCipheriv(
13+
'aes-256-cfb',
14+
this.hash(key),
15+
iv,
16+
).setAutoPadding(false);
17+
return Buffer.concat([
18+
iv,
19+
cipher.update(message, 'utf8'),
20+
cipher.final(),
21+
]).toString('base64');
1622
}
1723

1824
public decrypt(key: string, message: string): string {
19-
const iv = randomBytes(16);
20-
const decipher = createDecipheriv('aes-256-cfb', this.hash(key), iv);
21-
return Buffer.from(
22-
Buffer.concat([
23-
iv,
24-
decipher.update(message, 'utf8'),
25-
decipher.final(),
26-
]),
27-
).toString('utf8');
25+
const cipherBytes = Buffer.from(message, 'base64');
26+
const iv = cipherBytes.subarray(0, 16);
27+
const decipher = createDecipheriv(
28+
'aes-256-cfb',
29+
this.hash(key),
30+
iv,
31+
).setAutoPadding(false);
32+
33+
return Buffer.concat([
34+
decipher.update(cipherBytes.subarray(16, cipherBytes.length)),
35+
decipher.final(),
36+
]).toString();
2837
}
2938

3039
private hash(value: string): Buffer {

tests/cipher-service.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {expect} from 'chai';
2+
import {describe, it} from 'mocha';
3+
import CipherService from '../dist/src/cipher-service.js';
4+
5+
describe('CipherService', () => {
6+
it('can encrypt and decrypt the same content', () => {
7+
// Arrange
8+
const key = 'Password123';
9+
const message = 'This is a test text message1';
10+
const service = new CipherService();
11+
12+
// Act
13+
const cipherText = service.encrypt(key, message);
14+
const plainText = service.decrypt(key, cipherText);
15+
16+
// Assert
17+
expect(plainText).to.equal(message);
18+
});
19+
});

tsconfig.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"extends": "@sindresorhus/tsconfig",
33
"compilerOptions": {
44
"outDir": "dist",
5-
"experimentalDecorators": true
5+
"experimentalDecorators": true,
6+
"esModuleInterop": true
67
},
7-
"files": [
8-
"index.ts"
9-
],
8+
"files": ["index.ts"],
109
"ts-node": {
11-
"transpileOnly": true
10+
"transpileOnly": true,
11+
"esm": true
1212
}
1313
}

0 commit comments

Comments
 (0)