Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
- name: Run Lint
run: pnpm lint

- name: Run Tests
run: pnpm test

- name: Run Build
run: pnpm build

Expand Down
14 changes: 14 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
roots: ["<rootDir>/src", "<rootDir>/test"],
testRegex: ".*\\.(spec|e2e-spec)\\.ts$",
testTimeout: 15000,
setupFiles: ["<rootDir>/test/jest.setup.ts"],
collectCoverageFrom: ["src/**/*.ts", "!src/config/index.ts"],
moduleNameMapper: {
// Matches both "src/etc/esm-fix" and the relative "./esm-fix".
"^(.*/)?esm-fix$": "<rootDir>/test/esm-fix-mock.ts",
"^src/(.*)$": "<rootDir>/src/$1",
},
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:cov": "jest --coverage",
"build": "npm run build:nest && npm run build:docs",
"build:nest": "nest build",
"build:docs": "typedoc",
Expand Down Expand Up @@ -63,6 +65,7 @@
"@types/supertest": "^7.2.1",
"@typescript-eslint/eslint-plugin": "^8.65.0",
"@typescript-eslint/parser": "^8.65.0",
"aws-sdk-client-mock": "^4.1.0",
"eslint": "^8.0.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.6",
Expand Down
78 changes: 78 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/config/Helper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { $bool, $int, $list, $oneOf, $str } from "./Helper";

describe("config helpers", () => {
afterEach(() => {
delete process.env.TEST_HELPER_VAR;
});

it("returns the default when the env var is unset", () => {
expect($str("TEST_HELPER_VAR", "fallback")).toBe("fallback");
expect($int("TEST_HELPER_VAR", 42)).toBe(42);
expect($bool("TEST_HELPER_VAR", true)).toBe(true);
expect($list("TEST_HELPER_VAR", ["a", "b"])).toEqual(["a", "b"]);
});

it("throws when a required env var is missing", () => {
expect(() => $str("TEST_HELPER_VAR")).toThrow(
"Missing environment variable: TEST_HELPER_VAR",
);
});

it("reads and parses values from the environment", () => {
process.env.TEST_HELPER_VAR = "7";
expect($int("TEST_HELPER_VAR", 42)).toBe(7);
process.env.TEST_HELPER_VAR = "TRUE";
expect($bool("TEST_HELPER_VAR", false)).toBe(true);
});

it("$oneOf accepts listed values and rejects others", () => {
process.env.TEST_HELPER_VAR = "b";
expect($oneOf("TEST_HELPER_VAR", ["a", "b"], "a")).toBe("b");
process.env.TEST_HELPER_VAR = "c";
expect(() => $oneOf("TEST_HELPER_VAR", ["a", "b"], "a")).toThrow(
"Invalid value for environment variable: TEST_HELPER_VAR",
);
});
});
4 changes: 4 additions & 0 deletions src/controller/fetch.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
applyDecorators,
Controller,
Get,
HttpCode,
HttpException,
HttpStatus,
Param,
Expand Down Expand Up @@ -95,6 +96,8 @@ export class FetchController {
if (key.length > 32)
throw new HttpException("The key is too long", HttpStatus.BAD_REQUEST);
const note = await this.db.getNote(id);
if (!note)
throw new HttpException("The note was not found", HttpStatus.NOT_FOUND);
await this.db.createToken(
await getIp(req),
Buffer.byteLength(note.content, "utf8"),
Expand All @@ -114,6 +117,7 @@ export class FetchController {
}

@Post("decrypt")
@HttpCode(HttpStatus.OK)
@ApiBody({ type: String, description: "The decryption key for the note" })
@ApiConsumes("text/plain")
@DecryptDecorator()
Expand Down
4 changes: 3 additions & 1 deletion src/controller/files.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Controller,
Delete,
Get,
HttpCode,
HttpException,
HttpStatus,
Param,
Expand Down Expand Up @@ -183,7 +184,7 @@ export class FilesController {
"The part query parameter must be a positive number",
HttpStatus.BAD_REQUEST,
);
this.db.updateFile(id, { part: totalPart });
await this.db.updateFile(id, { part: totalPart });
const key = `${file.id}/${file.name}`;
return {
url: await this.s3.createUploadPartUrl(
Expand All @@ -196,6 +197,7 @@ export class FilesController {
}

@Put("upload/:id")
@HttpCode(HttpStatus.NO_CONTENT)
@ApiBody({ type: FileCloseUploadRequest })
@ApiResponse({
status: HttpStatus.NO_CONTENT,
Expand Down
Loading