mirror of https://github.com/MaxLeiter/Drift
server: add basic is-admin tests and bug fixes
parent
06d847dfa3
commit
e5b9b65b55
@ -1,16 +1,16 @@
|
||||
import * as request from 'supertest'
|
||||
import { app } from '../app'
|
||||
import * as request from "supertest"
|
||||
import { app } from "../app"
|
||||
|
||||
describe('GET /health', () => {
|
||||
it('should return 200 and a status up', (done) => {
|
||||
request(app)
|
||||
.get(`/health`)
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200)
|
||||
.end((err, res) => {
|
||||
if (err) return done(err)
|
||||
expect(res.body).toMatchObject({ 'status': 'UP' })
|
||||
done()
|
||||
})
|
||||
})
|
||||
describe("GET /health", () => {
|
||||
it("should return 200 and a status up", (done) => {
|
||||
request(app)
|
||||
.get(`/health`)
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(200)
|
||||
.end((err, res) => {
|
||||
if (err) return done(err)
|
||||
expect(res.body).toMatchObject({ status: "UP" })
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,62 +1,64 @@
|
||||
import { config } from "../config"
|
||||
|
||||
describe("Config", () => {
|
||||
it("should build a valid development config when no environment is set", () => {
|
||||
const emptyEnv = {};
|
||||
const result = config(emptyEnv);
|
||||
|
||||
expect(result).toHaveProperty("is_production", false)
|
||||
expect(result).toHaveProperty("port")
|
||||
expect(result).toHaveProperty("jwt_secret")
|
||||
expect(result).toHaveProperty("drift_home")
|
||||
expect(result).toHaveProperty("memory_db")
|
||||
expect(result).toHaveProperty("enable_admin")
|
||||
expect(result).toHaveProperty("secret_key")
|
||||
expect(result).toHaveProperty("registration_password")
|
||||
expect(result).toHaveProperty("welcome_content")
|
||||
expect(result).toHaveProperty("welcome_title")
|
||||
})
|
||||
|
||||
it("should fail when building a prod environment without SECRET_KEY", () => {
|
||||
expect(() => config({ NODE_ENV: "production" }))
|
||||
.toThrow(new Error("Missing environment variable: SECRET_KEY"))
|
||||
})
|
||||
|
||||
it("should build a prod config with a SECRET_KEY", () => {
|
||||
const result = config({ NODE_ENV: "production", SECRET_KEY: "secret" })
|
||||
|
||||
expect(result).toHaveProperty("is_production", true)
|
||||
expect(result).toHaveProperty("secret_key", "secret")
|
||||
})
|
||||
|
||||
describe("jwt_secret", () => {
|
||||
it("should use default jwt_secret when environment is blank string", () => {
|
||||
const result = config({ JWT_SECRET: "" })
|
||||
|
||||
expect(result).toHaveProperty("is_production", false)
|
||||
expect(result).toHaveProperty("jwt_secret", "myjwtsecret")
|
||||
})
|
||||
})
|
||||
|
||||
describe("booleans", () => {
|
||||
it("should parse 'true' as true", () => {
|
||||
const result = config({ MEMORY_DB: "true" })
|
||||
|
||||
expect(result).toHaveProperty("memory_db", true)
|
||||
})
|
||||
it("should parse 'false' as false", () => {
|
||||
const result = config({ MEMORY_DB: "false" })
|
||||
|
||||
expect(result).toHaveProperty("memory_db", false)
|
||||
})
|
||||
it("should fail when it is not parseable", () => {
|
||||
expect(() => config({ MEMORY_DB: "foo" }))
|
||||
.toThrow(new Error("Invalid boolean value: foo"))
|
||||
})
|
||||
it("should default to false when the string is empty", () => {
|
||||
const result = config({ MEMORY_DB: "" })
|
||||
|
||||
expect(result).toHaveProperty("memory_db", false)
|
||||
})
|
||||
})
|
||||
it("should build a valid development config when no environment is set", () => {
|
||||
const emptyEnv = {}
|
||||
const result = config(emptyEnv)
|
||||
|
||||
expect(result).toHaveProperty("is_production", false)
|
||||
expect(result).toHaveProperty("port")
|
||||
expect(result).toHaveProperty("jwt_secret")
|
||||
expect(result).toHaveProperty("drift_home")
|
||||
expect(result).toHaveProperty("memory_db")
|
||||
expect(result).toHaveProperty("enable_admin")
|
||||
expect(result).toHaveProperty("secret_key")
|
||||
expect(result).toHaveProperty("registration_password")
|
||||
expect(result).toHaveProperty("welcome_content")
|
||||
expect(result).toHaveProperty("welcome_title")
|
||||
})
|
||||
|
||||
it("should fail when building a prod environment without SECRET_KEY", () => {
|
||||
expect(() => config({ NODE_ENV: "production" })).toThrow(
|
||||
new Error("Missing environment variable: SECRET_KEY")
|
||||
)
|
||||
})
|
||||
|
||||
it("should build a prod config with a SECRET_KEY", () => {
|
||||
const result = config({ NODE_ENV: "production", SECRET_KEY: "secret" })
|
||||
|
||||
expect(result).toHaveProperty("is_production", true)
|
||||
expect(result).toHaveProperty("secret_key", "secret")
|
||||
})
|
||||
|
||||
describe("jwt_secret", () => {
|
||||
it("should use default jwt_secret when environment is blank string", () => {
|
||||
const result = config({ JWT_SECRET: "" })
|
||||
|
||||
expect(result).toHaveProperty("is_production", false)
|
||||
expect(result).toHaveProperty("jwt_secret", "myjwtsecret")
|
||||
})
|
||||
})
|
||||
|
||||
describe("booleans", () => {
|
||||
it("should parse 'true' as true", () => {
|
||||
const result = config({ MEMORY_DB: "true" })
|
||||
|
||||
expect(result).toHaveProperty("memory_db", true)
|
||||
})
|
||||
it("should parse 'false' as false", () => {
|
||||
const result = config({ MEMORY_DB: "false" })
|
||||
|
||||
expect(result).toHaveProperty("memory_db", false)
|
||||
})
|
||||
it("should fail when it is not parseable", () => {
|
||||
expect(() => config({ MEMORY_DB: "foo" })).toThrow(
|
||||
new Error("Invalid boolean value: foo")
|
||||
)
|
||||
})
|
||||
it("should default to false when the string is empty", () => {
|
||||
const result = config({ MEMORY_DB: "" })
|
||||
|
||||
expect(result).toHaveProperty("memory_db", false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
import getHtmlFromFile from "@lib/get-html-from-drift-file"
|
||||
|
||||
describe("get-html-from-drift-file", () => {
|
||||
it("should not wrap markdown in code blocks", () => {
|
||||
const markdown = `## My Markdown`
|
||||
const html = getHtmlFromFile({ content: markdown, title: "my-markdown.md" })
|
||||
// the string is <h2><a href=\"#my-markdown\" id=\"my-markdown\" style=\"color:inherit\">My Markdown</a></h2>,
|
||||
// but we dont wan't to be too strict in case markup changes
|
||||
expect(html).toMatch(/<h2><a.*<\/a><\/h2>/)
|
||||
})
|
||||
|
||||
it("should wrap code in code blocks", () => {
|
||||
const code = `const foo = "bar"`
|
||||
const html = getHtmlFromFile({ content: code, title: "my-code.js" })
|
||||
expect(html).toMatch(/<pre><code class="prism-code language-js">/)
|
||||
})
|
||||
})
|
||||
@ -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<UserJwtRequest>
|
||||
let mockResponse: Partial<Response>
|
||||
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
|
||||
})
|
||||
Loading…
Reference in New Issue