Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
16 changes: 8 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Test CI

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
test:
Expand All @@ -30,18 +30,18 @@ jobs:
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v
uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'
node-version: "18"
cache: "npm"

- name: Install dependencies
run: npm ci

- name: Build Docker image
run: |
docker build -t volunchain-backend .
docker run -d --name volunchain_test --link postgres:db -e DATABASE_URL=postgresql://volunchain:volunchain123@db:5432/volunchain_test volunchain-backend
docker build -t volunchain-backend .
docker run -d --name volunchain_test --link postgres:db -e DATABASE_URL=postgresql://volunchain:volunchain123@db:5432/volunchain_test volunchain-backend

- name: Generate Prisma Client
run: npx prisma generate
Expand Down Expand Up @@ -72,4 +72,4 @@ jobs:
if: always()
run: |
docker stop volunchain_test
docker rm volunchain_test
docker rm volunchain_test
Binary file removed src/.DS_Store
Binary file not shown.
Binary file removed src/modules/.DS_Store
Binary file not shown.
22 changes: 22 additions & 0 deletions src/modules/auth/__tests__/controllers/AuthController.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Request, Response } from "express";
// Integration test for AuthController
import AuthController from "../../presentation/controllers/Auth.controller";

describe("AuthController Integration", () => {
it("should have a register method", () => {
expect(typeof AuthController.register).toBe("function");
});

test("should return error if required fields are missing on register", async () => {
const req = { body: {} } as Partial<Request>;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as Partial<Response>;
await AuthController.register(req as Request, res as Response);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith(
expect.objectContaining({ message: expect.any(String) })
);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from "express";
import AuthService from "../services/AuthService";
import { AuthenticatedRequest } from "../types/auth.types";
import AuthService from "../../../../services/AuthService";
import { AuthenticatedRequest } from "../../../../types/auth.types";

class AuthController {
private authService: AuthService;
Expand All @@ -22,12 +22,9 @@ class AuthController {
);
res.status(201).json(response);
} catch (error) {
res
.status(400)
.json({
message:
error instanceof Error ? error.message : "Registration failed",
});
res.status(400).json({
message: error instanceof Error ? error.message : "Registration failed",
});
}
};

Expand All @@ -48,12 +45,9 @@ class AuthController {
const response = await this.authService.verifyEmail(token);
res.json(response);
} catch (error) {
res
.status(400)
.json({
message:
error instanceof Error ? error.message : "Verification failed",
});
res.status(400).json({
message: error instanceof Error ? error.message : "Verification failed",
});
}
};

Expand All @@ -72,12 +66,10 @@ class AuthController {
const response = await this.authService.resendVerificationEmail(email);
res.json(response);
} catch (error) {
res
.status(400)
.json({
message:
error instanceof Error ? error.message : "Could not resend email",
});
res.status(400).json({
message:
error instanceof Error ? error.message : "Could not resend email",
});
}
};

Expand All @@ -88,11 +80,9 @@ class AuthController {
const token = await this.authService.authenticate(walletAddress);
res.json({ token });
} catch (error) {
res
.status(401)
.json({
message: error instanceof Error ? error.message : "Unknown error",
});
res.status(401).json({
message: error instanceof Error ? error.message : "Unknown error",
});
}
};

