Skip to content

Commit

Permalink
🧪 chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
rezk2ll committed Jan 21, 2025
1 parent 54c11ab commit a29d071
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 54 deletions.
48 changes: 24 additions & 24 deletions packages/matrix-identity-server/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let app: express.Application
let validToken: string
let conf: Config

beforeAll((done) => {
beforeAll(async () => {
conf = {
...defaultConfig,
database_engine: 'sqlite',
Expand All @@ -44,17 +44,16 @@ beforeAll((done) => {
conf.database_password = process.env.PG_PASSWORD ?? 'twake'
conf.database_name = process.env.PG_DATABASE ?? 'test'
}
buildUserDB(conf)
.then(() => {
done()
})
.catch((e) => {
done(e)
})

await buildUserDB(conf)
})

afterAll(() => {
fs.unlinkSync('src/__testData__/test.db')
try {
fs.unlinkSync('src/__testData__/test.db')
} catch (error) {
console.log('failed to unlink test db', { error })
}
})

beforeEach(() => {
Expand Down Expand Up @@ -83,29 +82,24 @@ describe('Error on server start', () => {
})

describe('Use configuration file', () => {
beforeAll((done) => {
beforeAll(async () => {
idServer = new IdServer()
app = express()
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

idServer.ready
.then(() => {
Object.keys(idServer.api.get).forEach((k) => {
app.get(k, idServer.api.get[k])
})
Object.keys(idServer.api.post).forEach((k) => {
app.post(k, idServer.api.post[k])
})
done()
})
.catch((e) => {
done(e)
})
await idServer.ready

Object.keys(idServer.api.get).forEach((k) => {
app.get(k, idServer.api.get[k])
})
Object.keys(idServer.api.post).forEach((k) => {
app.post(k, idServer.api.post[k])
})
})

afterAll(() => {
idServer.cleanJobs()
idServer?.cleanJobs()
})

test('Reject unimplemented endpoint with 404', async () => {
Expand Down Expand Up @@ -1502,6 +1496,9 @@ describe('Use environment variables', () => {
process.env.HASHES_RATE_LIMIT = '4'
idServer = new IdServer()
app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

idServer.ready
// eslint-disable-next-line @typescript-eslint/promise-function-async
.then(() => {
Expand Down Expand Up @@ -1629,6 +1626,9 @@ describe('_matrix/identity/v2/terms', () => {
}
idServer2 = new IdServer(conf2)
app2 = express()
app2.use(express.json())
app2.use(express.urlencoded({ extended: true }))

idServer2.ready
.then(() => {
Object.keys(idServer2.api.get).forEach((k) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/matrix-identity-server/src/terms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ beforeAll((done) => {
.then(() => {
idServer = new IdServer()
app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

idServer.ready
.then(() => {
Expand Down
41 changes: 11 additions & 30 deletions packages/utils/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,7 @@ describe('Utility Functions', () => {
it('should parse JSON content and call the callback', (done) => {
const req = {
headers: { 'content-type': 'application/json' },
on: (event: string, callback: any) => {
if (event === 'data') {
callback(JSON.stringify({ key: 'value' }))
}
if (event === 'end') {
callback()
}
}
body: { key: 'value' }
} as unknown as Request

jsonContent(
Expand All @@ -76,14 +69,7 @@ describe('Utility Functions', () => {
it('should handle form-urlencoded content', (done) => {
const req = {
headers: { 'content-type': 'application/x-www-form-urlencoded' },
on: (event: string, callback: any) => {
if (event === 'data') {
callback(querystring.stringify({ key: 'value' }))
}
if (event === 'end') {
callback()
}
}
body: { key: 'value' }
} as unknown as Request

jsonContent(
Expand All @@ -97,7 +83,7 @@ describe('Utility Functions', () => {
)
})

it('should handle JSON parsing errors', (done) => {
it('should handle JSON parsing errors', () => {
const req = {
headers: { 'content-type': 'application/json' },
on: (event: string, callback: any) => {
Expand All @@ -110,20 +96,15 @@ describe('Utility Functions', () => {
}
} as unknown as Request

jsonContent(req, mockResponse as Response, mockLogger, () => {
// No-op
})
jsonContent(req, mockResponse as Response, mockLogger, () => {})

setImmediate(() => {
expect(mockLogger.error).toHaveBeenCalled()
expect(mockResponse.writeHead).toHaveBeenCalledWith(
400,
expect.any(Object)
)
expect(mockResponse.write).toHaveBeenCalled()
expect(mockResponse.end).toHaveBeenCalled()
done()
})
expect(mockLogger.error).toHaveBeenCalled()
expect(mockResponse.writeHead).toHaveBeenCalledWith(
400,
expect.any(Object)
)
expect(mockResponse.write).toHaveBeenCalled()
expect(mockResponse.end).toHaveBeenCalled()
})
})

Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export const jsonContent = (
try {
const obj = (req as Request).body

console.log({ obj })

if (!obj) {
throw new Error('No body')
}
Expand Down

0 comments on commit a29d071

Please sign in to comment.