Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lynetteluth committed Jul 21, 2023
2 parents 986c17c + 3cbbbdf commit 76e2335
Show file tree
Hide file tree
Showing 49 changed files with 1,705 additions and 631 deletions.
8 changes: 4 additions & 4 deletions nextjs/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig.json');
const { compilerOptions } = require('./tsconfig.test.json');
module.exports = {
preset: 'ts-jest/presets/js-with-ts-esm',
testEnvironment: 'jsdom',
Expand All @@ -10,14 +10,14 @@ module.exports = {
modulePaths: ['<rootDir>'],
transform: {
'^.+\\.(ts|js)x?$': 'babel-jest',
'^.+\\.(ts|js)x?$': ['ts-jest', { tsconfig: './tsconfig.json' }]
'^.+\\.(ts|js)x?$': ['ts-jest', { tsconfig: './tsconfig.test.json' }]
},
transformIgnorePatterns: ['/node_modules/'],
modulePathIgnorePatterns: ['<rootDir>/~'],
// extensionsToTreatAsEsm: ['.ts', '.tsx', '.js', '.jsx'],
moduleNameMapper: {
'\\.(css|less|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',
'\\.(gif|ttf|eot|svg)$': '<rootDir>/__mocks__/fileMock.js',
'\\.(css|less|sass|scss)$': '<rootDir>/src/tests/__mocks__/styleMock.js',
'\\.(gif|ttf|eot|svg)$': '<rootDir>/src/tests/__mocks__/fileMock.js',
...pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/'
})
Expand Down
1 change: 1 addition & 0 deletions nextjs/src/components/dashboard/Authorization.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use client';
import React from 'react';
import axios, { AxiosError, AxiosResponse } from 'axios';
import { ReactNode, useLayoutEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
Expand Down
1 change: 1 addition & 0 deletions nextjs/src/components/dashboard/layout/ChooseSemester.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function ChooseSemester() {
};

useLayoutEffect(() => {
console.log(sessionStorage.getItem('ay'))
setAy(parseInt(sessionStorage.getItem('ay') || '2023'));
setSem(parseInt(sessionStorage.getItem('sem') || '1'));
});
Expand Down
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions nextjs/src/tests/dashboard/chooseSemester.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { render } from '@testing-library/react';
import ChooseSemester from '@components/dashboard/layout/ChooseSemester';
import '@testing-library/jest-dom';

// beforeEach(function() {
// global.sessionStorage = jest.genMockFunction();
// global.sessionStorage.setItem = jest.genMockFunction();
// global.sessionStorage.getItem = jest.genMockFunction();
// }

test('renders status bar with correct descriptor', () => {
const color = '#404040';
const height = '20px';
const descriptor = 'Test Descriptor';

const { getByText } = render(<ChooseSemester />);

const descriptorElement = getByText(descriptor);
expect(descriptorElement).toBeInTheDocument();
});

test('calculates the width correctly based on the descriptor length', () => {
const color = '#404040';
const height = '20px';
const descriptor = 'Test Descriptor sdjknjdnkadnsnkjnasfnn';

const { container } = render(<ChooseSemester />);

const wrapperElement = container.firstChild;
const backgroundElement = container.firstChild?.firstChild;

// Check if the width is dynamically calculated based on the descriptor length
expect(wrapperElement).toHaveStyle({ width: '47px' }); // Expected width: (descriptor length + 25)px + 5px for padding
expect(backgroundElement).toHaveStyle({ width: '42px' }); // Expected width: descriptor length + 5px for padding
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { render } from '@testing-library/react';
// import StatusBar from '@components/dashboard/dashboard/StatusBar';
import StatusBar from '@components/dashboard/dashboard/StatusBar';
import '@testing-library/jest-dom';

Expand All @@ -20,16 +19,16 @@ test('renders status bar with correct descriptor', () => {
test('calculates the width correctly based on the descriptor length', () => {
const color = '#404040';
const height = '20px';
const descriptor = 'Test Descriptor';
const descriptor = 'Test Descriptor sdjknjdnkadnsnkjnasfnn';

const { container } = render(
<StatusBar color={color} height={height} descriptor={descriptor} />
);

const wrapperElement = container.firstChild;
const backgroundElement = container.querySelector('.background');
const backgroundElement = container.firstChild?.firstChild;

// Check if the width is dynamically calculated based on the descriptor length
expect(wrapperElement).toHaveStyle({ width: '192px' }); // Expected width: (descriptor length + 25)px + 5px for padding
expect(backgroundElement).toHaveStyle({ width: '167px' }); // Expected width: descriptor length + 5px for padding
expect(wrapperElement).toHaveStyle({ width: '47px' }); // Expected width: (descriptor length + 25)px + 5px for padding
expect(backgroundElement).toHaveStyle({ width: '42px' }); // Expected width: descriptor length + 5px for padding
});
113 changes: 54 additions & 59 deletions nextjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,64 +1,59 @@
{
"compilerOptions": {
"target": "esnext",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"outDir": ".next",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@app/*": [
"./src/app/*"
],
"@models/*": [
"./src/models/*"
],
"@components/*": [
"./src/components/*"
],
"@server/*": [
"./src/server/*"
],
"@/*": [
"./src/*"
]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
"src/**/*",
"src/tests/setupTests.ts"
"compilerOptions": {
"target": "esnext",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"exclude": [
"node_modules"
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"outDir": ".next",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"jest": {
"moduleNameMapper": {
"^@app/(.*)$": "<rootDir>/src/app/$1",
"^@models/(.*)$": "<rootDir>/src/models/$1",
"^@components/(.*)$": "<rootDir>/src/components/",
"^@/(.*)$": "<rootDir>/src/$1"
}
"paths": {
"@app/*": [
"./src/app/*"
],
"@models/*": [
"./src/models/*"
],
"@components/*": [
"./src/components/*"
],
"@/*": [
"./src/*"
]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
],
"jest": {
"moduleNameMapper": {
"^@app/(.*)$": "<rootDir>/src/app/$1",
"^@models/(.*)$": "<rootDir>/src/models/$1",
"^@components/(.*)$": "<rootDir>/src/components/",
"^@/(.*)$": "<rootDir>/src/$1"
}
}
}
41 changes: 41 additions & 0 deletions server/database/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Sequelize } from 'sequelize';

// // For development
// const sequelize = new Sequelize('ClassMate', 'postgres', 'postgres', {
// host: 'localhost',
// port: 5432,
// dialect: 'postgres',
// // logging: false
// // logging: process.env.NODE_ENV === 'production' ? false : console.log,
// });

// For testing
const sequelize = new Sequelize('ClassMateTest', 'postgres', 'postgres', {
host: 'localhost',
port: 5432,
dialect: 'postgres',
logging: false
// logging: process.env.NODE_ENV === 'production' ? false : console.log,
});

// For Production
// const sequelize = new Sequelize('postgres', 'postgres', 'ClassMate123!1', {
// host: 'db.tubxvcrohjakulghoizg.supabase.co',
// port: 6543,
// dialect: 'postgres'
// });


export async function createSequelizeConnection(instance: Sequelize) {
try {
await instance.authenticate();
console.log('Connection to database established!');
return 'Connection to database established!';
} catch (err) {
console.error('Unable to connect to database', err);
return 'Failed to connect to database!';
}
}

// createSequelizeConnection(sequelize);
export default sequelize;
27 changes: 0 additions & 27 deletions server/database/connection.tsx

This file was deleted.

55 changes: 55 additions & 0 deletions server/database/controllers/authController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Request, Response } from 'express';
import jwt, { JwtPayload } from 'jsonwebtoken';
import bcrypt from 'bcrypt';
import config from '@server/config';
import Users from '@models/Users';

/**
req: {
headers: {
Authorization: ~token~
},
}
Verifies the JSON web token passed in request headers
**/
async function validateRequest(req: Request, res: Response) {
const token = req.headers.authorization as string;

if (!token || !token.trim()) {
return res.status(404).json({ message: 'Token required!' });
}

return await new Promise((resolve, reject) =>
jwt.verify(token, config.JWT_SECRET, async (err, decoded) => {
if (err) {
reject(err);
}
// Check if decoded password is correct for user
if (decoded) {
const username = (decoded as JwtPayload).username as string;
const password = (decoded as JwtPayload).password as string;
const user = await Users.findByPk(username, {
attributes: ['password']
}).catch((err) => reject(err));
await bcrypt
.compare(password, user?.password as string)
.then((authenticated) => {
if (authenticated) {
resolve('');
} else {
reject('Wrong password!');
}
})
.catch((err) => reject(err));
} else {
reject('False JWT hash!');
}
})
)
.then(() => res.status(200).json({ message: 'User authorized!' }))
.catch((err) =>
res.status(401).json({ message: 'Unauthorized!', error: err })
);
}

export default validateRequest;
28 changes: 0 additions & 28 deletions server/database/controllers/authController.tsx

This file was deleted.

Loading

0 comments on commit 76e2335

Please sign in to comment.