From dc1b955a8a5eedc734b35b6d4750c3c503ccc593 Mon Sep 17 00:00:00 2001 From: Filip Rojek Date: Tue, 30 Apr 2024 00:50:27 +0200 Subject: [PATCH] Pending --- api/package.json | 23 ++++++++----- api/src/controllers/authController.ts | 2 +- api/src/routes/authRoutes.ts | 2 +- api/src/utils/test_mariadb.ts | 48 +++++++++++++++++++++++++++ api/tests/_setupFile.ts | 9 +++++ api/tests/auth.test.ts | 17 ++++++++++ 6 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 api/src/utils/test_mariadb.ts create mode 100644 api/tests/_setupFile.ts create mode 100644 api/tests/auth.test.ts diff --git a/api/package.json b/api/package.json index ec289fe..da6e875 100644 --- a/api/package.json +++ b/api/package.json @@ -13,7 +13,7 @@ "clean": "rimraf dist", "copy-assets": "ts-node src/utils/copyAssets", "build": "npm-run-all clean tsc copy-assets", - "test": "mocha --config .mocharc.json --watch src/**/*.test.ts", + "test": "npx jest . --runInBand", "format": "npx prettier --write ." }, "author": "Filip Rojek", @@ -29,33 +29,38 @@ "path": "^0.12.7" }, "devDependencies": { + "@biomejs/biome": "1.7.1", "@types/chai": "^4.2.22", "@types/express": "^4.17.21", "@types/fs-extra": "^9.0.13", "@types/inquirer": "^8.1.3", + "@types/jest": "^29.5.12", "@types/mocha": "^9.0.0", "@types/morgan": "^1.9.9", "@types/shelljs": "^0.8.11", + "@types/supertest": "^6.0.2", "@typescript-eslint/eslint-plugin": "^5.5.0", "@typescript-eslint/parser": "^5.5.0", "chai": "^4.3.4", "eslint": "^8.3.0", "http": "^0.0.1-security", + "jest": "^29.7.0", "mocha": "^9.1.3", "npm-run-all": "^4.1.5", "prettier": "^2.7.1", "rimraf": "^3.0.2", "shelljs": "^0.8.5", + "supertest": "^7.0.0", + "ts-jest": "^29.1.2", "ts-node": "^10.9.2", "tsx": "^4.7.3", "typescript": "^4.5.2" }, - "repository": { - "type": "git", - "url": "git+https://github.com/filiprojek/nork.git" - }, - "bugs": { - "url": "https://github.com/filiprojek/nork/issues" - }, - "homepage": "https://github.com/filiprojek/nork/blob/master/README.md" + "jest": { + "preset": "ts-jest", + "testEnvironment": "node", + "setupFilesAfterEnv": [ + "./tests/_setupFile.ts" + ] + } } diff --git a/api/src/controllers/authController.ts b/api/src/controllers/authController.ts index 7ac1f64..2e39bf5 100644 --- a/api/src/controllers/authController.ts +++ b/api/src/controllers/authController.ts @@ -1,5 +1,5 @@ import { Request, Response } from "express"; -export function login_post(req: Request, res: Response) { +export function signup_post(req: Request, res: Response) { res.send("logged in"); } diff --git a/api/src/routes/authRoutes.ts b/api/src/routes/authRoutes.ts index c62cd4e..ee10c77 100644 --- a/api/src/routes/authRoutes.ts +++ b/api/src/routes/authRoutes.ts @@ -8,7 +8,7 @@ const router = Router(); //const mws = [requireAuth, handleValidation.handleValidationError]; -router.post("/login", authController.login_post); +router.post("/signup", authController.signup_post); //router.post( // "/login", diff --git a/api/src/utils/test_mariadb.ts b/api/src/utils/test_mariadb.ts new file mode 100644 index 0000000..ce8d355 --- /dev/null +++ b/api/src/utils/test_mariadb.ts @@ -0,0 +1,48 @@ +import { Sequelize } from 'sequelize'; + +let sequelize: Sequelize | null = null; + +const connectDB = async () => { + sequelize = new Sequelize({ + dialect: 'mariadb', + host: 'localhost', + username: 'your_username', + password: 'your_password', + database: 'your_database', + }); + + try { + await sequelize.authenticate(); + console.log('Connection to the database has been established successfully.'); + } catch (error) { + console.error('Unable to connect to the database:', error); + } +}; + +const dropDB = async () => { + if (sequelize) { + try { + await sequelize.drop(); + console.log('Database dropped successfully.'); + } catch (error) { + console.error('Error dropping database:', error); + } finally { + await sequelize.close(); + } + } +}; + +const dropTables = async () => { + if (sequelize) { + try { + await sequelize.sync({ force: true }); + console.log('All tables dropped successfully.'); + } catch (error) { + console.error('Error dropping tables:', error); + } finally { + await sequelize.close(); + } + } +}; + +export { connectDB, dropDB, dropTables }; diff --git a/api/tests/_setupFile.ts b/api/tests/_setupFile.ts new file mode 100644 index 0000000..efe36b2 --- /dev/null +++ b/api/tests/_setupFile.ts @@ -0,0 +1,9 @@ +import { connectDB, dropDB, dropCollections } from '../src/utils/test_mongodb'; + +beforeAll(async () => { + await connectDB(); +}); + +afterAll(async () => { + await dropDB(); +}); diff --git a/api/tests/auth.test.ts b/api/tests/auth.test.ts new file mode 100644 index 0000000..a8147a7 --- /dev/null +++ b/api/tests/auth.test.ts @@ -0,0 +1,17 @@ +import request from "supertest" +import {server as app} from "../server" + +describe('Auth API Endpoints', () => { + afterAll((done) => { + app.close(done); + }) + + it('should signup new user', async () => { + const res = await request(app) + .post('/api/auth/signup') + .send({}); + + expect(res.statusCode).toEqual(200) + + }) +})