-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add test * fix: update path * Rename test.yaml to test.yml * Update test.yaml * Create test.yaml * fix: update test * feat: update env * Update test.yml * Update test.yml
- Loading branch information
1 parent
f07beb6
commit ea1b3a8
Showing
9 changed files
with
4,773 additions
and
766 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,36 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
env: | ||
MONGO_CONNECTION_STRING: mongodb://root:example@localhost:27017/invitation?authSource=admin | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-version: ['20.x', '22.x'] | ||
services: | ||
# Label used to access the service container | ||
mongo: | ||
image: mongo:7 | ||
ports: | ||
- 27017:27017 | ||
env: | ||
MONGO_INITDB_ROOT_USERNAME: root | ||
MONGO_INITDB_ROOT_PASSWORD: example | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Use Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: corepack enable && yarn --immutable | ||
- run: yarn build | ||
- run: yarn test |
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,6 @@ | ||
module.exports = { | ||
presets: [ | ||
['@babel/preset-env', {targets: {node: 'current'}}], | ||
'@babel/preset-typescript', | ||
], | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import express, { Express, Request, Response } from 'express'; | ||
import { rateLimit } from 'express-rate-limit'; | ||
import bodyParser from 'body-parser'; | ||
import invitation from './invitation'; | ||
|
||
const app: Express = express(); | ||
|
||
const limiter = rateLimit({ | ||
windowMs: 15 * 60 * 1000, // 15 minutes | ||
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes). | ||
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header | ||
legacyHeaders: false, // Disable the `X-RateLimit-*` headers. | ||
// store: ... , // Redis, Memcached, etc. See below. | ||
}); | ||
|
||
app.use(limiter); | ||
app.use(bodyParser.json()); | ||
|
||
app.get('/', (req: Request, res: Response) => { | ||
res.send('Express + TypeScript Server'); | ||
}); | ||
|
||
app.use('/invitation', invitation); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import request from "supertest"; | ||
import app from "../src/app"; | ||
|
||
describe("Test the root path", () => { | ||
test("It should response the GET method", async () => { | ||
const response = await request(app).get("/"); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.text).toBe("Express + TypeScript Server"); | ||
}); | ||
}); |
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,66 @@ | ||
import request from "supertest"; | ||
import mongoose from "mongoose"; | ||
import app from "../src/app"; | ||
import Invitation from "../src/models/invitation"; | ||
|
||
describe("Test the invitation path", () => { | ||
beforeAll(async () => { | ||
await mongoose.connect(process.env.MONGO_CONNECTION_STRING || ''); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await Invitation.deleteMany(); | ||
}) | ||
|
||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
}); | ||
|
||
test("It should response the GET method", async () => { | ||
let dateNow = Date.now(); | ||
let objectItem = { | ||
invitee: 'every where', | ||
inviter: 'berviantoleo', | ||
message: 'Hello!', | ||
createdAt: dateNow | ||
}; | ||
await Invitation.create(objectItem); | ||
const response = await request(app).get("/invitation/berviantoleo"); | ||
expect(response.statusCode).toBe(200); | ||
expect(Array.isArray(response.body)).toBe(true); | ||
expect(response.body.length).toBe(1) | ||
let responseItem = response.body[0]; | ||
expect(responseItem.invitee).toBe(objectItem.invitee); | ||
expect(responseItem.inviter).toBe(objectItem.inviter); | ||
expect(responseItem.message).toBe(objectItem.message); | ||
expect(responseItem.createdAt).toBe(new Date(objectItem.createdAt).toISOString()); | ||
}); | ||
|
||
test("It should response the POST method", async () => { | ||
let objectItem = { | ||
invitee: 'every where', | ||
inviter: 'berviantoleo', | ||
message: 'Hello!' | ||
}; | ||
const response = await request(app).post("/invitation").send(objectItem); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBeTruthy(); | ||
let responseItem = response.body; | ||
expect(responseItem.invitee).toBe(objectItem.invitee); | ||
expect(responseItem.inviter).toBe(objectItem.inviter); | ||
expect(responseItem.message).toBe(objectItem.message); | ||
expect(responseItem.createdAt).toBeTruthy(); | ||
expect(responseItem._id).toBeTruthy(); | ||
expect(responseItem.__v).toBe(0); | ||
|
||
const dataDb = await Invitation.findById(responseItem._id); | ||
|
||
expect(dataDb).toBeTruthy(); | ||
expect(dataDb?.invitee).toBe(objectItem.invitee); | ||
expect(dataDb?.inviter).toBe(objectItem.inviter); | ||
expect(dataDb?.message).toBe(objectItem.message); | ||
expect(dataDb?.createdAt?.toISOString()).toBe(responseItem.createdAt); | ||
expect(dataDb?._id?.toString()).toBe(responseItem._id); | ||
expect(dataDb?.__v).toBe(0); | ||
}); | ||
}); |
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.