Convert test suite to TypeScript with async/await#1363
Conversation
Co-authored-by: alexanmtz <88840+alexanmtz@users.noreply.github.com>
| } | ||
|
|
||
| const login = (agent: SuperAgentTest, params: LoginParams = {}) => { | ||
| params.username = params.email |
Check failure
Code scanning / CodeQL
Insecure randomness High test
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 3 months ago
In general, to fix insecure randomness in a security-sensitive context, replace uses of Math.random() with a cryptographically secure source such as Node’s crypto.randomBytes (or crypto.randomUUID) and derive the needed string/number from that. This ensures the generated values are not trivially predictable.
Here, the only insecure use is generateTestEmail, which uses Math.random() to generate the local-part suffix of a test email. The best minimal fix is to keep the overall behavior (unique-ish test emails) but change the implementation to use crypto.randomBytes to produce a random hex suffix instead of Math.random(). Concretely:
- Add an import of Node’s built-in
cryptomodule at the top oftest/helpers/index.ts. - Change
generateTestEmailfrom using`teste+${Math.random()}@gmail.com`to using a random string fromcrypto.randomBytes, e.g.`teste+${crypto.randomBytes(8).toString('hex')}@gmail.com`.
No other logic (register, login, etc.) needs to change.
| @@ -1,11 +1,12 @@ | ||
| import Models from '../../src/models' | ||
| import { SuperAgentTest } from 'supertest' | ||
| import crypto from 'crypto' | ||
|
|
||
| const models = Models as any | ||
| const testPassword = 'test12345678' | ||
| const testName = 'Test' | ||
|
|
||
| const generateTestEmail = (): string => `teste+${Math.random()}@gmail.com` | ||
| const generateTestEmail = (): string => `teste+${crypto.randomBytes(8).toString('hex')}@gmail.com` | ||
|
|
||
| interface RegisterParams { | ||
| email?: string |
Co-authored-by: alexanmtz <88840+alexanmtz@users.noreply.github.com>
Co-authored-by: alexanmtz <88840+alexanmtz@users.noreply.github.com>
Co-authored-by: alexanmtz <88840+alexanmtz@users.noreply.github.com>
Co-authored-by: alexanmtz <88840+alexanmtz@users.noreply.github.com>
Co-authored-by: alexanmtz <88840+alexanmtz@users.noreply.github.com>
Co-authored-by: alexanmtz <88840+alexanmtz@users.noreply.github.com>
alexanmtz
left a comment
There was a problem hiding this comment.
- Users
login User social networks
should user wrong authentication:
Error: expected "Content-Type" header field
at Context. (test/user.test.ts:224:10)
at process.processImmediate (node:internal/timers:476:21)
at Test._assertHeader (node_modules/supertest/lib/test.js:239:47)
at /home/circleci/repo/node_modules/supertest/lib/test.js:323:13
at Test._assertFunction (node_modules/supertest/lib/test.js:300:13)
at Test.assert (node_modules/supertest/lib/test.js:179:23)
at Server.localAssert (node_modules/supertest/lib/test.js:135:14)
at Object.onceWrapper (node:events:631:28)
at Server.emit (node:events:517:28)
at emitCloseNT (node:net:2221:8)
at process.processTicksAndRejections (node:internal/process/task_queues:81:21)
Co-authored-by: alexanmtz <88840+alexanmtz@users.noreply.github.com>
Fixed in commit eb9a255. The |
Modernized the test suite by converting all JavaScript test files to TypeScript and refactoring promise chains to async/await.
Description
Converted 21 test files and helpers from JavaScript to TypeScript. Replaced
.then()/.catch()chains and callback-style tests with async/await patterns.Key changes:
test/*.test.js→.test.tswith ES6 importstest/helpers/index.js→.tswith proper typing(done) => {...}→async () => {...}Infrastructure:
NODE_OPTIONS="--import tsx"for TypeScript module loadingtest/tsconfig.jsonfor test-specific compiler options--require ./test/bootstrap.cjsapproachExample conversion:
Bug Fixes:
test/user.test.ts). The/authenticatedendpoint returnsres.status(401).end()without a Content-Type header when authentication fails.All tests maintain identical behavior. Type checking passes (
npm run typecheck).Thank you for your contribution!
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.