From 7a930a9af29f3a6fd5286ee8d6bedef8eb2c54cf Mon Sep 17 00:00:00 2001 From: Sergey Konovalov Date: Sun, 31 Aug 2025 00:01:32 +0300 Subject: [PATCH] [linter] manual-fix --- .../forgottenFilesCommnads.tests.js | 69 +++++++++---------- .../withServerInstance/storage.tests.js | 33 ++++----- tests/unit/request.tests.js | 35 ++++------ tests/unit/requestSSRF.tests.js | 2 +- 4 files changed, 62 insertions(+), 77 deletions(-) diff --git a/tests/integration/withServerInstance/forgottenFilesCommnads.tests.js b/tests/integration/withServerInstance/forgottenFilesCommnads.tests.js index 0255f508..224bc8cf 100644 --- a/tests/integration/withServerInstance/forgottenFilesCommnads.tests.js +++ b/tests/integration/withServerInstance/forgottenFilesCommnads.tests.js @@ -31,7 +31,6 @@ */ const {describe, test, expect, afterAll, beforeAll} = require('@jest/globals'); -const http = require('http'); const {signToken} = require('../../../DocService/sources/DocsCoServer'); const storage = require('../../../Common/sources/storage/storage-base'); @@ -62,49 +61,45 @@ const testFilesNames = { getList: 'DocService-DocsCoServer-forgottenFilesCommands-getForgottenList-integration-test' }; -function makeRequest(requestBody, timeout = 5000) { - return new Promise(async (resolve, reject) => { - const timer = setTimeout(() => reject('Request timeout'), timeout); +/** + * Makes HTTP request to the command service + * @param {Object} requestBody - Request payload + * @param {number} timeout - Request timeout in milliseconds + * @returns {Promise} Response data + */ +async function makeRequest(requestBody, timeout = 5000) { + let body = ''; + if (cfgTokenEnableRequestOutbox) { + const secret = utils.getSecretByElem(cfgSecretOutbox); + const token = await signToken(ctx, requestBody, cfgTokenAlgorithm, cfgTokenOutboxExpires, constants.c_oAscSecretType.Inbox, secret); + body = JSON.stringify({token}); + } else { + body = JSON.stringify(requestBody); + } - let body = ''; - if (cfgTokenEnableRequestOutbox) { - const secret = utils.getSecretByElem(cfgSecretOutbox); - const token = await signToken(ctx, requestBody, cfgTokenAlgorithm, cfgTokenOutboxExpires, constants.c_oAscSecretType.Inbox, secret); - body = JSON.stringify({token}); - } else { - body = JSON.stringify(requestBody); - } + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), timeout); - const options = { - port: '8000', - path: '/coauthoring/CommandService.ashx', + try { + const response = await fetch('http://localhost:8000/coauthoring/CommandService.ashx', { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Content-Length': Buffer.byteLength(body) - } - }; - const request = http.request(options, response => { - response.setEncoding('utf8'); - - let data = ''; - response.on('data', chunk => { - data += chunk; - }); - response.on('end', () => { - resolve(data); - clearTimeout(timer); - }); + 'Content-Length': Buffer.byteLength(body).toString() + }, + body, + signal: controller.signal }); - request.on('error', error => { - reject(error); - clearTimeout(timer); - }); - - request.write(body); - request.end(); - }); + clearTimeout(timeoutId); + return await response.text(); + } catch (error) { + clearTimeout(timeoutId); + if (error.name === 'AbortError') { + throw new Error('Request timeout'); + } + throw error; + } } function getKeysDirectories(keys) { diff --git a/tests/integration/withServerInstance/storage.tests.js b/tests/integration/withServerInstance/storage.tests.js index de28e6ff..112799f0 100644 --- a/tests/integration/withServerInstance/storage.tests.js +++ b/tests/integration/withServerInstance/storage.tests.js @@ -242,7 +242,7 @@ function runTestForDir(ctx, isMultitenantMode, specialDir) { expect(outputText.toString('utf8')).toEqual(testFileData3); }); test('getSignedUrl', async () => { - let url, urls, data; + let url, data; url = await storage.getSignedUrl(ctx, baseUrl, testFile1, urlType, undefined, undefined, specialDir); data = await request(url); expect(data).toEqual(testFileData1); @@ -260,27 +260,24 @@ function runTestForDir(ctx, isMultitenantMode, specialDir) { expect(data).toEqual(testFileData4); }); test('getSignedUrls', async () => { - let urls, data; - urls = await storage.getSignedUrls(ctx, baseUrl, testDir, urlType, undefined, specialDir); - data = []; + const urls = await storage.getSignedUrls(ctx, baseUrl, testDir, urlType, undefined, specialDir); + const data = []; for (const i in urls) { data.push(await request(urls[i])); } expect(data.sort()).toEqual([testFileData1, testFileData2, testFileData3, testFileData4].sort()); }); test('getSignedUrlsArrayByArray', async () => { - let urls, data; - urls = await storage.getSignedUrlsArrayByArray(ctx, baseUrl, [testFile1, testFile2], urlType, specialDir); - data = []; + const urls = await storage.getSignedUrlsArrayByArray(ctx, baseUrl, [testFile1, testFile2], urlType, specialDir); + const data = []; for (let i = 0; i < urls.length; ++i) { data.push(await request(urls[i])); } expect(data.sort()).toEqual([testFileData1, testFileData2].sort()); }); test('getSignedUrlsByArray', async () => { - let urls, data; - urls = await storage.getSignedUrlsByArray(ctx, baseUrl, [testFile3, testFile4], undefined, urlType, specialDir); - data = []; + const urls = await storage.getSignedUrlsByArray(ctx, baseUrl, [testFile3, testFile4], undefined, urlType, specialDir); + const data = []; for (const i in urls) { data.push(await request(urls[i])); } @@ -361,32 +358,30 @@ describe('storage mix common and forgotten dir', () => { tenantManager.setMultitenantMode(false); let buffer = Buffer.from(testFileData1); - let res = await storage.putObject(ctx, testFile1, buffer, buffer.length, specialDirCache); + const res = await storage.putObject(ctx, testFile1, buffer, buffer.length, specialDirCache); expect(res).toEqual(undefined); let list = await storage.listObjects(ctx, testDir, specialDirCache); expect(list.sort()).toEqual([testFile1].sort()); buffer = Buffer.from(testFileData2); - res = await storage.putObject(ctx, testFile2, buffer, buffer.length, specialDirForgotten); - expect(res).toEqual(undefined); + const res2 = await storage.putObject(ctx, testFile2, buffer, buffer.length, specialDirForgotten); + expect(res2).toEqual(undefined); list = await storage.listObjects(ctx, testDir, specialDirForgotten); expect(list.sort()).toEqual([testFile2].sort()); }); test('copyPath', async () => { - let list, res; - res = await storage.copyPath(ctx, testDir, testDir, specialDirCache, specialDirForgotten); + const res = await storage.copyPath(ctx, testDir, testDir, specialDirCache, specialDirForgotten); expect(res).toEqual(undefined); - list = await storage.listObjects(ctx, testDir, specialDirForgotten); + const list = await storage.listObjects(ctx, testDir, specialDirForgotten); expect(list.sort()).toEqual([testFile1, testFile2].sort()); }); test('copyObject', async () => { - let list, res; - res = await storage.copyObject(ctx, testFile2, testFile2, specialDirForgotten, specialDirCache); + const res = await storage.copyObject(ctx, testFile2, testFile2, specialDirForgotten, specialDirCache); expect(res).toEqual(undefined); - list = await storage.listObjects(ctx, testDir, specialDirCache); + const list = await storage.listObjects(ctx, testDir, specialDirCache); expect(list.sort()).toEqual([testFile1, testFile2].sort()); }); diff --git a/tests/unit/request.tests.js b/tests/unit/request.tests.js index 0b6a5ae1..46e99cd5 100644 --- a/tests/unit/request.tests.js +++ b/tests/unit/request.tests.js @@ -1,20 +1,16 @@ const {describe, test, expect, beforeAll, afterAll} = require('@jest/globals'); -const {Writable, Readable} = require('stream'); +const {Readable} = require('stream'); const {buffer} = require('node:stream/consumers'); const http = require('http'); -const https = require('https'); const express = require('express'); const operationContext = require('../../Common/sources/operationContext'); const utils = require('../../Common/sources/utils'); -const fs = require('fs').promises; -const path = require('path'); // Create operation context for tests const ctx = new operationContext.Context(); // Test server setup let server; -let testServer; let proxyServer; const PORT = 3456; const BASE_URL = `http://localhost:${PORT}`; @@ -98,9 +94,8 @@ describe('HTTP Request Unit Tests', () => { }); // Endpoint that simulates timeout - app.get('/api/timeout', (req, res) => { + app.get('/api/timeout', (_req, _res) => { // Never send response to trigger timeout - }); app.use('/api/status/:code', (req, res) => { @@ -116,8 +111,9 @@ describe('HTTP Request Unit Tests', () => { app.get('/api/error', (req, res) => { res.status(500).json({error: 'Internal Server Error'}); }); + // Endpoint that simulates a slow response headers - app.get('/api/slow-headers', (req, res) => { + app.get('/api/slow-headers', (_req, res) => { // Delay sending headers setTimeout(() => { res.json({success: true}); @@ -136,7 +132,7 @@ describe('HTTP Request Unit Tests', () => { }); // Endpoint that simulates slow/chunked response with inactivity periods - app.get('/api/slow-body', (req, res) => { + app.get('/api/slow-body', (_req, res) => { // Send headers immediately res.setHeader('Content-Type', 'application/json'); res.write('{"part1": "First part of the response",'); @@ -161,12 +157,11 @@ describe('HTTP Request Unit Tests', () => { }); // POST endpoint that times out - app.post('/api/timeout', express.json(), (req, res) => { + app.post('/api/timeout', express.json(), (_req, _res) => { // Never send response to trigger timeout - }); - app.get('/api/binary', (req, res) => { + app.get('/api/binary', (_req, res) => { // PNG file signature as binary data const binaryData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); res.setHeader('content-type', 'image/png'); @@ -175,13 +170,13 @@ describe('HTTP Request Unit Tests', () => { }); // Large file endpoint - app.get('/api/large', (req, res) => { + app.get('/api/large', (_req, res) => { res.setHeader('content-type', 'application/octet-stream'); res.send(Buffer.alloc(2 * 1024 * 1024)); //2MB }); // Large file endpoint with truly no content-length header - app.get('/api/large-chunked', (req, res) => { + app.get('/api/large-chunked', (_req, res) => { res.setHeader('content-type', 'application/octet-stream'); res.setHeader('transfer-encoding', 'chunked'); res.write(Buffer.alloc(2 * 1024 * 1024)); @@ -217,7 +212,7 @@ describe('HTTP Request Unit Tests', () => { // Setup proxy server const proxyApp = express(); - proxyApp.use((req, res, next) => { + proxyApp.use((req, res) => { // Record request details const requestInfo = { method: req.method, @@ -497,7 +492,7 @@ describe('HTTP Request Unit Tests', () => { }); try { - const result = await utils.downloadUrlPromise( + await utils.downloadUrlPromise( mockCtx, `${BASE_URL}/api/redirect`, {wholeCycle: '5s', connectionAndInactivity: '3s'}, @@ -528,7 +523,7 @@ describe('HTTP Request Unit Tests', () => { }); try { - const result = await utils.downloadUrlPromise( + await utils.downloadUrlPromise( mockCtx, `${BASE_URL}/api/redirect`, {wholeCycle: '5s', connectionAndInactivity: '3s'}, @@ -607,7 +602,7 @@ describe('HTTP Request Unit Tests', () => { null, true ); - const receivedData = await buffer(stream); + await buffer(stream); throw new Error('Expected an error to be thrown'); } catch (error) { expect(error.code).toBe('EMSGSIZE'); @@ -623,7 +618,7 @@ describe('HTTP Request Unit Tests', () => { null, true ); - const receivedData = await buffer(stream); + await buffer(stream); throw new Error('Expected an error to be thrown'); } catch (error) { expect(error.code).toBe('EMSGSIZE'); @@ -1269,7 +1264,7 @@ describe('HTTP Request Unit Tests', () => { try { const postData = JSON.stringify({test: 'data'}); - const result = await utils.postRequestPromise( + await utils.postRequestPromise( ctx, `${BASE_URL}/api/status/205`, postData, diff --git a/tests/unit/requestSSRF.tests.js b/tests/unit/requestSSRF.tests.js index f3ab105a..65589d12 100644 --- a/tests/unit/requestSSRF.tests.js +++ b/tests/unit/requestSSRF.tests.js @@ -18,7 +18,7 @@ process.env['NODE_CONFIG'] = JSON.stringify({ }); // Required modules -const {describe, test, expect, beforeAll, afterAll, it, jest} = require('@jest/globals'); +const {describe, expect, beforeAll, afterAll, it} = require('@jest/globals'); const http = require('http'); const operationContext = require('../../Common/sources/operationContext');