Add eslint rule and fix as many issues as possible

This commit is contained in:
Khoa Le
2023-06-19 23:42:04 -04:00
parent e9eacc445d
commit 9d33e4756b
689 changed files with 9446 additions and 21821 deletions
+55 -55
View File
@@ -1,78 +1,78 @@
import { describe, test, expect } from '@jest/globals';
import { describe, expect, test } from "@jest/globals";
import {
decryptAsymmetric,
encryptAsymmetric,
} from '../../../src/utils/crypto';
} from "../../../src/utils/crypto";
describe('Crypto', () => {
describe('encryptAsymmetric', () => {
describe('given all valid publicKey, privateKey and plaintext', () => {
const publicKey = '6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=';
const privateKey = 'Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=';
const plaintext = 'secret-message';
describe("Crypto", () => {
describe("encryptAsymmetric", () => {
describe("given all valid publicKey, privateKey and plaintext", () => {
const publicKey = "6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=";
const privateKey = "Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=";
const plaintext = "secret-message";
test('should encrypt plain text', () => {
test("should encrypt plain text", () => {
const result = encryptAsymmetric({ plaintext, publicKey, privateKey });
expect(result.ciphertext).toBeDefined();
expect(result.nonce).toBeDefined();
});
});
describe('given empty/undefined publicKey', () => {
describe("given empty/undefined publicKey", () => {
let publicKey: string;
const privateKey = 'Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=';
const plaintext = 'secret-message';
const privateKey = "Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=";
const plaintext = "secret-message";
test('should throw error if publicKey is undefined', () => {
test("should throw error if publicKey is undefined", () => {
expect(() => {
encryptAsymmetric({ plaintext, publicKey, privateKey });
}).toThrowError('invalid encoding');
}).toThrowError("invalid encoding");
});
test('should throw error if publicKey is empty string', () => {
publicKey = '';
test("should throw error if publicKey is empty string", () => {
publicKey = "";
expect(() => {
encryptAsymmetric({ plaintext, publicKey, privateKey });
}).toThrowError('bad public key size');
}).toThrowError("bad public key size");
});
});
describe('given empty/undefined privateKey', () => {
const publicKey = '6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=';
describe("given empty/undefined privateKey", () => {
const publicKey = "6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=";
let privateKey: string;
const plaintext = 'secret-message';
const plaintext = "secret-message";
test('should throw error if privateKey is undefined', () => {
test("should throw error if privateKey is undefined", () => {
expect(() => {
encryptAsymmetric({ plaintext, publicKey, privateKey });
}).toThrowError('invalid encoding');
}).toThrowError("invalid encoding");
});
test('should throw error if privateKey is empty string', () => {
privateKey = '';
test("should throw error if privateKey is empty string", () => {
privateKey = "";
expect(() => {
encryptAsymmetric({ plaintext, publicKey, privateKey });
}).toThrowError('bad secret key size');
}).toThrowError("bad secret key size");
});
});
describe('given undefined/invalid plaint text', () => {
const publicKey = '6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=';
const privateKey = 'Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=';
describe("given undefined/invalid plaint text", () => {
const publicKey = "6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=";
const privateKey = "Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=";
let plaintext: string;
test('should throw error if plaintext is undefined', () => {
test("should throw error if plaintext is undefined", () => {
expect(() => {
encryptAsymmetric({ plaintext, publicKey, privateKey });
}).toThrowError('expected string');
}).toThrowError("expected string");
});
test('should encrypt plaintext containing special characters', () => {
plaintext = '131@#$%235!@#&*(&123sadfkjadjf';
test("should encrypt plaintext containing special characters", () => {
plaintext = "131@#$%235!@#&*(&123sadfkjadjf";
const result = encryptAsymmetric({
plaintext,
publicKey,
privateKey
privateKey,
});
expect(result.ciphertext).toBeDefined();
expect(result.nonce).toBeDefined();
@@ -80,17 +80,17 @@ describe('Crypto', () => {
});
});
describe('decryptAsymmetric', () => {
describe('given all valid publicKey, privateKey and plaintext', () => {
const publicKey = '6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=';
const privateKey = 'Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=';
const plaintext = 'secret-message';
describe("decryptAsymmetric", () => {
describe("given all valid publicKey, privateKey and plaintext", () => {
const publicKey = "6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=";
const privateKey = "Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=";
const plaintext = "secret-message";
test('should decrypt the encrypted plaintext', () => {
test("should decrypt the encrypted plaintext", () => {
const encryptedResult = encryptAsymmetric({
plaintext,
publicKey,
privateKey
privateKey,
});
const ciphertext = encryptedResult.ciphertext;
const nonce = encryptedResult.nonce;
@@ -99,7 +99,7 @@ describe('Crypto', () => {
ciphertext,
nonce,
publicKey,
privateKey
privateKey,
});
expect(decryptedResult).toBeDefined();
@@ -107,18 +107,18 @@ describe('Crypto', () => {
});
});
describe('given ciphertext or nonce is modified before decrypt', () => {
const publicKey = '6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=';
const privateKey = 'Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=';
const plaintext = 'secret-message';
describe("given ciphertext or nonce is modified before decrypt", () => {
const publicKey = "6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=";
const privateKey = "Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=";
const plaintext = "secret-message";
test('should throw error if ciphertext is modified', () => {
test("should throw error if ciphertext is modified", () => {
const encryptedResult = encryptAsymmetric({
plaintext,
publicKey,
privateKey
privateKey,
});
const ciphertext = '=12adfJ@#52af1231=123'; // modified
const ciphertext = "=12adfJ@#52af1231=123"; // modified
const nonce = encryptedResult.nonce;
expect(() => {
@@ -126,28 +126,28 @@ describe('Crypto', () => {
ciphertext,
nonce,
publicKey,
privateKey
privateKey,
});
}).toThrowError('invalid encoding');
}).toThrowError("invalid encoding");
});
test('should throw error if nonce is modified', () => {
test("should throw error if nonce is modified", () => {
const encryptedResult = encryptAsymmetric({
plaintext,
publicKey,
privateKey
privateKey,
});
const ciphertext = encryptedResult.ciphertext;
const nonce = '=12adfJ@#52af1231=123'; // modified
const nonce = "=12adfJ@#52af1231=123"; // modified
expect(() => {
decryptAsymmetric({
ciphertext,
nonce,
publicKey,
privateKey
privateKey,
});
}).toThrowError('invalid encoding');
}).toThrowError("invalid encoding");
});
});
});