Skip to content

Commit

Permalink
fix: Only use defaultDir in the manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
Crash-- committed Jul 29, 2019
1 parent 296829f commit dee6277
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
79 changes: 50 additions & 29 deletions packages/cozy-harvest-lib/src/helpers/konnectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import trim from 'lodash/trim'
import * as accounts from './accounts'

// Default name for base directory
const DEFAULT_LOCALIZED_BAR_DIR = 'Administrative'
const DEFAULT_LOCALIZED_BASE_DIR = 'Administrative'

// Type of errors returned by konnector
const CHALLENGE_ASKED = 'CHALLENGE_ASKED'
Expand Down Expand Up @@ -127,15 +127,9 @@ const allowedBaseDirVariables = ['$administrative', '$photos']
* @return {String} Localized directory
*/
const renderBaseDir = (baseDir, folders = {}) => {
if (!allowedBaseDirVariables.includes(baseDir))
throw new Error(
`baseDir may only have the following values: ${allowedBaseDirVariables.join(
', '
)})`
)

// Look for variable name into folders but without $ prefix
const renderedBaseDir = folders[baseDir.slice(1)] || DEFAULT_LOCALIZED_BAR_DIR
const renderedBaseDir =
folders[baseDir.slice(1)] || DEFAULT_LOCALIZED_BASE_DIR
// Trim `/` and avoid multiple `/` characters with regexp
return trim(renderedBaseDir.replace(/(\/+)/g, '/'), '/')
}
Expand All @@ -148,7 +142,7 @@ const renderBaseDir = (baseDir, folders = {}) => {
* @param {Object} variables Object mapping variable to actual values
* @return {String} Rendered path
*/
const renderFolderPath = (path, variables = {}) => {
const renderSubDir = (path, variables = {}) => {
// Trim `/` and avoid multiple `/` characters with regexp
const sanitizedPath = trim(path.replace(/(\/+)/g, '/'), '/')

Expand All @@ -160,12 +154,33 @@ const renderFolderPath = (path, variables = {}) => {
.join('/')
}

/**
* Check if the provided Path start withs our allowedBaseDirPath to see
* @param {String} path
* @return {Boolean}
*/
const hasBaseDir = path => {
return allowedBaseDirVariables.some(baseDirVar => {
return path.startsWith(baseDirVar)
})
}
/**
* This method creates the subDir. We can't have an empty subDir, so we set
* it to our default '$konnector/$account'
* @param {String} fullPath String containing potentially the defaultDir
* @param {String} defaultDir String to remove from the fullPath
*/
const buildSubDir = (fullPath, defaultDir) => {
let buildedSubDir = fullPath.substring(defaultDir.length)
if (buildedSubDir === '') {
buildedSubDir = '$konnector/$account'
}
return buildedSubDir
}
/**
* Build folder path for a given konnector and a given account.
*
* If konnector.folders[0].defaultDir exists, it is used as default directory.
* If konnector.folders[0].defaultPath exists, it is used as default folder path
* as well.
*
* Occurrences of following strings in base directory are replaced by:
* * `$administrative`: Administrative folder
Expand All @@ -175,11 +190,8 @@ const renderFolderPath = (path, variables = {}) => {
* * `$account: Account label (id or name)`
* * `$konnector`: Konnector name
*
* If no konnectors.folders[0].defaultDir is set, the default base dir used is
* `$administrative`.
*
* If no konnectors.folders[0].defaultPath is set, the default path used is
* `/$konnector/$account`.
* If no konnectors.folders[0].defaultDir is set, the default dir used is
* * `$administrative/$konnector/$account`
*
* @param {Object} konnector Konnector document
* @param {Object} account Account document
Expand All @@ -188,30 +200,39 @@ const renderFolderPath = (path, variables = {}) => {
* Administratif).
* @return {String} The result path
*/

export const buildFolderPath = (konnector, account, folders = {}) => {
const baseDir = get(
const fullPath = get(
konnector,
// For now konnectors are only defining one folder in their folders array
'folders[0].defaultDir',
`$administrative`
`$administrative/$konnector/$account`
)
// Trim `/` and avoid multiple `/` characters with regexp
let sanitizedPath = trim(fullPath.replace(/(\/+)/g, '/'), '/')
//We remove the first / if needed
if (sanitizedPath.startsWith('/')) sanitizedPath = sanitizedPath.substring(1)
//If the konnector doesn't have any of our base dir, we set it to $administrative
if (!hasBaseDir(sanitizedPath)) {
sanitizedPath = '$administrative/' + sanitizedPath
}
/**
* Now that we have our sanitizedPath, we can split it in two strings
* * `baseDir` containing the baseDir path
* * `buildedSubDir` containing the rest of the path (ie the path without baseDir)
*/
const baseDir = sanitizedPath.split('/', 1)
const buildedSubDir = buildSubDir(sanitizedPath, baseDir[0])

const path = get(konnector, 'folders[0].defaultPath', `/$konnector/$account`)

// Separate baseDir parsing and path parsing to avoid allowing unexpected
// variables in both dir and path : we do not want $konnector in baseDir for
// example.
const renderedBaseDir = renderBaseDir(baseDir, folders)
const renderedPath = renderFolderPath(path, {
const renderedBaseDir = renderBaseDir(baseDir[0], folders)
const renderedPath = renderSubDir(buildedSubDir, {
// When adding a new allowed variable here, please keep documentation
// of `renderFolderPath` function up to date.
// of `renderSubDir` function up to date.
konnector: konnector.name,
account: accounts.getLabel(account)
})

return `/${renderedBaseDir}/${renderedPath}`
}

/**
* Returns a permission ready to be passed to
* client.collection('io.cozy.permissions').add().
Expand Down
22 changes: 6 additions & 16 deletions packages/cozy-harvest-lib/test/helpers/konnectors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('Konnectors Helpers', () => {
it('should build path from konnector with defaultPath', () => {
const konnector = {
...fixtures.konnector,
folders: [{ defaultPath: '/Custom folder/$konnector' }]
folders: [{ defaultDir: '/Custom folder/$konnector' }]
}
expect(
buildFolderPath(konnector, fixtures.account, { photos: 'Photos' })
Expand All @@ -93,27 +93,17 @@ describe('Konnectors Helpers', () => {
it('should build path from konnector with defaultDir and defaultPath', () => {
const konnector = {
...fixtures.konnector,
folders: [
{ defaultDir: '$photos', defaultPath: '/Custom folder/$konnector' }
]
folders: [{ defaultDir: '$photos/Custom folder/$konnector' }]
}
expect(
buildFolderPath(konnector, fixtures.account, { photos: 'Photos' })
).toEqual('/Photos/Custom folder/Test Konnector')
})

it('should throw error for unexpected defaultDir', () => {
const konnector = {
...fixtures.konnector,
folders: [{ defaultDir: '$stuff' }]
}
expect(() => buildFolderPath(konnector, fixtures.account)).toThrow()
})

it('should add leading slash to path', () => {
const konnector = {
...fixtures.konnector,
folders: [{ defaultPath: 'custom folder' }]
folders: [{ defaultDir: 'custom folder' }]
}
expect(buildFolderPath(konnector, fixtures.account)).toEqual(
'/Administrative/custom folder'
Expand All @@ -123,7 +113,7 @@ describe('Konnectors Helpers', () => {
it('should remove trailing slash from path', () => {
const konnector = {
...fixtures.konnector,
folders: [{ defaultPath: 'custom folder/' }]
folders: [{ defaultDir: 'custom folder/' }]
}
expect(buildFolderPath(konnector, fixtures.account)).toEqual(
'/Administrative/custom folder'
Expand All @@ -133,7 +123,7 @@ describe('Konnectors Helpers', () => {
it('should not replace string containing variable name', () => {
const konnector = {
...fixtures.konnector,
folders: [{ defaultPath: '/$konnectorToNotReplace/custom' }]
folders: [{ defaultDir: '/$konnectorToNotReplace/custom' }]
}
expect(buildFolderPath(konnector, fixtures.account)).toEqual(
'/Administrative/$konnectorToNotReplace/custom'
Expand All @@ -143,7 +133,7 @@ describe('Konnectors Helpers', () => {
it('should handle multiple slash in folder path', () => {
const konnector = {
...fixtures.konnector,
folders: [{ defaultPath: '///too//much///slashes/////' }]
folders: [{ defaultDir: '///too//much///slashes/////' }]
}
expect(buildFolderPath(konnector, fixtures.account)).toEqual(
'/Administrative/too/much/slashes'
Expand Down

0 comments on commit dee6277

Please sign in to comment.