diff --git a/client/components/badges/expiration-badge/index.tsx b/client/components/badges/expiration-badge/index.tsx
index 4d6a1acb..7901d31c 100644
--- a/client/components/badges/expiration-badge/index.tsx
+++ b/client/components/badges/expiration-badge/index.tsx
@@ -49,9 +49,9 @@ const ExpirationBadge = ({
return (
/)
+ })
+})
diff --git a/server/src/lib/__tests__/middleware/is-admin.ts b/server/src/lib/__tests__/middleware/is-admin.ts
new file mode 100644
index 00000000..ae585132
--- /dev/null
+++ b/server/src/lib/__tests__/middleware/is-admin.ts
@@ -0,0 +1,50 @@
+// import * as request from 'supertest'
+// import { app } from '../../../app'
+import { NextFunction, Response } from "express"
+import isAdmin from "@lib/middleware/is-admin"
+import { UserJwtRequest } from "@lib/middleware/jwt"
+
+describe("is-admin middlware", () => {
+ let mockRequest: Partial
+ let mockResponse: Partial
+ let nextFunction: NextFunction = jest.fn()
+
+ beforeEach(() => {
+ mockRequest = {}
+ mockResponse = {
+ sendStatus: jest.fn()
+ }
+ })
+
+ it("should return 401 if no authorization header", async () => {
+ const res = mockResponse as Response
+ isAdmin(mockRequest as UserJwtRequest, res, nextFunction)
+ expect(res.sendStatus).toHaveBeenCalledWith(401)
+ })
+
+ it("should return 401 if no token is supplied", async () => {
+ const req = mockRequest as UserJwtRequest
+ req.headers = {
+ authorization: "Bearer"
+ }
+ isAdmin(req, mockResponse as Response, nextFunction)
+ expect(mockResponse.sendStatus).toBeCalledWith(401)
+ })
+
+ it("should return 404 if config.enable_admin is false", async () => {
+ jest.mock("../../config", () => ({
+ enable_admin: false
+ }))
+
+ const req = mockRequest as UserJwtRequest
+ req.headers = {
+ authorization: "Bearer 123"
+ }
+ isAdmin(req, mockResponse as Response, nextFunction)
+ expect(mockResponse.sendStatus).toBeCalledWith(404)
+ })
+
+ // TODO: 403 if !isAdmin
+ // Verify it calls next() if admin
+ // Requires mocking config.enable_admin
+})
diff --git a/server/src/lib/config.ts b/server/src/lib/config.ts
index 9776f543..e4015357 100644
--- a/server/src/lib/config.ts
+++ b/server/src/lib/config.ts
@@ -6,12 +6,12 @@ type Config = {
memory_db: boolean
enable_admin: boolean
secret_key: string
- registration_password: string,
- welcome_content: string | undefined,
- welcome_title: string | undefined,
+ registration_password: string
+ welcome_content: string | undefined
+ welcome_title: string | undefined
}
-type EnvironmentValue = string | undefined;
+type EnvironmentValue = string | undefined
type Environment = { [key: string]: EnvironmentValue }
export const config = (env: Environment): Config => {
@@ -34,7 +34,10 @@ export const config = (env: Environment): Config => {
return str
}
- const defaultIfUndefined = (str: EnvironmentValue, defaultValue: string): string => {
+ const defaultIfUndefined = (
+ str: EnvironmentValue,
+ defaultValue: string
+ ): string => {
if (str === undefined) {
return defaultValue
}
@@ -52,11 +55,15 @@ export const config = (env: Environment): Config => {
}
}
- const is_production = env.NODE_ENV === "production";
+ const is_production = env.NODE_ENV === "production"
- const developmentDefault = (str: EnvironmentValue, name: string, defaultValue: string): string => {
- if (is_production) return throwIfUndefined(str, name);
- return defaultIfUndefined(str, defaultValue);
+ const developmentDefault = (
+ str: EnvironmentValue,
+ name: string,
+ defaultValue: string
+ ): string => {
+ if (is_production) return throwIfUndefined(str, name)
+ return defaultIfUndefined(str, defaultValue)
}
validNodeEnvs(env.NODE_ENV)
@@ -72,7 +79,6 @@ export const config = (env: Environment): Config => {
registration_password: env.REGISTRATION_PASSWORD ?? "",
welcome_content: env.WELCOME_CONTENT,
welcome_title: env.WELCOME_TITLE
-
}
return config
}
diff --git a/server/src/lib/get-html-from-drift-file.ts b/server/src/lib/get-html-from-drift-file.ts
index 4e796021..bbee1ea0 100644
--- a/server/src/lib/get-html-from-drift-file.ts
+++ b/server/src/lib/get-html-from-drift-file.ts
@@ -5,37 +5,35 @@ import { File } from "@lib/models/File"
* returns rendered HTML from a Drift file
*/
function getHtmlFromFile({ content, title }: Pick) {
- const renderAsMarkdown = [
- "markdown",
- "md",
- "mdown",
- "mkdn",
- "mkd",
- "mdwn",
- "mdtxt",
- "mdtext",
- "text",
- ""
- ]
- const fileType = () => {
- const pathParts = title.split(".")
- const language = pathParts.length > 1 ? pathParts[pathParts.length - 1] : ""
- return language
- }
- const type = fileType()
- let contentToRender: string = content || ""
+ const renderAsMarkdown = [
+ "markdown",
+ "md",
+ "mdown",
+ "mkdn",
+ "mkd",
+ "mdwn",
+ "mdtxt",
+ "mdtext",
+ "text",
+ ""
+ ]
+ const fileType = () => {
+ const pathParts = title.split(".")
+ const language = pathParts.length > 1 ? pathParts[pathParts.length - 1] : ""
+ return language
+ }
+ const type = fileType()
+ let contentToRender: string = content || ""
- if (!renderAsMarkdown.includes(type)) {
- contentToRender = `~~~${type}
+ if (!renderAsMarkdown.includes(type)) {
+ contentToRender = `~~~${type}
${content}
~~~`
- } else {
- contentToRender = "\n" + content
- }
- console.log(contentToRender.slice(0, 50))
- const html = markdown(contentToRender)
- return html
+ } else {
+ contentToRender = "\n" + content
+ }
+ const html = markdown(contentToRender)
+ return html
}
-
-export default getHtmlFromFile
\ No newline at end of file
+export default getHtmlFromFile
diff --git a/server/src/lib/middleware/is-admin.ts b/server/src/lib/middleware/is-admin.ts
index 81efc7ca..3d50ad70 100644
--- a/server/src/lib/middleware/is-admin.ts
+++ b/server/src/lib/middleware/is-admin.ts
@@ -11,16 +11,20 @@ export interface UserJwtRequest extends Request {
user?: User
}
-export default function authenticateToken(
+export default function isAdmin(
req: UserJwtRequest,
res: Response,
next: NextFunction
) {
+ if (!req.headers?.authorization) {
+ return res.sendStatus(401)
+ }
+
const authHeader = req.headers["authorization"]
const token = authHeader && authHeader.split(" ")[1]
- if (token == null) return res.sendStatus(401)
+ if (!token) return res.sendStatus(401)
+ console.log(config)
if (!config.enable_admin) return res.sendStatus(404)
-
jwt.verify(token, config.jwt_secret, async (err: any, user: any) => {
if (err) return res.sendStatus(403)
const userObj = await UserModel.findByPk(user.id, {
diff --git a/server/src/routes/posts.ts b/server/src/routes/posts.ts
index 7795a1e1..d5869017 100644
--- a/server/src/routes/posts.ts
+++ b/server/src/routes/posts.ts
@@ -357,4 +357,3 @@ posts.delete("/:id", jwt, async (req: UserJwtRequest, res, next) => {
next(e)
}
})
-