mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-28 14:04:43 -04:00
Merge pull request #355 from davidmehren/tests-in-typescript
Migrate tests to typescript
This commit is contained in:
commit
7fe44cb867
14 changed files with 354 additions and 378 deletions
|
@ -6,9 +6,9 @@
|
|||
"license": "AGPL-3.0",
|
||||
"scripts": {
|
||||
"test": "npm run-script eslint && npm run-script jsonlint && npm run-script mocha-suite",
|
||||
"eslint": "node_modules/.bin/eslint --max-warnings 0 src",
|
||||
"eslint": "node_modules/.bin/eslint --ext .ts,.js --max-warnings 0 src",
|
||||
"jsonlint": "find . -not -path './node_modules/*' -type f -name '*.json' -o -type f -name '*.json.example' | while read json; do echo $json ; jq . $json; done",
|
||||
"mocha-suite": "NODE_ENV=test CMD_DB_URL=\"sqlite::memory:\" mocha --exit",
|
||||
"mocha-suite": "tsc && NODE_ENV=test CMD_DB_URL=\"sqlite::memory:\" mocha --exit built/test/*.js",
|
||||
"standard": "echo 'standard is no longer being used, use `npm run eslint` instead!' && exit 1",
|
||||
"dev": "webpack --config webpack.dev.js --progress --colors --watch",
|
||||
"heroku-prebuild": "bin/heroku",
|
||||
|
@ -175,6 +175,7 @@
|
|||
"@types/helmet": "^0.0.45",
|
||||
"@types/lodash": "^4.14.149",
|
||||
"@types/minio": "^7.0.5",
|
||||
"@types/mocha": "^7.0.2",
|
||||
"@types/node": "^13.11.1",
|
||||
"@types/passport": "^1.0.3",
|
||||
"@types/passport-facebook": "^2.1.9",
|
||||
|
@ -186,6 +187,7 @@
|
|||
"@types/passport-twitter": "^1.0.34",
|
||||
"@types/passport.socketio": "^3.7.2",
|
||||
"@types/randomcolor": "^0.5.4",
|
||||
"@types/sinon": "^9.0.0",
|
||||
"@types/validator": "^13.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^2.27.0",
|
||||
"@typescript-eslint/parser": "^2.27.0",
|
||||
|
@ -214,11 +216,12 @@
|
|||
"less-loader": "^5.0.0",
|
||||
"mini-css-extract-plugin": "^0.8.0",
|
||||
"mocha": "^5.2.0",
|
||||
"mock-require": "^3.0.3",
|
||||
"optimize-css-assets-webpack-plugin": "^5.0.3",
|
||||
"script-loader": "^0.7.2",
|
||||
"sinon": "^9.0.2",
|
||||
"string-loader": "^0.0.1",
|
||||
"style-loader": "^1.0.0",
|
||||
"ts-mock-imports": "^1.3.0",
|
||||
"ts-node": "^8.8.2",
|
||||
"typescript": "^3.7.2",
|
||||
"url-loader": "^2.3.0",
|
||||
|
|
|
@ -21,6 +21,8 @@ const debugConfig = {
|
|||
}
|
||||
|
||||
// Get version string from package.json
|
||||
// TODO: There are other ways to geht the current version
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const { version, repository } = require(path.join(appRootPath, 'package.json'))
|
||||
|
||||
const commitID = getGitCommit(appRootPath)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const config = require('./config')
|
||||
import { config } from './config'
|
||||
|
||||
function responseError (res, code: number, detail: string, msg: string): void {
|
||||
res.status(code).render('error.ejs', {
|
||||
|
|
1
src/lib/library-ext.d.ts
vendored
1
src/lib/library-ext.d.ts
vendored
|
@ -1,6 +1,5 @@
|
|||
import { User } from './models'
|
||||
|
||||
|
||||
declare module 'express' {
|
||||
export interface Request {
|
||||
user?: User;
|
||||
|
|
|
@ -6,7 +6,7 @@ import fs from 'fs'
|
|||
|
||||
import { logger } from './logger'
|
||||
|
||||
import { NoteUtils } from './web/note/util'
|
||||
import * as NoteUtils from './web/note/util'
|
||||
|
||||
import { errors } from './errors'
|
||||
|
||||
|
|
|
@ -3,10 +3,10 @@ import passport from 'passport'
|
|||
import LDAPStrategy from 'passport-ldapauth'
|
||||
|
||||
import { config } from '../../../config'
|
||||
import { User } from '../../../models'
|
||||
import { logger } from '../../../logger'
|
||||
import { urlencodedParser } from '../../utils'
|
||||
import { errors } from '../../../errors'
|
||||
import { logger } from '../../../logger'
|
||||
import { User } from '../../../models'
|
||||
import { urlencodedParser } from '../../utils'
|
||||
import { AuthMiddleware } from '../interface'
|
||||
|
||||
export const LdapMiddleware: AuthMiddleware = {
|
||||
|
@ -22,7 +22,7 @@ export const LdapMiddleware: AuthMiddleware = {
|
|||
searchFilter: config.ldap.searchFilter || null,
|
||||
searchAttributes: config.ldap.searchAttributes || null,
|
||||
tlsOptions: config.ldap.tlsOptions || null,
|
||||
starttls: config.ldap.starttls || null
|
||||
starttls: config.ldap.starttls || null
|
||||
}
|
||||
}, function (user, done) {
|
||||
let uuid = user.uidNumber || user.uid || user.sAMAccountName || undefined
|
||||
|
|
|
@ -1,140 +1,138 @@
|
|||
import { NextFunction, Request, Response } from 'express'
|
||||
import { NoteUtils } from './util'
|
||||
import * as ActionController from './actions'
|
||||
import { errors } from '../../errors'
|
||||
import { config } from '../../config'
|
||||
import { errors } from '../../errors'
|
||||
import { logger } from '../../logger'
|
||||
import { User, Note } from '../../models'
|
||||
import { Note, User } from '../../models'
|
||||
import * as ActionController from './actions'
|
||||
import * as NoteUtils from './util'
|
||||
|
||||
export module NoteController {
|
||||
export function publishNoteActions (req: any, res: Response, next: NextFunction) {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
const action = req.params.action
|
||||
switch (action) {
|
||||
case 'download':
|
||||
exports.downloadMarkdown(req, res, note)
|
||||
break
|
||||
case 'edit':
|
||||
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
|
||||
break
|
||||
default:
|
||||
res.redirect(config.serverURL + '/s/' + note.shortid)
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function showPublishNote (req: any, res: Response, next: NextFunction) {
|
||||
const include = [{
|
||||
model: User,
|
||||
as: 'owner'
|
||||
}, {
|
||||
model: User,
|
||||
as: 'lastchangeuser'
|
||||
}]
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
// force to use short id
|
||||
const shortid = req.params.shortid
|
||||
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
||||
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||
}
|
||||
note.increment('viewcount').then(function (note) {
|
||||
if (!note) {
|
||||
return errors.errorNotFound(res)
|
||||
}
|
||||
NoteUtils.getPublishData(req, res, note, (data) => {
|
||||
res.set({
|
||||
'Cache-Control': 'private' // only cache by client
|
||||
})
|
||||
return res.render('pretty.ejs', data)
|
||||
})
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
}, include)
|
||||
}
|
||||
|
||||
export function showNote (req: any, res: Response, next: NextFunction) {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
// force to use note id
|
||||
const noteId = req.params.noteId
|
||||
const id = Note.encodeNoteId(note.id)
|
||||
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) {
|
||||
return res.redirect(config.serverURL + '/' + (note.alias || id))
|
||||
}
|
||||
const body = note.content
|
||||
const extracted = Note.extractMeta(body)
|
||||
const meta = Note.parseMeta(extracted.meta)
|
||||
let title = Note.decodeTitle(note.title)
|
||||
title = Note.generateWebTitle(meta.title || title)
|
||||
const opengraph = Note.parseOpengraph(meta, title)
|
||||
res.set({
|
||||
'Cache-Control': 'private', // only cache by client
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
return res.render('codimd.ejs', {
|
||||
title: title,
|
||||
opengraph: opengraph
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function createFromPOST (req: any, res: Response, next: NextFunction) {
|
||||
let body = ''
|
||||
if (req.body && req.body.length > config.documentMaxLength) {
|
||||
return errors.errorTooLong(res)
|
||||
} else if (req.body) {
|
||||
body = req.body
|
||||
export function publishNoteActions (req: any, res: Response, next: NextFunction) {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
const action = req.params.action
|
||||
switch (action) {
|
||||
case 'download':
|
||||
exports.downloadMarkdown(req, res, note)
|
||||
break
|
||||
case 'edit':
|
||||
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
|
||||
break
|
||||
default:
|
||||
res.redirect(config.serverURL + '/s/' + note.shortid)
|
||||
break
|
||||
}
|
||||
body = body.replace(/[\r]/g, '')
|
||||
return NoteUtils.newNote(req, res, body)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function doAction (req: any, res: Response, next: NextFunction) {
|
||||
const noteId = req.params.noteId
|
||||
NoteUtils.findNoteOrCreate(req, res, (note) => {
|
||||
const action = req.params.action
|
||||
// TODO: Don't switch on action, choose action in Router and use separate functions
|
||||
switch (action) {
|
||||
case 'publish':
|
||||
case 'pretty': // pretty deprecated
|
||||
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||
break
|
||||
case 'slide':
|
||||
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
|
||||
break
|
||||
case 'download':
|
||||
exports.downloadMarkdown(req, res, note)
|
||||
break
|
||||
case 'info':
|
||||
ActionController.getInfo(req, res, note)
|
||||
break
|
||||
case 'gist':
|
||||
ActionController.createGist(req, res, note)
|
||||
break
|
||||
case 'revision':
|
||||
ActionController.getRevision(req, res, note)
|
||||
break
|
||||
default:
|
||||
return res.redirect(config.serverURL + '/' + noteId)
|
||||
export function showPublishNote (req: any, res: Response, next: NextFunction) {
|
||||
const include = [{
|
||||
model: User,
|
||||
as: 'owner'
|
||||
}, {
|
||||
model: User,
|
||||
as: 'lastchangeuser'
|
||||
}]
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
// force to use short id
|
||||
const shortid = req.params.shortid
|
||||
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
||||
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||
}
|
||||
note.increment('viewcount').then(function (note) {
|
||||
if (!note) {
|
||||
return errors.errorNotFound(res)
|
||||
}
|
||||
NoteUtils.getPublishData(req, res, note, (data) => {
|
||||
res.set({
|
||||
'Cache-Control': 'private' // only cache by client
|
||||
})
|
||||
return res.render('pretty.ejs', data)
|
||||
})
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
}
|
||||
}, include)
|
||||
}
|
||||
|
||||
export function downloadMarkdown (req: Request, res: Response, note: any) {
|
||||
export function showNote (req: any, res: Response, next: NextFunction) {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
// force to use note id
|
||||
const noteId = req.params.noteId
|
||||
const id = Note.encodeNoteId(note.id)
|
||||
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) {
|
||||
return res.redirect(config.serverURL + '/' + (note.alias || id))
|
||||
}
|
||||
const body = note.content
|
||||
let filename = Note.decodeTitle(note.title)
|
||||
filename = encodeURIComponent(filename)
|
||||
const extracted = Note.extractMeta(body)
|
||||
const meta = Note.parseMeta(extracted.meta)
|
||||
let title = Note.decodeTitle(note.title)
|
||||
title = Note.generateWebTitle(meta.title || title)
|
||||
const opengraph = Note.parseOpengraph(meta, title)
|
||||
res.set({
|
||||
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
||||
'Access-Control-Allow-Headers': 'Range',
|
||||
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
||||
'Content-Type': 'text/markdown; charset=UTF-8',
|
||||
'Cache-Control': 'private',
|
||||
'Content-disposition': 'attachment; filename=' + filename + '.md',
|
||||
'Cache-Control': 'private', // only cache by client
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
res.send(body)
|
||||
}
|
||||
return res.render('codimd.ejs', {
|
||||
title: title,
|
||||
opengraph: opengraph
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function createFromPOST (req: any, res: Response, next: NextFunction) {
|
||||
let body = ''
|
||||
if (req.body && req.body.length > config.documentMaxLength) {
|
||||
return errors.errorTooLong(res)
|
||||
} else if (req.body) {
|
||||
body = req.body
|
||||
}
|
||||
body = body.replace(/[\r]/g, '')
|
||||
return NoteUtils.newNote(req, res, body)
|
||||
}
|
||||
|
||||
export function doAction (req: any, res: Response, next: NextFunction) {
|
||||
const noteId = req.params.noteId
|
||||
NoteUtils.findNoteOrCreate(req, res, (note) => {
|
||||
const action = req.params.action
|
||||
// TODO: Don't switch on action, choose action in Router and use separate functions
|
||||
switch (action) {
|
||||
case 'publish':
|
||||
case 'pretty': // pretty deprecated
|
||||
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||
break
|
||||
case 'slide':
|
||||
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
|
||||
break
|
||||
case 'download':
|
||||
exports.downloadMarkdown(req, res, note)
|
||||
break
|
||||
case 'info':
|
||||
ActionController.getInfo(req, res, note)
|
||||
break
|
||||
case 'gist':
|
||||
ActionController.createGist(req, res, note)
|
||||
break
|
||||
case 'revision':
|
||||
ActionController.getRevision(req, res, note)
|
||||
break
|
||||
default:
|
||||
return res.redirect(config.serverURL + '/' + noteId)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function downloadMarkdown (req: Request, res: Response, note: any) {
|
||||
const body = note.content
|
||||
let filename = Note.decodeTitle(note.title)
|
||||
filename = encodeURIComponent(filename)
|
||||
res.set({
|
||||
'Access-Control-Allow-Origin': '*', // allow CORS as API
|
||||
'Access-Control-Allow-Headers': 'Range',
|
||||
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
||||
'Content-Type': 'text/markdown; charset=UTF-8',
|
||||
'Cache-Control': 'private',
|
||||
'Content-disposition': 'attachment; filename=' + filename + '.md',
|
||||
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
||||
})
|
||||
res.send(body)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { markdownParser } from '../utils'
|
||||
|
||||
import { SlideController } from './slide'
|
||||
import { NoteController } from './controller'
|
||||
import { Router } from 'express'
|
||||
import { markdownParser } from '../utils'
|
||||
import * as NoteController from './controller'
|
||||
|
||||
import * as SlideController from './slide'
|
||||
|
||||
const NoteRouter = Router()
|
||||
// get new note
|
||||
|
|
|
@ -1,52 +1,48 @@
|
|||
import { NextFunction, Response } from "express";
|
||||
import { NoteUtils } from "./util";
|
||||
import { NextFunction, Response } from 'express'
|
||||
import { config } from '../../config'
|
||||
import { errors } from '../../errors'
|
||||
import { logger } from '../../logger'
|
||||
import { config } from '../../config'
|
||||
import { User } from "../../models/user";
|
||||
import { Note } from "../../models/note";
|
||||
import { Note, User } from '../../models'
|
||||
import * as NoteUtils from './util'
|
||||
|
||||
|
||||
export module SlideController {
|
||||
export function publishSlideActions(req: any, res: Response, next: NextFunction) {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
const action = req.params.action
|
||||
if (action === 'edit') {
|
||||
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
|
||||
} else { res.redirect(config.serverURL + '/p/' + note.shortid) }
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function showPublishSlide(req: any, res: Response, next: NextFunction) {
|
||||
const include = [{
|
||||
model: User,
|
||||
as: 'owner'
|
||||
}, {
|
||||
model: User,
|
||||
as: 'lastchangeuser'
|
||||
}]
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
// force to use short id
|
||||
const shortid = req.params.shortid
|
||||
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
||||
return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
|
||||
}
|
||||
note.increment('viewcount').then(function (note) {
|
||||
if (!note) {
|
||||
return errors.errorNotFound(res)
|
||||
}
|
||||
NoteUtils.getPublishData(req, res, note, (data) => {
|
||||
res.set({
|
||||
'Cache-Control': 'private' // only cache by client
|
||||
})
|
||||
return res.render('slide.ejs', data)
|
||||
})
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
}, include)
|
||||
}
|
||||
export function publishSlideActions (req: any, res: Response, next: NextFunction) {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
const action = req.params.action
|
||||
if (action === 'edit') {
|
||||
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)) + '?both')
|
||||
} else {
|
||||
res.redirect(config.serverURL + '/p/' + note.shortid)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function showPublishSlide (req: any, res: Response, next: NextFunction) {
|
||||
const include = [{
|
||||
model: User,
|
||||
as: 'owner'
|
||||
}, {
|
||||
model: User,
|
||||
as: 'lastchangeuser'
|
||||
}]
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
// force to use short id
|
||||
const shortid = req.params.shortid
|
||||
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
||||
return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
|
||||
}
|
||||
note.increment('viewcount').then(function (note) {
|
||||
if (!note) {
|
||||
return errors.errorNotFound(res)
|
||||
}
|
||||
NoteUtils.getPublishData(req, res, note, (data) => {
|
||||
res.set({
|
||||
'Cache-Control': 'private' // only cache by client
|
||||
})
|
||||
return res.render('slide.ejs', data)
|
||||
})
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
}, include)
|
||||
}
|
||||
|
|
|
@ -1,113 +1,111 @@
|
|||
import { Includeable } from 'sequelize'
|
||||
import { Response } from 'express'
|
||||
import fs from 'fs'
|
||||
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
import { errors } from '../../errors'
|
||||
import { Includeable } from 'sequelize'
|
||||
import { config } from '../../config'
|
||||
import { errors } from '../../errors'
|
||||
import { logger } from '../../logger'
|
||||
import { Note , User } from '../../models'
|
||||
import { Note, User } from '../../models'
|
||||
|
||||
export module NoteUtils {
|
||||
export function findNoteOrCreate(req, res, callback: (note: any) => void, include?: Includeable[]) {
|
||||
const id = req.params.noteId || req.params.shortid
|
||||
Note.parseNoteId(id, function (err, _id) {
|
||||
if (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
export function newNote (req: any, res: Response, body: string | null) {
|
||||
let owner = null
|
||||
const noteId = req.params.noteId ? req.params.noteId : null
|
||||
if (req.isAuthenticated()) {
|
||||
owner = req.user.id
|
||||
} else if (!config.allowAnonymous) {
|
||||
return errors.errorForbidden(res)
|
||||
}
|
||||
if (config.allowFreeURL && noteId && !config.forbiddenNoteIDs.includes(noteId)) {
|
||||
req.alias = noteId
|
||||
} else if (noteId) {
|
||||
return req.method === 'POST' ? errors.errorForbidden(res) : errors.errorNotFound(res)
|
||||
}
|
||||
Note.create({
|
||||
ownerId: owner,
|
||||
alias: req.alias ? req.alias : null,
|
||||
content: body
|
||||
}).then(function (note) {
|
||||
return res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)))
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
}
|
||||
|
||||
export function checkViewPermission (req: any, note: any) {
|
||||
if (note.permission === 'private') {
|
||||
return req.isAuthenticated() && note.ownerId === req.user.id
|
||||
} else if (note.permission === 'limited' || note.permission === 'protected') {
|
||||
return req.isAuthenticated()
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
export function findNoteOrCreate (req, res, callback: (note: any) => void, include?: Includeable[]) {
|
||||
const id = req.params.noteId || req.params.shortid
|
||||
Note.parseNoteId(id, function (err, _id) {
|
||||
if (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
}
|
||||
Note.findOne({
|
||||
where: {
|
||||
id: _id
|
||||
}
|
||||
Note.findOne({
|
||||
where: {
|
||||
id: _id
|
||||
}
|
||||
}).then(function (note) {
|
||||
if (!note) {
|
||||
return newNote(req, res, "")
|
||||
}
|
||||
if (!checkViewPermission(req, note)) {
|
||||
return errors.errorForbidden(res)
|
||||
} else {
|
||||
return callback(note)
|
||||
}
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function checkViewPermission (req: any, note: any) {
|
||||
if (note.permission === 'private') {
|
||||
return req.isAuthenticated() && note.ownerId === req.user.id
|
||||
} else if (note.permission === 'limited' || note.permission === 'protected') {
|
||||
return req.isAuthenticated()
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
export function newNote (req: any, res: Response, body: string | null) {
|
||||
let owner = null
|
||||
const noteId = req.params.noteId ? req.params.noteId : null
|
||||
if (req.isAuthenticated()) {
|
||||
owner = req.user.id
|
||||
} else if (!config.allowAnonymous) {
|
||||
return errors.errorForbidden(res)
|
||||
}
|
||||
if (config.allowFreeURL && noteId && !config.forbiddenNoteIDs.includes(noteId)) {
|
||||
req.alias = noteId
|
||||
} else if (noteId) {
|
||||
return req.method === 'POST' ? errors.errorForbidden(res) : errors.errorNotFound(res)
|
||||
}
|
||||
Note.create({
|
||||
ownerId: owner,
|
||||
alias: req.alias ? req.alias : null,
|
||||
content: body
|
||||
}).then(function (note) {
|
||||
return res.redirect(config.serverURL + '/' + (note.alias ? note.alias : Note.encodeNoteId(note.id)))
|
||||
if (!note) {
|
||||
return newNote(req, res, '')
|
||||
}
|
||||
if (!checkViewPermission(req, note)) {
|
||||
return errors.errorForbidden(res)
|
||||
} else {
|
||||
return callback(note)
|
||||
}
|
||||
}).catch(function (err) {
|
||||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
}
|
||||
|
||||
export function getPublishData (req: any, res: Response, note: any, callback: (data: any) => void) {
|
||||
const body = note.content
|
||||
const extracted = Note.extractMeta(body)
|
||||
const markdown = extracted.markdown
|
||||
const meta = Note.parseMeta(extracted.meta)
|
||||
const createtime = note.createdAt
|
||||
const updatetime = note.lastchangeAt
|
||||
let title = Note.decodeTitle(note.title)
|
||||
title = Note.generateWebTitle(meta.title || title)
|
||||
const ogdata = Note.parseOpengraph(meta, title)
|
||||
const data = {
|
||||
title: title,
|
||||
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
|
||||
viewcount: note.viewcount,
|
||||
createtime: createtime,
|
||||
updatetime: updatetime,
|
||||
body: markdown,
|
||||
theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme),
|
||||
meta: JSON.stringify(extracted.meta),
|
||||
owner: note.owner ? note.owner.id : null,
|
||||
ownerprofile: note.owner ? User.getProfile(note.owner) : null,
|
||||
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
||||
lastchangeuserprofile: note.lastchangeuser ? User.getProfile(note.lastchangeuser) : null,
|
||||
robots: meta.robots || false, // default allow robots
|
||||
GA: meta.GA,
|
||||
disqus: meta.disqus,
|
||||
cspNonce: res.locals.nonce,
|
||||
dnt: req.headers.dnt,
|
||||
opengraph: ogdata
|
||||
}
|
||||
callback(data)
|
||||
}
|
||||
|
||||
function isRevealTheme (theme: string) {
|
||||
if (fs.existsSync(path.join(__dirname, '..', '..', '..', 'public', 'build', 'reveal.js', 'css', 'theme', theme + '.css'))) {
|
||||
return theme
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function isRevealTheme (theme: string) {
|
||||
if (fs.existsSync(path.join(__dirname, '..', '..', '..', '..', 'public', 'build', 'reveal.js', 'css', 'theme', theme + '.css'))) {
|
||||
return theme
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export function getPublishData (req: any, res: Response, note: any, callback: (data: any) => void) {
|
||||
const body = note.content
|
||||
const extracted = Note.extractMeta(body)
|
||||
const markdown = extracted.markdown
|
||||
const meta = Note.parseMeta(extracted.meta)
|
||||
const createtime = note.createdAt
|
||||
const updatetime = note.lastchangeAt
|
||||
let title = Note.decodeTitle(note.title)
|
||||
title = Note.generateWebTitle(meta.title || title)
|
||||
const ogdata = Note.parseOpengraph(meta, title)
|
||||
const data = {
|
||||
title: title,
|
||||
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
|
||||
viewcount: note.viewcount,
|
||||
createtime: createtime,
|
||||
updatetime: updatetime,
|
||||
body: markdown,
|
||||
theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme),
|
||||
meta: JSON.stringify(extracted.meta),
|
||||
owner: note.owner ? note.owner.id : null,
|
||||
ownerprofile: note.owner ? User.getProfile(note.owner) : null,
|
||||
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
||||
lastchangeuserprofile: note.lastchangeuser ? User.getProfile(note.lastchangeuser) : null,
|
||||
robots: meta.robots || false, // default allow robots
|
||||
GA: meta.GA,
|
||||
disqus: meta.disqus,
|
||||
cspNonce: res.locals.nonce,
|
||||
dnt: req.headers.dnt,
|
||||
opengraph: ogdata
|
||||
}
|
||||
callback(data)
|
||||
}
|
||||
|
|
|
@ -110,47 +110,47 @@ process.on('message', function (data: Data) {
|
|||
return logger.error('dmp worker error: not enough data')
|
||||
}
|
||||
switch (data.msg) {
|
||||
case 'create patch':
|
||||
if (data.lastDoc === undefined || data.currDoc === undefined) {
|
||||
return logger.error('dmp worker error: not enough data on create patch')
|
||||
}
|
||||
try {
|
||||
const patch: string = createPatch(data.lastDoc, data.currDoc)
|
||||
processSend({
|
||||
msg: 'check',
|
||||
result: patch,
|
||||
cacheKey: data.cacheKey
|
||||
})
|
||||
} catch (err) {
|
||||
logger.error('create patch: dmp worker error', err)
|
||||
processSend({
|
||||
msg: 'error',
|
||||
error: err,
|
||||
cacheKey: data.cacheKey
|
||||
})
|
||||
}
|
||||
break
|
||||
case 'get revision':
|
||||
if (data.revisions === undefined || data.count === undefined) {
|
||||
return logger.error('dmp worker error: not enough data on get revision')
|
||||
}
|
||||
try {
|
||||
case 'create patch':
|
||||
if (data.lastDoc === undefined || data.currDoc === undefined) {
|
||||
return logger.error('dmp worker error: not enough data on create patch')
|
||||
}
|
||||
try {
|
||||
const patch: string = createPatch(data.lastDoc, data.currDoc)
|
||||
processSend({
|
||||
msg: 'check',
|
||||
result: patch,
|
||||
cacheKey: data.cacheKey
|
||||
})
|
||||
} catch (err) {
|
||||
logger.error('create patch: dmp worker error', err)
|
||||
processSend({
|
||||
msg: 'error',
|
||||
error: err,
|
||||
cacheKey: data.cacheKey
|
||||
})
|
||||
}
|
||||
break
|
||||
case 'get revision':
|
||||
if (data.revisions === undefined || data.count === undefined) {
|
||||
return logger.error('dmp worker error: not enough data on get revision')
|
||||
}
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
const result: { content: string; patch: patch_obj[]; authorship: string } = getRevision(data.revisions, data.count)
|
||||
processSend({
|
||||
msg: 'check',
|
||||
result: result,
|
||||
cacheKey: data.cacheKey
|
||||
})
|
||||
} catch (err) {
|
||||
logger.error('get revision: dmp worker error', err)
|
||||
processSend({
|
||||
msg: 'error',
|
||||
error: err,
|
||||
cacheKey: data.cacheKey
|
||||
})
|
||||
}
|
||||
break
|
||||
const result: { content: string; patch: patch_obj[]; authorship: string } = getRevision(data.revisions, data.count)
|
||||
processSend({
|
||||
msg: 'check',
|
||||
result: result,
|
||||
cacheKey: data.cacheKey
|
||||
})
|
||||
} catch (err) {
|
||||
logger.error('get revision: dmp worker error', err)
|
||||
processSend({
|
||||
msg: 'error',
|
||||
error: err,
|
||||
cacheKey: data.cacheKey
|
||||
})
|
||||
}
|
||||
break
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
/* eslint-env node, mocha */
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert')
|
||||
const crypto = require('crypto')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const mock = require('mock-require')
|
||||
import assert from 'assert'
|
||||
import crypto from 'crypto'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import * as configModule from '../lib/config'
|
||||
import { ImportMock } from 'ts-mock-imports'
|
||||
|
||||
describe('Content security policies', function () {
|
||||
let defaultConfig, csp
|
||||
|
@ -31,22 +32,11 @@ describe('Content security policies', function () {
|
|||
}
|
||||
})
|
||||
|
||||
afterEach(function () {
|
||||
mock.stop('../lib/config')
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
})
|
||||
|
||||
after(function () {
|
||||
mock.stopAll()
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
})
|
||||
|
||||
// beginnging Tests
|
||||
it('Disable CDN', function () {
|
||||
let testconfig = defaultConfig
|
||||
const testconfig = defaultConfig
|
||||
testconfig.useCDN = false
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig)
|
||||
|
||||
assert(!csp.computeDirectives().scriptSrc.includes('https://cdnjs.cloudflare.com'))
|
||||
assert(!csp.computeDirectives().scriptSrc.includes('https://cdn.mathjax.org'))
|
||||
|
@ -57,19 +47,17 @@ describe('Content security policies', function () {
|
|||
})
|
||||
|
||||
it('Disable Google Analytics', function () {
|
||||
let testconfig = defaultConfig
|
||||
const testconfig = defaultConfig
|
||||
testconfig.csp.addGoogleAnalytics = false
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig)
|
||||
|
||||
assert(!csp.computeDirectives().scriptSrc.includes('https://www.google-analytics.com'))
|
||||
})
|
||||
|
||||
it('Disable Disqus', function () {
|
||||
let testconfig = defaultConfig
|
||||
const testconfig = defaultConfig
|
||||
testconfig.csp.addDisqus = false
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig)
|
||||
|
||||
assert(!csp.computeDirectives().scriptSrc.includes('https://disqus.com'))
|
||||
assert(!csp.computeDirectives().scriptSrc.includes('https://*.disqus.com'))
|
||||
|
@ -79,18 +67,16 @@ describe('Content security policies', function () {
|
|||
})
|
||||
|
||||
it('Set ReportURI', function () {
|
||||
let testconfig = defaultConfig
|
||||
const testconfig = defaultConfig
|
||||
testconfig.csp.reportURI = 'https://example.com/reportURI'
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig)
|
||||
|
||||
assert.strictEqual(csp.computeDirectives().reportUri, 'https://example.com/reportURI')
|
||||
})
|
||||
|
||||
it('Set own directives', function () {
|
||||
let testconfig = defaultConfig
|
||||
mock('../lib/config', defaultConfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
const testconfig = defaultConfig
|
||||
ImportMock.mockOther(configModule, 'config', testconfig)
|
||||
const unextendedCSP = csp.computeDirectives()
|
||||
testconfig.csp.directives = {
|
||||
defaultSrc: ['https://default.example.com'],
|
||||
|
@ -103,8 +89,7 @@ describe('Content security policies', function () {
|
|||
childSrc: ['https://child.example.com'],
|
||||
connectSrc: ['https://connect.example.com']
|
||||
}
|
||||
mock('../lib/config', testconfig)
|
||||
csp = mock.reRequire('../lib/csp')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig)
|
||||
|
||||
const variations = ['default', 'script', 'img', 'style', 'font', 'object', 'media', 'child', 'connect']
|
||||
|
||||
|
@ -118,7 +103,7 @@ describe('Content security policies', function () {
|
|||
*/
|
||||
it('Unchanged hash for reveal.js speaker notes plugin', function () {
|
||||
const hash = crypto.createHash('sha1')
|
||||
hash.update(fs.readFileSync(path.resolve(__dirname, '../node_modules/reveal.js/plugin/notes/notes.html'), 'utf8'), 'utf8')
|
||||
hash.update(fs.readFileSync(path.join(process.cwd(), '/node_modules/reveal.js/plugin/notes/notes.html'), 'utf8'), 'utf8')
|
||||
assert.strictEqual(hash.digest('hex'), 'd5d872ae49b5db27f638b152e6e528837204d380')
|
||||
})
|
||||
})
|
|
@ -2,20 +2,21 @@
|
|||
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert')
|
||||
const mock = require('mock-require')
|
||||
import { ImportMock } from 'ts-mock-imports'
|
||||
import * as configModule from '../lib/config'
|
||||
|
||||
import assert from 'assert'
|
||||
import * as avatars from '../lib/letter-avatars'
|
||||
|
||||
describe('generateAvatarURL() gravatar enabled', function () {
|
||||
let avatars
|
||||
beforeEach(function () {
|
||||
// Reset config to make sure we don't influence other tests
|
||||
let testconfig = {
|
||||
const testconfig = {
|
||||
allowGravatar: true,
|
||||
serverURL: 'http://localhost:3000',
|
||||
port: 3000
|
||||
}
|
||||
mock('../lib/config', testconfig)
|
||||
avatars = mock.reRequire('../lib/letter-avatars')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig)
|
||||
})
|
||||
|
||||
it('should return correct urls', function () {
|
||||
|
@ -29,16 +30,14 @@ describe('generateAvatarURL() gravatar enabled', function () {
|
|||
})
|
||||
|
||||
describe('generateAvatarURL() gravatar disabled', function () {
|
||||
let avatars
|
||||
beforeEach(function () {
|
||||
// Reset config to make sure we don't influence other tests
|
||||
let testconfig = {
|
||||
const testconfig = {
|
||||
allowGravatar: false,
|
||||
serverURL: 'http://localhost:3000',
|
||||
port: 3000
|
||||
}
|
||||
mock('../lib/config', testconfig)
|
||||
avatars = mock.reRequire('../lib/letter-avatars')
|
||||
ImportMock.mockOther(configModule, 'config', testconfig)
|
||||
})
|
||||
|
||||
it('should return correct urls', function () {
|
|
@ -1,15 +1,11 @@
|
|||
/* eslint-env node, mocha */
|
||||
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert')
|
||||
|
||||
const models = require('../lib/models')
|
||||
const User = models.User
|
||||
import { User, sequelize } from '../lib/models'
|
||||
import assert = require('assert')
|
||||
|
||||
describe('User Sequelize model', function () {
|
||||
beforeEach(() => {
|
||||
return models.sequelize.sync({ force: true })
|
||||
return sequelize.sync({ force: true })
|
||||
})
|
||||
|
||||
it('stores a password hash on creation and verifies that password', function () {
|
Loading…
Add table
Add a link
Reference in a new issue