Fix auth hook so it returns forbidden if the user is authenticated but not allowed to hit an endpoint.

This commit is contained in:
2025-01-29 14:49:53 -08:00
parent 5487a23c86
commit 53214644b4
23 changed files with 835 additions and 187 deletions

View File

@ -1,15 +1,19 @@
import { describe, it } from "node:test";
import { Game } from "../../Game";
import { describe, it } from "vitest";
import { Game } from "$lib/server/Game";
import { deepEqual, ok, throws } from "node:assert/strict";
import { createId, idFromString, stringFromId } from "$lib/Id";
import { equal } from "node:assert";
describe("Game", () => {
const idString = stringFromId(createId());
describe("addPlayer", () => {
it("should push a player id into the player array", () => {
const game = new Game();
deepEqual(game.players, []);
game.addPlayer("some-id");
deepEqual(game.players, ["some-id"]);
game.addPlayer(idFromString(idString));
equal(game.players.length, 1);
equal(stringFromId(game.players[0]), idString);
});
});

View File

@ -1,26 +1,29 @@
import { describe, it } from "node:test";
import { GameData, isGameData } from "../../GameData";
import { describe, it } from "vitest";
import { type GameData, isGameData } from "$lib/GameData";
import { equal, ok } from "node:assert/strict";
import { createId, idFromString, stringFromId } from "$lib/Id";
describe("GameData", () => {
const idString = stringFromId(createId());
describe("isGameData", () => {
it("rejects a malformed object", () => {
let data: unknown = {
players: ["id", 3],
players: [idFromString(idString), idString],
isStarted: false,
state: {},
};
equal(isGameData(data), false);
data = {
players: ["id"],
players: [idFromString(idString)],
isStarted: null,
state: {},
};
equal(isGameData(data), false);
data = {
players: ["id"],
players: [idFromString(idString)],
isStarted: false,
};
equal(isGameData(data), false);
@ -28,7 +31,7 @@ describe("GameData", () => {
it("rejects an object with extra properties", () => {
const data: GameData & { extra: boolean } = {
players: ["id"],
players: [idFromString(idString)],
isStarted: false,
state: {},
extra: true,
@ -39,7 +42,7 @@ describe("GameData", () => {
it("should accept a proper GameData object", () => {
const data: GameData = {
players: ["id"],
players: [idFromString(idString)],
state: {},
isStarted: false,
};

View File

@ -12,19 +12,14 @@ import {
} from "../../GameEvent";
import type { GameEventData } from "../../GameEvent";
import type { GameData } from "../../GameData";
import { describe, it } from "node:test";
import { describe, it } from "vitest";
import type { State } from "../../State";
import { doesNotThrow, deepStrictEqual, equal, ok, throws } from "assert";
import { createId, idFromString, stringFromId } from "$lib/Id";
describe("Game Events", () => {
describe("isGameEventData", () => {
it("should return false if the target is not an object", () => {
// const target = {
// kind: GameEventKind.Hold,
// player: 0,
// value: [1, 4],
// };
equal(isGameEventData("target"), false);
});
@ -60,10 +55,13 @@ describe("Game Events", () => {
});
describe("getGameEvent", () => {
const idString = stringFromId(createId());
const anotherIdString = stringFromId(createId());
it("should throw if the kind is unkown", () => {
const data: GameData = {
isStarted: false,
players: ["42", "1,"],
players: [idFromString(idString), idFromString(anotherIdString)],
state: {},
};
@ -79,7 +77,7 @@ describe("Game Events", () => {
it("should throw when SeatPlayers has the wrong number of players", () => {
const data: GameData = {
isStarted: true,
players: ["42", "1,"],
players: [idFromString(idString), idFromString(anotherIdString)],
state: {},
};
@ -94,7 +92,7 @@ describe("Game Events", () => {
it("should return a SeatPlayers object when the number of players is correct", () => {
const data: GameData = {
isStarted: true,
players: ["42", "1,"],
players: [idFromString(idString), idFromString(anotherIdString)],
state: {},
};
@ -109,7 +107,7 @@ describe("Game Events", () => {
it("should throw an error if the player passes a full roll with Roll", () => {
const data: GameData = {
isStarted: true,
players: ["42", "1,"],
players: [idFromString(idString), idFromString(anotherIdString)],
state: {},
};
@ -125,7 +123,7 @@ describe("Game Events", () => {
it("should return a Roll object with dice values when the player passes a die count as a value", () => {
const data: GameData = {
isStarted: true,
players: ["42", "1,"],
players: [idFromString(idString), idFromString(anotherIdString)],
state: {},
};
@ -145,7 +143,7 @@ describe("Game Events", () => {
it("should return the class that corresponds with a given kind", () => {
const data: GameData = {
isStarted: true,
players: ["42", "1,"],
players: [idFromString(idString), idFromString(anotherIdString)],
state: {},
};
@ -222,10 +220,7 @@ describe("Game Events", () => {
});
it("should throw if the value is not a number", () => {
throws(
() =>
new RollForFirst({ kind: GameEventKind.RollForFirst, player: 0, value: [4] }),
);
throws(() => new RollForFirst({ kind: GameEventKind.RollForFirst, player: 0, value: [4] }));
});
});
@ -267,7 +262,6 @@ describe("Game Events", () => {
const state: State = { scores: [FIRST_ROLL_PENDING, FIRST_ROLL_PENDING] };
throws(() => ev.run(state));
console.log("done");
});
it("should throw if the player has already rolled", () => {
@ -296,12 +290,7 @@ describe("Game Events", () => {
it("should reset the scores and set the winning player when everyone has rolled", () => {
const state: State = {
scores: [
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
],
scores: [FIRST_ROLL_PENDING, FIRST_ROLL_PENDING, FIRST_ROLL_PENDING, FIRST_ROLL_PENDING],
};
let ev = new RollForFirst({
@ -324,24 +313,14 @@ describe("Game Events", () => {
deepStrictEqual(state, {
dieCount: 6,
scores: [
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
],
scores: [FIRST_ROLL_PENDING, FIRST_ROLL_PENDING, FIRST_ROLL_PENDING, FIRST_ROLL_PENDING],
playing: 2,
});
});
it("should reset tied players for tie breaker", () => {
const state: State = {
scores: [
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
],
scores: [FIRST_ROLL_PENDING, FIRST_ROLL_PENDING, FIRST_ROLL_PENDING, FIRST_ROLL_PENDING],
};
let ev = new RollForFirst({
@ -361,23 +340,13 @@ describe("Game Events", () => {
ev.run(state);
deepStrictEqual(state, {
scores: [
FIRST_ROLL_PENDING,
FIRST_ROLL_LOST,
FIRST_ROLL_LOST,
FIRST_ROLL_PENDING,
],
scores: [FIRST_ROLL_PENDING, FIRST_ROLL_LOST, FIRST_ROLL_LOST, FIRST_ROLL_PENDING],
});
});
it("should throw if a player whose lost tries to roll again", () => {
const state: State = {
scores: [
FIRST_ROLL_PENDING,
FIRST_ROLL_LOST,
FIRST_ROLL_LOST,
FIRST_ROLL_PENDING,
],
scores: [FIRST_ROLL_PENDING, FIRST_ROLL_LOST, FIRST_ROLL_LOST, FIRST_ROLL_PENDING],
};
const ev = new RollForFirst({
@ -390,12 +359,7 @@ describe("Game Events", () => {
it("should allow tied players to keep rolling until somoene wins", () => {
const state: State = {
scores: [
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
FIRST_ROLL_LOST,
FIRST_ROLL_PENDING,
],
scores: [FIRST_ROLL_PENDING, FIRST_ROLL_PENDING, FIRST_ROLL_LOST, FIRST_ROLL_PENDING],
};
// simulate another 3-way tie
@ -415,12 +379,7 @@ describe("Game Events", () => {
deepStrictEqual(
state,
{
scores: [
FIRST_ROLL_PENDING,
FIRST_ROLL_PENDING,
FIRST_ROLL_LOST,
FIRST_ROLL_PENDING,
],
scores: [FIRST_ROLL_PENDING, FIRST_ROLL_PENDING, FIRST_ROLL_LOST, FIRST_ROLL_PENDING],
},
"shouldn't change in a 3-way tie",
);
@ -438,12 +397,7 @@ describe("Game Events", () => {
deepStrictEqual(
state,
{
scores: [
FIRST_ROLL_PENDING,
FIRST_ROLL_LOST,
FIRST_ROLL_LOST,
FIRST_ROLL_PENDING,
],
scores: [FIRST_ROLL_PENDING, FIRST_ROLL_LOST, FIRST_ROLL_LOST, FIRST_ROLL_PENDING],
},
"should update for a smaller tie",
);

View File

@ -1,7 +1,8 @@
import { describe, it } from "node:test";
import { describe, it } from "vitest";
import { createNewListing, updateListing } from "../modifyListing";
import { Game } from "../../Game";
import { Game } from "$lib/server/Game";
import { deepEqual, equal, ok } from "node:assert/strict";
import { isId } from "$lib/Id";
describe("Listing", () => {
describe("createNewListing", () => {
@ -10,10 +11,10 @@ describe("Listing", () => {
const listing = createNewListing(game);
ok(listing.data instanceof Game);
ok(listing.createdAt instanceof Date);
ok(listing.modifiedAt === null);
ok(!listing.deleted);
equal(typeof listing.id, "string");
ok(isId(listing.id));
equal(typeof listing.createdAt, "string");
});
});
@ -27,7 +28,7 @@ describe("Listing", () => {
const updatedListing = updateListing(listing, update);
deepEqual(updatedListing.data, update);
ok(updatedListing.modifiedAt instanceof Date);
equal(typeof updatedListing.modifiedAt, "string");
});
});
});

View File

@ -1,5 +1,5 @@
import { describe, it } from "node:test";
import { getDiceRoll } from "../../getDiceRoll";
import { describe, it } from "vitest";
import { getDiceRoll } from "$lib/server/getDiceRoll";
import { deepEqual } from "node:assert/strict";
function testRandom() {
@ -11,5 +11,8 @@ describe("getDiceRoll", () => {
it("should return an array of numbers from 1 to 6 with a given length", () => {
let rand = getDiceRoll(6, testRandom());
deepEqual(rand, [0, 1, 3, 3, 4, 6]);
rand = getDiceRoll(3, testRandom());
deepEqual(rand, [0, 1, 3]);
});
});

View File

@ -1,4 +1,4 @@
import { describe, it } from "node:test";
import { describe, it } from "vitest";
import { equal, ok } from "node:assert/strict";
import { hasProperty, hasOnlyKeys } from "../../validation";
@ -46,7 +46,7 @@ describe("validation", () => {
third: false,
fourth: null,
fifth: { something: "important" },
sixth: ["one", "two"],
sixth: ["one", "two"]
};
ok(hasProperty(target, "first", "string"));
@ -59,7 +59,7 @@ describe("validation", () => {
it("should return false if passed an array type and the property isn't an array", () => {
const target = {
arr: "not array",
arr: "not array"
};
equal(hasProperty(target, "arr", "string[]"), false);
@ -67,7 +67,7 @@ describe("validation", () => {
it("should return false if the defined array contains a non-matching element", () => {
const target = {
arr: ["I", "was", "born", "in", 1989],
arr: ["I", "was", "born", "in", 1989]
};
equal(hasProperty(target, "arr", "string[]"), false);
@ -75,7 +75,7 @@ describe("validation", () => {
it("should return true if all the elements in a defined array match", () => {
const target = {
arr: ["I", "was", "born", "in", "1989"],
arr: ["I", "was", "born", "in", "1989"]
};
ok(hasProperty(target, "arr", "string[]"));
@ -83,7 +83,7 @@ describe("validation", () => {
it("should return true if all the elements in a defined array match one of multiple types", () => {
const target = {
arr: ["I", "was", "born", "in", 1989],
arr: ["I", "was", "born", "in", 1989]
};
ok(hasProperty(target, "arr", "(string|number)[]"));
@ -91,7 +91,7 @@ describe("validation", () => {
it("should return true if type is null but property is nullable", () => {
const target = {
nullable: null,
nullable: null
};
ok(hasProperty(target, "nullable", "string", true));
@ -107,7 +107,7 @@ describe("validation", () => {
const target = {
one: "one",
two: "two",
three: "three",
three: "three"
};
const keys = ["one", "two"];
@ -119,7 +119,7 @@ describe("validation", () => {
const target = {
one: "one",
two: "two",
three: "three",
three: "three"
};
const keys = ["one", "two", "three"];
@ -129,7 +129,7 @@ describe("validation", () => {
it("should return true if the target has only a subset of the provided keys", () => {
const target = {
one: "one",
one: "one"
};
const keys = ["one", "two", "three"];