Add eslint rule and fix as many issues as possible

This commit is contained in:
Khoa Le
2023-06-19 23:32:35 -04:00
parent e9eacc445d
commit 9d33e4756b
689 changed files with 9446 additions and 21821 deletions

View File

@@ -4,15 +4,15 @@ import axiosInstance from "../../src/config/request";
import { Secret } from "../../src/models";
import { testUserEmail, testUserPassword } from "../../src/utils/addDevelopmentUser";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const crypto = require('crypto')
const crypto = require("crypto")
// eslint-disable-next-line @typescript-eslint/no-var-requires
const jsrp = require('jsrp');
const jsrp = require("jsrp");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const axios = require('axios');
const axios = require("axios");
import { plainTextWorkspaceKey, testWorkspaceId } from "../../src/utils/addDevelopmentUser";
import {
encryptSymmetric128BitHexKeyUTF8
} from '../../src/utils/crypto';
encryptSymmetric128BitHexKeyUTF8,
} from "../../src/utils/crypto";
interface TokenData {
token: string;
@@ -37,11 +37,11 @@ export const getJWTFromTestUser = (): Promise<TokenData> => {
// POST: /login1
const reqBody = {
email: EMAIL,
clientPublicKey
clientPublicKey,
}
const loginOneRes = await axiosInstance.post('http://localhost:4000/api/v1/auth/login1', reqBody);
const loginOneRes = await axiosInstance.post("http://localhost:4000/api/v1/auth/login1", reqBody);
const serverPublicKey = loginOneRes.data.serverPublicKey;
const salt = loginOneRes.data.salt;
@@ -53,10 +53,10 @@ export const getJWTFromTestUser = (): Promise<TokenData> => {
// POST: /login2
const reqBody2 = {
email: EMAIL,
clientProof
clientProof,
}
const response2 = await axiosInstance.post('http://localhost:4000/api/v1/auth/login2', reqBody2);
const response2 = await axiosInstance.post("http://localhost:4000/api/v1/auth/login2", reqBody2);
resolve(response2.data)
})
@@ -65,25 +65,25 @@ export const getJWTFromTestUser = (): Promise<TokenData> => {
export const getServiceTokenFromTestUser = async () => {
const loggedInUserDetails = await getJWTFromTestUser()
const randomBytes = crypto.randomBytes(16).toString('hex');
const randomBytes = crypto.randomBytes(16).toString("hex");
const { ciphertext, iv, tag } = encryptSymmetric128BitHexKeyUTF8({
plaintext: plainTextWorkspaceKey,
key: randomBytes,
});
const newServiceToken = await axiosInstance.post('http://localhost:4000/api/v2/service-token/', {
'name': "test service token",
'workspaceId': testWorkspaceId,
'environment': "dev",
'encryptedKey': ciphertext,
'iv': iv,
'tag': tag,
'expiresIn': Date.now() + 90000,
'permissions': ["read"]
const newServiceToken = await axiosInstance.post("http://localhost:4000/api/v2/service-token/", {
"name": "test service token",
"workspaceId": testWorkspaceId,
"environment": "dev",
"encryptedKey": ciphertext,
"iv": iv,
"tag": tag,
"expiresIn": Date.now() + 90000,
"permissions": ["read"],
}, {
headers: {
'Authorization': `Bearer ${loggedInUserDetails.token}`
}
"Authorization": `Bearer ${loggedInUserDetails.token}`,
},
});
return `${newServiceToken.data.serviceToken}.${randomBytes}`

View File

@@ -1,6 +1,6 @@
import request from 'supertest'
import main from '../../../../src/index'
import { getServiceTokenFromTestUser } from '../../../helper/helper';
import request from "supertest"
import main from "../../../../src/index"
import { getServiceTokenFromTestUser } from "../../../helper/helper";
let server: any;
beforeAll(async () => {
@@ -20,18 +20,18 @@ describe("GET /api/v2/service-token", () => {
// get the service token details
const serviceTokenDetails = await request(server)
.get("/api/v2/service-token")
.set('Authorization', `Bearer ${serviceToken}`)
.set("Authorization", `Bearer ${serviceToken}`)
expect(serviceTokenDetails.body).toMatchObject({
_id: expect.any(String),
name: 'test service token',
workspace: '63cefb15c8d3175601cfa989',
environment: 'dev',
name: "test service token",
workspace: "63cefb15c8d3175601cfa989",
environment: "dev",
user: {
_id: '63cefa6ec8d3175601cfa980',
email: 'test@localhost.local',
firstName: 'Jake',
lastName: 'Moni',
_id: "63cefa6ec8d3175601cfa980",
email: "test@localhost.local",
firstName: "Jake",
lastName: "Moni",
isMfaEnabled: false,
mfaMethods: expect.any(Array),
devices: [
@@ -49,7 +49,7 @@ describe("GET /api/v2/service-token", () => {
encryptedKey: expect.any(String),
iv: expect.any(String),
tag: expect.any(String),
permissions: ['read'],
permissions: ["read"],
createdAt: expect.any(String),
updatedAt: expect.any(String),
});

View File

@@ -1,7 +1,7 @@
import { Server } from 'http';
import main from '../src';
import { describe, expect, it, beforeAll, afterAll } from '@jest/globals';
import request from 'supertest';
import { Server } from "http";
import main from "../src";
import { afterAll, beforeAll, describe, expect, it } from "@jest/globals";
import request from "supertest";
let server: Server;
@@ -13,9 +13,9 @@ afterAll(async () => {
server.close();
});
describe('Healthcheck endpoint', () => {
it('GET /healthcheck should return OK', async () => {
const res = await request(server).get('/healthcheck');
describe("Healthcheck endpoint", () => {
it("GET /healthcheck should return OK", async () => {
const res = await request(server).get("/healthcheck");
expect(res.status).toEqual(200);
});
});

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");
});
});
});

View File

@@ -1,29 +1,29 @@
import { describe, test, expect } from '@jest/globals';
import { getChannelFromUserAgent } from '../../../src/utils/posthog';
import { describe, expect, test } from "@jest/globals";
import { getChannelFromUserAgent } from "../../../src/utils/posthog";
describe('posthog getChannelFromUserAgent', () => {
describe("posthog getChannelFromUserAgent", () => {
test("should return 'web' when userAgent includes 'mozilla'", () => {
const userAgent =
'Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.5563.115 Mobile Safari/537.36';
"Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.5563.115 Mobile Safari/537.36";
const channel = getChannelFromUserAgent(userAgent);
expect(channel).toBe('web');
expect(channel).toBe("web");
});
test("should return 'cli'", () => {
const userAgent = 'cli';
const userAgent = "cli";
const channel = getChannelFromUserAgent(userAgent);
expect(channel).toBe('cli');
expect(channel).toBe("cli");
});
test("should return 'k8-operator'", () => {
const userAgent = 'k8-operator';
const userAgent = "k8-operator";
const channel = getChannelFromUserAgent(userAgent);
expect(channel).toBe('k8-operator');
expect(channel).toBe("k8-operator");
});
test('should return undefined if no userAgent', () => {
test("should return undefined if no userAgent", () => {
const userAgent = undefined;
const channel = getChannelFromUserAgent(userAgent);
expect(channel).toBe('other');
expect(channel).toBe("other");
});
});