Skip to content

Commit

Permalink
feat(Querries):Admin querry management
Browse files Browse the repository at this point in the history
  • Loading branch information
YvetteNyibuka committed Jul 25, 2024
1 parent e2a9453 commit 5b8a815
Show file tree
Hide file tree
Showing 49 changed files with 1,290 additions and 260 deletions.
20 changes: 20 additions & 0 deletions src/__test__/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ describe("CART API TEST", () => {
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("added to cart successfully");
});

it("should return 201 and added to cart successfully again", async () => {
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
Expand Down Expand Up @@ -375,4 +376,23 @@ describe("CART API TEST", () => {
expect(body.status).toStrictEqual("ERROR");
expect(body.message).toStrictEqual("Internal Server Error");
});

it("should return 400 and fail to delete product due to foreign key constraint", async () => {
const mockError = new Error();
mockError.name = "SequelizeForeignKeyConstraintError";
jest
.spyOn(database_models.Product, "destroy")
.mockImplementationOnce(() => {
throw mockError;
});

const { body } = await Jest_request.delete(`/api/v1/products/${product_id}`)
.set("Authorization", `Bearer ${seller_token}`)
.expect(400);

expect(body.status).toStrictEqual("CASCADE DELETE REQUIRED");
expect(body.message).toStrictEqual(
"Cannot delete the product because it is linked to other records.",
);
});
});
11 changes: 6 additions & 5 deletions src/__test__/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ describe("ORDER API TEST", () => {

it("should return 201 and added to cart successfully", async () => {
await database_models.Product.update(
{ isAvailable, price: 5 },
{ isAvailable: true, price: 500 },
{ where: { id: product_id } },
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 1 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand All @@ -243,19 +243,20 @@ describe("ORDER API TEST", () => {

it("should return 201 and added to cart successfully", async () => {
await database_models.Product.update(
{ isAvailable, price: 499000 },
{ isAvailable: true, price: 499000 },
{ where: { id: product_id } },
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });

expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
it("should return 201 and added to cart successfully again", async () => {
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand Down
97 changes: 95 additions & 2 deletions src/__test__/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import {
search_product,
search_product_Not_found,
review_user,
new_buyer_user,
login_user,
review_user_login,
buyer_login,
} from "../mock/static";
import { generateAccessToken } from "../helpers/security.helpers";
import { read_function } from "../utils/db_methods";
import { response } from "express";
import searchProduct from "../controllers/searchProduct";
import { Console } from "node:console";

jest.setTimeout(100000);

Expand All @@ -35,6 +38,7 @@ function logErrors(

const Jest_request = request(app.use(logErrors));
let seller_token: string;
let buyer_token: string;
let category_id: string;
let product_id: any;
describe("PRODUCT API TEST", () => {
Expand All @@ -58,6 +62,38 @@ describe("PRODUCT API TEST", () => {
);
});

//buyer

it("should register a buyer and return 200", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(new_buyer_user)
.expect(201);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual(
"Account Created successfully, Please Verify your Account",
);

buyer_token = body.data;
});

it("should successfully login a user and return 200", async () => {
await database_models.User.update(
{ isVerified: true },
{
where: { email: buyer_login.email },
},
);

const { body } = await Jest_request.post("/api/v1/users/login")
.send(buyer_login)
.expect(200);

expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Login successfully!");

buyer_token = body.data;
});

it("should authenticate the user and return SUCCESS", async () => {
const user = await database_models.User.findOne();
await database_models.User.update(
Expand Down Expand Up @@ -184,11 +220,10 @@ describe("PRODUCT API TEST", () => {
expect(body.message).toStrictEqual("Product not found or not owned!");
});

it("should fetch single and return 200", async () => {
it("should fetch single product and return 200", async () => {
const { body } = await Jest_request.get(`/api/v1/products/${product_id}`)
.set("Authorization", `Bearer ${seller_token}`)
.expect(200);

expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Product fetched successfully!");
expect(body.data).toBeDefined();
Expand Down Expand Up @@ -242,6 +277,31 @@ describe("PRODUCT API TEST", () => {
expect(body.message).toStrictEqual("You provided Invalid ID!");
});

it("it should fetch all guest products", async () => {
const { body } = await Jest_request.get(
"/api/v1/products/guest-products",
).expect(200);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Products fetched successfully!");
expect(body.data).toBeDefined();
});

it("it should fetch guest single product and return 404 if not found", async () => {
const { body } = await Jest_request.get(
`/api/v1/products/guest-products/96ff9146-ad09-4dbc-b100-94d3b0c33562`,
).expect(404);
expect(body.status).toStrictEqual("NOT FOUND");
expect(body.message).toStrictEqual("Product not found or not available!");
});

it("it should return 400 to the guest when product is invalid", async () => {
const { body } = await Jest_request.get(
`/api/v1/products/guest-products/97dcfe1e-0686-4876-808a-f3d3ec36c`,
).expect(400);
expect(body.status).toStrictEqual("BAD REQUEST");
expect(body.message).toStrictEqual("You provided Invalid ID!");
});

it("should update product and return 200", async () => {
const { body } = await Jest_request.patch(`/api/v1/products/${product_id}`)
.set("Authorization", `Bearer ${seller_token}`)
Expand Down Expand Up @@ -294,6 +354,39 @@ describe("PRODUCT API TEST", () => {
expect(body.data).toBeDefined();
});

//recommended

it("seller should get recommended product and return 200", async () => {
const { body } = await Jest_request.post(`/api/v1/products/recommended`)
.send({ id: `${product_id}` })
.set("Authorization", `Bearer ${seller_token}`)
.expect(200);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual(
"Recommended Products fetched successfully!",
);
});

it("buyer should get recommended product and return 200", async () => {
const { body } = await Jest_request.post(`/api/v1/products/recommended`)
.send({ id: `${product_id}` })
.set("Authorization", `Bearer ${buyer_token}`)
.expect(200);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual(
"Recommended Products fetched successfully!",
);
});

it("should return 404 when product is not found to get recommended product", async () => {
const { body } = await Jest_request.post(`/api/v1/products/recommended`)
.send({ id: "97dcfe1e-0686-4876-808a-f3d3ec36c7ff" })
.set("Authorization", `Bearer ${seller_token}`)
.expect(404);
expect(body.status).toStrictEqual("NOT FOUND");
expect(body.message).toStrictEqual("Product not found or not owned!");
});

it("it should return searched product with name only", async () => {
const { body } = await Jest_request.get("/api/v1/products/search?")
.query({
Expand Down
105 changes: 105 additions & 0 deletions src/__test__/querries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import app from "../app";
import request from "supertest";
import database_models, {
connectionToDatabase,
} from "../database/config/db.config";
import { deleteTableData } from "../utils/database.utils";
import { NewUser } from "../mock/static";
import { v4 as uuidV4 } from "uuid";
import { Token } from "../database/models/token";

jest.setTimeout(30000);

function logErrors(
err: { stack: any },
_req: any,
_res: any,
next: (arg0: any) => void,
) {
console.log(err.stack);
next(err);
}

const Jest_request = request(app.use(logErrors));

let token = "";
let querryId: string;

describe("NOTIFICATIONS TEST", () => {
beforeAll(async () => {
await connectionToDatabase();
});
afterAll(async () => {
await deleteTableData(database_models.User, "users");
await deleteTableData(database_models.Querries, "querries");
});
it("it should register a user and return 201", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(NewUser)
.expect(201);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual(
"Account Created successfully, Please Verify your Account",
);

const tokenRecord = await Token.findOne();
token = tokenRecord?.dataValues.token ?? "";
});

it("should successfully login a user and return 200", async () => {
const loginUser = {
email: NewUser.email,
password: NewUser.password,
};

await database_models.User.update(
{ isVerified: true, role: "12afd4f1-0bed-4a3b-8ad5-0978dabf8fcd" },
{ where: { email: loginUser.email } },
);

const { body } = await Jest_request.post("/api/v1/users/login")
.send(loginUser)
.expect(200);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Login successfully!");
expect(body.data).toBeDefined();
token = body.data;
});

// =================================QUERRIES TESTS================================

it("It should create querry when customer want to communicate to app owner", async () => {
const newQuerry = {
firstName: "Izanyibuka",
lastName: "Yvette",
subject: "Biiter",
email: "[email protected]",
message: "I hated this app",
};
const { body } = await Jest_request.post("/api/v1/querries")
.send(newQuerry)
.expect(201);
expect(body.status).toStrictEqual("CREATED");
querryId = body.data.id;
});

it("It should return all querry", async () => {
const { body } = await Jest_request.get("/api/v1/querries")
.set("Authorization", `Bearer ${token}`)
.expect(200);
expect(body.status).toStrictEqual("SUCCESS");
});
it("It should return a single querry", async () => {
const { body } = await Jest_request.get(`/api/v1/querries/${querryId}`)
.set("Authorization", `Bearer ${token}`)
.expect(200);
expect(body.status).toStrictEqual("SUCCESS");
});

it("it shouldreturn 404 if querry not found", async () => {
const { body } = await Jest_request.get(`/api/v1/querries/${uuidV4()}`)
.set("Authorization", `Bearer ${token}`)
.expect(404);
expect(body.status).toStrictEqual("NOT FOUND");
});
});
4 changes: 2 additions & 2 deletions src/__test__/review.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ describe("REVIEW API TEST", () => {
expect(body.message).toStrictEqual("review added successfully!");
});

it("it should return all reviews on product and return 201", async () => {
it("it should return all reviews on product and return 200", async () => {
const { body } = await Jest_request.get(
`/api/v1/products/${product_id}/reviews/`,
).expect(201);
).expect(200);
expect(body.message).toStrictEqual("review retrieved successfully!");
});

Expand Down
8 changes: 4 additions & 4 deletions src/__test__/sales.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ describe("SALE API TEST", () => {

it("should return 201 and added to cart successfully", async () => {
await database_models.Product.update(
{ isAvailable, price: 5 },
{ isAvailable, price: 500 },
{ where: { id: product_id } },
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 1 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand All @@ -257,14 +257,14 @@ describe("SALE API TEST", () => {
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
it("should return 201 and added to cart successfully again", async () => {
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand Down
Loading

0 comments on commit 5b8a815

Please sign in to comment.