-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use correct definition for swagger doc
Typo correction
- Loading branch information
Showing
30 changed files
with
5,785 additions
and
328 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,14 @@ | ||
APP_NAME=NoPestsAllowed | ||
DB_CONNECTION_STRING=mongodb+srv://.../NoPestsAllowed | ||
ACCESS_TOKEN_SECRET=maSuperPhrasePourHasher | ||
|
||
MAIL_MAILER=smtp | ||
MAIL_HOST=smtp.mailtrap.io | ||
MAIL_PORT=2525 | ||
MAIL_USERNAME= | ||
MAIL_PASSWORD= | ||
|
||
MAIL_FROM_ADDRESS=[email protected] | ||
MAIL_FROM_NAME="${APP_NAME}" | ||
|
||
CLOUDINARY_URL=cloudinary://... |
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 @@ | ||
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs | ||
|
||
name: JEST tests | ||
|
||
on: | ||
push: | ||
branches: ["dev", "test"] | ||
pull_request: | ||
branches: ["main"] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [20.x, 21.x, 22.x] | ||
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/ | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
cache: "yarn" | ||
- run: yarn install | ||
- 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 |
---|---|---|
|
@@ -59,3 +59,8 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
|
||
|
||
# misc files excluded from git | ||
modules/findOrCreatePlace.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
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,58 @@ | ||
const request = require("supertest"); | ||
const app = require("../app"); | ||
const User = require("../models/users"); | ||
// const mongoose = require("mongoose"); | ||
|
||
beforeEach(async () => { | ||
await User.deleteMany({}); | ||
global.console = { log: jest.fn() }; | ||
}); | ||
|
||
test("it can register new user", async () => { | ||
const response = await request(app).post("/register").send({ | ||
firstname: "John", | ||
lastname: "Doe", | ||
email: "[email protected]", | ||
password: "secret", | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
}); | ||
|
||
test("it can login user", async () => { | ||
await User({ | ||
firstname: "John", | ||
lastname: "Doe", | ||
email: "[email protected]", | ||
password: "$2a$10$Qsu.gk.u76oq9Gx4uFq5F.b7JSsrx1/Vh8vROou/9ilZAnNoKniGW", // secret hashed | ||
}).save(); | ||
const response = await request(app).post("/login").send({ | ||
email: "[email protected]", | ||
password: "secret", | ||
}); | ||
expect(response.statusCode).toBe(200); | ||
}); | ||
|
||
test("an unauthenticated user can not logout", async () => { | ||
const response = await request(app).post("/logout"); | ||
expect(response.statusCode).toBe(401); | ||
}); | ||
|
||
test("it can return all depositions", async () => { | ||
const response = await request(app).get("/depositions"); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body.result).toBe(true); | ||
response.body.depositions.map((deposition) => { | ||
expect(deposition).toHaveProperty("_id"); | ||
expect(deposition).toHaveProperty("name"); | ||
expect(deposition).toHaveProperty("description"); | ||
expect(deposition).toHaveProperty("status"); | ||
expect(deposition).toHaveProperty("type"); | ||
expect(deposition).toHaveProperty("visualProofs"); | ||
expect(deposition).toHaveProperty("placeOwnerEmail"); | ||
expect(deposition).toHaveProperty("placeId"); | ||
expect(deposition.placeId).toHaveProperty("_id"); | ||
expect(deposition.placeId).toHaveProperty("address"); | ||
expect(deposition.placeId).toHaveProperty("geojson"); | ||
expect(deposition.placeId).toHaveProperty("type"); | ||
}); | ||
}); |
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,15 @@ | ||
const mongoose = require("mongoose"); | ||
const { MongoMemoryServer } = require("mongodb-memory-server"); | ||
|
||
module.exports = async function globalSetup() { | ||
const instance = await MongoMemoryServer.create(); | ||
const uri = instance.getUri(); | ||
global.__MONGOINSTANCE = instance; | ||
process.env.DB_CONNECTION_STRING = uri.slice(0, uri.lastIndexOf("/")); | ||
|
||
// The following is to make sure the database is clean before an test starts | ||
const conn = await mongoose.connect(`${instance.getUri()}`, { dbName: "npa_test_db" }); | ||
await conn.connection.db.dropDatabase(); | ||
await mongoose.disconnect(); | ||
process.env.ACCESS_TOKEN_SECRET = "maSuperPhrasePourHasher"; | ||
}; |
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,7 @@ | ||
const mongoose = require("mongoose"); | ||
const { MongoMemoryServer } = require("mongodb-memory-server"); | ||
|
||
module.exports = async function globalTeardown() { | ||
const instance = global.__MONGOINSTANCE; | ||
await instance.stop(); | ||
}; |
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,12 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
beforeAll(async () => { | ||
// put your client connection code here, example with mongoose: | ||
await mongoose.disconnect(); | ||
await mongoose.connect(process.env["DB_CONNECTION_STRING"]); | ||
}); | ||
|
||
afterAll(async () => { | ||
// put your client disconnection code here, example with mongodb: | ||
await mongoose.disconnect(); | ||
}); |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,12 @@ | ||
const fs = require("node:fs"); | ||
/** @type {import('jest').Config} */ | ||
const config = { | ||
verbose: true, | ||
testEnvironment: "node", | ||
modulePathIgnorePatterns: ["__tests__/utils/*"], | ||
globalSetup: "./__tests__/utils/globalSetup.js", | ||
globalTeardown: "./__tests__/utils/globalTeardown.js", | ||
setupFilesAfterEnv: ["./__tests__/utils/setup.js"], | ||
}; | ||
|
||
module.exports = config; |
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
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,14 +1,35 @@ | ||
const jwt = require("jsonwebtoken"); | ||
|
||
const generateAccessAndRefreshToken = (email) => { | ||
const accessToken = jwt.sign({ email: email }, process.env.ACCESS_TOKEN_SECRET, { | ||
expiresIn: "10s", | ||
}); | ||
const refreshToken = jwt.sign({ email: email }, process.env.ACCESS_TOKEN_SECRET, { | ||
expiresIn: "1d", | ||
const generateAccessAndRefreshToken = (payload) => { | ||
const accessToken = generateAccessToken({ ...payload }); | ||
const refreshToken = jwt.sign({ ...payload }, process.env.ACCESS_TOKEN_SECRET, { | ||
expiresIn: "10d", | ||
}); | ||
|
||
return [accessToken, refreshToken]; | ||
}; | ||
|
||
module.exports = { generateAccessAndRefreshToken }; | ||
const generateAccessToken = (payload) => { | ||
return jwt.sign({ ...payload }, process.env.ACCESS_TOKEN_SECRET, { | ||
// expiresIn: "10d", | ||
}); | ||
}; | ||
|
||
const clearTokens = async (req, res) => { | ||
const { signedCookies = {} } = req; | ||
const { refreshToken } = signedCookies; | ||
if (refreshToken) { | ||
const tokenFromDB = await refreshToken.findOne({ refreshToken }); | ||
if (tokenFromDB) { | ||
console.log("revocking token from db"); | ||
tokenFromDB.update({ revokedAt: Date.now() }); | ||
} | ||
} | ||
res.clearCookie("nopestsallowed_jwt", { | ||
httpOnly: true, | ||
secure: false, | ||
signed: true, | ||
}); | ||
}; | ||
|
||
module.exports = { generateAccessAndRefreshToken, generateAccessToken, clearTokens }; |
Oops, something went wrong.