Created
March 26, 2020 20:43
-
-
Save samohovets/d83a756a1d22ffaf2fb4a912be9e85d7 to your computer and use it in GitHub Desktop.
Setup strapi for testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Strapi = require('strapi'); | |
const { MongoMemoryServer } = require('mongodb-memory-server'); | |
const mongod = new MongoMemoryServer(); | |
async function setupStrapi (entryNamesToOpen) { | |
const uri = await mongod.getUri(); | |
process.env.DATABASE_URI = uri; | |
await Strapi().start(); | |
const roles = await strapi.plugins[ | |
'users-permissions' | |
].services.userspermissions.getRoles(); | |
const publicRoleId = roles.find(role => role.type === 'public').id; | |
const actions = await strapi.plugins[ | |
'users-permissions' | |
].services.userspermissions.getActions(); | |
const actionsCopy = Object.assign({}, actions); | |
entryNamesToOpen.forEach(entryName => { | |
Object.values(actionsCopy.application.controllers[entryName]).forEach( | |
controller => { | |
controller.enabled = true; | |
} | |
); | |
}); | |
Object.keys(actionsCopy.application.controllers).forEach(entry => { | |
if (!entryNamesToOpen.includes(entry)) { | |
delete actionsCopy.application.controllers[entry]; | |
} | |
}); | |
const body = { | |
permissions: { | |
application: { | |
controllers: actionsCopy.application.controllers | |
} | |
} | |
}; | |
await strapi.plugins[ | |
'users-permissions' | |
].services.userspermissions.updateRole(publicRoleId, body); | |
return strapi; | |
} | |
module.exports = { setupStrapi }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment