Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:atlp-rwanda/e-commerce-ninjas-bn…
Browse files Browse the repository at this point in the history
… into fx-email-name
  • Loading branch information
Aime-Patrick committed Jul 25, 2024
2 parents 07a083d + 866e8a2 commit 4d3beb2
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 66 deletions.
16 changes: 9 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ orbs:
jobs:
build:
docker:
- image: cimg/node:18.17.0
- image: cimg/node:20.0
steps:
- setup_remote_docker:
version: docker24
Expand All @@ -23,26 +23,28 @@ jobs:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package-lock.json" }}
- run:
name: Setup database
command: |
# Replace with your actual database setup commands
npm run migrate
npm run seed
- run:
name: Run tests
command: npm test
when: always
command: npm test -- --verbose
- run:
name: Run coverage
command: npm run coverage
when: always
- run:
name: Setup Code Climate test-reporter
command: |
# download test reporter as a static binary
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
when: always
- run:
name: Send coverage report to Code Climate
command: ./cc-test-reporter after-build -t lcov
when: always
- store_artifacts:
path: ./coverage/lcov.info
prefix: tests
prefix: tests
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ jobs:
- name: Set NODE_ENV to test
run: echo "NODE_ENV=test" >> $GITHUB_ENV

- name: Setup database
run: |
# Replace with your actual database setup commands
npm run migrate
npm run seed
- run: npm run build --if-present

- run: npm test --if-present
- run: npm test --if-present -- --verbose

- run: npm run coverage --if-present

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
"createAllSeeders": "tsc && npx sequelize db:seed:all",
"deleteAllTables": "tsc && npx sequelize db:migrate:undo:all",
"deleteAllSeeders": "tsc && npx sequelize db:seed:undo:all",
"migrate": "knex migrate:latest",
"seed": "knex seed:run",
"start": "nodemon src/index.ts",
"dev": "nodemon src/index.ts",
"test": "cross-env NODE_ENV=test npm run deleteAllTables && cross-env NODE_ENV=test npm run createAllTables && cross-env NODE_ENV=test npm run createAllSeeders && nyc cross-env NODE_ENV=test mocha --require ts-node/register 'src/**/*.spec.ts' --timeout 600000 --exit",
"test": "cross-env NODE_ENV=test npm run deleteAllTables && cross-env NODE_ENV=test npm run createAllTables && cross-env NODE_ENV=test npm run createAllSeeders && nyc cross-env NODE_ENV=test mocha --require ts-node/register 'src/**/*.spec.ts' --timeout 6000000 --exit",
"test-local": "cross-env NODE_ENV=development npm run deleteAllTables && cross-env NODE_ENV=development npm run createAllTables && cross-env NODE_ENV=development npm run createAllSeeders && nyc cross-env NODE_ENV=development mocha --require ts-node/register 'src/**/*.spec.ts' --timeout 600000 --exit",
"coveralls": "nyc --reporter=lcov --reporter=text-lcov npm test | coveralls",
"coverage": "cross-env NODE_ENV=test npm run deleteAllTables && cross-env NODE_ENV=test npm run createAllTables && cross-env NODE_ENV=test npm run createAllSeeders && nyc cross-env NODE_ENV=test mocha --require ts-node/register 'src/**/*.spec.ts' --timeout 600000 --exit",
"coverage": "cross-env NODE_ENV=test npm run deleteAllTables && cross-env NODE_ENV=test npm run createAllTables && cross-env NODE_ENV=test npm run createAllSeeders && nyc cross-env NODE_ENV=test mocha --require ts-node/register 'src/**/*.spec.ts' --timeout 6000000 --exit",
"lint": "eslint . --ext .ts",
"lint-staged": "lint-staged",
"prepare": "husky",
Expand Down
4 changes: 1 addition & 3 deletions src/helpers/passwordExpiryNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ export const checkPasswordExpirations = async () => {
const emailMessage = `${salutation}, your password will expire in ${interval} minutes. Please update your password to continue using the platform.`;
eventEmitter.emit("passwordExpiry", { userId: user.id, message: emailMessage, minutes: interval });
}

// console.log(`${usersToWarn.length} users warned for ${interval}-minute password expiration.`);
}

