Skip to content

Commit

Permalink
removed unnecessary tests & added /profile/leaderboard tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsharma1 committed Oct 22, 2023
1 parent 21565ff commit 11a4139
Showing 1 changed file with 101 additions and 25 deletions.
126 changes: 101 additions & 25 deletions src/services/profile/profile-router.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, expect, it, beforeEach } from "@jest/globals";
import Models from "../../database/models.js";
import { TESTER, del, delAsUser, get, getAsAdmin, getAsUser, post, postAsAttendee, postAsUser } from "../../testTools.js";
import { TESTER, delAsUser, getAsAdmin, getAsUser, postAsAttendee, postAsUser } from "../../testTools.js";
import { ProfileFormat } from "./profile-formats.js";
import Constants from "../../constants.js";

beforeEach(async () => {
Models.initialize();
Expand All @@ -16,12 +17,6 @@ const profile: ProfileFormat = {
};

describe("POST /profile", () => {
it("posts with an unauthorized user", async () => {
const response = await post("/profile/").send(profile).expect(401);

expect(JSON.parse(response.text)).toHaveProperty("error", "NoToken");
});

it("posts for an attendee", async () => {
const response = await postAsAttendee("/profile/").send(profile).expect(200);

Expand All @@ -37,15 +32,21 @@ describe("POST /profile", () => {
const response2 = await postAsUser("/profile/").send(profile).expect(400);
expect(JSON.parse(response2.text)).toHaveProperty("error", "UserAlreadyExists");
});
});

describe("GET /profile", () => {
it("gets with an invalid attendee", async () => {
const response = await get("/profile").expect(401);
it("posts with invalid data", async () => {
const response = await postAsUser("/profile/")
.send({
displayName: 123,
avatarUrl: 1,
discordTag: "test",
})
.expect(400);

expect(JSON.parse(response.text)).toHaveProperty("error", "NoToken");
expect(JSON.parse(response.text)).toHaveProperty("error", "InvalidParams");
});
});

describe("GET /profile", () => {
it("gets with an attendee that doesn't exist", async () => {
const response = await getAsUser("/profile/").expect(404);

Expand All @@ -67,18 +68,16 @@ describe("GET /profile", () => {
});

describe("GET /profile/id/:USERID", () => {
it("gets with an invalid user", async () => {
it("gets without a :USERID", async () => {
await Models.AttendeeProfile.create({
userId: "abcdefgabcdefgaaa",
displayName: "tst",
userId: TESTER.id,
displayName: TESTER.name,
avatarUrl: TESTER.avatarUrl,
discordTag: TESTER.discordTag,
points: 0,
});

const response = await get("/profile/id/abcdefgabcdefgaaa").expect(401);

expect(JSON.parse(response.text)).toHaveProperty("error", "NoToken");
await getAsUser("/profile/id").expect(302);
});

it("gets with a user", async () => {
Expand Down Expand Up @@ -125,17 +124,12 @@ describe("GET /profile/id/:USERID", () => {
});

describe("DELETE /profile/", () => {
it("delete with an invalid user", async () => {
const response = await del("/profile").expect(401);
expect(JSON.parse(response.text)).toHaveProperty("error", "NoToken");
});

it("delete with a user that doesn't exist", async () => {
it("deletes with a attendee that doesn't exist", async () => {
const response = await delAsUser("/profile").expect(404);
expect(JSON.parse(response.text)).toHaveProperty("error", "AttendeeNotFound");
});

it("delete with a valid user", async () => {
it("deletes with a valid user", async () => {
await Models.AttendeeProfile.create({
userId: TESTER.id,
displayName: TESTER.name,
Expand All @@ -153,3 +147,85 @@ describe("DELETE /profile/", () => {
expect(JSON.parse(response.text)).toHaveProperty("success", true);
});
});

describe("GET /profile/leaderboard", () => {
it("gets with no limit but only 3 entries", async () => {
await Models.AttendeeProfile.create({
userId: TESTER.id,
displayName: TESTER.name,
avatarUrl: TESTER.avatarUrl,
discordTag: TESTER.discordTag,
points: 25,
});

await Models.AttendeeProfile.create({
userId: "tester2",
displayName: TESTER.name + "2",
avatarUrl: TESTER.avatarUrl,
discordTag: TESTER.discordTag,
points: 12,
});

await Models.AttendeeProfile.create({
userId: "tester3",
displayName: TESTER.name + "3",
avatarUrl: TESTER.avatarUrl,
discordTag: TESTER.discordTag,
points: 27,
});

await getAsUser("/profile/leaderboard").expect(200);
});

it("gets with a limit of 2", async () => {
await Models.AttendeeProfile.create({
userId: TESTER.id,
displayName: TESTER.name,
avatarUrl: TESTER.avatarUrl,
discordTag: TESTER.discordTag,
points: 25,
});

await Models.AttendeeProfile.create({
userId: "tester2",
displayName: TESTER.name + "2",
avatarUrl: TESTER.avatarUrl,
discordTag: TESTER.discordTag,
points: 12,
});

await Models.AttendeeProfile.create({
userId: "tester3",
displayName: TESTER.name + "3",
avatarUrl: TESTER.avatarUrl,
discordTag: TESTER.discordTag,
points: 27,
});

await getAsUser("/profile/leaderboard?limit=2").expect(200);
});

it("gets with no limit & more entries in db than the max query limit", async () => {
for (let i = 0; i < Constants.LEADERBOARD_QUERY_LIMIT + 15; i++) {
await Models.AttendeeProfile.create({
userId: TESTER.id + " " + i,
displayName: TESTER.name + " " + i,
avatarUrl: TESTER.avatarUrl,
discordTag: TESTER.discordTag,
points: i,
});
}

const response = await getAsUser("/profile/leaderboard").expect(200);

const responseArray = JSON.parse(response.text);

expect(responseArray.profiles.length).toBeLessThan(Constants.LEADERBOARD_QUERY_LIMIT + 1);
});

it("gets with an invalid limit", async () => {
const response = await getAsUser("/profile/leaderboard?limit=0").expect(400);

expect(JSON.parse(response.text)).toHaveProperty("error", "InvalidLimit");
});
});

0 comments on commit 11a4139

Please sign in to comment.