Skip to content

Commit

Permalink
Merge branch 'monkeytypegame:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcosatc147 authored Aug 22, 2024
2 parents c05dc72 + 59a1868 commit ba7d6f0
Show file tree
Hide file tree
Showing 114 changed files with 3,603 additions and 2,934 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/monkey-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ concurrency:

jobs:
pre-ci:
if: github.event.pull_request.draft == false
if: github.event.pull_request.draft == false || contains(github.event.pull_request.labels.*.name, 'force-ci') || contains(github.event.pull_request.labels.*.name, 'force-full-ci')
name: pre-ci
runs-on: ubuntu-latest
outputs:
Expand Down Expand Up @@ -72,7 +72,7 @@ jobs:
name: prime-cache
runs-on: ubuntu-latest
needs: [pre-ci]
if: needs.pre-ci.outputs.should-build-be == 'true' || needs.pre-ci.outputs.should-build-fe == 'true' || needs.pre-ci.outputs.should-build-pkg == 'true' || needs.pre-ci.outputs.assets-json == 'true'
if: needs.pre-ci.outputs.should-build-be == 'true' || needs.pre-ci.outputs.should-build-fe == 'true' || needs.pre-ci.outputs.should-build-pkg == 'true' || needs.pre-ci.outputs.assets-json == 'true' || contains(github.event.pull_request.labels.*.name, 'force-full-ci')
steps:

- name: Checkout pnpm-lock
Expand Down Expand Up @@ -119,7 +119,7 @@ jobs:
name: check-pretty
needs: [pre-ci, prime-cache]
runs-on: ubuntu-latest
if: needs.pre-ci.outputs.should-build-be == 'true' || needs.pre-ci.outputs.should-build-fe == 'true' || needs.pre-ci.outputs.should-build-pkg == 'true' || needs.pre-ci.outputs.assets-json == 'true'
if: needs.pre-ci.outputs.should-build-be == 'true' || needs.pre-ci.outputs.should-build-fe == 'true' || needs.pre-ci.outputs.should-build-pkg == 'true' || needs.pre-ci.outputs.assets-json == 'true' || contains(github.event.pull_request.labels.*.name, 'force-full-ci')
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -169,7 +169,7 @@ jobs:
name: ci-be
needs: [pre-ci, prime-cache, check-pretty]
runs-on: ubuntu-latest
if: needs.pre-ci.outputs.should-build-be == 'true' || needs.pre-ci.outputs.should-build-pkg == 'true'
if: needs.pre-ci.outputs.should-build-be == 'true' || needs.pre-ci.outputs.should-build-pkg == 'true' || contains(github.event.pull_request.labels.*.name, 'force-full-ci')
steps:
- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -217,7 +217,7 @@ jobs:
name: ci-fe
needs: [pre-ci, prime-cache, check-pretty]
runs-on: ubuntu-latest
if: needs.pre-ci.outputs.should-build-fe == 'true' || needs.pre-ci.outputs.should-build-pkg == 'true'
if: needs.pre-ci.outputs.should-build-fe == 'true' || needs.pre-ci.outputs.should-build-pkg == 'true' || contains(github.event.pull_request.labels.*.name, 'force-full-ci')
steps:

- uses: actions/checkout@v4
Expand Down Expand Up @@ -270,7 +270,7 @@ jobs:
name: ci-assets
needs: [pre-ci, prime-cache, check-pretty]
runs-on: ubuntu-latest
if: needs.pre-ci.outputs.assets-json == 'true'
if: needs.pre-ci.outputs.assets-json == 'true' || contains(github.event.pull_request.labels.*.name, 'force-full-ci')
steps:
- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -339,7 +339,7 @@ jobs:
name: ci-pkg
needs: [pre-ci, prime-cache,check-pretty]
runs-on: ubuntu-latest
if: needs.pre-ci.outputs.should-build-pkg == 'true'
if: needs.pre-ci.outputs.should-build-pkg == 'true' || contains(github.event.pull_request.labels.*.name, 'force-full-ci')
steps:

- uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/semantic-pr-title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
main:
name: check
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.user.login != 'dependabot[bot]' }}
steps:
- name: Lint and verify PR title
uses: amannn/action-semantic-pull-request@v5
Expand Down Expand Up @@ -49,7 +50,7 @@ jobs:
- uses: marocchino/sticky-pull-request-comment@v2
# When the previous steps fails, the workflow would stop. By adding this
# condition you can continue the execution with the populated error message.
if: always() && (steps.lint_pr_title.outputs.error_message != null)
if: always() && (steps.lint_pr_title.outputs.error_message != null)
with:
header: pr-title-lint-error
message: |
Expand Down
2 changes: 1 addition & 1 deletion backend/__tests__/api/controllers/ape-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const configuration = Configuration.getCachedConfiguration();
const uid = new ObjectId().toHexString();

describe("ApeKeyController", () => {
const getUserMock = vi.spyOn(UserDal, "getUser");
const getUserMock = vi.spyOn(UserDal, "getPartialUser");

beforeEach(async () => {
await enableApeKeysEndpoints(true);
Expand Down
16 changes: 9 additions & 7 deletions backend/__tests__/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import * as MongoDbMock from "vitest-mongodb";
export async function setup(): Promise<void> {
process.env.TZ = "UTC";
await MongoDbMock.setup({
serverOptions: {
binary: {
version: "6.0.12",
},
},
});
await MongoDbMock.setup(MongoDbMockConfig);
}

export async function teardown(): Promise<void> {
await MongoDbMock.teardown();
}

export const MongoDbMockConfig = {
serverOptions: {
binary: {
version: "6.0.12",
},
},
};
6 changes: 3 additions & 3 deletions backend/__tests__/setup-tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Collection, Db, MongoClient, WithId } from "mongodb";
import { afterAll, beforeAll, afterEach } from "vitest";
import * as MongoDbMock from "vitest-mongodb";
import { MongoDbMockConfig } from "./global-setup";

process.env["MODE"] = "dev";
//process.env["MONGOMS_DISTRO"] = "ubuntu-22.04";
Expand All @@ -15,9 +16,8 @@ let client: MongoClient;
const collectionsForCleanUp = ["users"];

beforeAll(async () => {
await MongoDbMock.setup({
//don't add any configuration here, add to global-setup.ts instead.
});
//don't add any configuration here, add to global-setup.ts instead.
await MongoDbMock.setup(MongoDbMockConfig);

client = new MongoClient(globalThis.__MONGO_URI__);
await client.connect();
Expand Down
16 changes: 8 additions & 8 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"cors": "2.8.5",
"cron": "2.3.0",
"date-fns": "3.6.0",
"dotenv": "10.0.0",
"dotenv": "16.4.5",
"express": "4.19.2",
"express-rate-limit": "6.2.1",
"firebase-admin": "12.0.0",
Expand All @@ -46,15 +46,15 @@
"mongodb": "6.3.0",
"mustache": "4.2.0",
"node-fetch": "2.6.7",
"nodemailer": "6.9.9",
"nodemailer": "6.9.14",
"nodemon": "3.1.4",
"object-hash": "3.0.0",
"path": "0.12.7",
"prom-client": "14.0.1",
"rate-limiter-flexible": "2.3.7",
"prom-client": "15.1.3",
"rate-limiter-flexible": "5.0.3",
"simple-git": "3.16.0",
"string-similarity": "4.0.4",
"swagger-stats": "0.99.5",
"swagger-stats": "0.99.7",
"swagger-ui-express": "4.3.0",
"ua-parser-js": "0.7.33",
"uuid": "10.0.0",
Expand All @@ -76,10 +76,10 @@
"@types/mustache": "4.2.2",
"@types/node": "20.14.11",
"@types/node-fetch": "2.6.1",
"@types/nodemailer": "6.4.7",
"@types/object-hash": "2.2.1",
"@types/nodemailer": "6.4.15",
"@types/object-hash": "3.0.6",
"@types/readline-sync": "1.4.8",
"@types/string-similarity": "4.0.0",
"@types/string-similarity": "4.0.2",
"@types/supertest": "2.0.12",
"@types/swagger-stats": "0.95.11",
"@types/swagger-ui-express": "4.1.3",
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/routes/ape-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const commonMiddleware = [
},
invalidMessage: "ApeKeys are currently disabled.",
}),
checkUserPermissions({
checkUserPermissions(["canManageApeKeys"], {
criteria: (user) => {
return user.canManageApeKeys ?? true;
},
Expand Down
24 changes: 16 additions & 8 deletions backend/src/api/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function addApiRoutes(app: Application): void {
function applyTsRestApiRoutes(app: IRouter): void {
createExpressEndpoints(contract, router, app, {
jsonQuery: true,
requestValidationErrorHandler(err, _req, res, next) {
requestValidationErrorHandler(err, req, res, _next) {
let message: string | undefined = undefined;
let validationErrors: string[] | undefined = undefined;

Expand All @@ -90,16 +90,24 @@ function applyTsRestApiRoutes(app: IRouter): void {
} else if (err.body?.issues !== undefined) {
message = "Invalid request data schema";
validationErrors = err.body.issues.map(prettyErrorMessage);
}

if (message !== undefined) {
res
.status(422)
.json({ message, validationErrors } as MonkeyValidationError);
} else if (err.headers?.issues !== undefined) {
message = "Invalid header schema";
validationErrors = err.headers.issues.map(prettyErrorMessage);
} else {
next();
Logger.error(
`Unknown validation error for ${req.method} ${
req.path
}: ${JSON.stringify(err)}`
);
res
.status(500)
.json({ message: "Unknown validation error. Contact support." });
return;
}

res
.status(422)
.json({ message, validationErrors } as MonkeyValidationError);
},
globalMiddleware: [authenticateTsRestRequest()],
});
Expand Down
4 changes: 2 additions & 2 deletions backend/src/api/routes/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { validateRequest } from "../../middlewares/validation";

const router = Router();

const checkIfUserIsQuoteMod = checkUserPermissions({
const checkIfUserIsQuoteMod = checkUserPermissions(["quoteMod"], {
criteria: (user) => {
return (
user.quoteMod === true ||
Expand Down Expand Up @@ -171,7 +171,7 @@ router.post(
captcha: withCustomMessages.regex(/[\w-_]+/).required(),
},
}),
checkUserPermissions({
checkUserPermissions(["canReport"], {
criteria: (user) => {
return user.canReport !== false;
},
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ router.post(
captcha: withCustomMessages.regex(/[\w-_]+/).required(),
},
}),
checkUserPermissions({
checkUserPermissions(["canReport"], {
criteria: (user) => {
return user.canReport !== false;
},
Expand Down
13 changes: 9 additions & 4 deletions backend/src/middlewares/permission.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import MonkeyError from "../utils/error";
import type { Response, NextFunction, RequestHandler } from "express";
import { getUser } from "../dal/user";
import { getPartialUser } from "../dal/user";
import { isAdmin } from "../dal/admin-uids";
import type { ValidationOptions } from "./configuration";

Expand Down Expand Up @@ -34,8 +34,9 @@ export function checkIfUserIsAdmin(): RequestHandler {
* Check user permissions before handling request.
* Note that this middleware must be used after authentication in the middleware stack.
*/
export function checkUserPermissions(
options: ValidationOptions<MonkeyTypes.DBUser>
export function checkUserPermissions<K extends keyof MonkeyTypes.DBUser>(
fields: K[],
options: ValidationOptions<Pick<MonkeyTypes.DBUser, K>>
): RequestHandler {
const { criteria, invalidMessage = "You don't have permission to do this." } =
options;
Expand All @@ -48,7 +49,11 @@ export function checkUserPermissions(
try {
const { uid } = req.ctx.decodedToken;

const userData = await getUser(uid, "check user permissions");
const userData = await getPartialUser(
uid,
"check user permissions",
fields
);
const hasPermission = criteria(userData);

if (!hasPermission) {
Expand Down
1 change: 1 addition & 0 deletions backend/vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default defineConfig({
environment: "node",
globalSetup: "__tests__/global-setup.ts",
setupFiles: ["__tests__/setup-tests.ts"],
pool: "forks", //this should be the default value, however the CI fails without this set.

coverage: {
include: ["**/*.ts"],
Expand Down
21 changes: 6 additions & 15 deletions frontend/__tests__/root/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ describe("Config", () => {
false
);
});
it("setAccountChartResults", () => {
expect(Config.setAccountChartResults(true)).toBe(true);
expect(Config.setAccountChartResults("on" as any)).toBe(false);
});
it("setStopOnError", () => {
expect(Config.setStopOnError("off")).toBe(true);
expect(Config.setStopOnError("word")).toBe(true);
Expand Down Expand Up @@ -254,17 +250,12 @@ describe("Config", () => {
it("setBlindMode", () => {
testBoolean(Config.setBlindMode);
});
it("setAccountChartResults", () => {
testBoolean(Config.setAccountChartResults);
});
it("setAccountChartAccuracy", () => {
testBoolean(Config.setAccountChartAccuracy);
});
it("setAccountChartAvg10", () => {
testBoolean(Config.setAccountChartAvg10);
});
it("setAccountChartAvg100", () => {
testBoolean(Config.setAccountChartAvg100);
it("setAccountChart", () => {
expect(Config.setAccountChart(["on", "off", "off", "on"])).toBe(true);
expect(Config.setAccountChart(["on", "off"] as any)).toBe(true);
expect(Config.setAccountChart(["on", "off", "on", "true"] as any)).toBe(
false
);
});
it("setAlwaysShowDecimalPlaces", () => {
testBoolean(Config.setAlwaysShowDecimalPlaces);
Expand Down
Loading

0 comments on commit ba7d6f0

Please sign in to comment.