const usersToNotifyExpired = await Users.findAll({
Expand All @@ -68,7 +66,7 @@ export const checkPasswordExpirations = async () => {
eventEmitter.emit("passwordExpiry", { userId: user.id, message: emailMessage, minutes: 0 });
}

// console.log(`${usersToNotifyExpired.length} users notified for password expiration.`);


} catch (error) {
console.error("Error checking password expiration:", error);
Expand Down
2 changes: 1 addition & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ describe("checkPasswordExpiration middleware", () => {
expect(sendEmailStub).to.have.been.calledOnceWith(
"[email protected]",
"Password Expired - Reset Required",
`Your password has expired. Please reset your password using the following link: ${process.env.SERVER_URL_PRO}/api/auth/forget-password`
`Your password has expired. Please reset your password using the following link: ${process.env.SERVER_URL_PRO}/api/auth/reset-password`
);
expect(res.status).to.have.been.calledWith(httpStatus.FORBIDDEN);
expect(res.json).to.have.been.calledWith({
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/passwordExpiryCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface ExtendedRequest extends Request {
}

const PASSWORD_EXPIRATION_MINUTES = Number(process.env.PASSWORD_EXPIRATION_MINUTES) || 90;
const PASSWORD_RESET_URL = `${process.env.SERVER_URL_PRO}/api/auth/forget-password`;
const PASSWORD_RESET_URL = `${process.env.SERVER_URL_PRO}/api/auth/reset-password`;

const addMinutes = (date: Date, minutes: number): Date => {
const result = new Date(date);
Expand Down
71 changes: 21 additions & 50 deletions src/modules/cart/test/cart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let token1: string = null;
const router = () => chai.request(app);
let cartId;
let cartId2;
let token2: string = null;
// let token2: string = null;
describe("Buyer Get Cart", () => {
afterEach(() => {
sinon.restore();
Expand Down Expand Up @@ -313,41 +313,12 @@ describe(" Cart Controller Tests ", () => {
status: sinon.stub().returnsThis(),
json: sinon.stub().returnsThis()
};
const carts = await db.Carts.findAll();
cartId2 = carts[1].id;
const product = await db.CartProducts.findOne({ where: { cartId: cartId2 } });
console.log(product)
productId = product.productId;

});

afterEach(() => {
sandbox.restore();
});
it("should login user", (done) => {
router()
.post("/api/auth/login")
.send({ email: "[email protected]", password: "Password@123" })
.end((error, response) => {
token2 = response.body.data.token;
done(error);
});
});
it("should update cart product if already exist", (done) => {
router()
.post("/api/cart/create-update-cart")
.set("authorization", `Bearer ${token2}`)
.send({ productId: productId, quantity: 3 })
.end((error, response) => {
expect(response).to.have.status(httpStatus.CREATED);
expect(response.body).to.be.a("object");
expect(response.body).to.have.property("status", httpStatus.CREATED);
expect(response.body).to.have.property("message", "Cart added successfully");
expect(response.body).to.have.property("data")
done(error);
})

});

it("should add product to existing cart if cart exists", async () => {
const mockCart = { id: "cart-id", userId: "user-id", status: "pending" };
Expand Down Expand Up @@ -1247,27 +1218,27 @@ describe("Payment Handlers", () => {
afterEach(() => {
});

it("should handle payment success", (done) => {
router()
.get("/api/cart/payment-success")
.set("authorization", `Bearer ${token2}`)
.end((error, response) => {
expect(response.status).to.equal(httpStatus.OK);
expect(response.body).to.deep.equal({ status: httpStatus.OK, message: 'Payment successful!' });
done(error)
});
})
// it("should handle payment success", (done) => {
// router()
// .get("/api/cart/payment-success")
// .set("authorization", `Bearer ${token2}`)
// .end((error, response) => {
// expect(response.status).to.equal(httpStatus.OK);
// expect(response.body).to.deep.equal({ status: httpStatus.OK, message: 'Payment successful!' });
// done(error)
// });
// })

it("should handle payment cancellation", (done) => {
router()
.get("/api/cart/payment-canceled")
.set("authorization", `Bearer ${token2}`)
.end((error, response) => {
expect(response.status).to.equal(httpStatus.OK);
expect(response.body).to.deep.equal({ status: httpStatus.OK, message: 'Payment canceled' });
done(error)
});
});
// it("should handle payment cancellation", (done) => {
// router()
// .get("/api/cart/payment-canceled")
// .set("authorization", `Bearer ${token2}`)
// .end((error, response) => {
// expect(response.status).to.equal(httpStatus.OK);
// expect(response.body).to.deep.equal({ status: httpStatus.OK, message: 'Payment canceled' });
// done(error)
// });
// });
});

describe("isOrderExist Middleware", () => {
Expand Down
8 changes: 7 additions & 1 deletion src/services/googleAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ const authenticateWithGoogle = (req: Request, res: Response, next: NextFunction)
const email = user.email;
try {
const register = await authRepositories.findUserByAttributes("email", email);

if (register) {
if(register.dataValues.isGoogleAccount){
const token = generateToken(register.id);
const sessions = {
userId: register.id,
Expand All @@ -96,6 +98,10 @@ const authenticateWithGoogle = (req: Request, res: Response, next: NextFunction)
};
await authRepositories.createSession(sessions);
res.status(httpStatus.OK).json({status:httpStatus.OK, message: "Logged in successfully", data: { token } });
}
else{
res.status(httpStatus.BAD_REQUEST).json({status:httpStatus.BAD_REQUEST, message: "this is not google account, please login with Normal Account"});
}
} else {
const newUser = await authRepositories.createUser({
email: user.email,
Expand All @@ -115,7 +121,7 @@ const authenticateWithGoogle = (req: Request, res: Response, next: NextFunction)
otp: null
};
await authRepositories.createSession(session);
res.status(httpStatus.OK).json({status:httpStatus.OK, message: "Logged in successfully", data: { token } });
res.status(httpStatus.OK).json({status:httpStatus.OK, message: "Logged in successfully", data: { token} });
}
} catch (error) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({status:httpStatus.INTERNAL_SERVER_ERROR, message: error.message });
Expand Down

0 comments on commit 4d3beb2

Please sign in to comment.