diff --git a/.circleci/config.yml b/.circleci/config.yml index 00cbdf5bc..2025e8fe7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -70,9 +70,15 @@ jobs: - run: name: Test - Hard Breaks command: npm run test:hard-breaks + - run: + name: Test - Non-Breaking Spaces + command: npm run test:non-breaking-spaces - run: name: Test - Variables command: npm run test:variables + - run: + name: Test - Emojis + command: npm run test:emojis - run: name: Test - Routes command: npm run test:route diff --git a/package-lock.json b/package-lock.json index 94178b331..bc4c66238 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,7 +40,7 @@ "marked-smartypants-lite": "^1.0.2", "markedLegacy": "npm:marked@^0.3.19", "moment": "^2.30.1", - "mongoose": "^8.9.1", + "mongoose": "^8.9.2", "nanoid": "5.0.9", "nconf": "^0.12.1", "react": "^18.3.1", @@ -55,9 +55,9 @@ "@stylistic/stylelint-plugin": "^3.1.1", "babel-plugin-transform-import-meta": "^2.2.1", "eslint": "^9.17.0", - "eslint-plugin-jest": "^28.9.0", + "eslint-plugin-jest": "^28.10.0", "eslint-plugin-react": "^7.37.2", - "globals": "^15.13.0", + "globals": "^15.14.0", "jest": "^29.7.0", "jest-expect-message": "^1.1.3", "jsdom-global": "^3.0.2", @@ -5618,9 +5618,9 @@ } }, "node_modules/eslint-plugin-jest": { - "version": "28.9.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-jest/-/eslint-plugin-jest-28.9.0.tgz", - "integrity": "sha512-rLu1s1Wf96TgUUxSw6loVIkNtUjq1Re7A9QdCCHSohnvXEBAjuL420h0T/fMmkQlNsQP2GhQzEUpYHPfxBkvYQ==", + "version": "28.10.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-jest/-/eslint-plugin-jest-28.10.0.tgz", + "integrity": "sha512-hyMWUxkBH99HpXT3p8hc7REbEZK3D+nk8vHXGgpB+XXsi0gO4PxMSP+pjfUzb67GnV9yawV9a53eUmcde1CCZA==", "dev": true, "dependencies": { "@typescript-eslint/utils": "^6.0.0 || ^7.0.0 || ^8.0.0" @@ -6714,11 +6714,10 @@ } }, "node_modules/globals": { - "version": "15.13.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-15.13.0.tgz", - "integrity": "sha512-49TewVEz0UxZjr1WYYsWpPrhyC/B/pA8Bq0fUmet2n+eR7yn0IvNzNaoBwnK6mdkzcN+se7Ez9zUgULTz2QH4g==", + "version": "15.14.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-15.14.0.tgz", + "integrity": "sha512-OkToC372DtlQeje9/zHIo5CT8lRP/FUgEOKBEhU4e0abL7J7CD24fD9ohiLN5hagG/kWCYj4K5oaxxtj2Z0Dig==", "dev": true, - "license": "MIT", "engines": { "node": ">=18" }, @@ -10127,9 +10126,9 @@ } }, "node_modules/mongoose": { - "version": "8.9.1", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.9.1.tgz", - "integrity": "sha512-whM6lWMdeKlUm4d2LSLS/q6cWtTp13lUrL5hy2YTsQdTSN+dsAu8HLdLUQOEgtBE59qp4IqLrjSXCSETbxhkQQ==", + "version": "8.9.2", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.9.2.tgz", + "integrity": "sha512-mLWynmZS1v8HTeMxyLhskQncS1SkrjW1eLNuFDYGQMQ/5QrFrxTLNwWXeCRZeKT2lXyaxW8bnJC9AKPT9jYMkw==", "dependencies": { "bson": "^6.10.1", "kareem": "2.6.3", diff --git a/package.json b/package.json index 38b25bc77..2ce79f112 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "test:mustache-syntax:injection": "jest \".*(mustache-syntax).*\" -t '^Injection:.*' --verbose --noStackTrace", "test:definition-lists": "jest tests/markdown/definition-lists.test.js --verbose --noStackTrace", "test:hard-breaks": "jest tests/markdown/hard-breaks.test.js --verbose --noStackTrace", + "test:non-breaking-spaces": "jest tests/markdown/non-breaking-spaces.test.js --verbose --noStackTrace", "test:emojis": "jest tests/markdown/emojis.test.js --verbose --noStackTrace", "test:route": "jest tests/routes/static-pages.test.js --verbose", "test:safehtml": "jest tests/html/safeHTML.test.js --verbose", @@ -113,7 +114,7 @@ "marked-smartypants-lite": "^1.0.2", "markedLegacy": "npm:marked@^0.3.19", "moment": "^2.30.1", - "mongoose": "^8.9.1", + "mongoose": "^8.9.2", "nanoid": "5.0.9", "nconf": "^0.12.1", "react": "^18.3.1", @@ -128,9 +129,9 @@ "@stylistic/stylelint-plugin": "^3.1.1", "babel-plugin-transform-import-meta": "^2.2.1", "eslint": "^9.17.0", - "eslint-plugin-jest": "^28.9.0", + "eslint-plugin-jest": "^28.10.0", "eslint-plugin-react": "^7.37.2", - "globals": "^15.13.0", + "globals": "^15.14.0", "jest": "^29.7.0", "jest-expect-message": "^1.1.3", "jsdom-global": "^3.0.2", diff --git a/server/admin.api.js b/server/admin.api.js index 02cdcb2f7..1a39f020b 100644 --- a/server/admin.api.js +++ b/server/admin.api.js @@ -1,5 +1,5 @@ -import {model as HomebrewModel } from './homebrew.model.js'; -import {model as NotificationModel } from './notifications.model.js'; +import { model as HomebrewModel } from './homebrew.model.js'; +import { model as NotificationModel } from './notifications.model.js'; import express from 'express'; import Moment from 'moment'; import zlib from 'zlib'; @@ -108,6 +108,9 @@ router.put('/admin/clean/script/:id', asyncHandler(HomebrewAPI.getBrew('admin', req.body = brew; + // Remove Account from request to prevent Admin user from being added to brew as an Author + req.account = undefined; + return await HomebrewAPI.updateBrew(req, res); }); diff --git a/server/app.js b/server/app.js index c91bbc57f..4dec6b4c4 100644 --- a/server/app.js +++ b/server/app.js @@ -351,11 +351,12 @@ app.get('/user/:username', async (req, res, next)=>{ //Change author name on brews app.put('/api/user/rename', async (req, res)=>{ const { username, newUsername } = req.body; - console.log(req.account); + const ownAccount = req.account && (req.account.username == newUsername); - if(!username || !newUsername) { + if(!username || !newUsername) return res.status(400).json({ error: 'Username and newUsername are required.' }); - } + if(!ownAccount) + return res.status(403).json({ error: 'Must be logged in to change your username' }); try { const brews = await HomebrewModel.getByUser(username, true, ['authors']); const renamePromises = brews.map(async (brew)=>{ diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 159c08b47..9a479732e 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -106,12 +106,12 @@ const api = { stub = stub?.toObject(); googleId ??= stub?.googleId; - const isOwner = stub?.authors?.length === 0 || stub?.authors?.[0] === req.account?.username; + const isOwner = (accessType == 'edit' && (!stub || stub?.authors?.length === 0)) || stub?.authors?.[0] === req.account?.username; const isAuthor = stub?.authors?.includes(req.account?.username); const isInvited = stub?.invitedAuthors?.includes(req.account?.username); if(accessType === 'edit' && !(isOwner || isAuthor || isInvited)) { - const accessError = { name: 'Access Error', status: 401, authors: stub.authors, brewTitle: stub.title, shareId: stub.shareId }; + const accessError = { name: 'Access Error', status: 401, authors: stub?.authors, brewTitle: stub?.title, shareId: stub?.shareId }; if(req.account) throw { ...accessError, message: 'User is not an Author', HBErrorCode: '03' }; else @@ -119,13 +119,13 @@ const api = { } if(stub?.lock?.locked && accessType != 'edit') { - throw { HBErrorCode: '51', code: stub.lock.code, message: stub.lock.shareMessage, brewId: stub.shareId, brewTitle: stub.title }; + throw { HBErrorCode: '51', code: stub?.lock.code, message: stub?.lock.shareMessage, brewId: stub?.shareId, brewTitle: stub?.title }; } - // If there is a google id, try to find the google brew - if(!stubOnly && googleId) { - const oAuth2Client = isOwner? GoogleActions.authCheck(req.account, res) : undefined; - + // If there's a google id, get it if requesting the full brew or if no stub found yet + if(googleId && (!stubOnly || !stub)) { + const oAuth2Client = isOwner ? GoogleActions.authCheck(req.account, res) : undefined; + const googleBrew = await GoogleActions.getGoogleBrew(oAuth2Client, googleId, id, accessType) .catch((googleError)=>{ const reason = googleError.errors?.[0].reason; diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index c87121ddc..1a5ca465f 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -428,10 +428,31 @@ const forcedParagraphBreaks = { } }; +const nonbreakingSpaces = { + name : 'nonbreakingSpaces', + level : 'inline', + start(src) { return src.match(/:>+/m)?.index; }, // Hint to Marked.js to stop and check for a match + tokenizer(src, tokens) { + const regex = /:(>+)/ym; + const match = regex.exec(src); + if(match?.length) { + return { + type : 'nonbreakingSpaces', // Should match "name" above + raw : match[0], // Text to consume from the source + length : match[1].length, + text : '' + }; + } + }, + renderer(token) { + return ` `.repeat(token.length).concat(''); + } +}; + const definitionListsSingleLine = { name : 'definitionListsSingleLine', level : 'block', - start(src) { return src.match(/\n[^\n]*?::[^\n]*/m)?.index; }, // Hint to Marked.js to stop and check for a match + start(src) { return src.match(/\n[^\n]*?::[^\n]*/m)?.index; }, // Hint to Marked.js to stop and check for a match tokenizer(src, tokens) { const regex = /^([^\n]*?)::([^\n]*)(?:\n|$)/ym; let match; @@ -785,11 +806,12 @@ const tableTerminators = [ ]; Marked.use(MarkedVariables()); -Marked.use({ extensions : [justifiedParagraphs, definitionListsMultiLine, definitionListsSingleLine, forcedParagraphBreaks, superSubScripts, - mustacheSpans, mustacheDivs, mustacheInjectInline] }); +Marked.use({ extensions : [justifiedParagraphs, definitionListsMultiLine, definitionListsSingleLine, forcedParagraphBreaks, + nonbreakingSpaces, superSubScripts, mustacheSpans, mustacheDivs, mustacheInjectInline] }); Marked.use(mustacheInjectBlock); Marked.use({ renderer: renderer, tokenizer: tokenizer, mangle: false }); -Marked.use(MarkedExtendedTables(tableTerminators), MarkedGFMHeadingId({ globalSlugs: true }), MarkedSmartypantsLite(), MarkedEmojis(MarkedEmojiOptions)); +Marked.use(MarkedExtendedTables(tableTerminators), MarkedGFMHeadingId({ globalSlugs: true }), + MarkedSmartypantsLite(), MarkedEmojis(MarkedEmojiOptions)); function cleanUrl(href) { try { diff --git a/tests/markdown/non-breaking-spaces.test.js b/tests/markdown/non-breaking-spaces.test.js new file mode 100644 index 000000000..9dad4eb0f --- /dev/null +++ b/tests/markdown/non-breaking-spaces.test.js @@ -0,0 +1,72 @@ +/* eslint-disable max-lines */ + +import Markdown from 'naturalcrit/markdown.js'; + +describe('Non-Breaking Spaces', ()=>{ + test('Single Space', function() { + const source = ':>\n\n'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

 

`); + }); + + test('Double Space', function() { + const source = ':>>\n\n'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

  

`); + }); + + test('Triple Space', function() { + const source = ':>>>\n\n'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

   

`); + }); + + test('Many Space', function() { + const source = ':>>>>>>>>>>\n\n'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

          

`); + }); + + test('Multiple sets of Spaces', function() { + const source = ':>>>\n:>>>\n:>>>'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

   \n   \n   

`); + }); + + test('Pair of inline Spaces', function() { + const source = ':>>:>>'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

    

`); + }); + + test('Space directly between two paragraphs', function() { + const source = 'Line 1\n:>>\nLine 2'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

Line 1\n  \nLine 2

`); + }); + + test('Ignored inside a code block', function() { + const source = '```\n\n:>\n\n```\n'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`
\n:>\n
`); + }); + + test('I am actually a single-line definition list!', function() { + const source = 'Term ::> Definition 1\n'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`
Term
> Definition 1
\n
`); + }); + + test('I am actually a definition list!', function() { + const source = 'Term\n::> Definition 1\n'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`
Term
\n
> Definition 1
`); + }); + + test('I am actually a two-term definition list!', function() { + const source = 'Term\n::> Definition 1\n::>> Definition 2'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`
Term
\n
> Definition 1
\n
>> Definition 2
`); + }); +}); + diff --git a/tests/markdown/justification.test.js b/tests/markdown/paragraph-justification.test.js similarity index 100% rename from tests/markdown/justification.test.js rename to tests/markdown/paragraph-justification.test.js