-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add profile service tests & fix profile service (#102)
* added more fields to test tools * added profile router testing file * profile router bug fixes * added leaderboard query limit * changed all status codes to constants
- Loading branch information
1 parent
ceb2a2d
commit 968ff96
Showing
4 changed files
with
205 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { describe, expect, it, beforeEach } from "@jest/globals"; | ||
import Models from "../../database/models.js"; | ||
import { TESTER, delAsUser, getAsAdmin, getAsUser, postAsAttendee, postAsUser } from "../../testTools.js"; | ||
import { ProfileFormat } from "./profile-formats.js"; | ||
import Constants from "../../constants.js"; | ||
import { AttendeeMetadata, AttendeeProfile } from "database/attendee-db.js"; | ||
import { StatusCode } from "status-code-enum"; | ||
|
||
const TESTER_USER = { | ||
userId: TESTER.id, | ||
displayName: TESTER.name, | ||
avatarUrl: TESTER.avatarUrl, | ||
discordTag: TESTER.discordTag, | ||
points: 0, | ||
} satisfies AttendeeProfile; | ||
|
||
const TESTER_METADATA = { | ||
userId: TESTER.id, | ||
foodWave: 0, | ||
} satisfies AttendeeMetadata; | ||
|
||
const TESTER_USER_2 = { | ||
userId: "tester2", | ||
displayName: TESTER.name + "2", | ||
avatarUrl: TESTER.avatarUrl, | ||
discordTag: TESTER.discordTag, | ||
points: 12, | ||
} satisfies AttendeeProfile; | ||
|
||
const TESTER_USER_3 = { | ||
userId: "tester3", | ||
displayName: TESTER.name + "3", | ||
avatarUrl: TESTER.avatarUrl, | ||
discordTag: TESTER.discordTag, | ||
points: 12, | ||
} satisfies AttendeeProfile; | ||
|
||
const profile: ProfileFormat = { | ||
userId: TESTER.id, | ||
displayName: TESTER.name, | ||
avatarUrl: TESTER.avatarUrl, | ||
discordTag: TESTER.discordTag, | ||
points: 0, | ||
}; | ||
|
||
beforeEach(async () => { | ||
Models.initialize(); | ||
await Models.AttendeeProfile.create(TESTER_USER); | ||
await Models.AttendeeMetadata.create(TESTER_METADATA); | ||
await Models.AttendeeProfile.create(TESTER_USER_2); | ||
await Models.AttendeeProfile.create(TESTER_USER_3); | ||
}); | ||
|
||
describe("POST /profile", () => { | ||
it("works for an attendee", async () => { | ||
await Models.AttendeeProfile.deleteOne({ userId: TESTER_USER.userId }); | ||
const response = await postAsAttendee("/profile/").send(profile).expect(StatusCode.SuccessOK); | ||
|
||
expect(JSON.parse(response.text)).toHaveProperty("displayName", TESTER.name); | ||
}); | ||
|
||
it("fails when a profile is already created", async () => { | ||
await Models.AttendeeProfile.deleteOne({ userId: TESTER_USER.userId }); | ||
const response = await postAsUser("/profile/").send(profile).expect(StatusCode.SuccessOK); | ||
|
||
expect(JSON.parse(response.text)).toHaveProperty("displayName", TESTER.name); | ||
|
||
// to verify they can't double create | ||
const response2 = await postAsUser("/profile/").send(profile).expect(StatusCode.ClientErrorBadRequest); | ||
expect(JSON.parse(response2.text)).toHaveProperty("error", "UserAlreadyExists"); | ||
}); | ||
|
||
it("fails when invalid data is provided", async () => { | ||
const response = await postAsUser("/profile/") | ||
.send({ | ||
displayName: 123, | ||
avatarUrl: 1, | ||
discordTag: "test", | ||
}) | ||
.expect(StatusCode.ClientErrorBadRequest); | ||
|
||
expect(JSON.parse(response.text)).toHaveProperty("error", "InvalidParams"); | ||
}); | ||
}); | ||
|
||
describe("GET /profile", () => { | ||
it("fails to get a profile that doesn't exist", async () => { | ||
await Models.AttendeeProfile.deleteOne({ userId: TESTER_USER.userId }); | ||
|
||
const response = await getAsUser("/profile/").expect(StatusCode.ClientErrorNotFound); | ||
|
||
expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound"); | ||
}); | ||
|
||
it("gets a profile that exists", async () => { | ||
const response = await getAsUser("/profile/").expect(StatusCode.SuccessOK); | ||
expect(JSON.parse(response.text)).toHaveProperty("displayName", TESTER.name); | ||
}); | ||
}); | ||
|
||
describe("GET /profile/id/:USERID", () => { | ||
it("redirects with no id provided", async () => { | ||
await getAsUser("/profile/id").expect(StatusCode.RedirectFound); | ||
}); | ||
|
||
it("fails to get a profile as a user", async () => { | ||
const response = await getAsUser("/profile/id/" + TESTER.id).expect(StatusCode.ClientErrorForbidden); | ||
|
||
expect(JSON.parse(response.text)).toHaveProperty("error", "Forbidden"); | ||
}); | ||
|
||
it("gets a profile as an admin", async () => { | ||
const response = await getAsAdmin("/profile/id/" + TESTER.id).expect(StatusCode.SuccessOK); | ||
|
||
expect(JSON.parse(response.text)).toHaveProperty("displayName", TESTER.name); | ||
}); | ||
|
||
it("gets a user that doesnt exist", async () => { | ||
const response = await getAsAdmin("/profile/id/doesnotexist").expect(StatusCode.ClientErrorNotFound); | ||
|
||
expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound"); | ||
}); | ||
}); | ||
|
||
describe("DELETE /profile/", () => { | ||
it("fails to delete a profile that doesn't exist", async () => { | ||
await Models.AttendeeProfile.deleteOne({ userId: TESTER_USER.userId }); | ||
const response = await delAsUser("/profile").expect(StatusCode.ClientErrorNotFound); | ||
expect(JSON.parse(response.text)).toHaveProperty("error", "AttendeeNotFound"); | ||
}); | ||
|
||
it("deletes a profile", async () => { | ||
const response = await delAsUser("/profile").expect(StatusCode.SuccessOK); | ||
expect(JSON.parse(response.text)).toHaveProperty("success", true); | ||
}); | ||
}); | ||
|
||
describe("GET /profile/leaderboard", () => { | ||
it("gets 3 entries when no limit is set", async () => { | ||
await getAsUser("/profile/leaderboard").expect(StatusCode.SuccessOK); | ||
}); | ||
|
||
it("gets with a limit of 2", async () => { | ||
const response = await getAsUser("/profile/leaderboard?limit=2").expect(StatusCode.SuccessOK); | ||
|
||
const responseArray = JSON.parse(response.text); | ||
expect(responseArray.profiles.length).toBeLessThan(3); | ||
}); | ||
|
||
it("only gets the max limit when no limit is set", 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(StatusCode.SuccessOK); | ||
|
||
const responseArray = JSON.parse(response.text); | ||
|
||
expect(responseArray.profiles.length).toBeLessThan(Constants.LEADERBOARD_QUERY_LIMIT + 1); | ||
}); | ||
|
||
it("fails when an invalid limit is set", async () => { | ||
const response = await getAsUser("/profile/leaderboard?limit=0").expect(StatusCode.ClientErrorBadRequest); | ||
|
||
expect(JSON.parse(response.text)).toHaveProperty("error", "InvalidLimit"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters