Skip to content

Commit

Permalink
Merge pull request #1 from tuanlc/OpenPaaS-Suite/esn-frontend-calenda…
Browse files Browse the repository at this point in the history
…r/issues#51

linagora/esn-frontend-calendar#51 implement ESNDavImportClient
  • Loading branch information
Lê Công Tuấn authored Aug 26, 2020
2 parents 5d72ce3 + 1dbd4c2 commit efdb044
Show file tree
Hide file tree
Showing 10 changed files with 869 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .eslintrc.json
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"
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
package-lock.json
668 changes: 668 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion README.md
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
5 changes: 5 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"@babel/preset-env"
]
}
39 changes: 39 additions & 0 deletions package.json
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"
}
}
29 changes: 29 additions & 0 deletions src/ESNDavImportClient.js
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));
}
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ESNDavImportClient from './ESNDavImportClient';

export { ESNDavImportClient };
7 changes: 7 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"node": true,
"jest": true
},
"plugins": ["jest"]
}
88 changes: 88 additions & 0 deletions test/src/ESNDavImportClient.spec.js
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')));
});
});
});

0 comments on commit efdb044

Please sign in to comment.