Skip to content

Commit

Permalink
Use new status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez committed Oct 22, 2023
1 parent f9da3ea commit 52ee428
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import { describe, expect, test, it } from "@jest/globals";

import { get } from "./testTools.js";
import { StatusCode } from "status-code-enum";

describe("sanity tests for app", () => {
test("life is not a lie", () => {
expect(7 * 6).toBe(42);
});

it("should run", async () => {
const response = await get("/").expect(200);
const response = await get("/").expect(StatusCode.SuccessOK);

expect(response.text).toBe("API is working!!!");
});
Expand Down
35 changes: 18 additions & 17 deletions src/services/user/user-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as authLib from "../auth/auth-lib.js";
import { Role } from "../auth/auth-models.js";
import Constants from "../../constants.js";
import { SpiedFunction } from "jest-mock";
import { StatusCode } from "status-code-enum";

const TESTER_USER = {
userId: TESTER.id,
Expand Down Expand Up @@ -46,11 +47,11 @@ function mockGenerateJwtTokenWithWrapper(): SpiedFunction<typeof authLib.generat
return mockedGenerateJwtToken;
}

describe("GET /qr/", () => {
describe("GET /user/qr/", () => {
it("works for a attendee", async () => {
const mockedGenerateJwtToken = mockGenerateJwtTokenWithWrapper();

const response = await getAsAttendee("/user/qr/").expect(200);
const response = await getAsAttendee("/user/qr/").expect(StatusCode.SuccessOK);

const jwtReturned = mockedGenerateJwtToken.mock.results[mockedGenerateJwtToken.mock.results.length - 1]!.value;

Expand All @@ -69,17 +70,17 @@ describe("GET /qr/", () => {
});
});

describe("GET /qr/:USERID/", () => {
describe("GET /user/qr/:USERID/", () => {
it("gives a forbidden error for a non-staff user", async () => {
const response = await getAsAttendee(`/user/qr/${OTHER_USER.userId}/`).expect(403);
const response = await getAsAttendee(`/user/qr/${OTHER_USER.userId}/`).expect(StatusCode.ClientErrorForbidden);

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

it("works for a non-staff user requesting their qr code", async () => {
const mockedGenerateJwtToken = mockGenerateJwtTokenWithWrapper();

const response = await getAsAttendee(`/user/qr/${TESTER_USER.userId}/`).expect(200);
const response = await getAsAttendee(`/user/qr/${TESTER_USER.userId}/`).expect(StatusCode.SuccessOK);

const jwtReturned = mockedGenerateJwtToken.mock.results[mockedGenerateJwtToken.mock.results.length - 1]!.value;

Expand All @@ -100,7 +101,7 @@ describe("GET /qr/:USERID/", () => {
it("works for a staff user", async () => {
const mockedGenerateJwtToken = mockGenerateJwtTokenWithWrapper();

const response = await getAsStaff(`/user/qr/${OTHER_USER.userId}/`).expect(200);
const response = await getAsStaff(`/user/qr/${OTHER_USER.userId}/`).expect(StatusCode.SuccessOK);

const jwtReturned = mockedGenerateJwtToken.mock.results[mockedGenerateJwtToken.mock.results.length - 1]!.value;

Expand All @@ -119,9 +120,9 @@ describe("GET /qr/:USERID/", () => {
});
});

describe("GET /", () => {
describe("GET /user/", () => {
it("gives an unauthorized error for an unauthenticated user", async () => {
const response = await get("/user/").expect(401);
const response = await get("/user/").expect(StatusCode.ClientErrorUnauthorized);

expect(JSON.parse(response.text)).toHaveProperty("error", "NoToken");
});
Expand All @@ -131,33 +132,33 @@ describe("GET /", () => {
userId: TESTER_USER.userId,
});

const response = await getAsAttendee("/user/").expect(404);
const response = await getAsAttendee("/user/").expect(StatusCode.ClientErrorNotFound);

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

it("works for an attendee user", async () => {
const response = await getAsAttendee("/user/").expect(200);
const response = await getAsAttendee("/user/").expect(StatusCode.SuccessOK);

expect(JSON.parse(response.text)).toMatchObject(TESTER_USER);
});

it("works for an staff user", async () => {
const response = await getAsStaff("/user/").expect(200);
const response = await getAsStaff("/user/").expect(StatusCode.SuccessOK);

expect(JSON.parse(response.text)).toMatchObject(TESTER_USER);
});

it("works for an admin user", async () => {
const response = await getAsAdmin("/user/").expect(200);
const response = await getAsAdmin("/user/").expect(StatusCode.SuccessOK);

expect(JSON.parse(response.text)).toMatchObject(TESTER_USER);
});
});

describe("GET /:USERID/", () => {
describe("GET /user/:USERID/", () => {
it("gives an forbidden error for a non-staff user", async () => {
const response = await getAsAttendee(`/user/${OTHER_USER.userId}/`).expect(403);
const response = await getAsAttendee(`/user/${OTHER_USER.userId}/`).expect(StatusCode.ClientErrorForbidden);

expect(JSON.parse(response.text)).toHaveProperty("error", "Forbidden");
});
Expand All @@ -167,19 +168,19 @@ describe("GET /:USERID/", () => {
userId: OTHER_USER.userId,
});

const response = await getAsStaff(`/user/${OTHER_USER.userId}/`).expect(404);
const response = await getAsStaff(`/user/${OTHER_USER.userId}/`).expect(StatusCode.ClientErrorNotFound);

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

it("works for a non-staff user requesting themselves", async () => {
const response = await getAsAttendee(`/user/${TESTER_USER.userId}/`).expect(200);
const response = await getAsAttendee(`/user/${TESTER_USER.userId}/`).expect(StatusCode.SuccessOK);

expect(JSON.parse(response.text)).toMatchObject(TESTER_USER);
});

it("works for a staff user", async () => {
const response = await getAsStaff(`/user/${OTHER_USER.userId}/`).expect(200);
const response = await getAsStaff(`/user/${OTHER_USER.userId}/`).expect(StatusCode.SuccessOK);

expect(JSON.parse(response.text)).toMatchObject(OTHER_USER);
});
Expand Down

0 comments on commit 52ee428

Please sign in to comment.