mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-29 14:25:38 -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 = {
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
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) {
|
||||
export function publishNoteActions (req: any, res: Response, next: NextFunction) {
|
||||
NoteUtils.findNoteOrCreate(req, res, function (note) {
|
||||
const action = req.params.action
|
||||
switch (action) {
|
||||
|
@ -22,9 +21,9 @@ export module NoteController {
|
|||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function showPublishNote (req: any, res: Response, next: NextFunction) {
|
||||
export function showPublishNote (req: any, res: Response, next: NextFunction) {
|
||||
const include = [{
|
||||
model: User,
|
||||
as: 'owner'
|
||||
|
@ -53,9 +52,9 @@ export module NoteController {
|
|||
return errors.errorInternalError(res)
|
||||
})
|
||||
}, include)
|
||||
}
|
||||
}
|
||||
|
||||
export function showNote (req: any, res: Response, next: NextFunction) {
|
||||
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
|
||||
|
@ -78,9 +77,9 @@ export module NoteController {
|
|||
opengraph: opengraph
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function createFromPOST (req: any, res: Response, next: NextFunction) {
|
||||
export function createFromPOST (req: any, res: Response, next: NextFunction) {
|
||||
let body = ''
|
||||
if (req.body && req.body.length > config.documentMaxLength) {
|
||||
return errors.errorTooLong(res)
|
||||
|
@ -89,9 +88,9 @@ export module NoteController {
|
|||
}
|
||||
body = body.replace(/[\r]/g, '')
|
||||
return NoteUtils.newNote(req, res, body)
|
||||
}
|
||||
}
|
||||
|
||||
export function doAction (req: any, res: Response, next: NextFunction) {
|
||||
export function doAction (req: any, res: Response, next: NextFunction) {
|
||||
const noteId = req.params.noteId
|
||||
NoteUtils.findNoteOrCreate(req, res, (note) => {
|
||||
const action = req.params.action
|
||||
|
@ -120,9 +119,9 @@ export module NoteController {
|
|||
return res.redirect(config.serverURL + '/' + noteId)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function downloadMarkdown (req: Request, res: Response, note: any) {
|
||||
export function downloadMarkdown (req: Request, res: Response, note: any) {
|
||||
const body = note.content
|
||||
let filename = Note.decodeTitle(note.title)
|
||||
filename = encodeURIComponent(filename)
|
||||
|
@ -136,5 +135,4 @@ export module NoteController {
|
|||
'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,25 +1,22 @@
|
|||
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) {
|
||||
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) }
|
||||
})
|
||||
} else {
|
||||
res.redirect(config.serverURL + '/p/' + note.shortid)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function showPublishSlide(req: any, res: Response, next: NextFunction) {
|
||||
export function showPublishSlide (req: any, res: Response, next: NextFunction) {
|
||||
const include = [{
|
||||
model: User,
|
||||
as: 'owner'
|
||||
|
@ -48,5 +45,4 @@ export module SlideController {
|
|||
return errors.errorInternalError(res)
|
||||
})
|
||||
}, include)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,52 +1,14 @@
|
|||
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)
|
||||
}
|
||||
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) {
|
||||
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()) {
|
||||
|
@ -69,9 +31,53 @@ export module NoteUtils {
|
|||
logger.error(err)
|
||||
return errors.errorInternalError(res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function getPublishData (req: any, res: Response, note: any, callback: (data: any) => void) {
|
||||
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
|
||||
}
|
||||
}).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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -102,12 +108,4 @@ export module NoteUtils {
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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