Skip to content

Commit

Permalink
Merge pull request #70 from incredible-phoenix246/chore/unit-testing
Browse files Browse the repository at this point in the history
Chore/unit testing
  • Loading branch information
Idimmusix authored Jul 21, 2024
2 parents ba6d3e2 + db7f098 commit 2670193
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 349 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "commonjs",
"scripts": {
"start:dev": "ts-node-dev --respawn --transpile-only ./src/index",
"start": "ts-node --transpile-only src/index.ts",
"test": "jest ",
"typeorm": "typeorm-ts-node-commonjs",
"build": "tsc",
"prod": "node dist/index.js"
"prod": "node dist/index.js",
"migrate": "typeorm migration:run -d src/data-source.ts"
},
"keywords": [],
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/TestimonialsController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import { AppDataSource } from "../data-source";
import AppDataSource from "../data-source";
import { Testimonial } from "../models/Testimonial";

export default class TestimonialsController {
Expand Down
4 changes: 3 additions & 1 deletion src/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "reflect-metadata";
import { DataSource, Tree } from "typeorm";
import config from "./config";

export const AppDataSource = new DataSource({
const AppDataSource = new DataSource({
type: "postgres",
host: config.DB_HOST,
port: 5432,
Expand All @@ -19,3 +19,5 @@ export const AppDataSource = new DataSource({
},
},
});

export default AppDataSource;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// src/index.ts
import "reflect-metadata";
import { AppDataSource } from "./data-source";
import AppDataSource from "./data-source";
import log from "./utils/logger";
import express, { Express, Request, Response } from "express";
import config from "./config";
Expand Down
46 changes: 30 additions & 16 deletions src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
ServerError,
Unauthorized,
} from "./error";
import log from "../utils/logger";
import jwt from "jsonwebtoken";
import config from "../config";

export const authMiddleware = async (
req: Request,
Expand All @@ -17,30 +20,41 @@ export const authMiddleware = async (
const authHeader = req.headers.authorization;

if (!authHeader || !authHeader.startsWith("Bearer ")) {
throw new HttpError(400, "Bad Request");
return res.status(401).json({
status_code: "401",
message: "Invalid token",
});
}

const token = authHeader.split(" ")[1];
if (!token) {
throw new Unauthorized("Invalid token");
return res.status(401).json({
status_code: "401",
message: "Invalid token",
});
}

const payload = verifyToken(token);

if (!payload) {
throw new Unauthorized("Unauthroized");
}

const user = await User.findOne({
where: { email: payload["email"] as string },
jwt.verify(token, config.TOKEN_SECRET, async (err, decoded: any) => {
if (err) {
return res.status(401).json({
status_code: "401",
message: "Invalid token",
});
}
const user = await User.findOne({
where: { email: decoded["email"] as string },
});
if (!user) {
return res.status(401).json({
status_code: "401",
message: "Invalid token",
});
}
req.user = user;
next();
});

if (!user) {
throw new ResourceNotFound("User not found");
}
req.user = user;
next();
} catch (error) {
log.error(error);
throw new ServerError("INTERNAL_SERVER_ERROR");
}
};
152 changes: 80 additions & 72 deletions src/seeder.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,93 @@
// src/seeder.ts
import { AppDataSource } from "./data-source";
import { User, Organization, Product, Profile } from "./models";
import log from "./utils/logger";
// // src/seeder.ts
// import { AppDataSource } from "./data-source";
// import { User, Organization, Product, Profile } from "./models";
// import log from "./utils/logger";

const seed = async () => {
// Create first user
const user1 = new User();
user1.name = "John Doe";
user1.email = "[email protected]";
user1.password = "password";
user1.otp = Math.floor(Math.random() * 10000); // Generate a random OTP
user1.otp_expires_at = new Date(Date.now() + 3600 * 1000); // OTP expires in 1 hour
user1.profile = new Profile();
user1.profile.first_name = "John";
user1.profile.last_name = "Doe";
user1.profile.phone = "1234567890";
user1.profile.avatarUrl = "http://example.com/avatar.jpg";
// const seed = async () => {
// // Create first user
// const user1 = new User();
// user1.name = "John Doe";
// user1.email = "[email protected]";
// user1.password = "password";
// user1.otp = Math.floor(Math.random() * 10000); // Generate a random OTP
// user1.otp_expires_at = new Date(Date.now() + 3600 * 1000); // OTP expires in 1 hour
// user1.profile = new Profile();
// user1.profile.first_name = "John";
// user1.profile.last_name = "Doe";
// user1.profile.phone = "1234567890";
// user1.profile.avatarUrl = "http://example.com/avatar.jpg";

// Create second user
const user2 = new User();
user2.name = "Jane Doe";
user2.email = "[email protected]";
user2.password = "password";
user2.otp = Math.floor(Math.random() * 10000); // Generate a random OTP
user2.otp_expires_at = new Date(Date.now() + 3600 * 1000); // OTP expires in 1 hour
user2.profile = new Profile();
user2.profile.first_name = "Jane";
user2.profile.last_name = "Doe";
user2.profile.phone = "0987654321";
user2.profile.avatarUrl = "http://example.com/avatar.jpg";
// // Create second user
// const user2 = new User();
// user2.name = "Jane Doe";
// user2.email = "[email protected]";
// user2.password = "password";
// user2.otp = Math.floor(Math.random() * 10000); // Generate a random OTP
// user2.otp_expires_at = new Date(Date.now() + 3600 * 1000); // OTP expires in 1 hour
// user2.profile = new Profile();
// user2.profile.first_name = "Jane";
// user2.profile.last_name = "Doe";
// user2.profile.phone = "0987654321";
// user2.profile.avatarUrl = "http://example.com/avatar.jpg";

// Create products
const product1 = new Product();
product1.name = "Product 1";
product1.description = "Description for product 1";
product1.user = user1;
// // Create products
// const product1 = new Product();
// product1.name = "Product 1";
// product1.description = "Description for product 1";
// product1.user = user1;

const product2 = new Product();
product2.name = "Product 2";
product2.description = "Description for product 2";
product2.user = user1;
// const product2 = new Product();
// product2.name = "Product 2";
// product2.description = "Description for product 2";
// product2.user = user1;

const product3 = new Product();
product3.name = "Product 3";
product3.description = "Description for product 3";
product3.user = user2;
// const product3 = new Product();
// product3.name = "Product 3";
// product3.description = "Description for product 3";
// product3.user = user2;

const product4 = new Product();
product4.name = "Product 4";
product4.description = "Description for product 4";
product4.user = user2;
// <<<<<<< chore/unit-testing
// // const product4 = new Product();
// // product4.name = "Product 4";
// // product4.description = "Description for product 4";

// Create organizations
const organization1 = new Organization();
organization1.name = "Org 1";
organization1.description = "Description for org 1";
// // product4.user = user2;
// =======
// const product4 = new Product();
// product4.name = "Product 4";
// product4.description = "Description for product 4";
// product4.user = user2;
// >>>>>>> dev

const organization2 = new Organization();
organization2.name = "Org 2";
organization2.description = "Description for org 2";
// // Create organizations
// const organization1 = new Organization();
// organization1.name = "Org 1";
// organization1.description = "Description for org 1";

const organization3 = new Organization();
organization3.name = "Org 3";
organization3.description = "Description for org 3";
// const organization2 = new Organization();
// organization2.name = "Org 2";
// organization2.description = "Description for org 2";

// Assign organizations to users
user1.organizations = [organization1, organization2];
user2.organizations = [organization1, organization2, organization3];
// const organization3 = new Organization();
// organization3.name = "Org 3";
// organization3.description = "Description for org 3";

// Save entities
await AppDataSource.manager.save(user1);
await AppDataSource.manager.save(user2);
await AppDataSource.manager.save(organization1);
await AppDataSource.manager.save(organization2);
await AppDataSource.manager.save(organization3);
await AppDataSource.manager.save(product1);
await AppDataSource.manager.save(product2);
await AppDataSource.manager.save(product3);
await AppDataSource.manager.save(product4);
// // Assign organizations to users
// user1.organizations = [organization1, organization2];
// user2.organizations = [organization1, organization2, organization3];

log.info("Seeding completed successfully.");
};
// // Save entities
// await AppDataSource.manager.save(user1);
// await AppDataSource.manager.save(user2);
// await AppDataSource.manager.save(organization1);
// await AppDataSource.manager.save(organization2);
// await AppDataSource.manager.save(organization3);
// await AppDataSource.manager.save(product1);
// await AppDataSource.manager.save(product2);
// await AppDataSource.manager.save(product3);
// await AppDataSource.manager.save(product4);

export { seed };
// log.info("Seeding completed successfully.");
// };

// export { seed };
2 changes: 1 addition & 1 deletion src/services/auth.services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppDataSource } from "../data-source";
import AppDataSource from "../data-source";
import { Profile, User } from "../models";
import { IAuthService, IUserSignUp, IUserLogin } from "../types";
import { Conflict, HttpError } from "../middleware";
Expand Down
Loading

0 comments on commit 2670193

Please sign in to comment.