Skip to content

Commit

Permalink
Merge pull request #188 from Vincit/release/summer-2024
Browse files Browse the repository at this point in the history
Release/summer 2024
  • Loading branch information
villepynttari authored Aug 16, 2024
2 parents d7a7113 + 05d1dac commit 22a0fce
Show file tree
Hide file tree
Showing 138 changed files with 17,341 additions and 39,299 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ yarn-error.log*

# Env file
.env
*.iml
.idea
3 changes: 3 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ DB_PASSWORD=""
# Database url
DB_URL=""

#Overrides whole url for local development (mongodb://getaroom:getaroom@localhost/getaroom?retryWrites=true&w=majority)
DB_OVERRIDE_URL=""

# Secret for JWT token, could be any long random string
JWT_SECRET=""

Expand Down
11,794 changes: 5,793 additions & 6,001 deletions backend/package-lock.json

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "src/app.ts",
"engines": {
"node": ">=16.0.0"
"node": ">=20.12.2"
},
"scripts": {
"test": "jest",
Expand All @@ -19,45 +19,45 @@
},
"author": "",
"dependencies": {
"@types/cookie-parser": "^1.4.2",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/express-unless": "^0.5.2",
"@types/jsonwebtoken": "^8.5.5",
"@types/lodash": "^4.14.175",
"@types/luxon": "^2.0.5",
"@types/morgan": "^1.9.3",
"@types/node": "^16.10.2",
"cookie-parser": "^1.4.5",
"@types/cookie-parser": "^1.4.7",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/express-unless": "^2.0.1",
"@types/jsonwebtoken": "^9.0.6",
"@types/lodash": "^4.17.5",
"@types/luxon": "^3.4.2",
"@types/morgan": "^1.9.9",
"@types/node": "^18.19.34",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-unless": "^1.0.0",
"express-validator": "^6.12.2",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-unless": "^2.1.3",
"express-validator": "^7.1.0",
"googleapis": "^88.2.0",
"helmet": "^4.6.0",
"jsonwebtoken": "^8.5.1",
"helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"luxon": "^2.0.2",
"mongoose": "^6.0.11",
"luxon": "^3.4.4",
"mongoose": "^8.4.1",
"morgan": "^1.10.0",
"node-schedule": "^2.1.0",
"ts-node": "^10.2.1",
"typescript": "^4.4.3",
"web-push": "^3.5.0"
"node-schedule": "^2.1.1",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
},
"devDependencies": {
"@types/jest": "^27.0.2",
"@types/node-schedule": "^2.1.0",
"@types/web-push": "^3.3.2",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"@types/jest": "^29.5.12",
"@types/node-schedule": "^2.1.7",
"@types/web-push": "^3.6.3",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-node": "^11.1.0",
"jest": "^27.2.5",
"nodemon": "^2.0.13",
"ts-jest": "^27.0.6"
"jest": "^29.7.0",
"nodemon": "^3.1.3",
"ts-jest": "^29.1.4",
"web-push": "^3.6.7"
},
"eslintConfig": {
"env": {
Expand Down
8 changes: 4 additions & 4 deletions backend/src/authMiddleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { mocked } from 'ts-jest/utils';
import { mocked } from 'jest-mock';
import { authFilter, parseToken, validateAccessToken } from './authMiddleware';
import { invalidToken } from './utils/responses';
import { readToken, updateToken } from './controllers/auth/token';
Expand All @@ -9,9 +9,9 @@ jest.mock('./utils/responses');
jest.mock('./controllers/auth/token');
jest.mock('google-auth-library');

const mockedInvalidToken = mocked(invalidToken, false);
const mockedReadToken = mocked(readToken, false);
const mockedUpdateToken = mocked(updateToken, false);
const mockedInvalidToken = mocked(invalidToken, { shallow: false });
const mockedReadToken = mocked(readToken, { shallow: false });
const mockedUpdateToken = mocked(updateToken, { shallow: false });
const mockedAuth = mocked(OAuth2Client, true);

describe('authMiddleware', () => {
Expand Down
14 changes: 11 additions & 3 deletions backend/src/authMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import unless from 'express-unless';
import { unless } from 'express-unless';
import { getOAuthClient } from './utils/oAuthClient';
import * as responses from './utils/responses';
import { readToken, updateToken } from './controllers/auth/token';
Expand Down Expand Up @@ -29,7 +29,15 @@ export const authFilter = (req: express.Request) => {
* httpOnly cookie and sets them to res.locals
* @returns -
*/
export const parseToken = () => {
export type ExpressMiddlewareWithUnless = {
(
req: express.Request,
res: express.Response,
next: express.NextFunction
): Promise<express.Response | undefined>;
unless: typeof unless;
};
export const parseToken = (): ExpressMiddlewareWithUnless => {
const middleware = async (
req: express.Request,
res: express.Response,
Expand Down Expand Up @@ -71,7 +79,7 @@ export const parseToken = () => {
* @param noAuthPaths Array of paths that don't require authentication
* @returns -
*/
export const validateAccessToken = () => {
export const validateAccessToken = (): ExpressMiddlewareWithUnless => {
const middleware = async (
req: express.Request,
res: express.Response,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controllers/auth/google.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
verifyCode
} from './google';
import { Credentials, LoginTicket, OAuth2Client } from 'google-auth-library';
import { mocked } from 'ts-jest/utils';
import { mocked } from 'jest-mock';
import { DateTime } from 'luxon';

jest.mock('google-auth-library');
Expand Down
40 changes: 29 additions & 11 deletions backend/src/controllers/auth/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ import { TokenPayload } from 'google-auth-library';
import jwt, { JwtPayload } from 'jsonwebtoken';
import { DateTime } from 'luxon';

import { createToken, updateToken, readToken, writeToken } from './token';
import { createToken, readToken, updateToken, writeToken } from './token';

import jwtTokenPayload from '../../types/jwtTokenPayload';

describe('token', () => {
let mockRequest: Partial<Request>;
let mockResponse: Partial<Response>;
let mockNext: jest.Mock;

const createMockToken = async (): Promise<string> => {
await createToken()(
mockRequest as Request,
mockResponse as Response,
mockNext
);
return mockResponse?.locals?.token;
};
const testPayload: TokenPayload = {
iss: 'https://accounts.google.com',
sub: 'testSub',
Expand All @@ -21,10 +28,11 @@ describe('token', () => {
name: 'testName',
email: '[email protected]'
};
const testToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0U3ViIiwibmFtZSI6InRlc3ROYW1lIiwiZW1haWwiOiJ0ZXN0QGVtYWlsLmNvbSIsImFjY2Vzc1Rva2VuIjoidG9rZW4iLCJyZWZyZXNoVG9rZW4iOiJydG9rZW4iLCJpYXQiOjE2Mzg0NjUyODAsImV4cCI6MTcwMTQzNzEzMywiaXNzIjoidGVzdERvbWFpbi5jb20ifQ.kgQjW9mcWP6C1jODwuaOojQVE8f4DfOz3wMB2mbK24Y';
let testToken: string;

beforeEach(async () => {
jest.resetAllMocks();

beforeEach(() => {
mockRequest = {};
mockResponse = {
locals: {
Expand All @@ -34,13 +42,9 @@ describe('token', () => {
}
};
mockNext = jest.fn();

process.env.HOSTED_DOMAIN = 'testDomain.com';
process.env.JWT_SECRET = 'testSecret';

jest.resetAllMocks();
});

describe('createToken', () => {
test('Should set JWT token to locals', async () => {
if (mockResponse.locals) {
Expand All @@ -66,7 +70,9 @@ describe('token', () => {
);
});

test('Should update the accessToken field of JWT', () => {
test('Should update the accessToken field of JWT', async () => {
testToken = await createMockToken();

const newJwt = updateToken(testToken, 'token2');
const payload = jwt.verify(
newJwt,
Expand All @@ -84,7 +90,19 @@ describe('token', () => {
expect(() => readToken(testToken)).toThrow('invalid signature');
});

test('Should return JWT payload', () => {
test('Should return JWT payload', async () => {
const testData = {
...mockResponse.locals,
payload: {
sub: 'testSub',
name: 'testName',
email: '[email protected]',
accessToken: 'token',
refreshToken: 'rtoken'
}
};
mockResponse.locals = testData;
testToken = await createMockToken();
const decoded = readToken(testToken);

expect(decoded.sub).toEqual('testSub');
Expand Down
1 change: 0 additions & 1 deletion backend/src/controllers/auth/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const createToken = () => {
) => {
try {
const payload = res.locals.payload as TokenPayload | undefined;

const jwtPayload: jwtTokenPayload = {
sub: payload?.sub as string,
name: payload?.name as string,
Expand Down
12 changes: 8 additions & 4 deletions backend/src/controllers/booking/bookingUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from 'express';
import { DateTime } from 'luxon';
import { mocked } from 'ts-jest/utils';
import { mocked } from 'jest-mock';
import { internalServerError } from '../../utils/responses';
import { getSingleRoomData } from '../googleAPI/adminAPI';
import { simplifySingleRoomData } from '../roomController';
Expand All @@ -12,9 +12,13 @@ jest.mock('../roomController');
jest.mock('../googleAPI/adminAPI');
jest.mock('../../utils/responses');

const mockedInternalServerError = mocked(internalServerError, false);
const mockedGetSingleRoomData = mocked(getSingleRoomData, false);
const mockedSimplifySingleRoomData = mocked(simplifySingleRoomData, false);
const mockedInternalServerError = mocked(internalServerError, {
shallow: false
});
const mockedGetSingleRoomData = mocked(getSingleRoomData, { shallow: false });
const mockedSimplifySingleRoomData = mocked(simplifySingleRoomData, {
shallow: false
});

const MOCK_ROOM_DATA: RoomData = {
id: 'roomID',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { mocked } from 'ts-jest/utils';
import { mocked } from 'jest-mock';
import * as schema from '../../utils/googleSchema';
import { DateTime } from 'luxon';
import { internalServerError } from '../../utils/responses';
Expand All @@ -16,9 +16,11 @@ jest.mock('../../utils/responses');
jest.mock('../googleAPI/calendarAPI');
jest.mock('../googleAPI/adminAPI');

const mockedGetCurrentBookings = mocked(getCurrentBookings, false);
const mockedGetRoomData = mocked(getRoomData, false);
const mockedInternalServerError = mocked(internalServerError, false);
const mockedGetCurrentBookings = mocked(getCurrentBookings, { shallow: false });
const mockedGetRoomData = mocked(getRoomData, { shallow: false });
const mockedInternalServerError = mocked(internalServerError, {
shallow: false
});

describe('currentBookingsController', () => {
let mockRequest: Partial<Request>;
Expand Down
6 changes: 5 additions & 1 deletion backend/src/controllers/booking/currentBookingsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ export const addNextCalendarEventMiddleware = () => {
const endDt = DateTime.fromISO(
req.query.until as string
).toUTC();
end = endDt.toISO();
end = endDt.toISO() || '';

if (end === '') {
throw new Error('End date not found');
}

if (endDt <= startDt) {
return responses.badRequest(req, res);
Expand Down
10 changes: 6 additions & 4 deletions backend/src/controllers/booking/deleteBookingController.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { mocked } from 'ts-jest/utils';
import { mocked } from 'jest-mock';
import { badRequest, internalServerError } from '../../utils/responses';
import { deleteEvent } from '../googleAPI/calendarAPI';
import { deleteBooking } from './deleteBookingController';
Expand All @@ -8,9 +8,11 @@ jest.mock('../../utils/responses');
jest.mock('../googleAPI/calendarAPI');
jest.mock('../googleAPI/adminAPI');

const mockedDeleteEvent = mocked(deleteEvent, false);
const mockedBadRequest = mocked(badRequest, false);
const mockedInternalServerError = mocked(internalServerError, false);
const mockedDeleteEvent = mocked(deleteEvent, { shallow: false });
const mockedBadRequest = mocked(badRequest, { shallow: false });
const mockedInternalServerError = mocked(internalServerError, {
shallow: false
});

describe('deleteBookingController', () => {
let mockRequest: Partial<Request>;
Expand Down
6 changes: 3 additions & 3 deletions backend/src/controllers/booking/getBookingController.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { mocked } from 'ts-jest/utils';
import { mocked } from 'jest-mock';
import { badRequest } from '../../utils/responses';
import { getBooking } from './getBookingController';
import { getEventData } from '../googleAPI/calendarAPI';
Expand All @@ -8,8 +8,8 @@ import { EventData } from '../../utils/googleSchema';
jest.mock('../../utils/responses');
jest.mock('../googleAPI/calendarAPI');

const mockedGetEventData = mocked(getEventData, false);
const mockedBadRequest = mocked(badRequest, false);
const mockedGetEventData = mocked(getEventData, { shallow: false });
const mockedBadRequest = mocked(badRequest, { shallow: false });

describe('getBookingController', () => {
let mockRequest: Partial<Request>;
Expand Down
18 changes: 10 additions & 8 deletions backend/src/controllers/booking/makeBookingController.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { mocked } from 'ts-jest/utils';
import { mocked } from 'jest-mock';
import { badRequest, custom, internalServerError } from '../../utils/responses';
import { query } from 'express-validator';
import { DateTime } from 'luxon';
Expand All @@ -20,14 +20,16 @@ import {
jest.mock('../../utils/responses');
jest.mock('../googleAPI/calendarAPI');

const mockedCreateEvent = mocked(createEvent, false);
const mockedDeleteEvent = mocked(deleteEvent, false);
const mockedFreeBusyQuery = mocked(freeBusyQuery, false);
const mockedGetEventData = mocked(getEventData, false);
const mockedCreateEvent = mocked(createEvent, { shallow: false });
const mockedDeleteEvent = mocked(deleteEvent, { shallow: false });
const mockedFreeBusyQuery = mocked(freeBusyQuery, { shallow: false });
const mockedGetEventData = mocked(getEventData, { shallow: false });

const mockedBadRequest = mocked(badRequest, false);
const mockedCustomResponse = mocked(custom, false);
const mockedInternalServerError = mocked(internalServerError, false);
const mockedBadRequest = mocked(badRequest, { shallow: false });
const mockedCustomResponse = mocked(custom, { shallow: false });
const mockedInternalServerError = mocked(internalServerError, {
shallow: false
});

describe('makeBookingController', () => {
let mockRequest: Partial<Request>;
Expand Down
Loading

0 comments on commit 22a0fce

Please sign in to comment.