mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-08 18:22:40 +00:00
Merge branch 'experimentalNotificationDB' of https://github.com/G-Ambatte/homebrewery into experimentalNotificationDB
This commit is contained in:
@@ -55,9 +55,15 @@ jobs:
|
|||||||
at: .
|
at: .
|
||||||
|
|
||||||
# run tests!
|
# run tests!
|
||||||
|
- run:
|
||||||
|
name: Test - API Unit Tests
|
||||||
|
command: npm run test:api-unit
|
||||||
- run:
|
- run:
|
||||||
name: Test - Basic
|
name: Test - Basic
|
||||||
command: npm run test:basic
|
command: npm run test:basic
|
||||||
|
- run:
|
||||||
|
name: Test - Coverage
|
||||||
|
command: npm run test:coverage
|
||||||
- run:
|
- run:
|
||||||
name: Test - Mustache Spans
|
name: Test - Mustache Spans
|
||||||
command: npm run test:mustache-span
|
command: npm run test:mustache-span
|
||||||
|
|||||||
@@ -57,6 +57,13 @@ pre {
|
|||||||
## changelog
|
## changelog
|
||||||
For a full record of development, visit our [Github Page](https://github.com/naturalcrit/homebrewery).
|
For a full record of development, visit our [Github Page](https://github.com/naturalcrit/homebrewery).
|
||||||
|
|
||||||
|
### v3.6.0
|
||||||
|
{{taskList
|
||||||
|
##### Jeddai
|
||||||
|
|
||||||
|
* [x] Add unit tests with full coverage for the Homebrewery API
|
||||||
|
}}
|
||||||
|
|
||||||
### Friday 23/12/2022 - v3.5.0
|
### Friday 23/12/2022 - v3.5.0
|
||||||
{{taskList
|
{{taskList
|
||||||
|
|
||||||
|
|||||||
66
package-lock.json
generated
66
package-lock.json
generated
@@ -10,7 +10,7 @@
|
|||||||
"hasInstallScript": true,
|
"hasInstallScript": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/core": "^7.20.7",
|
"@babel/core": "^7.20.12",
|
||||||
"@babel/plugin-transform-runtime": "^7.19.6",
|
"@babel/plugin-transform-runtime": "^7.19.6",
|
||||||
"@babel/preset-env": "^7.19.4",
|
"@babel/preset-env": "^7.19.4",
|
||||||
"@babel/preset-react": "^7.18.6",
|
"@babel/preset-react": "^7.18.6",
|
||||||
@@ -1452,24 +1452,24 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@babel/core": {
|
"node_modules/@babel/core": {
|
||||||
"version": "7.20.7",
|
"version": "7.20.12",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/core/-/core-7.20.7.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/core/-/core-7.20.12.tgz",
|
||||||
"integrity": "sha512-t1ZjCluspe5DW24bn2Rr1CDb2v9rn/hROtg9a2tmd0+QYf4bsloYfLQzjG4qHPNMhWtKdGC33R5AxGR2Af2cBw==",
|
"integrity": "sha512-XsMfHovsUYHFMdrIHkZphTN/2Hzzi78R08NuHfDBehym2VsPDL6Zn/JAD/JQdnRvbSsbQc4mVaU1m6JgtTEElg==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ampproject/remapping": "^2.1.0",
|
"@ampproject/remapping": "^2.1.0",
|
||||||
"@babel/code-frame": "^7.18.6",
|
"@babel/code-frame": "^7.18.6",
|
||||||
"@babel/generator": "^7.20.7",
|
"@babel/generator": "^7.20.7",
|
||||||
"@babel/helper-compilation-targets": "^7.20.7",
|
"@babel/helper-compilation-targets": "^7.20.7",
|
||||||
"@babel/helper-module-transforms": "^7.20.7",
|
"@babel/helper-module-transforms": "^7.20.11",
|
||||||
"@babel/helpers": "^7.20.7",
|
"@babel/helpers": "^7.20.7",
|
||||||
"@babel/parser": "^7.20.7",
|
"@babel/parser": "^7.20.7",
|
||||||
"@babel/template": "^7.20.7",
|
"@babel/template": "^7.20.7",
|
||||||
"@babel/traverse": "^7.20.7",
|
"@babel/traverse": "^7.20.12",
|
||||||
"@babel/types": "^7.20.7",
|
"@babel/types": "^7.20.7",
|
||||||
"convert-source-map": "^1.7.0",
|
"convert-source-map": "^1.7.0",
|
||||||
"debug": "^4.1.0",
|
"debug": "^4.1.0",
|
||||||
"gensync": "^1.0.0-beta.2",
|
"gensync": "^1.0.0-beta.2",
|
||||||
"json5": "^2.2.1",
|
"json5": "^2.2.2",
|
||||||
"semver": "^6.3.0"
|
"semver": "^6.3.0"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
@@ -1676,9 +1676,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@babel/helper-module-transforms": {
|
"node_modules/@babel/helper-module-transforms": {
|
||||||
"version": "7.20.7",
|
"version": "7.20.11",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.20.7.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.20.11.tgz",
|
||||||
"integrity": "sha512-FNdu7r67fqMUSVuQpFQGE6BPdhJIhitoxhGzDbAXNcA07uoVG37fOiMk3OSV8rEICuyG6t8LGkd9EE64qIEoIA==",
|
"integrity": "sha512-uRy78kN4psmji1s2QtbtcCSaj/LILFDp0f/ymhpQH5QY3nljUZCaNWz9X1dEj/8MBdBEFECs7yRhKn8i7NjZgg==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/helper-environment-visitor": "^7.18.9",
|
"@babel/helper-environment-visitor": "^7.18.9",
|
||||||
"@babel/helper-module-imports": "^7.18.6",
|
"@babel/helper-module-imports": "^7.18.6",
|
||||||
@@ -1686,7 +1686,7 @@
|
|||||||
"@babel/helper-split-export-declaration": "^7.18.6",
|
"@babel/helper-split-export-declaration": "^7.18.6",
|
||||||
"@babel/helper-validator-identifier": "^7.19.1",
|
"@babel/helper-validator-identifier": "^7.19.1",
|
||||||
"@babel/template": "^7.20.7",
|
"@babel/template": "^7.20.7",
|
||||||
"@babel/traverse": "^7.20.7",
|
"@babel/traverse": "^7.20.10",
|
||||||
"@babel/types": "^7.20.7"
|
"@babel/types": "^7.20.7"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
@@ -3047,9 +3047,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@babel/traverse": {
|
"node_modules/@babel/traverse": {
|
||||||
"version": "7.20.8",
|
"version": "7.20.12",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.20.8.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.20.12.tgz",
|
||||||
"integrity": "sha512-/RNkaYDeCy4MjyV70+QkSHhxbvj2JO/5Ft2Pa880qJOG8tWrqcT/wXUuCCv43yogfqPzHL77Xu101KQPf4clnQ==",
|
"integrity": "sha512-MsIbFN0u+raeja38qboyF8TIT7K0BFzz/Yd/77ta4MsUsmP2RAnidIlwq7d5HFQrH/OZJecGV6B71C4zAgpoSQ==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/code-frame": "^7.18.6",
|
"@babel/code-frame": "^7.18.6",
|
||||||
"@babel/generator": "^7.20.7",
|
"@babel/generator": "^7.20.7",
|
||||||
@@ -9770,9 +9770,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/json5": {
|
"node_modules/json5": {
|
||||||
"version": "2.2.1",
|
"version": "2.2.3",
|
||||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
|
||||||
"integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==",
|
"integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==",
|
||||||
"bin": {
|
"bin": {
|
||||||
"json5": "lib/cli.js"
|
"json5": "lib/cli.js"
|
||||||
},
|
},
|
||||||
@@ -17777,24 +17777,24 @@
|
|||||||
"integrity": "sha512-KZXo2t10+/jxmkhNXc7pZTqRvSOIvVv/+lJwHS+B2rErwOyjuVRh60yVpb7liQ1U5t7lLJ1bz+t8tSypUZdm0g=="
|
"integrity": "sha512-KZXo2t10+/jxmkhNXc7pZTqRvSOIvVv/+lJwHS+B2rErwOyjuVRh60yVpb7liQ1U5t7lLJ1bz+t8tSypUZdm0g=="
|
||||||
},
|
},
|
||||||
"@babel/core": {
|
"@babel/core": {
|
||||||
"version": "7.20.7",
|
"version": "7.20.12",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/core/-/core-7.20.7.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/core/-/core-7.20.12.tgz",
|
||||||
"integrity": "sha512-t1ZjCluspe5DW24bn2Rr1CDb2v9rn/hROtg9a2tmd0+QYf4bsloYfLQzjG4qHPNMhWtKdGC33R5AxGR2Af2cBw==",
|
"integrity": "sha512-XsMfHovsUYHFMdrIHkZphTN/2Hzzi78R08NuHfDBehym2VsPDL6Zn/JAD/JQdnRvbSsbQc4mVaU1m6JgtTEElg==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@ampproject/remapping": "^2.1.0",
|
"@ampproject/remapping": "^2.1.0",
|
||||||
"@babel/code-frame": "^7.18.6",
|
"@babel/code-frame": "^7.18.6",
|
||||||
"@babel/generator": "^7.20.7",
|
"@babel/generator": "^7.20.7",
|
||||||
"@babel/helper-compilation-targets": "^7.20.7",
|
"@babel/helper-compilation-targets": "^7.20.7",
|
||||||
"@babel/helper-module-transforms": "^7.20.7",
|
"@babel/helper-module-transforms": "^7.20.11",
|
||||||
"@babel/helpers": "^7.20.7",
|
"@babel/helpers": "^7.20.7",
|
||||||
"@babel/parser": "^7.20.7",
|
"@babel/parser": "^7.20.7",
|
||||||
"@babel/template": "^7.20.7",
|
"@babel/template": "^7.20.7",
|
||||||
"@babel/traverse": "^7.20.7",
|
"@babel/traverse": "^7.20.12",
|
||||||
"@babel/types": "^7.20.7",
|
"@babel/types": "^7.20.7",
|
||||||
"convert-source-map": "^1.7.0",
|
"convert-source-map": "^1.7.0",
|
||||||
"debug": "^4.1.0",
|
"debug": "^4.1.0",
|
||||||
"gensync": "^1.0.0-beta.2",
|
"gensync": "^1.0.0-beta.2",
|
||||||
"json5": "^2.2.1",
|
"json5": "^2.2.2",
|
||||||
"semver": "^6.3.0"
|
"semver": "^6.3.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -17947,9 +17947,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@babel/helper-module-transforms": {
|
"@babel/helper-module-transforms": {
|
||||||
"version": "7.20.7",
|
"version": "7.20.11",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.20.7.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.20.11.tgz",
|
||||||
"integrity": "sha512-FNdu7r67fqMUSVuQpFQGE6BPdhJIhitoxhGzDbAXNcA07uoVG37fOiMk3OSV8rEICuyG6t8LGkd9EE64qIEoIA==",
|
"integrity": "sha512-uRy78kN4psmji1s2QtbtcCSaj/LILFDp0f/ymhpQH5QY3nljUZCaNWz9X1dEj/8MBdBEFECs7yRhKn8i7NjZgg==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@babel/helper-environment-visitor": "^7.18.9",
|
"@babel/helper-environment-visitor": "^7.18.9",
|
||||||
"@babel/helper-module-imports": "^7.18.6",
|
"@babel/helper-module-imports": "^7.18.6",
|
||||||
@@ -17957,7 +17957,7 @@
|
|||||||
"@babel/helper-split-export-declaration": "^7.18.6",
|
"@babel/helper-split-export-declaration": "^7.18.6",
|
||||||
"@babel/helper-validator-identifier": "^7.19.1",
|
"@babel/helper-validator-identifier": "^7.19.1",
|
||||||
"@babel/template": "^7.20.7",
|
"@babel/template": "^7.20.7",
|
||||||
"@babel/traverse": "^7.20.7",
|
"@babel/traverse": "^7.20.10",
|
||||||
"@babel/types": "^7.20.7"
|
"@babel/types": "^7.20.7"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -18847,9 +18847,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@babel/traverse": {
|
"@babel/traverse": {
|
||||||
"version": "7.20.8",
|
"version": "7.20.12",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.20.8.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.20.12.tgz",
|
||||||
"integrity": "sha512-/RNkaYDeCy4MjyV70+QkSHhxbvj2JO/5Ft2Pa880qJOG8tWrqcT/wXUuCCv43yogfqPzHL77Xu101KQPf4clnQ==",
|
"integrity": "sha512-MsIbFN0u+raeja38qboyF8TIT7K0BFzz/Yd/77ta4MsUsmP2RAnidIlwq7d5HFQrH/OZJecGV6B71C4zAgpoSQ==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@babel/code-frame": "^7.18.6",
|
"@babel/code-frame": "^7.18.6",
|
||||||
"@babel/generator": "^7.20.7",
|
"@babel/generator": "^7.20.7",
|
||||||
@@ -24053,9 +24053,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"json5": {
|
"json5": {
|
||||||
"version": "2.2.1",
|
"version": "2.2.3",
|
||||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
|
||||||
"integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA=="
|
"integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg=="
|
||||||
},
|
},
|
||||||
"jsonfile": {
|
"jsonfile": {
|
||||||
"version": "6.1.0",
|
"version": "6.1.0",
|
||||||
|
|||||||
20
package.json
20
package.json
@@ -19,6 +19,8 @@
|
|||||||
"circleci": "npm test && eslint **/*.{js,jsx} --max-warnings=0",
|
"circleci": "npm test && eslint **/*.{js,jsx} --max-warnings=0",
|
||||||
"verify": "npm run lint && npm test",
|
"verify": "npm run lint && npm test",
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
|
"test:api-unit": "jest server/*.spec.js --verbose",
|
||||||
|
"test:coverage": "jest --coverage --silent",
|
||||||
"test:dev": "jest --verbose --watch",
|
"test:dev": "jest --verbose --watch",
|
||||||
"test:basic": "jest tests/markdown/basic.test.js --verbose",
|
"test:basic": "jest tests/markdown/basic.test.js --verbose",
|
||||||
"test:mustache-span": "jest tests/markdown/mustache-span.test.js --verbose",
|
"test:mustache-span": "jest tests/markdown/mustache-span.test.js --verbose",
|
||||||
@@ -39,7 +41,21 @@
|
|||||||
"mode_modules",
|
"mode_modules",
|
||||||
"shared",
|
"shared",
|
||||||
"server"
|
"server"
|
||||||
]
|
],
|
||||||
|
"coverageThreshold" : {
|
||||||
|
"global" : {
|
||||||
|
"statements" : 25,
|
||||||
|
"branches" : 10,
|
||||||
|
"functions" : 22,
|
||||||
|
"lines" : 25
|
||||||
|
},
|
||||||
|
"server/homebrew.api.js" : {
|
||||||
|
"statements" : 71,
|
||||||
|
"branches" : 54,
|
||||||
|
"functions" : 66,
|
||||||
|
"lines" : 73
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"babel": {
|
"babel": {
|
||||||
"presets": [
|
"presets": [
|
||||||
@@ -51,7 +67,7 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/core": "^7.20.7",
|
"@babel/core": "^7.20.12",
|
||||||
"@babel/plugin-transform-runtime": "^7.19.6",
|
"@babel/plugin-transform-runtime": "^7.19.6",
|
||||||
"@babel/preset-env": "^7.19.4",
|
"@babel/preset-env": "^7.19.4",
|
||||||
"@babel/preset-react": "^7.18.6",
|
"@babel/preset-react": "^7.18.6",
|
||||||
|
|||||||
@@ -17,7 +17,11 @@ const { DEFAULT_BREW, DEFAULT_BREW_LOAD } = require('./brewDefaults.js');
|
|||||||
// });
|
// });
|
||||||
// };
|
// };
|
||||||
|
|
||||||
const getId = (req)=>{
|
const MAX_TITLE_LENGTH = 100;
|
||||||
|
|
||||||
|
const api = {
|
||||||
|
homebrewApi : router,
|
||||||
|
getId : (req)=>{
|
||||||
// Set the id and initial potential google id, where the google id is present on the existing brew.
|
// Set the id and initial potential google id, where the google id is present on the existing brew.
|
||||||
let id = req.params.id, googleId = req.body?.googleId;
|
let id = req.params.id, googleId = req.body?.googleId;
|
||||||
|
|
||||||
@@ -27,13 +31,12 @@ const getId = (req)=>{
|
|||||||
id = id.slice(-12);
|
id = id.slice(-12);
|
||||||
}
|
}
|
||||||
return { id, googleId };
|
return { id, googleId };
|
||||||
};
|
},
|
||||||
|
getBrew : (accessType, stubOnly = false)=>{
|
||||||
const getBrew = (accessType, stubOnly = false)=>{
|
|
||||||
// Create middleware with the accessType passed in as part of the scope
|
// Create middleware with the accessType passed in as part of the scope
|
||||||
return async (req, res, next)=>{
|
return async (req, res, next)=>{
|
||||||
// Get relevant IDs for the brew
|
// Get relevant IDs for the brew
|
||||||
const { id, googleId } = getId(req);
|
const { id, googleId } = api.getId(req);
|
||||||
|
|
||||||
// Try to find the document in the Homebrewery database -- if it doesn't exist, that's fine.
|
// Try to find the document in the Homebrewery database -- if it doesn't exist, that's fine.
|
||||||
let stub = await HomebrewModel.get(accessType === 'edit' ? { editId: id } : { shareId: id })
|
let stub = await HomebrewModel.get(accessType === 'edit' ? { editId: id } : { shareId: id })
|
||||||
@@ -57,7 +60,7 @@ const getBrew = (accessType, stubOnly = false)=>{
|
|||||||
// If we can't find the google brew and there is a google id for the brew, throw an error.
|
// If we can't find the google brew and there is a google id for the brew, throw an error.
|
||||||
if(!googleBrew) throw googleError;
|
if(!googleBrew) throw googleError;
|
||||||
// Combine the Homebrewery stub with the google brew, or if the stub doesn't exist just use the google brew
|
// Combine the Homebrewery stub with the google brew, or if the stub doesn't exist just use the google brew
|
||||||
stub = stub ? _.assign({ ...excludeStubProps(stub), stubbed: true }, excludeGoogleProps(googleBrew)) : googleBrew;
|
stub = stub ? _.assign({ ...api.excludeStubProps(stub), stubbed: true }, api.excludeGoogleProps(googleBrew)) : googleBrew;
|
||||||
}
|
}
|
||||||
const authorsExist = stub?.authors?.length > 0;
|
const authorsExist = stub?.authors?.length > 0;
|
||||||
const isAuthor = stub?.authors?.includes(req.account?.username);
|
const isAuthor = stub?.authors?.includes(req.account?.username);
|
||||||
@@ -78,13 +81,11 @@ If you believe you should have access to this brew, ask the file owner to invite
|
|||||||
stub.renderer = stub.renderer || undefined; // Clear empty strings
|
stub.renderer = stub.renderer || undefined; // Clear empty strings
|
||||||
stub = _.defaults(stub, DEFAULT_BREW_LOAD); // Fill in blank fields
|
stub = _.defaults(stub, DEFAULT_BREW_LOAD); // Fill in blank fields
|
||||||
|
|
||||||
|
|
||||||
req.brew = stub;
|
req.brew = stub;
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
};
|
},
|
||||||
|
mergeBrewText : (brew)=>{
|
||||||
const mergeBrewText = (brew)=>{
|
|
||||||
let text = brew.text;
|
let text = brew.text;
|
||||||
if(brew.style !== undefined) {
|
if(brew.style !== undefined) {
|
||||||
text = `\`\`\`css\n` +
|
text = `\`\`\`css\n` +
|
||||||
@@ -98,17 +99,13 @@ const mergeBrewText = (brew)=>{
|
|||||||
`\`\`\`\n\n` +
|
`\`\`\`\n\n` +
|
||||||
`${text}`;
|
`${text}`;
|
||||||
return text;
|
return text;
|
||||||
};
|
},
|
||||||
|
getGoodBrewTitle : (text)=>{
|
||||||
const MAX_TITLE_LENGTH = 100;
|
|
||||||
|
|
||||||
const getGoodBrewTitle = (text)=>{
|
|
||||||
const tokens = Markdown.marked.lexer(text);
|
const tokens = Markdown.marked.lexer(text);
|
||||||
return (tokens.find((token)=>token.type === 'heading' || token.type === 'paragraph')?.text || 'No Title')
|
return (tokens.find((token)=>token.type === 'heading' || token.type === 'paragraph')?.text || 'No Title')
|
||||||
.slice(0, MAX_TITLE_LENGTH);
|
.slice(0, MAX_TITLE_LENGTH);
|
||||||
};
|
},
|
||||||
|
excludePropsFromUpdate : (brew)=>{
|
||||||
const excludePropsFromUpdate = (brew)=>{
|
|
||||||
// Remove undesired properties
|
// Remove undesired properties
|
||||||
const modified = _.clone(brew);
|
const modified = _.clone(brew);
|
||||||
const propsToExclude = ['_id', 'views', 'lastViewed', 'editId', 'shareId', 'googleId'];
|
const propsToExclude = ['_id', 'views', 'lastViewed', 'editId', 'shareId', 'googleId'];
|
||||||
@@ -116,45 +113,40 @@ const excludePropsFromUpdate = (brew)=>{
|
|||||||
delete modified[prop];
|
delete modified[prop];
|
||||||
}
|
}
|
||||||
return modified;
|
return modified;
|
||||||
};
|
},
|
||||||
|
excludeGoogleProps : (brew)=>{
|
||||||
const excludeGoogleProps = (brew)=>{
|
|
||||||
const modified = _.clone(brew);
|
const modified = _.clone(brew);
|
||||||
const propsToExclude = ['version', 'tags', 'systems', 'published', 'authors', 'owner', 'views', 'thumbnail'];
|
const propsToExclude = ['version', 'tags', 'systems', 'published', 'authors', 'owner', 'views', 'thumbnail'];
|
||||||
for (const prop of propsToExclude) {
|
for (const prop of propsToExclude) {
|
||||||
delete modified[prop];
|
delete modified[prop];
|
||||||
}
|
}
|
||||||
return modified;
|
return modified;
|
||||||
};
|
},
|
||||||
|
excludeStubProps : (brew)=>{
|
||||||
const excludeStubProps = (brew)=>{
|
|
||||||
const propsToExclude = ['text', 'textBin', 'renderer', 'pageCount'];
|
const propsToExclude = ['text', 'textBin', 'renderer', 'pageCount'];
|
||||||
for (const prop of propsToExclude) {
|
for (const prop of propsToExclude) {
|
||||||
brew[prop] = undefined;
|
brew[prop] = undefined;
|
||||||
}
|
}
|
||||||
return brew;
|
return brew;
|
||||||
};
|
},
|
||||||
|
beforeNewSave : (account, brew)=>{
|
||||||
const beforeNewSave = (account, brew)=>{
|
|
||||||
if(!brew.title) {
|
if(!brew.title) {
|
||||||
brew.title = getGoodBrewTitle(brew.text);
|
brew.title = api.getGoodBrewTitle(brew.text);
|
||||||
}
|
}
|
||||||
|
|
||||||
brew.authors = (account) ? [account.username] : [];
|
brew.authors = (account) ? [account.username] : [];
|
||||||
brew.text = mergeBrewText(brew);
|
brew.text = api.mergeBrewText(brew);
|
||||||
|
|
||||||
_.defaults(brew, DEFAULT_BREW);
|
_.defaults(brew, DEFAULT_BREW);
|
||||||
};
|
},
|
||||||
|
newGoogleBrew : async (account, brew, res)=>{
|
||||||
const newGoogleBrew = async (account, brew, res)=>{
|
|
||||||
const oAuth2Client = GoogleActions.authCheck(account, res);
|
const oAuth2Client = GoogleActions.authCheck(account, res);
|
||||||
|
|
||||||
const newBrew = excludeGoogleProps(brew);
|
const newBrew = api.excludeGoogleProps(brew);
|
||||||
|
|
||||||
return await GoogleActions.newGoogleBrew(oAuth2Client, newBrew);
|
return await GoogleActions.newGoogleBrew(oAuth2Client, newBrew);
|
||||||
};
|
},
|
||||||
|
newBrew : async (req, res)=>{
|
||||||
const newBrew = async (req, res)=>{
|
|
||||||
const brew = req.body;
|
const brew = req.body;
|
||||||
const { saveToGoogle } = req.query;
|
const { saveToGoogle } = req.query;
|
||||||
|
|
||||||
@@ -162,7 +154,7 @@ const newBrew = async (req, res)=>{
|
|||||||
delete brew.shareId;
|
delete brew.shareId;
|
||||||
delete brew.googleId;
|
delete brew.googleId;
|
||||||
|
|
||||||
beforeNewSave(req.account, brew);
|
api.beforeNewSave(req.account, brew);
|
||||||
|
|
||||||
const newHomebrew = new HomebrewModel(brew);
|
const newHomebrew = new HomebrewModel(brew);
|
||||||
newHomebrew.editId = nanoid(12);
|
newHomebrew.editId = nanoid(12);
|
||||||
@@ -170,13 +162,13 @@ const newBrew = async (req, res)=>{
|
|||||||
|
|
||||||
let googleId, saved;
|
let googleId, saved;
|
||||||
if(saveToGoogle) {
|
if(saveToGoogle) {
|
||||||
googleId = await newGoogleBrew(req.account, newHomebrew, res)
|
googleId = await api.newGoogleBrew(req.account, newHomebrew, res)
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.status(err?.status || err?.response?.status || 500).send(err?.message || err);
|
res.status(err?.status || err?.response?.status || 500).send(err?.message || err);
|
||||||
});
|
});
|
||||||
if(!googleId) return;
|
if(!googleId) return;
|
||||||
excludeStubProps(newHomebrew);
|
api.excludeStubProps(newHomebrew);
|
||||||
newHomebrew.googleId = googleId;
|
newHomebrew.googleId = googleId;
|
||||||
} else {
|
} else {
|
||||||
// Compress brew text to binary before saving
|
// Compress brew text to binary before saving
|
||||||
@@ -194,11 +186,10 @@ const newBrew = async (req, res)=>{
|
|||||||
saved = saved.toObject();
|
saved = saved.toObject();
|
||||||
|
|
||||||
res.status(200).send(saved);
|
res.status(200).send(saved);
|
||||||
};
|
},
|
||||||
|
updateBrew : async (req, res)=>{
|
||||||
const updateBrew = async (req, res)=>{
|
|
||||||
// Initialize brew from request and body, destructure query params, set a constant for the google id, and set the initial value for the after-save method
|
// Initialize brew from request and body, destructure query params, set a constant for the google id, and set the initial value for the after-save method
|
||||||
const brewFromClient = excludePropsFromUpdate(req.body);
|
const brewFromClient = api.excludePropsFromUpdate(req.body);
|
||||||
if(req.brew.version && brewFromClient.version && req.brew.version > brewFromClient.version) {
|
if(req.brew.version && brewFromClient.version && req.brew.version > brewFromClient.version) {
|
||||||
console.log(`Version mismatch on brew ${req.body.editId}`);
|
console.log(`Version mismatch on brew ${req.body.editId}`);
|
||||||
// res.setHeader('Content-Type', 'application/json');
|
// res.setHeader('Content-Type', 'application/json');
|
||||||
@@ -210,12 +201,12 @@ const updateBrew = async (req, res)=>{
|
|||||||
const googleId = brew.googleId;
|
const googleId = brew.googleId;
|
||||||
let afterSave = async ()=>true;
|
let afterSave = async ()=>true;
|
||||||
|
|
||||||
brew.text = mergeBrewText(brew);
|
brew.text = api.mergeBrewText(brew);
|
||||||
|
|
||||||
if(brew.googleId && removeFromGoogle) {
|
if(brew.googleId && removeFromGoogle) {
|
||||||
// If the google id exists and we're removing it from google, set afterSave to delete the google brew and mark the brew's google id as undefined
|
// If the google id exists and we're removing it from google, set afterSave to delete the google brew and mark the brew's google id as undefined
|
||||||
afterSave = async ()=>{
|
afterSave = async ()=>{
|
||||||
return await deleteGoogleBrew(req.account, googleId, brew.editId, res)
|
return await api.deleteGoogleBrew(req.account, googleId, brew.editId, res)
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.status(err?.status || err?.response?.status || 500).send(err.message || err);
|
res.status(err?.status || err?.response?.status || 500).send(err.message || err);
|
||||||
@@ -225,7 +216,7 @@ const updateBrew = async (req, res)=>{
|
|||||||
brew.googleId = undefined;
|
brew.googleId = undefined;
|
||||||
} else if(!brew.googleId && saveToGoogle) {
|
} else if(!brew.googleId && saveToGoogle) {
|
||||||
// If we don't have a google id and the user wants to save to google, create the google brew and set the google id on the brew
|
// If we don't have a google id and the user wants to save to google, create the google brew and set the google id on the brew
|
||||||
brew.googleId = await newGoogleBrew(req.account, excludeGoogleProps(brew), res)
|
brew.googleId = await api.newGoogleBrew(req.account, api.excludeGoogleProps(brew), res)
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.status(err.status || err.response.status).send(err.message || err);
|
res.status(err.status || err.response.status).send(err.message || err);
|
||||||
@@ -233,7 +224,7 @@ const updateBrew = async (req, res)=>{
|
|||||||
if(!brew.googleId) return;
|
if(!brew.googleId) return;
|
||||||
} else if(brew.googleId) {
|
} else if(brew.googleId) {
|
||||||
// If the google id exists and no other actions are being performed, update the google brew
|
// If the google id exists and no other actions are being performed, update the google brew
|
||||||
const updated = await GoogleActions.updateGoogleBrew(excludeGoogleProps(brew))
|
const updated = await GoogleActions.updateGoogleBrew(api.excludeGoogleProps(brew))
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.status(err?.response?.status || 500).send(err);
|
res.status(err?.response?.status || 500).send(err);
|
||||||
@@ -243,7 +234,7 @@ const updateBrew = async (req, res)=>{
|
|||||||
|
|
||||||
if(brew.googleId) {
|
if(brew.googleId) {
|
||||||
// If the google id exists after all those actions, exclude the props that are stored in google and aren't needed for rendering the brew items
|
// If the google id exists after all those actions, exclude the props that are stored in google and aren't needed for rendering the brew items
|
||||||
excludeStubProps(brew);
|
api.excludeStubProps(brew);
|
||||||
} else {
|
} else {
|
||||||
// Compress brew text to binary before saving
|
// Compress brew text to binary before saving
|
||||||
brew.textBin = zlib.deflateRawSync(brew.text);
|
brew.textBin = zlib.deflateRawSync(brew.text);
|
||||||
@@ -267,7 +258,6 @@ const updateBrew = async (req, res)=>{
|
|||||||
if(!brew._id) {
|
if(!brew._id) {
|
||||||
// if the brew does not have a stub id, create and save it, then write the new value back to the brew.
|
// if the brew does not have a stub id, create and save it, then write the new value back to the brew.
|
||||||
saved = await new HomebrewModel(brew).save().catch(saveError);
|
saved = await new HomebrewModel(brew).save().catch(saveError);
|
||||||
brew = saved?.toObject();
|
|
||||||
} else {
|
} else {
|
||||||
// if the brew does have a stub id, update it using the stub id as the key.
|
// if the brew does have a stub id, update it using the stub id as the key.
|
||||||
brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew);
|
brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew);
|
||||||
@@ -280,20 +270,18 @@ const updateBrew = async (req, res)=>{
|
|||||||
if(!after) return;
|
if(!after) return;
|
||||||
|
|
||||||
res.status(200).send(saved);
|
res.status(200).send(saved);
|
||||||
};
|
},
|
||||||
|
deleteGoogleBrew : async (account, id, editId, res)=>{
|
||||||
const deleteGoogleBrew = async (account, id, editId, res)=>{
|
|
||||||
const auth = await GoogleActions.authCheck(account, res);
|
const auth = await GoogleActions.authCheck(account, res);
|
||||||
await GoogleActions.deleteGoogleBrew(auth, id, editId);
|
await GoogleActions.deleteGoogleBrew(auth, id, editId);
|
||||||
return true;
|
return true;
|
||||||
};
|
},
|
||||||
|
deleteBrew : async (req, res, next)=>{
|
||||||
const deleteBrew = async (req, res, next)=>{
|
|
||||||
// Delete an orphaned stub if its Google brew doesn't exist
|
// Delete an orphaned stub if its Google brew doesn't exist
|
||||||
try {
|
try {
|
||||||
await getBrew('edit')(req, res, ()=>{});
|
await api.getBrew('edit')(req, res, ()=>{});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const { id, googleId } = getId(req);
|
const { id, googleId } = api.getId(req);
|
||||||
console.warn(`No google brew found for id ${googleId}, the stub with id ${id} will be deleted.`);
|
console.warn(`No google brew found for id ${googleId}, the stub with id ${id} will be deleted.`);
|
||||||
await HomebrewModel.deleteOne({ editId: id });
|
await HomebrewModel.deleteOne({ editId: id });
|
||||||
return next();
|
return next();
|
||||||
@@ -311,7 +299,6 @@ const deleteBrew = async (req, res, next)=>{
|
|||||||
if(account) {
|
if(account) {
|
||||||
// Remove current user as author
|
// Remove current user as author
|
||||||
brew.authors = _.pull(brew.authors, account.username);
|
brew.authors = _.pull(brew.authors, account.username);
|
||||||
brew.markModified('authors');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(brew.authors.length === 0) {
|
if(brew.authors.length === 0) {
|
||||||
@@ -337,7 +324,7 @@ const deleteBrew = async (req, res, next)=>{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(shouldDeleteGoogleBrew) {
|
if(shouldDeleteGoogleBrew) {
|
||||||
const deleted = await deleteGoogleBrew(account, googleId, editId, res)
|
const deleted = await api.deleteGoogleBrew(account, googleId, editId, res)
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.status(500).send(err);
|
res.status(500).send(err);
|
||||||
@@ -346,15 +333,13 @@ const deleteBrew = async (req, res, next)=>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
res.status(204).send();
|
res.status(204).send();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
router.post('/api', asyncHandler(newBrew));
|
router.post('/api', asyncHandler(api.newBrew));
|
||||||
router.put('/api/:id', asyncHandler(getBrew('edit', true)), asyncHandler(updateBrew));
|
router.put('/api/:id', asyncHandler(api.getBrew('edit', true)), asyncHandler(api.updateBrew));
|
||||||
router.put('/api/update/:id', asyncHandler(getBrew('edit', true)), asyncHandler(updateBrew));
|
router.put('/api/update/:id', asyncHandler(api.getBrew('edit', true)), asyncHandler(api.updateBrew));
|
||||||
router.delete('/api/:id', asyncHandler(deleteBrew));
|
router.delete('/api/:id', asyncHandler(api.deleteBrew));
|
||||||
router.get('/api/remove/:id', asyncHandler(deleteBrew));
|
router.get('/api/remove/:id', asyncHandler(api.deleteBrew));
|
||||||
|
|
||||||
module.exports = {
|
module.exports = api;
|
||||||
homebrewApi : router,
|
|
||||||
getBrew
|
|
||||||
};
|
|
||||||
|
|||||||
751
server/homebrew.api.spec.js
Normal file
751
server/homebrew.api.spec.js
Normal file
@@ -0,0 +1,751 @@
|
|||||||
|
/* eslint-disable max-lines */
|
||||||
|
|
||||||
|
describe('Tests for api', ()=>{
|
||||||
|
let api;
|
||||||
|
let google;
|
||||||
|
let model;
|
||||||
|
let hbBrew;
|
||||||
|
let googleBrew;
|
||||||
|
let res;
|
||||||
|
|
||||||
|
let modelBrew;
|
||||||
|
let saveFunc;
|
||||||
|
let removeFunc;
|
||||||
|
let saved;
|
||||||
|
|
||||||
|
beforeEach(()=>{
|
||||||
|
saved = undefined;
|
||||||
|
saveFunc = jest.fn(async function() {
|
||||||
|
saved = { ...this, _id: '1' };
|
||||||
|
return saved;
|
||||||
|
});
|
||||||
|
removeFunc = jest.fn(async function() {});
|
||||||
|
|
||||||
|
modelBrew = (brew)=>({
|
||||||
|
...brew,
|
||||||
|
save : saveFunc,
|
||||||
|
remove : removeFunc,
|
||||||
|
toObject : function() {
|
||||||
|
delete this.save;
|
||||||
|
delete this.toObject;
|
||||||
|
delete this.remove;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
google = require('./googleActions.js');
|
||||||
|
model = require('./homebrew.model.js').model;
|
||||||
|
|
||||||
|
jest.mock('./googleActions.js');
|
||||||
|
google.authCheck = jest.fn(()=>'client');
|
||||||
|
google.newGoogleBrew = jest.fn(()=>'id');
|
||||||
|
google.deleteGoogleBrew = jest.fn(()=>true);
|
||||||
|
|
||||||
|
jest.mock('./homebrew.model.js');
|
||||||
|
model.mockImplementation((brew)=>modelBrew(brew));
|
||||||
|
|
||||||
|
res = {
|
||||||
|
status : jest.fn(()=>res),
|
||||||
|
send : jest.fn(()=>{})
|
||||||
|
};
|
||||||
|
|
||||||
|
api = require('./homebrew.api');
|
||||||
|
|
||||||
|
hbBrew = {
|
||||||
|
text : `brew text`,
|
||||||
|
style : 'hello yes i am css',
|
||||||
|
title : 'some title',
|
||||||
|
description : 'this is a description',
|
||||||
|
tags : ['something', 'fun'],
|
||||||
|
systems : ['D&D 5e'],
|
||||||
|
renderer : 'v3',
|
||||||
|
theme : 'phb',
|
||||||
|
published : true,
|
||||||
|
authors : ['1', '2'],
|
||||||
|
owner : '1',
|
||||||
|
thumbnail : '',
|
||||||
|
_id : 'mongoid',
|
||||||
|
editId : 'abcdefg',
|
||||||
|
shareId : 'hijklmnop',
|
||||||
|
views : 1,
|
||||||
|
lastViewed : new Date(),
|
||||||
|
version : 1,
|
||||||
|
pageCount : 1,
|
||||||
|
textBin : ''
|
||||||
|
};
|
||||||
|
googleBrew = {
|
||||||
|
...hbBrew,
|
||||||
|
googleId : '12345'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(()=>{
|
||||||
|
jest.restoreAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getId', ()=>{
|
||||||
|
it('should return only id if google id is not present', ()=>{
|
||||||
|
const { id, googleId } = api.getId({
|
||||||
|
params : {
|
||||||
|
id : 'abcdefgh'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(id).toEqual('abcdefgh');
|
||||||
|
expect(googleId).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return id and google id from request body', ()=>{
|
||||||
|
const { id, googleId } = api.getId({
|
||||||
|
params : {
|
||||||
|
id : 'abcdefgh'
|
||||||
|
},
|
||||||
|
body : {
|
||||||
|
googleId : '12345'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(id).toEqual('abcdefgh');
|
||||||
|
expect(googleId).toEqual('12345');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return id and google id from params', ()=>{
|
||||||
|
const { id, googleId } = api.getId({
|
||||||
|
params : {
|
||||||
|
id : '123456789012abcdefghijkl'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(id).toEqual('abcdefghijkl');
|
||||||
|
expect(googleId).toEqual('123456789012');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getBrew', ()=>{
|
||||||
|
const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew }));
|
||||||
|
const notFoundError = 'Brew not found in Homebrewery database or Google Drive';
|
||||||
|
|
||||||
|
it('returns middleware', ()=>{
|
||||||
|
const getFn = api.getBrew('share');
|
||||||
|
expect(getFn).toBeInstanceOf(Function);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fetch from mongoose', async ()=>{
|
||||||
|
const testBrew = { title: 'test brew', authors: [] };
|
||||||
|
api.getId = jest.fn(()=>({ id: '1', googleId: undefined }));
|
||||||
|
model.get = jest.fn(()=>toBrewPromise(testBrew));
|
||||||
|
|
||||||
|
const fn = api.getBrew('share', true);
|
||||||
|
const req = { brew: {} };
|
||||||
|
const next = jest.fn();
|
||||||
|
await fn(req, null, next);
|
||||||
|
|
||||||
|
expect(req.brew).toEqual(testBrew);
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
expect(api.getId).toHaveBeenCalledWith(req);
|
||||||
|
expect(model.get).toHaveBeenCalledWith({ shareId: '1' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle mongoose error', async ()=>{
|
||||||
|
api.getId = jest.fn(()=>({ id: '1', googleId: undefined }));
|
||||||
|
model.get = jest.fn(()=>new Promise((_, rej)=>rej('Unable to find brew')));
|
||||||
|
|
||||||
|
const fn = api.getBrew('share', false);
|
||||||
|
const req = { brew: {} };
|
||||||
|
const next = jest.fn();
|
||||||
|
let err;
|
||||||
|
try {
|
||||||
|
await fn(req, null, next);
|
||||||
|
} catch (e) {
|
||||||
|
err = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(err).toEqual(notFoundError);
|
||||||
|
expect(req.brew).toEqual({});
|
||||||
|
expect(next).not.toHaveBeenCalled();
|
||||||
|
expect(api.getId).toHaveBeenCalledWith(req);
|
||||||
|
expect(model.get).toHaveBeenCalledWith({ shareId: '1' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('changes tags from string to array', async ()=>{
|
||||||
|
const testBrew = { title: 'test brew', authors: [], tags: '' };
|
||||||
|
api.getId = jest.fn(()=>({ id: '1', googleId: undefined }));
|
||||||
|
model.get = jest.fn(()=>toBrewPromise(testBrew));
|
||||||
|
|
||||||
|
const fn = api.getBrew('share', true);
|
||||||
|
const req = { brew: {} };
|
||||||
|
const next = jest.fn();
|
||||||
|
await fn(req, null, next);
|
||||||
|
|
||||||
|
expect(req.brew.tags).toEqual([]);
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws if invalid author', async ()=>{
|
||||||
|
api.getId = jest.fn(()=>({ id: '1', googleId: undefined }));
|
||||||
|
model.get = jest.fn(()=>toBrewPromise({ title: 'test brew', authors: ['a'] }));
|
||||||
|
|
||||||
|
const fn = api.getBrew('edit', true);
|
||||||
|
const req = { brew: {} };
|
||||||
|
|
||||||
|
let err;
|
||||||
|
try {
|
||||||
|
await fn(req, null, null);
|
||||||
|
} catch (e) {
|
||||||
|
err = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(err).toEqual(`The current logged in user does not have editor access to this brew.
|
||||||
|
|
||||||
|
If you believe you should have access to this brew, ask the file owner to invite you as an author by opening the brew, viewing the Properties tab, and adding your username to the "invited authors" list. You can then try to access this document again.`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not throw if no authors', async ()=>{
|
||||||
|
api.getId = jest.fn(()=>({ id: '1', googleId: undefined }));
|
||||||
|
model.get = jest.fn(()=>toBrewPromise({ title: 'test brew', authors: [] }));
|
||||||
|
|
||||||
|
const fn = api.getBrew('edit', true);
|
||||||
|
const req = { brew: {} };
|
||||||
|
const next = jest.fn();
|
||||||
|
await fn(req, null, next);
|
||||||
|
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
expect(req.brew.title).toEqual('test brew');
|
||||||
|
expect(req.brew.authors).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not throw if valid author', async ()=>{
|
||||||
|
api.getId = jest.fn(()=>({ id: '1', googleId: undefined }));
|
||||||
|
model.get = jest.fn(()=>toBrewPromise({ title: 'test brew', authors: ['a'] }));
|
||||||
|
|
||||||
|
const fn = api.getBrew('edit', true);
|
||||||
|
const req = { brew: {}, account: { username: 'a' } };
|
||||||
|
const next = jest.fn();
|
||||||
|
await fn(req, null, next);
|
||||||
|
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
expect(req.brew.title).toEqual('test brew');
|
||||||
|
expect(req.brew.authors).toEqual(['a']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fetches google brew if needed', async()=>{
|
||||||
|
const stubBrew = { title: 'test brew', authors: ['a'] };
|
||||||
|
const googleBrew = { title: 'test google brew', text: 'brew text' };
|
||||||
|
api.getId = jest.fn(()=>({ id: '1', googleId: '2' }));
|
||||||
|
model.get = jest.fn(()=>toBrewPromise(stubBrew));
|
||||||
|
google.getGoogleBrew = jest.fn(()=>new Promise((res)=>res(googleBrew)));
|
||||||
|
|
||||||
|
const fn = api.getBrew('share', false);
|
||||||
|
const req = { brew: {} };
|
||||||
|
const next = jest.fn();
|
||||||
|
await fn(req, null, next);
|
||||||
|
|
||||||
|
expect(req.brew).toEqual({
|
||||||
|
title : 'test google brew',
|
||||||
|
authors : ['a'],
|
||||||
|
text : 'brew text',
|
||||||
|
stubbed : true,
|
||||||
|
description : '',
|
||||||
|
editId : undefined,
|
||||||
|
pageCount : 1,
|
||||||
|
published : false,
|
||||||
|
renderer : 'legacy',
|
||||||
|
shareId : undefined,
|
||||||
|
systems : [],
|
||||||
|
tags : [],
|
||||||
|
theme : '5ePHB',
|
||||||
|
thumbnail : '',
|
||||||
|
textBin : undefined,
|
||||||
|
version : undefined,
|
||||||
|
createdAt : undefined,
|
||||||
|
gDrive : false,
|
||||||
|
style : undefined,
|
||||||
|
trashed : false,
|
||||||
|
updatedAt : undefined
|
||||||
|
});
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
expect(api.getId).toHaveBeenCalledWith(req);
|
||||||
|
expect(model.get).toHaveBeenCalledWith({ shareId: '1' });
|
||||||
|
expect(google.getGoogleBrew).toHaveBeenCalledWith('2', '1', 'share');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('mergeBrewText', ()=>{
|
||||||
|
it('should set metadata and no style if it is not present', ()=>{
|
||||||
|
const result = api.mergeBrewText({
|
||||||
|
text : `brew`,
|
||||||
|
title : 'some title',
|
||||||
|
description : 'this is a description',
|
||||||
|
tags : ['something', 'fun'],
|
||||||
|
systems : ['D&D 5e'],
|
||||||
|
renderer : 'v3',
|
||||||
|
theme : 'phb',
|
||||||
|
googleId : '12345'
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqual(`\`\`\`metadata
|
||||||
|
title: some title
|
||||||
|
description: this is a description
|
||||||
|
tags:
|
||||||
|
- something
|
||||||
|
- fun
|
||||||
|
systems:
|
||||||
|
- D&D 5e
|
||||||
|
renderer: v3
|
||||||
|
theme: phb
|
||||||
|
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
brew`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set metadata and style', ()=>{
|
||||||
|
const result = api.mergeBrewText({
|
||||||
|
text : `brew`,
|
||||||
|
style : 'hello yes i am css',
|
||||||
|
title : 'some title',
|
||||||
|
description : 'this is a description',
|
||||||
|
tags : ['something', 'fun'],
|
||||||
|
systems : ['D&D 5e'],
|
||||||
|
renderer : 'v3',
|
||||||
|
theme : 'phb',
|
||||||
|
googleId : '12345'
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqual(`\`\`\`metadata
|
||||||
|
title: some title
|
||||||
|
description: this is a description
|
||||||
|
tags:
|
||||||
|
- something
|
||||||
|
- fun
|
||||||
|
systems:
|
||||||
|
- D&D 5e
|
||||||
|
renderer: v3
|
||||||
|
theme: phb
|
||||||
|
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
\`\`\`css
|
||||||
|
hello yes i am css
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
brew`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('exclusion methods', ()=>{
|
||||||
|
it('excludePropsFromUpdate removes the correct keys', ()=>{
|
||||||
|
const sent = Object.assign({}, googleBrew);
|
||||||
|
const result = api.excludePropsFromUpdate(sent);
|
||||||
|
|
||||||
|
expect(sent).toEqual(googleBrew);
|
||||||
|
expect(result._id).toBeUndefined();
|
||||||
|
expect(result.views).toBeUndefined();
|
||||||
|
expect(result.lastViewed).toBeUndefined();
|
||||||
|
expect(result.editId).toBeUndefined();
|
||||||
|
expect(result.shareId).toBeUndefined();
|
||||||
|
expect(result.googleId).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('excludeGoogleProps removes the correct keys', ()=>{
|
||||||
|
const sent = Object.assign({}, googleBrew);
|
||||||
|
const result = api.excludeGoogleProps(sent);
|
||||||
|
|
||||||
|
expect(sent).toEqual(googleBrew);
|
||||||
|
expect(result.tags).toBeUndefined();
|
||||||
|
expect(result.systems).toBeUndefined();
|
||||||
|
expect(result.published).toBeUndefined();
|
||||||
|
expect(result.authors).toBeUndefined();
|
||||||
|
expect(result.owner).toBeUndefined();
|
||||||
|
expect(result.views).toBeUndefined();
|
||||||
|
expect(result.thumbnail).toBeUndefined();
|
||||||
|
expect(result.version).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('excludeStubProps removes the correct keys from the original object', ()=>{
|
||||||
|
const sent = Object.assign({}, googleBrew);
|
||||||
|
const result = api.excludeStubProps(sent);
|
||||||
|
|
||||||
|
expect(sent).not.toEqual(googleBrew);
|
||||||
|
expect(result.text).toBeUndefined();
|
||||||
|
expect(result.textBin).toBeUndefined();
|
||||||
|
expect(result.renderer).toBeUndefined();
|
||||||
|
expect(result.pageCount).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('beforeNewSave', ()=>{
|
||||||
|
it('sets the title if none', ()=>{
|
||||||
|
const brew = {
|
||||||
|
...hbBrew,
|
||||||
|
title : undefined
|
||||||
|
};
|
||||||
|
api.beforeNewSave({}, brew);
|
||||||
|
|
||||||
|
expect(brew.title).toEqual('brew text');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not override the title if present', ()=>{
|
||||||
|
const brew = {
|
||||||
|
...hbBrew,
|
||||||
|
title : 'test'
|
||||||
|
};
|
||||||
|
api.beforeNewSave({}, brew);
|
||||||
|
|
||||||
|
expect(brew.title).toEqual('test');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not set authors if account missing username', ()=>{
|
||||||
|
api.beforeNewSave({}, hbBrew);
|
||||||
|
|
||||||
|
expect(hbBrew.authors).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets authors if account has username', ()=>{
|
||||||
|
api.beforeNewSave({ username: 'hi' }, hbBrew);
|
||||||
|
|
||||||
|
expect(hbBrew.authors).toEqual(['hi']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('merges brew text', ()=>{
|
||||||
|
api.mergeBrewText = jest.fn(()=>'merged');
|
||||||
|
api.beforeNewSave({}, hbBrew);
|
||||||
|
|
||||||
|
expect(api.mergeBrewText).toHaveBeenCalled();
|
||||||
|
expect(hbBrew.text).toEqual('merged');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('newGoogleBrew', ()=>{
|
||||||
|
it('should call the correct methods', ()=>{
|
||||||
|
api.excludeGoogleProps = jest.fn(()=>'newBrew');
|
||||||
|
|
||||||
|
const acct = { username: 'test' };
|
||||||
|
const brew = { title: 'test title' };
|
||||||
|
api.newGoogleBrew(acct, brew, res);
|
||||||
|
|
||||||
|
expect(google.authCheck).toHaveBeenCalledWith(acct, res);
|
||||||
|
expect(api.excludeGoogleProps).toHaveBeenCalledWith(brew);
|
||||||
|
expect(google.newGoogleBrew).toHaveBeenCalledWith('client', 'newBrew');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('newBrew', ()=>{
|
||||||
|
it('should set up a default brew via Homebrew model', async ()=>{
|
||||||
|
await api.newBrew({ body: { text: 'asdf' }, query: {}, account: { username: 'test user' } }, res);
|
||||||
|
|
||||||
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
|
expect(res.send).toHaveBeenCalledWith({
|
||||||
|
_id : '1',
|
||||||
|
authors : ['test user'],
|
||||||
|
createdAt : undefined,
|
||||||
|
description : '',
|
||||||
|
editId : expect.any(String),
|
||||||
|
gDrive : false,
|
||||||
|
pageCount : 1,
|
||||||
|
published : false,
|
||||||
|
renderer : 'V3',
|
||||||
|
shareId : expect.any(String),
|
||||||
|
style : undefined,
|
||||||
|
systems : [],
|
||||||
|
tags : [],
|
||||||
|
text : undefined,
|
||||||
|
textBin : expect.objectContaining({}),
|
||||||
|
theme : '5ePHB',
|
||||||
|
thumbnail : '',
|
||||||
|
title : 'asdf',
|
||||||
|
trashed : false,
|
||||||
|
updatedAt : undefined
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove edit/share/google ids', async ()=>{
|
||||||
|
await api.newBrew({ body: { editId: '1234', shareId: '1234', googleId: '1234', text: 'asdf', title: '' }, query: {} }, res);
|
||||||
|
|
||||||
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
|
expect(res.send).toHaveBeenCalled();
|
||||||
|
const sent = res.send.mock.calls[0][0];
|
||||||
|
expect(sent.editId).not.toEqual('1234');
|
||||||
|
expect(sent.shareId).not.toEqual('1234');
|
||||||
|
expect(sent.googleId).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle mongo error', async ()=>{
|
||||||
|
saveFunc = jest.fn(async function() {
|
||||||
|
throw 'err';
|
||||||
|
});
|
||||||
|
model.mockImplementation((brew)=>modelBrew(brew));
|
||||||
|
|
||||||
|
let err;
|
||||||
|
try {
|
||||||
|
await api.newBrew({ body: { editId: '1234', shareId: '1234', googleId: '1234', text: 'asdf', title: '' }, query: {} }, res);
|
||||||
|
} catch (e) {
|
||||||
|
err = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(res.send).not.toHaveBeenCalled();
|
||||||
|
expect(err).not.toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should save to google if requested', async()=>{
|
||||||
|
await api.newBrew({ body: { text: 'asdf', title: '' }, query: { saveToGoogle: true }, account: { username: 'test user' } }, res);
|
||||||
|
|
||||||
|
expect(google.newGoogleBrew).toHaveBeenCalled();
|
||||||
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
|
expect(res.send).toHaveBeenCalledWith({
|
||||||
|
_id : '1',
|
||||||
|
authors : ['test user'],
|
||||||
|
createdAt : undefined,
|
||||||
|
description : '',
|
||||||
|
editId : expect.any(String),
|
||||||
|
gDrive : false,
|
||||||
|
pageCount : undefined,
|
||||||
|
published : false,
|
||||||
|
renderer : undefined,
|
||||||
|
shareId : expect.any(String),
|
||||||
|
googleId : expect.any(String),
|
||||||
|
style : undefined,
|
||||||
|
systems : [],
|
||||||
|
tags : [],
|
||||||
|
text : undefined,
|
||||||
|
textBin : undefined,
|
||||||
|
theme : '5ePHB',
|
||||||
|
thumbnail : '',
|
||||||
|
title : 'asdf',
|
||||||
|
trashed : false,
|
||||||
|
updatedAt : undefined
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle google error', async()=>{
|
||||||
|
google.newGoogleBrew = jest.fn(()=>{
|
||||||
|
throw 'err';
|
||||||
|
});
|
||||||
|
await api.newBrew({ body: { text: 'asdf', title: '' }, query: { saveToGoogle: true }, account: { username: 'test user' } }, res);
|
||||||
|
|
||||||
|
expect(res.status).toHaveBeenCalledWith(500);
|
||||||
|
expect(res.send).toHaveBeenCalledWith('err');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('deleteGoogleBrew', ()=>{
|
||||||
|
it('should check auth and delete brew', async ()=>{
|
||||||
|
const result = await api.deleteGoogleBrew({ username: 'test user' }, 'id', 'editId', res);
|
||||||
|
|
||||||
|
expect(result).toBe(true);
|
||||||
|
expect(google.authCheck).toHaveBeenCalledWith({ username: 'test user' }, expect.objectContaining({}));
|
||||||
|
expect(google.deleteGoogleBrew).toHaveBeenCalledWith('client', 'id', 'editId');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('deleteBrew', ()=>{
|
||||||
|
it('should handle case where fetching the brew returns an error', async ()=>{
|
||||||
|
api.getBrew = jest.fn(()=>async ()=>{ throw 'err'; });
|
||||||
|
api.getId = jest.fn(()=>({ id: '1', googleId: '2' }));
|
||||||
|
model.deleteOne = jest.fn(async ()=>{});
|
||||||
|
const next = jest.fn(()=>{});
|
||||||
|
|
||||||
|
await api.deleteBrew(null, null, next);
|
||||||
|
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
expect(model.deleteOne).toHaveBeenCalledWith({ editId: '1' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete if no authors', async ()=>{
|
||||||
|
const brew = {
|
||||||
|
...hbBrew,
|
||||||
|
authors : []
|
||||||
|
};
|
||||||
|
api.getBrew = jest.fn(()=>async (req)=>{
|
||||||
|
req.brew = brew;
|
||||||
|
});
|
||||||
|
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||||
|
const req = {};
|
||||||
|
|
||||||
|
await api.deleteBrew(req, res);
|
||||||
|
|
||||||
|
expect(api.getBrew).toHaveBeenCalled();
|
||||||
|
expect(model.findOne).toHaveBeenCalled();
|
||||||
|
expect(removeFunc).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw on delete error', async ()=>{
|
||||||
|
const brew = {
|
||||||
|
...hbBrew,
|
||||||
|
authors : []
|
||||||
|
};
|
||||||
|
api.getBrew = jest.fn(()=>async (req)=>{
|
||||||
|
req.brew = brew;
|
||||||
|
});
|
||||||
|
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||||
|
removeFunc = jest.fn(async ()=>{ throw 'err'; });
|
||||||
|
const req = {};
|
||||||
|
|
||||||
|
let err;
|
||||||
|
try {
|
||||||
|
await api.deleteBrew(req, res);
|
||||||
|
} catch (e) {
|
||||||
|
err = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(err).not.toBeUndefined();
|
||||||
|
expect(api.getBrew).toHaveBeenCalled();
|
||||||
|
expect(model.findOne).toHaveBeenCalled();
|
||||||
|
expect(removeFunc).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete when one author', async ()=>{
|
||||||
|
const brew = {
|
||||||
|
...hbBrew,
|
||||||
|
authors : ['test']
|
||||||
|
};
|
||||||
|
api.getBrew = jest.fn(()=>async (req)=>{
|
||||||
|
req.brew = brew;
|
||||||
|
});
|
||||||
|
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||||
|
const req = { account: { username: 'test' } };
|
||||||
|
|
||||||
|
await api.deleteBrew(req, res);
|
||||||
|
|
||||||
|
expect(api.getBrew).toHaveBeenCalled();
|
||||||
|
expect(model.findOne).toHaveBeenCalled();
|
||||||
|
expect(removeFunc).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove one author when multiple present', async ()=>{
|
||||||
|
const brew = {
|
||||||
|
...hbBrew,
|
||||||
|
authors : ['test', 'test2']
|
||||||
|
};
|
||||||
|
api.getBrew = jest.fn(()=>async (req)=>{
|
||||||
|
req.brew = brew;
|
||||||
|
});
|
||||||
|
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||||
|
const req = { account: { username: 'test' } };
|
||||||
|
|
||||||
|
await api.deleteBrew(req, res);
|
||||||
|
|
||||||
|
expect(api.getBrew).toHaveBeenCalled();
|
||||||
|
expect(model.findOne).toHaveBeenCalled();
|
||||||
|
expect(removeFunc).not.toHaveBeenCalled();
|
||||||
|
expect(saveFunc).toHaveBeenCalled();
|
||||||
|
expect(saved.authors).toEqual(['test2']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle save error', async ()=>{
|
||||||
|
const brew = {
|
||||||
|
...hbBrew,
|
||||||
|
authors : ['test', 'test2']
|
||||||
|
};
|
||||||
|
api.getBrew = jest.fn(()=>async (req)=>{
|
||||||
|
req.brew = brew;
|
||||||
|
});
|
||||||
|
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||||
|
saveFunc = jest.fn(async ()=>{ throw 'err'; });
|
||||||
|
const req = { account: { username: 'test' } };
|
||||||
|
|
||||||
|
let err;
|
||||||
|
try {
|
||||||
|
await api.deleteBrew(req, res);
|
||||||
|
} catch (e) {
|
||||||
|
err = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(err).not.toBeUndefined();
|
||||||
|
expect(api.getBrew).toHaveBeenCalled();
|
||||||
|
expect(model.findOne).toHaveBeenCalled();
|
||||||
|
expect(removeFunc).not.toHaveBeenCalled();
|
||||||
|
expect(saveFunc).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete google brew', async ()=>{
|
||||||
|
const brew = {
|
||||||
|
...googleBrew,
|
||||||
|
authors : ['test']
|
||||||
|
};
|
||||||
|
api.getBrew = jest.fn(()=>async (req)=>{
|
||||||
|
req.brew = brew;
|
||||||
|
});
|
||||||
|
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||||
|
api.deleteGoogleBrew = jest.fn(async ()=>true);
|
||||||
|
const req = { account: { username: 'test' } };
|
||||||
|
|
||||||
|
await api.deleteBrew(req, res);
|
||||||
|
|
||||||
|
expect(api.getBrew).toHaveBeenCalled();
|
||||||
|
expect(model.findOne).toHaveBeenCalled();
|
||||||
|
expect(removeFunc).toHaveBeenCalled();
|
||||||
|
expect(api.deleteGoogleBrew).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle google brew delete error', async ()=>{
|
||||||
|
const brew = {
|
||||||
|
...googleBrew,
|
||||||
|
authors : ['test']
|
||||||
|
};
|
||||||
|
api.getBrew = jest.fn(()=>async (req)=>{
|
||||||
|
req.brew = brew;
|
||||||
|
});
|
||||||
|
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||||
|
api.deleteGoogleBrew = jest.fn(async ()=>{
|
||||||
|
throw 'err';
|
||||||
|
});
|
||||||
|
const req = { account: { username: 'test' } };
|
||||||
|
|
||||||
|
await api.deleteBrew(req, res);
|
||||||
|
|
||||||
|
expect(api.getBrew).toHaveBeenCalled();
|
||||||
|
expect(model.findOne).toHaveBeenCalled();
|
||||||
|
expect(removeFunc).toHaveBeenCalled();
|
||||||
|
expect(api.deleteGoogleBrew).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should delete google brew and retain stub when multiple authors and owner request deletion', async ()=>{
|
||||||
|
const brew = {
|
||||||
|
...googleBrew,
|
||||||
|
authors : ['test', 'test2']
|
||||||
|
};
|
||||||
|
api.getBrew = jest.fn(()=>async (req)=>{
|
||||||
|
req.brew = brew;
|
||||||
|
});
|
||||||
|
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||||
|
api.deleteGoogleBrew = jest.fn(async ()=>true);
|
||||||
|
const req = { account: { username: 'test' } };
|
||||||
|
|
||||||
|
await api.deleteBrew(req, res);
|
||||||
|
|
||||||
|
expect(api.getBrew).toHaveBeenCalled();
|
||||||
|
expect(model.findOne).toHaveBeenCalled();
|
||||||
|
expect(removeFunc).not.toHaveBeenCalled();
|
||||||
|
expect(api.deleteGoogleBrew).toHaveBeenCalled();
|
||||||
|
expect(saveFunc).toHaveBeenCalled();
|
||||||
|
expect(saved.authors).toEqual(['test2']);
|
||||||
|
expect(saved.googleId).toEqual(undefined);
|
||||||
|
expect(saved.text).toEqual(undefined);
|
||||||
|
expect(saved.textBin).not.toEqual(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should retain google brew and update stub when multiple authors and extra author requests deletion', async ()=>{
|
||||||
|
const brew = {
|
||||||
|
...googleBrew,
|
||||||
|
authors : ['test', 'test2']
|
||||||
|
};
|
||||||
|
api.getBrew = jest.fn(()=>async (req)=>{
|
||||||
|
req.brew = brew;
|
||||||
|
});
|
||||||
|
model.findOne = jest.fn(async ()=>modelBrew(brew));
|
||||||
|
api.deleteGoogleBrew = jest.fn(async ()=>true);
|
||||||
|
const req = { account: { username: 'test2' } };
|
||||||
|
|
||||||
|
await api.deleteBrew(req, res);
|
||||||
|
|
||||||
|
expect(api.getBrew).toHaveBeenCalled();
|
||||||
|
expect(model.findOne).toHaveBeenCalled();
|
||||||
|
expect(removeFunc).not.toHaveBeenCalled();
|
||||||
|
expect(api.deleteGoogleBrew).not.toHaveBeenCalled();
|
||||||
|
expect(saveFunc).toHaveBeenCalled();
|
||||||
|
expect(saved.authors).toEqual(['test']);
|
||||||
|
expect(saved.googleId).toEqual(brew.googleId);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user