Commit da5b2edb authored by Laurent's avatar Laurent

SIP Loan: add sip requests

parent 6f32352a
......@@ -17,27 +17,32 @@ export class Manager {
this._configuration = {};
}
public setHTTP(http: HTTPClient): Manager {
this._http = http;
return this;
}
public connect(db: DataSource, account:Account): Promise<MBPortal> {
let portal = MBPortal.findByURL(db, account.getUrl());
if (portal == undefined)
portal = new MBPortal().setURL(account.getUrl());
public request(service: string, content: Object = undefined): Promise<any> {
let params = { url: this._url + '/api/' + service,
headers: { "Content-Type": "application/json" } };
if (content) params['content'] = content;
return this._http
.request({ url: this._url + '/api/portal/' + portal.getKey(),
headers: { "Content-Type": "application/json" } })
.request(params)
.then( (response) => {
let datas = response.content.toJSON();
return datas;
})
}
public connect(db: DataSource, account:Account): Promise<MBPortal> {
let portal = this._findOrCreatePortal(db, account);
return this
.request('portal/' + portal.getKey())
.then( (datas) => {
portal
.setLabel(datas.label)
.setURL(datas.url)
.setFeatures(datas.features);
.setLabel(datas['label'])
.setURL(datas['url'])
.setFeatures(datas['features']);
db.save(portal);
return portal;
})
......@@ -49,11 +54,18 @@ export class Manager {
}
public portals(): Promise<any> {
return this._http
.request({ url: this._url + '/api/portals' })
.then( (response) => {
let datas = response.content.toJSON();
protected _findOrCreatePortal(db: DataSource, account: Account): MBPortal {
let portal:MBPortal = MBPortal.findByURL(db, account.getUrl());
return portal == undefined
? new MBPortal().setURL(account.getUrl())
: portal;
}
public portals(): Promise<Array<MBPortal>> {
return this
.request('portals')
.then( (datas) => {
let portals = datas.map( (portal) => {
return new MBPortal()
.setLabel(portal.label)
......@@ -63,4 +75,15 @@ export class Manager {
return Promise.resolve(portals);
});
}
public loan(db: DataSource, account: Account, password: string, barcode: string): Promise<Object> {
let portal = this._findOrCreatePortal(db, account);
return this
.request('loan/' + portal.getKey(),
{ login: account.credentials['login'],
password: password,
barcode: barcode
})
}
}
......@@ -7,7 +7,8 @@ export class ManagerWithTwoPortals extends HTTPScenario {
public constructor(base_url: string) {
super();
this
.expect({ url: base_url + '/api/portals' },
.expect({ url: base_url + '/api/portals',
headers: { "Content-Type": "application/json" } },
{ content: this.portals() });
}
......@@ -66,6 +67,24 @@ export class ManagerConnectAnnecyNotFound extends HTTPScenario {
export class ManagerLoanAnnecy extends HTTPScenario {
public constructor(base_url: string) {
super();
this
.expect({ url: base_url + '/api/loan/library.annecy.fr',
headers: { 'Content-Type': 'application/json' },
content: {login: 'marco', password: 'polo', barcode: 'L9876'}
},
{
content: JSON.stringify({ title: 'I love sushis',
success: true,
date_due: '2018-07-23' })
});
}
}
describe('Manager', () => {
let manager: Manager;
let db: Database;
......@@ -76,16 +95,18 @@ describe('Manager', () => {
describe('on annecy account', () => {
let annecy: Account;
let annecy_account: Account;
beforeEach( () => {
annecy = (new Account()).setUrl('http://library.annecy.fr');
annecy_account = new Account()
.setUrl('http://library.annecy.fr')
.setCredentials({ login:'marco' });
manager = new Manager('http://mbam.org', new ManagerConnectAnnecy('http://mbam.org'));
});
it('with no existing portal it should create it with its features', () => {
return manager.connect(db, annecy).then( (portal) => {
return manager.connect(db, annecy_account).then( (portal) => {
expect(portal.getLabel()).to.equals('Annecy');
expect(portal.getURL()).to.equals('http://library.annecy.fr');
expect(portal.getKey()).to.equals('library.annecy.fr');
......@@ -97,7 +118,7 @@ describe('Manager', () => {
it('with existing portal assigned it should update it', () => {
let portal = new MBPortal().setURL('http://library.annecy.fr');
db.save(portal);
return manager.connect(db, annecy).then( (updated) => {
return manager.connect(db, annecy_account).then( (updated) => {
expect(updated.getId()).to.equals(portal.getId());
});
})
......@@ -105,13 +126,24 @@ describe('Manager', () => {
it('with no existing description it should stub it', () => {
return new Manager('http://mbam.org', new ManagerConnectAnnecyNotFound('http://mbam.org'))
.connect(db, annecy).then( (portal) => {
.connect(db, annecy_account).then( (portal) => {
expect(portal.getLabel()).to.equals('');
expect(portal.getURL()).to.equals('http://library.annecy.fr');
expect(portal.getKey()).to.equals('library.annecy.fr');
expect(portal.sip_loan_feature).to.be.false;
});
})
it('should send loan request and return result', () => {
let portal = new MBPortal().setURL('http://library.annecy.fr');
db.save(portal);
return new Manager('http://mbam.org', new ManagerLoanAnnecy('http://mbam.org'))
.loan(db, annecy_account, 'polo', 'L9876').then( (response: Object) => {
expect(response['title']).to.equals('I love sushis');
})
})
});
......@@ -137,5 +169,4 @@ describe('Manager', () => {
})
})
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment