Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

unit tests for utils/posthog and utils/crypto

Aashish-Upadhyay-1011c4649cc9e9f
diff --git a/backend/package.json b/backend/package.json
index 3d36797a59..44abffbe14 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -57,7 +57,7 @@
"lint-and-fix": "eslint . --ext .ts --fix",
"lint-staged": "lint-staged",
"pretest": "docker compose -f test-resources/docker-compose.test.yml up -d",
- "test": "cross-env NODE_ENV=test jest --testTimeout=10000 --detectOpenHandles",
+ "test": "cross-env NODE_ENV=test jest --verbose --testTimeout=10000 --detectOpenHandles",
"test:ci": "npm test -- --watchAll=false --ci --reporters=default --reporters=jest-junit --reporters=github-actions --coverage --testLocationInResults --json --outputFile=coverage/report.json",
"posttest": "docker compose -f test-resources/docker-compose.test.yml down"
},
diff --git a/backend/tests/example.test.ts b/backend/tests/example.test.ts
deleted file mode 100644
index 6d38025a5a..0000000000
--- a/backend/tests/example.test.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import { it, expect } from '@jest/globals';
-
-it('should return true', () => {
- expect(true).toBeTruthy();
-});
diff --git a/backend/tests/utils/crypto.test.ts b/backend/tests/utils/crypto.test.ts
new file mode 100644
index 0000000000..2cd442b17c
--- /dev/null
+++ b/backend/tests/utils/crypto.test.ts
@@ -0,0 +1,92 @@
+import { describe, test, expect } from '@jest/globals';
+import {
+ decryptAsymmetric,
+ decryptSymmetric,
+ encryptAsymmetric,
+ encryptSymmetric
+} from '../../src/utils/crypto';
+
+describe('Crypto', () => {
+ const publicKey = '6U5m6S5jlyazJ+R4z7Yf/Ah4th4JwKxDN8Wn7+upvzw=';
+ const privateKey = 'Z8W53YV+2ddjJCrFwzptjK96y2QsQI9oXuvfcx+qxz0=';
+ const plaintext = 'secret-message';
+
+ describe('encryptAsymmetric', () => {
+ test('should encrypt plain text', () => {
+ const result = encryptAsymmetric({ plaintext, publicKey, privateKey });
+ expect(result.ciphertext).toBeDefined();
+ expect(result.nonce).toBeDefined();
+ });
+ });
+
+ describe('decryptAsymmetric', () => {
+ test('should decrypt the encrypted plaintext', () => {
+ const encryptedResult = encryptAsymmetric({
+ plaintext,
+ publicKey,
+ privateKey
+ });
+ const ciphertext = encryptedResult.ciphertext;
+ const nonce = encryptedResult.nonce;
+
+ const decryptedResult = decryptAsymmetric({
+ ciphertext,
+ nonce,
+ publicKey,
+ privateKey
+ });
+
+ expect(decryptedResult).toBeDefined();
+ expect(decryptedResult).toEqual(plaintext);
+ });
+ });
+
+ describe('encryptSymmetric', () => {
+ const plaintext = 'secret-message';
+ const key = '7e8ee7e5cc667b9c1829783ad31f36f4';
+
+ test('should encrypt plaintext with the given key', () => {
+ const { ciphertext, iv, tag } = encryptSymmetric({ plaintext, key });
+ expect(ciphertext).toBeDefined();
+ expect(iv).toBeDefined();
+ expect(tag).toBeDefined();
+ });
+
+ test('should throw an error when encryption fails', () => {
+ const invalidKey = 'invalid-key';
+
+ expect(() => {
+ encryptSymmetric({ plaintext, key: invalidKey });
+ }).toThrowError();
+ });
+ });
+
+ describe('decryptSymmetric', () => {
+ const key = '7e8ee7e5cc667b9c1829783ad31f36f4';
+ const { ciphertext, iv, tag } = encryptSymmetric({ plaintext, key });
+
+ test('should decrypt encrypted plaintext', () => {
+ const result = decryptSymmetric({
+ ciphertext,
+ iv,
+ tag,
+ key
+ });
+
+ expect(result).toBeDefined();
+ expect(result).toEqual(plaintext);
+ });
+
+ test('should throw an error when decryption fails', () => {
+ const invalidKey = 'invalid-key';
+ expect(() => {
+ decryptSymmetric({
+ ciphertext,
+ iv,
+ tag,
+ key: invalidKey
+ });
+ }).toThrowError();
+ });
+ });
+});
diff --git a/backend/tests/utils/posthog.test.ts b/backend/tests/utils/posthog.test.ts
new file mode 100644
index 0000000000..a380060b0f
--- /dev/null
+++ b/backend/tests/utils/posthog.test.ts
@@ -0,0 +1,29 @@
+import { describe, test, expect } from '@jest/globals';
+import { getChannelFromUserAgent } from '../../src/utils/posthog';
+
+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';
+ const channel = getChannelFromUserAgent(userAgent);
+ expect(channel).toBe('web');
+ });
+
+ test("should return 'cli'", () => {
+ const userAgent = 'cli';
+ const channel = getChannelFromUserAgent(userAgent);
+ expect(channel).toBe('cli');
+ });
+
+ test("should return 'k8-operator'", () => {
+ const userAgent = 'k8-operator';
+ const channel = getChannelFromUserAgent(userAgent);
+ expect(channel).toBe('k8-operator');
+ });
+
+ test('should return undefined if no userAgent', () => {
+ const userAgent = undefined;
+ const channel = getChannelFromUserAgent(userAgent);
+ expect(channel).toBe('other');
+ });
+});