Expand All @@ -111,14 +101,12 @@ class AuthController {
);
res.json(status);
} catch (error) {
res
.status(400)
.json({
message:
error instanceof Error
? error.message
: "Could not check verification status",
});
res.status(400).json({
message:
error instanceof Error
? error.message
: "Could not check verification status",
});
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Integration test for certificate.controller
import { Request, Response } from "express";
import * as CertificateController from "../../presentation/controllers/certificate.controller";

describe("CertificateController Integration", () => {
it("should have a downloadCertificate function", () => {
expect(typeof CertificateController.downloadCertificate).toBe("function");
});

test("should return error if volunteer does not exist on downloadCertificate", async () => {
const req = {
params: { id: "nonexistent" },
user: {
id: "user1",
email: "[email protected]",
role: "volunteer",
isVerified: true,
},
query: {},
} as Partial<Request>;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as Partial<Response>;
await CertificateController.downloadCertificate(
req as Request,
res as Response
);
expect(res.status).toHaveBeenCalledWith(403);
expect(res.json).toHaveBeenCalledWith(
expect.objectContaining({ error: expect.any(String) })
);
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Request, Response } from "express";
import { container } from "../shared/infrastructure/container";
import { prisma } from "../config/prisma";
import { ICertificateService } from "../shared/domain/interfaces/ICertificateService";
import { AuthenticatedRequest } from "../types/auth.types";
import { container } from "../../../../shared/infrastructure/container";
import { prisma } from "../../../../config/prisma";
import { ICertificateService } from "../../../../shared/domain/interfaces/ICertificateService";
import { AuthenticatedRequest } from "../../../../types/auth.types";
import { Response } from "express";

const certificateService: ICertificateService = container.certificateService;

Expand Down
21 changes: 21 additions & 0 deletions src/modules/nft/__tests__/controllers/NFTController.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Request, Response } from "express";
import NFTController from "../../presentation/controllers/NFTController";

describe("NFTController Integration", () => {
it("should have a createNFT method", () => {
expect(typeof NFTController.createNFT).toBe("function");
});

test("should return error if createNFT is called with empty body", async () => {
const req = { body: {} } as Partial<Request>;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
await NFTController.createNFT(req as Request, res as Response);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith(
expect.objectContaining({ error: expect.any(String) })
);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import NFTService from "../services/NFTService";
import NFTService from "../../../../services/NFTService";

class NFTController {
// Creates_a_new_NFT_and_returns_the_created_NFT_data_OKK!!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Integration test for OrganizationController
import OrganizationController from "../../presentation/controllers/OrganizationController";
import request from "supertest";
import express, { Express } from "express";

// Mock the OrganizationService
jest.mock("../../../../services/OrganizationService");
import { OrganizationService } from "../../../../services/OrganizationService";

// Mock asyncHandler to just return the function (for test simplicity)
jest.mock("../../../../utils/asyncHandler", () => ({
asyncHandler: <T extends (...args: unknown[]) => unknown>(fn: T) => fn,
}));

function setupApp(): Express {
const app = express();
app.use(express.json());
// OrganizationController is a singleton instance
app.post("/organizations", OrganizationController.createOrganization);
app.get("/organizations/:id", OrganizationController.getOrganizationById);
app.get(
"/organizations/email/:email",
OrganizationController.getOrganizationByEmail
);
app.put("/organizations/:id", OrganizationController.updateOrganization);
app.delete("/organizations/:id", OrganizationController.deleteOrganization);
app.get("/organizations", OrganizationController.getAllOrganizations);
return app;
}

describe("OrganizationController", () => {
let app: Express;

beforeEach(() => {
app = setupApp();
jest.clearAllMocks();
});

describe("POST /organizations", () => {
it("should create an organization and return 201", async () => {
const mockOrg = { id: "1", name: "Org" };
(OrganizationService as jest.Mock).mockImplementation(() => ({
createOrganization: jest.fn().mockResolvedValue(mockOrg),
}));
const res = await request(app).post("/organizations").send({
name: "Org",
email: "[email protected]",
password: "pass",
category: "cat",
wallet: "wallet",
});
expect(res.status).toBe(201);
expect(res.body).toEqual(mockOrg);
});
});

describe("GET /organizations/:id", () => {
it("should return an organization by id", async () => {
const mockOrg = { id: "1", name: "Org" };
(OrganizationService as jest.Mock).mockImplementation(() => ({
getOrganizationById: jest.fn().mockResolvedValue(mockOrg),
}));
const res = await request(app).get("/organizations/1");
expect(res.status).toBe(200);
expect(res.body).toEqual(mockOrg);
});
it("should return 404 if not found", async () => {
(OrganizationService as jest.Mock).mockImplementation(() => ({
getOrganizationById: jest.fn().mockResolvedValue(null),
}));
const res = await request(app).get("/organizations/1");
expect(res.status).toBe(404);
expect(res.body.error).toBe("Organization not found");
});
});

describe("GET /organizations/email/:email", () => {
it("should return an organization by email", async () => {
const mockOrg = { id: "1", name: "Org" };
(OrganizationService as jest.Mock).mockImplementation(() => ({
getOrganizationByEmail: jest.fn().mockResolvedValue(mockOrg),
}));
const res = await request(app).get("/organizations/email/[email protected]");
expect(res.status).toBe(200);
expect(res.body).toEqual(mockOrg);
});
it("should return 404 if not found", async () => {
(OrganizationService as jest.Mock).mockImplementation(() => ({
getOrganizationByEmail: jest.fn().mockResolvedValue(null),
}));
const res = await request(app).get("/organizations/email/[email protected]");
expect(res.status).toBe(404);
expect(res.body.error).toBe("Organization not found");
});
});

describe("PUT /organizations/:id", () => {
it("should update an organization and return 200", async () => {
const mockOrg = { id: "1", name: "Updated Org" };
(OrganizationService as jest.Mock).mockImplementation(() => ({
updateOrganization: jest.fn().mockResolvedValue(mockOrg),
}));
const res = await request(app)
.put("/organizations/1")
.send({ name: "Updated Org" });
expect(res.status).toBe(200);
expect(res.body).toEqual(mockOrg);
});
});

describe("DELETE /organizations/:id", () => {
it("should delete an organization and return 204", async () => {
(OrganizationService as jest.Mock).mockImplementation(() => ({
deleteOrganization: jest.fn().mockResolvedValue(undefined),
}));
const res = await request(app).delete("/organizations/1");
expect(res.status).toBe(204);
});
});

describe("GET /organizations", () => {
it("should return all organizations", async () => {
const mockOrgs = [{ id: "1" }, { id: "2" }];
(OrganizationService as jest.Mock).mockImplementation(() => ({
getAllOrganizations: jest.fn().mockResolvedValue(mockOrgs),
}));
const res = await request(app).get("/organizations");
expect(res.status).toBe(200);
expect(res.body).toEqual(mockOrgs);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from "express";
import { OrganizationService } from "../services/OrganizationService";
import { asyncHandler } from "../utils/asyncHandler";
import { OrganizationService } from "../../../../services/OrganizationService";
import { asyncHandler } from "../../../../utils/asyncHandler";

class OrganizationController {
private organizationService: OrganizationService;
Expand Down
Loading
Loading