-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathorganizations.controller.spec.ts
More file actions
148 lines (126 loc) · 4.3 KB
/
Copy pathorganizations.controller.spec.ts
File metadata and controls
148 lines (126 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { NotFoundException } from "@nestjs/common";
import { ResourceStatus } from "@prisma/client";
import { Test, TestingModule } from "@nestjs/testing";
import { AuthTokenService } from "../auth/auth-token.service";
import { AuthGuard } from "../common/guards/auth.guard";
import { RoleGuard } from "../common/guards/role.guard";
import { OrganizationsController } from "./organizations.controller";
import { OrganizationsService } from "./organizations.service";
describe("OrganizationsController", () => {
let controller: OrganizationsController;
let service: OrganizationsService;
const mockUser = {
id: "user-1",
walletAddress: "G1111111111111111111111111111111111111111111111111111111",
walletHash: "sha256:hash1",
role: "ADMIN",
};
const mockOrganization = {
id: "org-1",
name: "Test Organization",
slug: "test-org",
website: "https://example.com",
status: ResourceStatus.PENDING,
createdById: mockUser.id,
createdAt: new Date("2026-01-01"),
updatedAt: new Date("2026-01-01"),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [OrganizationsController],
providers: [
{
provide: OrganizationsService,
useValue: {
createOrganization: jest.fn(),
updateOrganization: jest.fn(),
getOrganization: jest.fn(),
listOrganizations: jest.fn(),
},
},
{
provide: AuthTokenService,
useValue: {
verify: jest.fn(),
},
},
],
})
.overrideGuard(AuthGuard)
.useValue({ canActivate: jest.fn(() => true) })
.overrideGuard(RoleGuard)
.useValue({ canActivate: jest.fn(() => true) })
.compile();
controller = module.get<OrganizationsController>(OrganizationsController);
service = module.get<OrganizationsService>(OrganizationsService);
});
describe("createOrganization", () => {
it("should call service and return result", async () => {
const input = {
name: "Test Organization",
slug: "test-org",
website: "https://example.com",
};
jest
.spyOn(service, "createOrganization")
.mockResolvedValue(mockOrganization);
const result = await controller.createOrganization(mockUser, input);
expect(result).toEqual(mockOrganization);
expect(service.createOrganization).toHaveBeenCalledWith(mockUser, input);
});
});
describe("updateOrganization", () => {
it("should call service and return result", async () => {
const input = { name: "Updated Name" };
jest.spyOn(service, "updateOrganization").mockResolvedValue({
...mockOrganization,
name: input.name,
});
const result = await controller.updateOrganization(
mockUser,
"org-1",
input,
);
expect(result.name).toEqual(input.name);
expect(service.updateOrganization).toHaveBeenCalledWith(
mockUser,
"org-1",
input,
);
});
});
describe("getOrganization", () => {
it("should return organization with issuer count", async () => {
const response = { ...mockOrganization, issuerCount: 2 };
jest.spyOn(service, "getOrganization").mockResolvedValue(response);
const result = await controller.getOrganization(mockUser, "org-1");
expect(result).toEqual(response);
expect(service.getOrganization).toHaveBeenCalledWith(mockUser, "org-1");
});
it("should handle 404 from service", async () => {
jest
.spyOn(service, "getOrganization")
.mockRejectedValue(new NotFoundException());
await expect(
controller.getOrganization(mockUser, "nonexistent"),
).rejects.toThrow(NotFoundException);
});
});
describe("listOrganizations", () => {
it("should return paginated list", async () => {
const response = {
items: [mockOrganization],
total: 1,
page: 1,
limit: 20,
};
jest.spyOn(service, "listOrganizations").mockResolvedValue(response);
const result = await controller.listOrganizations(mockUser, {});
expect(result).toEqual(response);
expect(service.listOrganizations).toHaveBeenCalledWith(
mockUser,
expect.any(Object),
);
});
});
});