feat: added logged in based testing by injecting jwt token logic

This commit is contained in:
Akhil Mohan
2024-01-27 12:38:23 +05:30
parent dd7c449483
commit 0506389ada
10 changed files with 125 additions and 28 deletions
+1
View File
@@ -19,4 +19,5 @@ declare global {
// used only for testing
const testServer: FastifyZodProvider;
const jwtAuthToken: string;
}
+16
View File
@@ -0,0 +1,16 @@
export const seedData1 = {
id: "3dafd81d-4388-432b-a4c5-f735616868c1",
email: "[email protected]",
password: process.env.TEST_USER_PASSWORD || "testInfisical@1",
organization: {
id: "180870b7-f464-4740-8ffe-9d11c9245ea7",
name: "infisical"
},
project: {
id: "77fa7aed-9288-401e-a4c9-3a9430be62a0",
name: "first project"
},
token: {
id: "a9dfafba-a3b7-42e3-8618-91abb702fd36"
}
};
+25 -12
View File
@@ -1,17 +1,16 @@
import { Knex } from "knex";
import crypto from "node:crypto";
import argon2, { argon2id } from "argon2";
import jsrp from "jsrp";
import { Knex } from "knex";
import nacl from "tweetnacl";
import { encodeBase64 } from "tweetnacl-util";
import argon2, { argon2id } from "argon2";
import { AuthMethod } from "../../services/auth/auth-type";
import { TableName } from "../schemas";
import crypto from "node:crypto";
import { encryptSymmetric } from "@app/lib/crypto";
export const testUser = {
email: "[email protected]",
password: process.env.TEST_USER_PASSWORD || "testInfisical@1"
};
import { AuthMethod } from "../../services/auth/auth-type";
import { TableName } from "../schemas";
import { seedData1 } from "../seed-data";
export const generateUserSrpKeys = async (password: string) => {
const pair = nacl.box.keyPair();
@@ -23,7 +22,7 @@ export const generateUserSrpKeys = async (password: string) => {
// eslint-disable-next-line
const client = new jsrp.client();
await new Promise((resolve) => {
client.init({ username: testUser.email, password: testUser.password }, () => resolve(null));
client.init({ username: seedData1.email, password: seedData1.password }, () => resolve(null));
});
const { salt, verifier } = await new Promise<{ salt: string; verifier: string }>(
(resolve, reject) => {
@@ -85,7 +84,9 @@ export async function seed(knex: Knex): Promise<void> {
const [user] = await knex(TableName.Users)
.insert([
{
email: testUser.email,
// @ts-ignore to calculate predefined
id: seedData1.id,
email: seedData1.email,
superAdmin: true,
firstName: "test",
lastName: "",
@@ -98,7 +99,7 @@ export async function seed(knex: Knex): Promise<void> {
])
.returning("*");
const encKeys = await generateUserSrpKeys(testUser.password);
const encKeys = await generateUserSrpKeys(seedData1.password);
// password: testInfisical@1
await knex(TableName.UserEncryptionKey).insert([
{
@@ -115,4 +116,16 @@ export async function seed(knex: Knex): Promise<void> {
userId: user.id
}
]);
await knex(TableName.AuthTokenSession).insert({
// @ts-ignore
id: seedData1.token.id,
userId: seedData1.id,
ip: "151.196.220.213",
userAgent:
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 RuxitSynthetic/1.0 v3690753611340580436 t8052286838287810618",
accessVersion: 1,
refreshVersion: 1,
lastUsed: new Date()
});
}
+4 -2
View File
@@ -1,19 +1,21 @@
import { Knex } from "knex";
import { OrgMembershipRole, OrgMembershipStatus, TableName } from "../schemas";
import { testUser } from "./1-user";
import { seedData1 } from "../seed-data";
export async function seed(knex: Knex): Promise<void> {
// Deletes ALL existing entries
await knex(TableName.Organization).del();
await knex(TableName.OrgMembership).del();
const user = await knex(TableName.Users).where({ email: testUser.email }).first();
const user = await knex(TableName.Users).where({ email: seedData1.email }).first();
if (!user) throw new Error("User not found");
// Inserts seed entries
const [org] = await knex(TableName.Organization)
.insert([
{
// @ts-ignore because we need that id for api calls
id: seedData1.organization.id,
name: "infisical",
customerId: null
}
+39
View File
@@ -0,0 +1,39 @@
import { Knex } from "knex";
import { TableName } from "../schemas";
import { seedData1 } from "../seed-data";
export const DEFAULT_PROJECT_ENVS = [
{ name: "Development", slug: "dev" },
{ name: "Staging", slug: "staging" },
{ name: "Production", slug: "prod" }
];
export async function seed(knex: Knex): Promise<void> {
// Deletes ALL existing entries
await knex(TableName.Project).del();
await knex(TableName.Environment).del();
await knex(TableName.SecretFolder).del();
const [project] = await knex(TableName.Project)
.insert({
name: seedData1.project.name,
orgId: seedData1.organization.id,
// @ts-ignore pre calc id
id: seedData1.project.id
})
.returning("*");
const envs = await knex(TableName.Environment)
.insert(
DEFAULT_PROJECT_ENVS.map(({ name, slug }, index) => ({
name,
slug,
projectId: project.id,
position: index + 1
}))
)
.returning("*");
await knex(TableName.SecretFolder).insert(
envs.map(({ id }) => ({ name: "root", envId: id, parentId: null }))
);
}
@@ -22,7 +22,7 @@ import { ROOT_FOLDER_NAME, TSecretFolderDalFactory } from "../secret-folder/secr
import { TProjectDalFactory } from "./project-dal";
import { TCreateProjectDTO, TDeleteProjectDTO, TGetProjectDTO } from "./project-types";
const DEFAULT_PROJECT_ENVS = [
export const DEFAULT_PROJECT_ENVS = [
{ name: "Development", slug: "dev" },
{ name: "Staging", slug: "staging" },
{ name: "Production", slug: "prod" }