Skip to content

Commit e00f3ef

Browse files
committed
Fix backend tests
1 parent 4f58040 commit e00f3ef

5 files changed

Lines changed: 32 additions & 35 deletions

File tree

backend/src/services/rating-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class RatingService implements IRatingService {
108108
}
109109
});
110110

111-
if (existingRating !== null) {
111+
if (existingRating) {
112112
// Update
113113
return this._ratings.save({
114114
...rating,

backend/test/controllers/rating-controller.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ describe("RatingController", () => {
3737
const createdRating = new Rating();
3838
(createdRating as any).id = 1;
3939

40-
ratingService.mocks.createRating.mockResolvedValue(createdRating);
40+
ratingService.mocks.upsertRating.mockResolvedValue(createdRating);
4141

42-
const result = await controller.createRating({ data: ratingDTO }, user);
42+
const result = await controller.rate({ data: ratingDTO }, user);
4343

44-
expect(ratingService.mocks.createRating).toBeCalled();
44+
expect(ratingService.mocks.upsertRating).toBeCalled();
4545
expect(result).toBeDefined();
4646
});
4747

@@ -113,7 +113,7 @@ describe("RatingController", () => {
113113
it("allows requests from User-role users", async () => {
114114
expect.assertions(2);
115115

116-
ratingService.mocks.createRating.mockResolvedValue({} as any);
116+
ratingService.mocks.upsertRating.mockResolvedValue({} as any);
117117

118118
const response = await fetch(`http://localhost:${port}/api/ratings/rate`, {
119119
method: "POST",
@@ -125,7 +125,7 @@ describe("RatingController", () => {
125125
});
126126

127127
expect(response.status).toBe(200);
128-
expect(ratingService.mocks.createRating).toHaveBeenCalledWith(
128+
expect(ratingService.mocks.upsertRating).toHaveBeenCalledWith(
129129
expect.objectContaining({
130130
project: expect.objectContaining({ id: 1 }),
131131
user: expect.objectContaining({ id: regularUser.id }),
@@ -138,7 +138,7 @@ describe("RatingController", () => {
138138
it("passes authorization for admin (Root) users", async () => {
139139
expect.assertions(1);
140140

141-
ratingService.mocks.createRating.mockResolvedValue({} as any);
141+
ratingService.mocks.upsertRating.mockResolvedValue({} as any);
142142

143143
const response = await fetch(`http://localhost:${port}/api/ratings/rate`, {
144144
method: "POST",

backend/test/services/mock/mock-rating-service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ export const MockRatingService = jest.fn(
88
() =>
99
new MockedService<IRatingService>({
1010
bootstrap: jest.fn(),
11-
getAllRatings: jest.fn(),
12-
createRating: jest.fn(),
13-
updateRating: jest.fn(),
11+
getUsersRatingsForProject: jest.fn(),
12+
upsertRating: jest.fn(),
1413
getRatingByID: jest.fn(),
1514
deleteRatingByID: jest.fn(),
1615
getRatingResults: jest.fn(),

backend/test/services/rating-service.spec.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ describe("RatingService", () => {
6666
});
6767

6868
describe("checkPermission", () => {
69-
describe("via createRating", () => {
69+
describe("via upsertRating", () => {
7070
it("throws ForbiddenError when rating is globally disabled", async () => {
7171
expect.assertions(1);
7272

7373
settingsService.mocks.getSettings.mockResolvedValue(
7474
{ application: { allowRatingProjects: false } } as any
7575
);
7676

77-
await expect(ratingService.createRating(mockRating, mockUser)).rejects.toThrow(
77+
await expect(ratingService.upsertRating(mockRating, mockUser)).rejects.toThrow(
7878
ForbiddenError,
7979
);
8080
});
@@ -88,7 +88,7 @@ describe("RatingService", () => {
8888

8989
mockProjectsRepo.findOneBy.mockResolvedValue(null);
9090

91-
await expect(ratingService.createRating(mockRating, mockUser)).rejects.toThrow(
91+
await expect(ratingService.upsertRating(mockRating, mockUser)).rejects.toThrow(
9292
NotFoundError,
9393
);
9494
});
@@ -104,7 +104,15 @@ describe("RatingService", () => {
104104
Object.assign(new Project(), { ...mockProject, allowRating: false }),
105105
);
106106

107-
await expect(ratingService.createRating(mockRating, mockUser)).rejects.toThrow(
107+
// The backend should not be tricked by an allowRating: true in the payload
108+
const payload = {
109+
...mockRating,
110+
project: {
111+
...mockRating.project,
112+
allowRating: true
113+
}
114+
}
115+
await expect(ratingService.upsertRating(payload, mockUser)).rejects.toThrow(
108116
ForbiddenError,
109117
);
110118
});
@@ -119,7 +127,7 @@ describe("RatingService", () => {
119127
mockProjectsRepo.findOneBy.mockResolvedValue(mockProject);
120128
mockTeamsRepo.findOneBy.mockResolvedValue(null);
121129

122-
await expect(ratingService.createRating(mockRating, mockUser)).rejects.toThrow(
130+
await expect(ratingService.upsertRating(mockRating, mockUser)).rejects.toThrow(
123131
NotFoundError,
124132
);
125133
});
@@ -136,7 +144,7 @@ describe("RatingService", () => {
136144
Object.assign(new Team(), { ...mockTeam, users: ["1", "2", "3"] }),
137145
);
138146

139-
await expect(ratingService.createRating(mockRating, mockUser)).rejects.toThrow(
147+
await expect(ratingService.upsertRating(mockRating, mockUser)).rejects.toThrow(
140148
ForbiddenError,
141149
);
142150
});
@@ -154,28 +162,12 @@ describe("RatingService", () => {
154162
const savedRating = Object.assign(new Rating(), { ...mockRating, id: 42 });
155163
mockRatingsRepo.save.mockResolvedValue(savedRating);
156164

157-
const result = await ratingService.createRating(mockRating, mockUser);
165+
const result = await ratingService.upsertRating(mockRating, mockUser);
158166

159167
expect(result).toBe(savedRating);
160168
expect(mockRatingsRepo.save).toHaveBeenCalledWith(mockRating);
161169
});
162170

163-
it("throws BadRequestError when user has already rated the same project and criteria", async () => {
164-
expect.assertions(1);
165-
166-
settingsService.mocks.getSettings.mockResolvedValue(
167-
{ application: { allowRatingProjects: true } } as any
168-
);
169-
170-
mockProjectsRepo.findOneBy.mockResolvedValue(mockProject);
171-
mockTeamsRepo.findOneBy.mockResolvedValue(mockTeam);
172-
mockRatingsRepo.findOne.mockResolvedValue(mockRating);
173-
174-
await expect(ratingService.createRating(mockRating, mockUser)).rejects.toThrow(
175-
BadRequestError,
176-
);
177-
});
178-
179171
it("is forbidden to impersonate other users", async () => {
180172
expect.assertions(1);
181173

@@ -192,7 +184,7 @@ describe("RatingService", () => {
192184
id: 1234
193185
};
194186

195-
await expect(ratingService.createRating(mockRating, mockUser)).rejects.toThrow(
187+
await expect(ratingService.upsertRating(mockRating, mockUser)).rejects.toThrow(
196188
ForbiddenError,
197189
);
198190
});

frontend/src/components/pages/rating-form.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ export const RatingForm = ({
2525
const loginState = useLoginContext();
2626
const { user } = loginState;
2727

28-
const [ratingValue, setRatingValue] = useState(rating.rating);
28+
const [ratingValue, setRatingValue] = useState(rating?.rating);
2929
const [isSubmitting, setIsSubmitting] = useState(false);
3030
const [error, setError] = useState<string | null>(null);
3131

32+
React.useEffect(() => {
33+
if (rating) {
34+
setRatingValue(rating.rating);
35+
}
36+
}, [rating]);
37+
3238
const handleSubmit = async () => {
3339
setIsSubmitting(true);
3440
setError(null);

0 commit comments

Comments
 (0)