-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from tuanlc/OpenPaaS-Suite/esn-frontend-calenda…
…r/issues#51 linagora/esn-frontend-calendar#51 implement ESNDavImportClient
- Loading branch information
Showing
10 changed files
with
869 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "linagora-esn", | ||
"rules": { | ||
"consistent-this": ["warn", "self"], | ||
"no-unused-vars": ["error", { "vars": "all", "args": "after-used" }], | ||
"no-console": ["error", { "allow": ["warn", "error"] }], | ||
"no-process-env": "off", | ||
"arrow-parens": ["error", "as-needed"], | ||
"import/prefer-default-export": 0 | ||
}, | ||
"parserOptions": { | ||
"sourceType": "module" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
package-lock.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
# esn-dav-import-client | ||
JS library for dav import feature | ||
JS library for ESN DAV import client | ||
|
||
## Installation | ||
|
||
NPM and Yarn: | ||
|
||
`npm install https://github.com/OpenPaaS-Suite/esn-dav-import-client` | ||
|
||
`yarn add https://github.com/OpenPaaS-Suite/esn-dav-import-client` | ||
|
||
## Developing | ||
- `npm run test` run lint & unit tests | ||
- `npm run lint` run lint | ||
- `npm run unit-test` to run unit tests |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "esn-dav-import-client", | ||
"version": "0.0.1", | ||
"description": "JS library for ESN DAV import client", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "npm run lint && npm run unit-test", | ||
"lint": "eslint src", | ||
"unit-test": "jest" | ||
}, | ||
"files": [ | ||
"src", | ||
"dist" | ||
], | ||
"keywords": [ | ||
"esn", | ||
"dav", | ||
"import" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/OpenPaaS-Suite/esn-dav-import-client" | ||
}, | ||
"author": "", | ||
"license": "AGPL-3.0", | ||
"devDependencies": { | ||
"@babel/core": "^7.11.4", | ||
"@babel/preset-env": "^7.11.0", | ||
"eslint": "^7.7.0", | ||
"eslint-config-airbnb-base": "^14.2.0", | ||
"eslint-config-linagora-esn": "github:linagora/eslint-config-linagora-esn#v1.1.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint-plugin-jest": "^23.20.0", | ||
"jest": "^26.4.2" | ||
}, | ||
"dependencies": { | ||
"esn-api-client": "github:openpaas-suite/esn-api-client#main" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Client, davImportApi } from 'esn-api-client/src'; | ||
|
||
export default class ESNDavImportClient { | ||
/** | ||
* @constructor | ||
* @param {Function} uploadFile the function to upload file | ||
* @param {String} OPENPAAS_URL the deployed URL for OpenPaaS server | ||
*/ | ||
constructor(uploadFile, OPENPAAS_URL) { | ||
if (typeof uploadFile !== 'function') { | ||
throw new Error('uploadFile is required and must be a function'); | ||
} | ||
|
||
if (!OPENPAAS_URL) { | ||
throw new Error('OPENPAAS_URL is required'); | ||
} | ||
|
||
this.uploadFile = uploadFile; | ||
|
||
const esnApiClient = new Client({ baseURL: `${OPENPAAS_URL}/linagora.esn.dav.import/api` }); | ||
|
||
this.davImportApi = davImportApi(esnApiClient); | ||
} | ||
|
||
importFromFile(file, target) { | ||
return this.uploadFile(file) | ||
.then(fileId => this.davImportApi.importFromFile(fileId, target)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ESNDavImportClient from './ESNDavImportClient'; | ||
|
||
export { ESNDavImportClient }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"env": { | ||
"node": true, | ||
"jest": true | ||
}, | ||
"plugins": ["jest"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { ESNDavImportClient } from '../../src'; | ||
|
||
const OPENPAAS_URL = 'http://esn'; | ||
|
||
describe('The ESNDavImportClient class', () => { | ||
describe('The constructor method', () => { | ||
test('should throw error if the uploadFile method is not provided', () => { | ||
expect(() => new ESNDavImportClient()).toThrow(/uploadFile is required and must be a function/); | ||
}); | ||
|
||
test('should throw error if the uploadFile is not a function', () => { | ||
const uploadFile = 'not a function'; | ||
|
||
expect(() => new ESNDavImportClient(uploadFile)).toThrow(/uploadFile is required and must be a function/); | ||
}); | ||
|
||
test('should throw error if the OPENPAAS_URL is not provided', () => { | ||
const uploadFile = () => {}; | ||
|
||
expect(() => new ESNDavImportClient(uploadFile)).toThrow(/OPENPAAS_URL is required/); | ||
}); | ||
}); | ||
|
||
describe('The importFromFile method', () => { | ||
test('should reject if failed to upload file', done => { | ||
const errorMessage = 'something wrong'; | ||
const uploadFile = jest.fn().mockResolvedValue(Promise.reject(new Error(errorMessage))); | ||
const esnDavImportClient = new ESNDavImportClient(uploadFile, OPENPAAS_URL); | ||
const file = { foo: 'bar' }; | ||
const target = '/target/path'; | ||
|
||
esnDavImportClient.davImportApi = { | ||
importFromFile: jest.fn() | ||
}; | ||
|
||
esnDavImportClient.importFromFile(file, target) | ||
.then(() => done(new Error('should not resolve'))) | ||
.catch(err => { | ||
expect(err.message).toEqual(errorMessage); | ||
expect(uploadFile).toHaveBeenCalledWith(file); | ||
expect(esnDavImportClient.davImportApi.importFromFile).not.toHaveBeenCalled; | ||
done(); | ||
}); | ||
}); | ||
|
||
test('should reject if failed to import from file', done => { | ||
const errorMessage = 'something wrong'; | ||
const fileId = 'fileId'; | ||
const uploadFile = jest.fn().mockResolvedValue(Promise.resolve(fileId)); | ||
const esnDavImportClient = new ESNDavImportClient(uploadFile, OPENPAAS_URL); | ||
const file = { foo: 'bar' }; | ||
const target = '/target/path'; | ||
|
||
esnDavImportClient.davImportApi = { | ||
importFromFile: jest.fn().mockResolvedValue(Promise.reject(new Error(errorMessage))) | ||
}; | ||
|
||
esnDavImportClient.importFromFile(file, target) | ||
.then(() => done(new Error('should not resolve'))) | ||
.catch(err => { | ||
expect(err.message).toEqual(errorMessage); | ||
expect(uploadFile).toHaveBeenCalledWith(file); | ||
expect(esnDavImportClient.davImportApi.importFromFile).toHaveBeenCalledWith(fileId, target); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('should call the davImportClient with correct params to import from file', done => { | ||
const fileId = 'fileId'; | ||
const uploadFile = jest.fn().mockResolvedValue(Promise.resolve(fileId)); | ||
const esnDavImportClient = new ESNDavImportClient(uploadFile, OPENPAAS_URL); | ||
const file = { foo: 'bar' }; | ||
const target = '/target/path'; | ||
|
||
esnDavImportClient.davImportApi = { | ||
importFromFile: jest.fn().mockResolvedValue(Promise.resolve()) | ||
}; | ||
|
||
esnDavImportClient.importFromFile(file, target) | ||
.then(() => { | ||
expect(uploadFile).toHaveBeenCalledWith(file); | ||
expect(esnDavImportClient.davImportApi.importFromFile).toHaveBeenCalledWith(fileId, target); | ||
done(); | ||
}) | ||
.catch(err => done(err || new Error('should resolve'))); | ||
}); | ||
}); | ||
}); |