Skip to content
Draft
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
47 changes: 44 additions & 3 deletions backend/__tests__/__integration__/dal/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,8 @@ describe("UserDal", () => {
const uid = new ObjectId().toHexString();
await UserDAL.addUser("test name", "test email", uid);

await UserDAL.updateChallenge(uid, "69");

await UserDAL.updateProfile(
uid,
{
Expand Down Expand Up @@ -793,6 +795,7 @@ describe("UserDal", () => {
lastResultTimestamp: 0,
maxLength: 0,
});
expect(resetUser.challenges).toStrictEqual({});
});

it("getInbox should return the user's inbox", async () => {
Expand Down Expand Up @@ -1271,22 +1274,26 @@ describe("UserDal", () => {
describe("linkDiscord", () => {
it("throws for nonexisting user", async () => {
await expect(async () =>
UserDAL.linkDiscord("unknown", "", ""),
UserDAL.linkDiscord("unknown", "", "", {}),
).rejects.toThrow("User not found\nStack: link discord");
});
it("should update", async () => {
//given
const { uid } = await UserTestData.createUser({
discordId: "discordId",
discordAvatar: "discordAvatar",
challenges: {
"100hours": {},
},
});
//when
await UserDAL.linkDiscord(uid, "newId", "newAvatar");
await UserDAL.linkDiscord(uid, "newId", "newAvatar", { "250hours": {} });

//then
const read = await UserDAL.getUser(uid, "read");
expect(read.discordId).toEqual("newId");
expect(read.discordAvatar).toEqual("newAvatar");
expect(read.challenges).toEqual({ "250hours": {} });
});
it("should update without avatar", async () => {
//given
Expand All @@ -1312,9 +1319,13 @@ describe("UserDal", () => {
});
it("should update", async () => {
//given
const { uid } = await UserTestData.createUser({
const { uid, challenges } = await UserTestData.createUser({
discordId: "discordId",
discordAvatar: "discordAvatar",
challenges: {
"100hours": {},
"250hours": { addedAt: Date.now() },
},
});

//when
Expand All @@ -1324,6 +1335,36 @@ describe("UserDal", () => {
const read = await UserDAL.getUser(uid, "read");
expect(read.discordId).toBeUndefined();
expect(read.discordAvatar).toBeUndefined();
expect(read.challenges).toEqual(challenges);
});
});

describe("updateChallenge", () => {
it("throws for nonexisting user", async () => {
await expect(async () =>
UserDAL.updateChallenge("unknown", "69"),
).rejects.toThrow("User not found\nStack: update challenge");
});
it("should update", async () => {
//given
vi.useFakeTimers();
const { uid } = await UserTestData.createUser({
challenges: {
"100hours": {},
"250hours": { addedAt: 1 },
},
});

//when
await UserDAL.updateChallenge(uid, "69");

//then
const read = await UserDAL.getUser(uid, "read");
expect(read.challenges).toEqual({
"100hours": {},
"250hours": { addedAt: 1 },
"69": { addedAt: Date.now() },
});
});
});
describe("updateInbox", () => {
Expand Down
80 changes: 80 additions & 0 deletions backend/__tests__/api/controllers/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as ResultDal from "../../../src/dal/result";
import * as UserDal from "../../../src/dal/user";
import * as LogsDal from "../../../src/dal/logs";
import * as PublicDal from "../../../src/dal/public";
import * as GeorgeQueue from "../../../src/queues/george-queue";
import { ObjectId } from "mongodb";
import { mockAuthenticateWithApeKey } from "../../__testData__/auth";
import { enableRateLimitExpects } from "../../__testData__/rate-limit";
Expand Down Expand Up @@ -583,6 +584,11 @@ describe("result controller test", () => {
const userCheckIfPbMock = vi.spyOn(UserDal, "checkIfPb");
const userIncrementXpMock = vi.spyOn(UserDal, "incrementXp");
const userUpdateTypingStatsMock = vi.spyOn(UserDal, "updateTypingStats");
const userUpdateChallengeMock = vi.spyOn(UserDal, "updateChallenge");
const georgeAwardChallengeMock = vi.spyOn(
GeorgeQueue.default,
"awardChallenge",
);
const resultAddMock = vi.spyOn(ResultDal, "addResult");
const publicUpdateStatsMock = vi.spyOn(PublicDal, "updateStats");

Expand All @@ -597,6 +603,8 @@ describe("result controller test", () => {
userCheckIfPbMock,
userIncrementXpMock,
userUpdateTypingStatsMock,
userUpdateChallengeMock,
georgeAwardChallengeMock,
resultAddMock,
publicUpdateStatsMock,
].forEach((it) => it.mockClear());
Expand All @@ -605,6 +613,8 @@ describe("result controller test", () => {
userUpdateStreakMock.mockResolvedValue(0);
userCheckIfTagPbMock.mockResolvedValue([]);
userCheckIfPbMock.mockResolvedValue(true);
userUpdateChallengeMock.mockResolvedValue();
georgeAwardChallengeMock.mockResolvedValue();
resultAddMock.mockResolvedValue({ insertedId });
userIncrementXpMock.mockResolvedValue();
});
Expand Down Expand Up @@ -687,6 +697,76 @@ describe("result controller test", () => {
15.1 + 2 - 5, //duration + incompleteTestSeconds-afk
);
});

it("should add result with challenge", async () => {
//GIVEN
userGetMock.mockClear();
userGetMock.mockResolvedValue({
uid,
name: "bob",
discordId: "discordId",
} as any);

const completedEvent = buildCompletedEvent({
challenge: "69",
});
//WHEN
await mockApp
.post("/results")
.set("Authorization", `Bearer ${uid}`)
.send({
result: completedEvent,
})
.expect(200);

//THEN
expect(userUpdateChallengeMock).toHaveBeenCalledWith(uid, "69");
expect(georgeAwardChallengeMock).toHaveBeenCalledWith("discordId", "69");
});

it("should not add challenge if not auto-role", async () => {
//GIVEN
userGetMock.mockClear();
userGetMock.mockResolvedValue({
uid,
name: "bob",
discordId: "discordId",
} as any);

const completedEvent = buildCompletedEvent({
challenge: "roleAddict",
});
//WHEN
await mockApp
.post("/results")
.set("Authorization", `Bearer ${uid}`)
.send({
result: completedEvent,
})
.expect(200);

//THEN
expect(userUpdateChallengeMock).not.toHaveBeenCalled();
expect(georgeAwardChallengeMock).not.toHaveBeenCalled();
});
it("should dd challenge without discord connected", async () => {
const completedEvent = buildCompletedEvent({
challenge: "69",
});
//WHEN
await mockApp
.post("/results")
.set("Authorization", `Bearer ${uid}`)
.send({
result: completedEvent,
})
.expect(200);

//THEN
expect(userUpdateChallengeMock).toHaveBeenCalledWith(uid, "69");
expect(georgeAwardChallengeMock).not.toHaveBeenCalled();
});

it("should fail if result saving is disabled", async () => {
//GIVEN
await enableResultsSaving(false);
Expand Down
Loading
Loading