Browse Source

refactor(gatecrashers): use fs/promises for file access in tests, remove unused imports

DEV-4017-prototypes-exercise-1-animals
Michele Sessa 2 years ago committed by Michele
parent
commit
52c167a390
  1. 47
      js/tests/gatecrashers_test.mjs

47
js/tests/gatecrashers_test.mjs

@ -1,7 +1,6 @@
import { once } from 'node:events' import { once } from 'node:events'
import { spawn } from 'node:child_process' import { spawn } from 'node:child_process'
import { mkdir, writeFile, chmod } from 'fs/promises' import { mkdir, writeFile, access } from 'fs/promises'
import fs from 'fs'
import { join } from 'path' import { join } from 'path'
export const tests = [] export const tests = []
@ -23,7 +22,7 @@ export const setup = async ({}) => {
const sendRequest = async (path, options) => { const sendRequest = async (path, options) => {
const response = await fetch(`http://localhost:${port}${path}`, options) const response = await fetch(`http://localhost:${port}${path}`, options)
const { status, statusText, ok } = response const { status } = response
const headers = Object.fromEntries(response.headers) const headers = Object.fromEntries(response.headers)
let body = '' let body = ''
try { try {
@ -77,11 +76,9 @@ const testGoodRequests = async ({ path, eq, ctx }) => {
}, },
}) })
fs.access(`${dirPath}/Ana_Riber.json`, fs.F_OK, err => { access(`${dirPath}/Ana_Riber.json`).catch(err => {
if (err) { console.error(err)
console.error(err) isTestOk = false
isTestOk = false
}
}) })
if ( if (
status != 200 || status != 200 ||
@ -102,11 +99,9 @@ const testGoodRequests = async ({ path, eq, ctx }) => {
}, },
}) })
fs.access(`${dirPath}/Rob_Frie.json`, fs.F_OK, err => { access(`${dirPath}/Rob_Frie.json`).catch(err => {
if (err) { console.error(err)
console.error(err) isTestOk = false
isTestOk = false
}
}) })
if ( if (
status != 200 || status != 200 ||
@ -126,11 +121,9 @@ const testGoodRequests = async ({ path, eq, ctx }) => {
}, },
}) })
fs.access(`${dirPath}/George_Harl.json`, fs.F_OK, err => { access(`${dirPath}/George_Harl.json`).catch(err => {
if (err) { console.error(err)
console.error(err) isTestOk = false
isTestOk = false
}
}) })
if ( if (
status != 200 || status != 200 ||
@ -164,23 +157,17 @@ const testUnauthorizedRequests = async ({ path, ctx }) => {
} }
} }
{ {
const { status } = await ctx.sendRequest( const { status } = await ctx.sendRequest(`/Rahima_Young:wrongpass`, {
`/Rahima_Young:wrongpass`, method: 'POST',
{ })
method: 'POST',
},
)
if (status != 401) { if (status != 401) {
isTestOk = false isTestOk = false
} }
} }
{ {
const { status } = await ctx.sendRequest( const { status } = await ctx.sendRequest(`/Anonymus:abracadabra`, {
`/Anonymus:abracadabra`, method: 'POST',
{ })
method: 'POST',
},
)
if (status != 401) { if (status != 401) {
isTestOk = false isTestOk = false
} }

Loading…
Cancel
Save