-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e82c7ac
commit a532cdd
Showing
33 changed files
with
519 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'dotenv/config'; | ||
|
||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
|
||
// Import routes | ||
import EventRoutes from './routes/event.route.js'; | ||
|
||
import { readConfiguration } from './utils/config.utils.js'; | ||
import { errorMiddleware } from './middleware/error.middleware.js'; | ||
import CustomError from './errors/custom.error.js'; | ||
|
||
// Read env variables | ||
readConfiguration(); | ||
|
||
// Create the express app | ||
const app = express(); | ||
app.disable('x-powered-by'); | ||
|
||
// Define configurations | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
// Define routes | ||
app.use('/event', EventRoutes); | ||
app.use('*', () => { | ||
throw new CustomError(404, 'Path not found.'); | ||
}); | ||
|
||
// Global error handler | ||
app.use(errorMiddleware); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 47 additions & 5 deletions
52
application-templates/javascript/event/tests/integration/routes.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,51 @@ | ||
import { expect } from '@jest/globals'; | ||
import request from 'supertest'; | ||
import app from '../../src/app'; | ||
import * as eventController from '../../src/controllers/event.controller'; | ||
|
||
//@todo: test this with msw (see typescipt job) | ||
// make msw a shared package | ||
describe('Testing Event Controller', () => { | ||
test('POST `/event` route', async () => { | ||
expect(1).toBe(1); | ||
describe('Testing router', () => { | ||
test('Post to non existing route', async () => { | ||
const response = await request(app).post('/none'); | ||
expect(response.status).toBe(404); | ||
expect(response.body).toEqual({ | ||
message: 'Path not found.', | ||
}); | ||
}); | ||
test('Post invalid body', async () => { | ||
const response = await request(app).post('/event').send({ | ||
message: 'hello world', | ||
}); | ||
expect(response.status).toBe(400); | ||
expect(response.body).toEqual({ | ||
message: 'Bad request: No customer id in the Pub/Sub message', | ||
}); | ||
}); | ||
test('Post empty body', async () => { | ||
const response = await request(app).post('/event'); | ||
expect(response.status).toBe(400); | ||
expect(response.body).toEqual({ | ||
message: 'Bad request: Wrong No Pub/Sub message format', | ||
}); | ||
}); | ||
}); | ||
describe('unexpected error', () => { | ||
let postMock; | ||
|
||
beforeEach(() => { | ||
// Mock the post method to throw an error | ||
postMock = jest.spyOn(eventController, 'post').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore the original implementation | ||
postMock.mockRestore(); | ||
}); | ||
test('should handle errors thrown by post method', async () => { | ||
// Call the route handler | ||
const response = await request(app).post('/event'); | ||
expect(response.status).toBe(500); | ||
expect(response.body).toEqual({ message: 'Internal server error' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
import express from 'express'; | ||
|
||
// Import routes | ||
import JobRoutes from './routes/job.route.js'; | ||
|
||
import { readConfiguration } from './utils/config.utils.js'; | ||
import { errorMiddleware } from './middleware/error.middleware.js'; | ||
import CustomError from './errors/custom.error.js'; | ||
|
||
// Read env variables | ||
readConfiguration(); | ||
|
||
// Create the express app | ||
const app = express(); | ||
app.disable('x-powered-by'); | ||
|
||
// Define routes | ||
app.use('/job', JobRoutes); | ||
app.use('*', () => { | ||
throw new CustomError(404, 'Path not found.'); | ||
}); | ||
|
||
// Global error handler | ||
app.use(errorMiddleware); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 31 additions & 3 deletions
34
application-templates/javascript/job/tests/integration/orders.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,35 @@ | ||
import { expect } from '@jest/globals'; | ||
import request from 'supertest'; | ||
import app from '../../src/app'; | ||
import * as jobController from '../../src/controllers/job.controller'; | ||
|
||
describe('Testing Job Template', () => { | ||
test('GET all orders', async () => { | ||
expect(1).toBe(1); | ||
describe('Testing router', () => { | ||
test('Post to non existing route', async () => { | ||
const response = await request(app).post('/none'); | ||
expect(response.status).toBe(404); | ||
expect(response.body).toEqual({ | ||
message: 'Path not found.', | ||
}); | ||
}); | ||
}); | ||
describe('unexpected error', () => { | ||
let postMock; | ||
|
||
beforeEach(() => { | ||
// Mock the post method to throw an error | ||
postMock = jest.spyOn(jobController, 'post').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore the original implementation | ||
postMock.mockRestore(); | ||
}); | ||
test('should handle errors thrown by post method', async () => { | ||
// Call the route handler | ||
const response = await request(app).post('/job'); | ||
expect(response.status).toBe(500); | ||
expect(response.body).toEqual({ message: 'Internal server error' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'dotenv/config'; | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
|
||
// Import routes | ||
import ServiceRoutes from './routes/service.route.js'; | ||
|
||
import { readConfiguration } from './utils/config.utils.js'; | ||
import { errorMiddleware } from './middleware/error.middleware.js'; | ||
import CustomError from './errors/custom.error.js'; | ||
|
||
// Read env variables | ||
readConfiguration(); | ||
|
||
// Create the express app | ||
const app = express(); | ||
app.disable('x-powered-by'); | ||
|
||
// Define configurations | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
// Define routes | ||
app.use('/service', ServiceRoutes); | ||
app.use('*', () => { | ||
throw new CustomError(404, 'Path not found.'); | ||
}); | ||
|
||
// Global error handler | ||
app.use(errorMiddleware); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.