This repository has been archived by the owner on May 10, 2024. It is now read-only.
generated from Arquisoft/wiq_0
-
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.
Merge pull request #255 from Arquisoft/makeFriends
Make friends
- Loading branch information
Showing
26 changed files
with
13,421 additions
and
3 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
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
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,23 @@ | ||
# Use an official Node.js runtime as a parent image | ||
FROM node:20 | ||
|
||
# Set the working directory in the container | ||
WORKDIR /usr/src/friends | ||
|
||
# Copy package.json and package-lock.json to the working directory | ||
COPY package*.json ./ | ||
|
||
# Install app dependencies | ||
RUN npm install | ||
|
||
# Set the env var | ||
ENV NODE_ENV=production | ||
|
||
# Copy the app source code to the working directory | ||
COPY . . | ||
|
||
# Expose the port the app runs on | ||
EXPOSE 8003 | ||
|
||
# Define the command to run your app | ||
CMD ["sh", "-c", "npx sequelize-cli db:migrate && node friends.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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const privateKey = "ChangeMePlease!!!!" | ||
|
||
function validateRequiredFields(req, requiredFields) { | ||
for (const field of requiredFields) { | ||
if (!(field in req.body)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
const authMiddleware = (req, res, next) => { | ||
|
||
if(!validateRequiredFields(req,['token'])){ | ||
res.status(401).send(); | ||
return; | ||
} | ||
|
||
try{ | ||
jwt.verify(req.body.token, privateKey); | ||
}catch{ | ||
res.status(401).send(); | ||
return; | ||
} | ||
|
||
next() | ||
} | ||
|
||
module.exports = authMiddleware |
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,65 @@ | ||
const { mockRequest, mockResponse, mockNext } = require('jest-mock-req-res'); | ||
const jwt = require('jsonwebtoken'); | ||
const authMiddleware = require('./friendsAuthMiddleware'); // Replace with the actual path to your auth middleware module | ||
|
||
const privateKey = "ChangeMePlease!!!!" | ||
|
||
jest.mock('jsonwebtoken'); // Mocking the jwt module | ||
|
||
describe('authMiddleware', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should call next() for a valid token', () => { | ||
const req = mockRequest({ | ||
body: { | ||
token: 'validToken', | ||
}, | ||
}); | ||
const res = mockResponse(); | ||
const next = jest.fn(); | ||
|
||
jwt.verify.mockImplementation(() => {}); | ||
|
||
authMiddleware(req, res, next); | ||
|
||
expect(jwt.verify).toHaveBeenCalledWith(req.body.token, privateKey); | ||
expect(next).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should respond with 401 for missing required fields', () => { | ||
const req = mockRequest({ | ||
body: {}, | ||
}); | ||
const res = mockResponse(); | ||
const next = jest.fn(); | ||
|
||
authMiddleware(req, res, next); | ||
|
||
expect(res.status).toHaveBeenCalledWith(401); | ||
expect(res.send).toHaveBeenCalled(); | ||
expect(next).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should respond with 401 for invalid token', () => { | ||
const req = mockRequest({ | ||
body: { | ||
token: 'invalidToken', | ||
}, | ||
}); | ||
const res = mockResponse(); | ||
const next = jest.fn(); | ||
|
||
jwt.verify.mockImplementation(() => { | ||
throw new Error('Invalid token'); | ||
}); | ||
|
||
authMiddleware(req, res, next); | ||
|
||
expect(jwt.verify).toHaveBeenCalledWith(req.body.token, privateKey); | ||
expect(res.status).toHaveBeenCalledWith(401); | ||
expect(res.send).toHaveBeenCalled(); | ||
expect(next).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,20 @@ | ||
{ | ||
"production": { | ||
"username": "root", | ||
"password": null, | ||
"database": "db", | ||
"host": "FriendDataDB", | ||
"port": 9003, | ||
"dialect": "mariadb" | ||
}, | ||
"development": { | ||
"dialect": "sqlite", | ||
"storage": "./testDB.sqlite", | ||
"logging": false | ||
}, | ||
"test": { | ||
"dialect": "sqlite", | ||
"storage": "./testDB.sqlite", | ||
"logging": false | ||
} | ||
} |
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,60 @@ | ||
// External libs | ||
require('dotenv').config() | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const cors = require('cors'); | ||
|
||
//libraries required for OpenAPI-Swagger | ||
const swaggerUi = require('swagger-ui-express'); | ||
const fs = require("fs") | ||
const YAML = require('yaml') | ||
|
||
// My own libs | ||
const authMiddleware = require('./auth/friendsAuthMiddleware'); | ||
|
||
const port = 8005; | ||
const app = express(); | ||
|
||
//Prometheus configuration | ||
const promBundle = require('express-prom-bundle'); | ||
const metricsMiddleware = promBundle({includeMethod: true}); | ||
app.use(metricsMiddleware); | ||
|
||
// Middleware | ||
app.use(bodyParser.json()); // Parse the request into json | ||
app.use(cors()) // This api is listening on a different port from the frontend | ||
app.use('/api/*',authMiddleware); // Auth middleware for the questions API | ||
|
||
|
||
// Api endpoints | ||
const endpoints = require("./friends/endpoints"); | ||
|
||
app.post("/api/friends/request/send",endpoints.sendRequest); | ||
app.post("/api/friends/request/accept",endpoints.acceptRequest); | ||
app.post("/api/friends/request/",endpoints.getRequests); | ||
app.post("/api/friends/",endpoints.getFriends); | ||
|
||
// Read the OpenAPI YAML file synchronously | ||
let openapiPath='./openapi.yaml' | ||
if (fs.existsSync(openapiPath)) { | ||
const file = fs.readFileSync(openapiPath, 'utf8'); | ||
|
||
// Parse the YAML content into a JavaScript object representing the Swagger document | ||
const swaggerDocument = YAML.parse(file); | ||
|
||
// Serve the Swagger UI documentation at the '/api-doc' endpoint | ||
// This middleware serves the Swagger UI files and sets up the Swagger UI page | ||
// It takes the parsed Swagger document as input | ||
app.use('/api-doc', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); | ||
} else { | ||
console.log("Not configuring OpenAPI. Configuration file not present.") | ||
} | ||
|
||
|
||
// Start the server | ||
const server = app.listen(port, () => { | ||
console.log(`Friends service listening at http://localhost:${port}`); | ||
}); | ||
|
||
|
||
module.exports = server |
Oops, something went wrong.