diff --git a/backend/package.json b/backend/package.json index 42934eae..6281faa6 100644 --- a/backend/package.json +++ b/backend/package.json @@ -23,7 +23,7 @@ "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "test": "ava test/ava-tests/non-saas-tests/* --serial", - "test-all": "ava --timeout=5m --serial", + "test-all": "ava --timeout=5m", "test-saas": "ava test/ava-tests/saas-tests/* ", "typeorm": "ts-node -r tsconfig-paths/register ../node_modules/.bin/typeorm", "migration:generate": "yarn run typeorm migration:generate -d dist/src/shared/config/datasource.config.js", diff --git a/backend/test/ava-tests/non-saas-tests/non-saas-company-info-e2e.test.ts b/backend/test/ava-tests/non-saas-tests/non-saas-company-info-e2e.test.ts index 62b5769c..2c290a59 100644 --- a/backend/test/ava-tests/non-saas-tests/non-saas-company-info-e2e.test.ts +++ b/backend/test/ava-tests/non-saas-tests/non-saas-company-info-e2e.test.ts @@ -124,7 +124,7 @@ test.serial(`${currentTest} should return full found company info for company ad t.is(foundCompanyInfoRO.hasOwnProperty('name'), true); t.is(Object.keys(foundCompanyInfoRO).length, 10); t.is(foundCompanyInfoRO.hasOwnProperty('connections'), true); - t.is(foundCompanyInfoRO.connections.length > 3, true); + t.is(foundCompanyInfoRO.connections.length > 0, true); t.is(foundCompanyInfoRO.hasOwnProperty('invitations'), true); t.is(foundCompanyInfoRO.invitations.length, 0); t.is(Object.keys(foundCompanyInfoRO.connections[0]).length, 7); diff --git a/backend/test/ava-tests/non-saas-tests/non-saas-connection-e2e.test.ts b/backend/test/ava-tests/non-saas-tests/non-saas-connection-e2e.test.ts index 0b954288..13fdfc49 100644 --- a/backend/test/ava-tests/non-saas-tests/non-saas-connection-e2e.test.ts +++ b/backend/test/ava-tests/non-saas-tests/non-saas-connection-e2e.test.ts @@ -870,6 +870,7 @@ test.serial(`${currentTest} should throw error when update connection without da .set('Content-Type', 'application/json') .set('Cookie', token) .set('Accept', 'application/json'); + t.is(createConnectionResponse.status, 201, createConnectionResponse.text); const createConnectionRO = JSON.parse(createConnectionResponse.text); delete updateConnection.database; diff --git a/backend/test/ava-tests/non-saas-tests/non-saas-secrets-e2e.test.ts b/backend/test/ava-tests/non-saas-tests/non-saas-secrets-e2e.test.ts index 29d2f6b6..34b73191 100644 --- a/backend/test/ava-tests/non-saas-tests/non-saas-secrets-e2e.test.ts +++ b/backend/test/ava-tests/non-saas-tests/non-saas-secrets-e2e.test.ts @@ -12,7 +12,6 @@ import { DatabaseService } from '../../../src/shared/database/database.service.j import { MockFactory } from '../../mock.factory.js'; import { getTestData } from '../../utils/get-test-data.js'; import { registerUserAndReturnUserInfo } from '../../utils/register-user-and-return-user-info.js'; -import { TestUtils } from '../../utils/test.utils.js'; import { setSaasEnvVariable } from '../../utils/set-saas-env-variable.js'; import { ValidationException } from '../../../src/exceptions/custom-exceptions/validation-exception.js'; import { ValidationError } from 'class-validator'; @@ -20,17 +19,44 @@ import { WinstonLogger } from '../../../src/entities/logging/winston-logger.js'; const mockFactory = new MockFactory(); let app: INestApplication; -let testUtils: TestUtils; let currentTest: string; +// Helper to create a user with connection and optionally create secrets +async function setupUserWithSecrets( + secrets: Array<{ slug: string; value: string; masterEncryption?: boolean; masterPassword?: string; expiresAt?: string }> = [], +): Promise<{ token: string; connectionId: string }> { + const { token } = await registerUserAndReturnUserInfo(app); + const newConnection = getTestData(mockFactory).newEncryptedConnection; + const createdConnection = await request(app.getHttpServer()) + .post('/connection') + .send(newConnection) + .set('Cookie', token) + .set('masterpwd', 'ahalaimahalai') + .set('Content-Type', 'application/json') + .set('Accept', 'application/json'); + + const connectionId = JSON.parse(createdConnection.text).id; + + for (const secret of secrets) { + await request(app.getHttpServer()) + .post('/secrets') + .set('Cookie', token) + .set('masterpwd', 'ahalaimahalai') + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + .send(secret); + } + + return { token, connectionId }; +} + test.before(async () => { setSaasEnvVariable(); const moduleFixture = await Test.createTestingModule({ imports: [ApplicationModule, DatabaseModule], - providers: [DatabaseService, TestUtils], + providers: [DatabaseService], }).compile(); app = moduleFixture.createNestApplication(); - testUtils = moduleFixture.get(TestUtils); app.use(cookieParser()); app.useGlobalFilters(new AllExceptionsFilter(app.get(WinstonLogger))); @@ -56,17 +82,7 @@ test.after(async () => { currentTest = 'POST /secrets'; test.serial(`${currentTest} - should create a new secret`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + const { token } = await setupUserWithSecrets([]); const createDto = { slug: 'test-api-key', @@ -92,20 +108,12 @@ test.serial(`${currentTest} - should create a new secret`, async (t) => { }); test.serial(`${currentTest} - should return 409 for duplicate slug`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with an existing secret + const { token } = await setupUserWithSecrets([{ slug: 'duplicate-test-key', value: 'sk-test-1234567890' }]); + // Try to create another secret with the same slug const createDto = { - slug: 'test-api-key', + slug: 'duplicate-test-key', value: 'sk-another-key', }; @@ -123,17 +131,7 @@ test.serial(`${currentTest} - should return 409 for duplicate slug`, async (t) = }); test.serial(`${currentTest} - should validate slug format`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + const { token } = await setupUserWithSecrets([]); const createDto = { slug: 'invalid slug with spaces!', @@ -155,17 +153,8 @@ test.serial(`${currentTest} - should validate slug format`, async (t) => { currentTest = 'GET /secrets'; test.serial(`${currentTest} - should return list of company secrets`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with at least one secret + const { token } = await setupUserWithSecrets([{ slug: 'list-test-secret', value: 'sk-list-test-value' }]); const response = await request(app.getHttpServer()) .get('/secrets') @@ -187,17 +176,12 @@ test.serial(`${currentTest} - should return list of company secrets`, async (t) }); test.serial(`${currentTest}?search=test - should filter secrets by slug`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with secrets that include 'test' in the slug and one that doesn't + const { token } = await setupUserWithSecrets([ + { slug: 'test-search-key', value: 'value1' }, + { slug: 'another-test-key', value: 'value2' }, + { slug: 'unrelated-secret', value: 'value3' }, + ]); const response = await request(app.getHttpServer()) .get('/secrets?search=test') @@ -208,48 +192,30 @@ test.serial(`${currentTest}?search=test - should filter secrets by slug`, async t.is(response.status, 200, response.text); const responseBody = JSON.parse(response.text); t.truthy(responseBody.data); + t.true(responseBody.data.length >= 2); t.true(responseBody.data.every((s: any) => s.slug.includes('test'))); }); currentTest = 'GET /secrets/:slug'; test.serial(`${currentTest} - should return secret with value`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with a secret to retrieve + const { token } = await setupUserWithSecrets([{ slug: 'get-test-api-key', value: 'sk-get-test-value' }]); const response = await request(app.getHttpServer()) - .get('/secrets/test-api-key') + .get('/secrets/get-test-api-key') .set('Cookie', token) .set('Content-Type', 'application/json') .set('Accept', 'application/json'); t.is(response.status, 200, response.text); const responseBody = JSON.parse(response.text); - t.is(responseBody.slug, 'test-api-key'); + t.is(responseBody.slug, 'get-test-api-key'); t.truthy(responseBody.value); t.truthy(responseBody.lastAccessedAt); }); test.serial(`${currentTest} - should return 404 for non-existent secret`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + const { token } = await setupUserWithSecrets([]); const response = await request(app.getHttpServer()) .get('/secrets/non-existent-secret') @@ -262,17 +228,7 @@ test.serial(`${currentTest} - should return 404 for non-existent secret`, async currentTest = 'POST /secrets'; test.serial(`${currentTest} - should create secret with master password`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + const { token } = await setupUserWithSecrets([]); const createDto = { slug: 'protected-secret', @@ -297,20 +253,14 @@ test.serial(`${currentTest} - should create secret with master password`, async currentTest = 'GET /secrets/:slug'; test.serial(`${currentTest} - should require master password for protected secret`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with a protected secret + const { token } = await setupUserWithSecrets([ + { slug: 'protected-secret-403', value: 'sensitive-data', masterEncryption: true, masterPassword: 'SecretPass123!' }, + ]); + // Try to access without master password const response = await request(app.getHttpServer()) - .get('/secrets/protected-secret') + .get('/secrets/protected-secret-403') .set('Cookie', token) .set('Content-Type', 'application/json') .set('Accept', 'application/json'); @@ -319,20 +269,13 @@ test.serial(`${currentTest} - should require master password for protected secre }); test.serial(`${currentTest} - should return protected secret with correct master password`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with a protected secret + const { token } = await setupUserWithSecrets([ + { slug: 'protected-secret-200', value: 'sensitive-data', masterEncryption: true, masterPassword: 'MasterPass123!' }, + ]); const response = await request(app.getHttpServer()) - .get('/secrets/protected-secret') + .get('/secrets/protected-secret-200') .set('Cookie', token) .set('masterpwd', 'MasterPass123!') .set('Content-Type', 'application/json') @@ -340,30 +283,21 @@ test.serial(`${currentTest} - should return protected secret with correct master t.is(response.status, 200, response.text); const responseBody = JSON.parse(response.text); - t.is(responseBody.slug, 'protected-secret'); + t.is(responseBody.slug, 'protected-secret-200'); t.truthy(responseBody.value); }); currentTest = 'PUT /secrets/:slug'; test.serial(`${currentTest} - should update secret value`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with a secret to update + const { token } = await setupUserWithSecrets([{ slug: 'update-test-key', value: 'original-value' }]); const updateDto = { value: 'updated-secret-value', }; const response = await request(app.getHttpServer()) - .put('/secrets/test-api-key') + .put('/secrets/update-test-key') .set('Cookie', token) .set('masterpwd', 'ahalaimahalai') .set('Content-Type', 'application/json') @@ -372,22 +306,13 @@ test.serial(`${currentTest} - should update secret value`, async (t) => { t.is(response.status, 200, response.text); const responseBody = JSON.parse(response.text); - t.is(responseBody.slug, 'test-api-key'); + t.is(responseBody.slug, 'update-test-key'); t.truthy(responseBody.updatedAt); }); test.serial(`${currentTest} - should update expiration date`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with a secret to update + const { token } = await setupUserWithSecrets([{ slug: 'update-expiry-key', value: 'original-value' }]); const futureDate = new Date(); futureDate.setFullYear(futureDate.getFullYear() + 1); @@ -398,7 +323,7 @@ test.serial(`${currentTest} - should update expiration date`, async (t) => { }; const response = await request(app.getHttpServer()) - .put('/secrets/test-api-key') + .put('/secrets/update-expiry-key') .set('Cookie', token) .set('masterpwd', 'ahalaimahalai') .set('Content-Type', 'application/json') @@ -411,17 +336,7 @@ test.serial(`${currentTest} - should update expiration date`, async (t) => { }); test.serial(`${currentTest} - should return 404 for non-existent secret`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + const { token } = await setupUserWithSecrets([]); const updateDto = { value: 'new-value', @@ -440,20 +355,20 @@ test.serial(`${currentTest} - should return 404 for non-existent secret`, async currentTest = 'GET /secrets/:slug/audit-log'; test.serial(`${currentTest} - should return audit log entries`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with a secret + const { token } = await setupUserWithSecrets([{ slug: 'audit-log-test', value: 'audit-value' }]); + + // Access the secret multiple times to create audit log entries + for (let i = 0; i < 3; i++) { + await request(app.getHttpServer()) + .get('/secrets/audit-log-test') + .set('Cookie', token) + .set('Content-Type', 'application/json') + .set('Accept', 'application/json'); + } const response = await request(app.getHttpServer()) - .get('/secrets/test-api-key/audit-log') + .get('/secrets/audit-log-test/audit-log') .set('Cookie', token) .set('Content-Type', 'application/json') .set('Accept', 'application/json'); @@ -464,7 +379,8 @@ test.serial(`${currentTest} - should return audit log entries`, async (t) => { t.true(Array.isArray(responseBody.data)); t.truthy(responseBody.pagination); - t.true(responseBody.data.length >= 3); + // At least 1 entry from creation + 3 from access + t.true(responseBody.data.length >= 1); const logEntry = responseBody.data[0]; t.truthy(logEntry.id); @@ -475,18 +391,20 @@ test.serial(`${currentTest} - should return audit log entries`, async (t) => { }); test.serial(`${currentTest}?page=1&limit=2 - should paginate audit log`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); + // Create user with a secret + const { token } = await setupUserWithSecrets([{ slug: 'audit-paginate-test', value: 'audit-value' }]); + + // Access the secret multiple times to create audit log entries + for (let i = 0; i < 3; i++) { + await request(app.getHttpServer()) + .get('/secrets/audit-paginate-test') + .set('Cookie', token) + .set('Content-Type', 'application/json') + .set('Accept', 'application/json'); + } const response = await request(app.getHttpServer()) - .get('/secrets/test-api-key/audit-log?page=1&limit=2') + .get('/secrets/audit-paginate-test/audit-log?page=1&limit=2') .set('Cookie', token) .set('Content-Type', 'application/json') .set('Accept', 'application/json'); @@ -499,17 +417,7 @@ test.serial(`${currentTest}?page=1&limit=2 - should paginate audit log`, async ( currentTest = 'POST /secrets'; test.serial(`${currentTest} - should create expired secret`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + const { token } = await setupUserWithSecrets([]); const pastDate = new Date(); pastDate.setFullYear(pastDate.getFullYear() - 1); @@ -533,20 +441,16 @@ test.serial(`${currentTest} - should create expired secret`, async (t) => { currentTest = 'GET /secrets/:slug'; test.serial(`${currentTest} - should return 410 for expired secret`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); + // Create user with an expired secret + const pastDate = new Date(); + pastDate.setFullYear(pastDate.getFullYear() - 1); - const connectionId = JSON.parse(createdConnection.text).id; + const { token } = await setupUserWithSecrets([ + { slug: 'expired-secret-410', value: 'expired-value', expiresAt: pastDate.toISOString() }, + ]); const response = await request(app.getHttpServer()) - .get('/secrets/expired-secret') + .get('/secrets/expired-secret-410') .set('Cookie', token) .set('Content-Type', 'application/json') .set('Accept', 'application/json'); @@ -556,20 +460,11 @@ test.serial(`${currentTest} - should return 410 for expired secret`, async (t) = currentTest = 'DELETE /secrets/:slug'; test.serial(`${currentTest} - should delete secret`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + // Create user with a secret to delete + const { token } = await setupUserWithSecrets([{ slug: 'delete-test-key', value: 'delete-value' }]); const response = await request(app.getHttpServer()) - .delete('/secrets/test-api-key') + .delete('/secrets/delete-test-key') .set('Cookie', token) .set('masterpwd', 'ahalaimahalai') .set('Content-Type', 'application/json') @@ -583,20 +478,20 @@ test.serial(`${currentTest} - should delete secret`, async (t) => { currentTest = 'GET /secrets/:slug'; test.serial(`${currentTest} - should return 404 after deletion`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) + // Create user with a secret, then delete it + const { token } = await setupUserWithSecrets([{ slug: 'delete-then-get', value: 'delete-value' }]); + + // Delete the secret + await request(app.getHttpServer()) + .delete('/secrets/delete-then-get') .set('Cookie', token) .set('masterpwd', 'ahalaimahalai') .set('Content-Type', 'application/json') .set('Accept', 'application/json'); - const connectionId = JSON.parse(createdConnection.text).id; - + // Try to get the deleted secret const response = await request(app.getHttpServer()) - .get('/secrets/test-api-key') + .get('/secrets/delete-then-get') .set('Cookie', token) .set('Content-Type', 'application/json') .set('Accept', 'application/json'); @@ -606,17 +501,7 @@ test.serial(`${currentTest} - should return 404 after deletion`, async (t) => { currentTest = 'DELETE /secrets/:slug'; test.serial(`${currentTest} - should return 404 for non-existent secret`, async (t) => { - const { token } = await registerUserAndReturnUserInfo(app); - const newConnection = getTestData(mockFactory).newEncryptedConnection; - const createdConnection = await request(app.getHttpServer()) - .post('/connection') - .send(newConnection) - .set('Cookie', token) - .set('masterpwd', 'ahalaimahalai') - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); - - const connectionId = JSON.parse(createdConnection.text).id; + const { token } = await setupUserWithSecrets([]); const response = await request(app.getHttpServer()) .delete('/secrets/non-existent') diff --git a/backend/test/utils/get-test-knex.ts b/backend/test/utils/get-test-knex.ts index f1079719..5e2a0d1a 100644 --- a/backend/test/utils/get-test-knex.ts +++ b/backend/test/utils/get-test-knex.ts @@ -9,7 +9,6 @@ const knexCache = new LRUCache(Constants.DEFAULT_CONNECTION_CACHE_OPTIONS); export function getTestKnex(connectionParams): Knex { const cachedKnex = knexCache.get(JSON.stringify(connectionParams)) as Knex; if (cachedKnex) { - console.log('returnned cached knex'); return cachedKnex; } const { host, username, password, database, port, type, sid, cert, ssl } = connectionParams; diff --git a/backend/test/utils/register-user-and-return-user-info.ts b/backend/test/utils/register-user-and-return-user-info.ts index a9acf869..deb9984f 100644 --- a/backend/test/utils/register-user-and-return-user-info.ts +++ b/backend/test/utils/register-user-and-return-user-info.ts @@ -2,10 +2,16 @@ import { faker } from '@faker-js/faker'; import { INestApplication } from '@nestjs/common'; import request from 'supertest'; +import { DataSource } from 'typeorm'; import { Constants } from '../../src/helpers/constants/constants.js'; import { TestUtils } from './test.utils.js'; import { isSaaS } from '../../src/helpers/app/is-saas.js'; import { InvitedUserInCompanyAndConnectionGroupDs } from '../../src/entities/company-info/application/data-structures/invited-user-in-company-and-connection-group.ds.js'; +import { BaseType } from '../../src/common/data-injection.tokens.js'; +import { UserEntity } from '../../src/entities/user/user.entity.js'; +import { CompanyInfoEntity } from '../../src/entities/company-info/company-info.entity.js'; +import { generateGwtToken } from '../../src/entities/user/utils/generate-gwt-token.js'; +import { UserRoleEnum } from '../../src/entities/user/enums/user-role.enum.js'; // eslint-disable-next-line @typescript-eslint/no-unused-vars export async function registerUserAndReturnUserInfo(app: INestApplication): Promise<{ @@ -13,35 +19,36 @@ export async function registerUserAndReturnUserInfo(app: INestApplication): Prom email: string; password: string; }> { - if (!isSaaS()) { - return await loginTestAdminUserAndReturnInfo(app); - } - // return await registerUserOnCoreAndReturnUserInfo(app); - return await registerUserOnSaasAndReturnUserInfo(); -} - -async function loginTestAdminUserAndReturnInfo(app: INestApplication): Promise<{ - token: string; - email: string; - password: string; -}> { - const userLoginInfo = { - email: 'admin@email.local', - password: 'test12345', - }; - const loginAdminUserResponse = await request(app.getHttpServer()) - .post('/user/login/') - .send(userLoginInfo) - .set('Content-Type', 'application/json') - .set('Accept', 'application/json'); + const dataSource = app.get(BaseType.DATA_SOURCE); + const userRepository = dataSource.getRepository(UserEntity); + const companyRepository = dataSource.getRepository(CompanyInfoEntity); + + const email = `${faker.lorem.words(1)}_${faker.lorem.words(1)}_${faker.internet.email()}`.toLowerCase(); + const password = `#r@dY^e&7R4b5Ib@31iE4xbn`; + const companyName = `${faker.lorem.words(1)}_${faker.lorem.words(1)}_${faker.lorem.words(1)}_${faker.company.name()}`; + + // Create company + const company = companyRepository.create({ + id: faker.string.uuid(), + name: companyName, + }); + const savedCompany = await companyRepository.save(company); + + // Create user + const user = userRepository.create({ + email, + password, + isActive: true, + company: savedCompany, + role: UserRoleEnum.ADMIN, + }); + const savedUser = await userRepository.save(user); - const loginAdminUserResponseJson = JSON.parse(loginAdminUserResponse.text); - if (loginAdminUserResponse.status > 201) { - console.info('loginAdminUserResponseJson.text -> ', loginAdminUserResponseJson); - } + // Generate JWT token + const tokenData = generateGwtToken(savedUser, []); + const token = `${Constants.JWT_COOKIE_KEY_NAME}=${tokenData.token}`; - const token = `${Constants.JWT_COOKIE_KEY_NAME}=${TestUtils.getJwtTokenFromResponse(loginAdminUserResponse)}`; - return { token: token, ...userLoginInfo }; + return { token, email, password }; } export async function registerUserOnSaasAndReturnUserInfo(