mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-09 11:12:52 +00:00
127 lines
4.0 KiB
TypeScript
127 lines
4.0 KiB
TypeScript
/*
|
|
* Copyright (C) 2019 StApps
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
* Software Foundation, version 3.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
import {
|
|
SCBulkAddResponse,
|
|
SCBulkAddRoute,
|
|
SCBulkDoneResponse,
|
|
SCBulkDoneRoute,
|
|
SCBulkResponse,
|
|
SCBulkRoute,
|
|
SCThing,
|
|
} from '@openstapps/core';
|
|
import * as chai from 'chai';
|
|
import * as chaiAsPromised from 'chai-as-promised';
|
|
import * as chaiSpies from 'chai-spies';
|
|
import {existsSync, mkdirSync} from 'fs';
|
|
import {suite, test} from 'mocha-typescript';
|
|
import {join} from 'path';
|
|
import {getItemsFromSamples, indexSamples} from '../src/e2e';
|
|
import {ApiError} from '../src/errors';
|
|
import {HttpClient, RequestOptions, Response} from '../src/httpClient';
|
|
import {RecursivePartial} from './client.spec';
|
|
|
|
chai.should();
|
|
chai.use(chaiSpies);
|
|
chai.use(chaiAsPromised);
|
|
|
|
const sandbox = chai.spy.sandbox();
|
|
|
|
const bulkRoute = new SCBulkRoute();
|
|
const bulkAddRoute = new SCBulkAddRoute();
|
|
const bulkDoneRoute = new SCBulkDoneRoute();
|
|
|
|
const httpClient = new HttpClient();
|
|
|
|
@suite
|
|
export class E2EConnectorSpec {
|
|
async after() {
|
|
sandbox.restore();
|
|
}
|
|
|
|
@test
|
|
getCoreTestSamples() {
|
|
return getItemsFromSamples('./node_modules/@openstapps/core/test/resources')
|
|
.then(<T extends SCThing>(items: T[]) => {
|
|
// tslint:disable-next-line: no-unused-expression
|
|
chai.expect(items).to.not.be.a.instanceof(Error);
|
|
// tslint:disable-next-line: no-unused-expression
|
|
chai.expect(items).to.not.be.empty;
|
|
});
|
|
}
|
|
|
|
@test
|
|
getCoreTestSamplesShouldFail() {
|
|
return getItemsFromSamples('./nonexistantdirectory')
|
|
.then(<T extends SCThing>(items: T[]) => {
|
|
// tslint:disable-next-line: no-unused-expression
|
|
chai.expect(items).to.be.a.instanceof(Error);
|
|
});
|
|
}
|
|
|
|
@test
|
|
async index() {
|
|
type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse>;
|
|
|
|
sandbox.on(httpClient, 'request', async (request: RequestOptions): Promise<RecursivePartial<responses>> => {
|
|
if (request.url.toString() === 'http://localhost' + bulkRoute.getUrlFragment().toString()) {
|
|
return {
|
|
body: {
|
|
state: 'in progress',
|
|
uid: 'foo',
|
|
},
|
|
statusCode: bulkRoute.statusCodeSuccess,
|
|
};
|
|
} else if (request.url.toString() === 'http://localhost' + bulkAddRoute.getUrlFragment({
|
|
UID: 'foo',
|
|
}).toString()) {
|
|
return {
|
|
body: {},
|
|
statusCode: bulkAddRoute.statusCodeSuccess,
|
|
};
|
|
}
|
|
|
|
return {
|
|
body: {},
|
|
statusCode: bulkDoneRoute.statusCodeSuccess,
|
|
};
|
|
});
|
|
await indexSamples(httpClient, {to: 'http://localhost', samples: './node_modules/@openstapps/core/test/resources'});
|
|
}
|
|
|
|
@test
|
|
async indexShouldFail() {
|
|
type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse>;
|
|
|
|
sandbox.on(httpClient, 'request', async (): Promise<RecursivePartial<responses>> => {
|
|
return {
|
|
body: {},
|
|
statusCode: Number.MAX_SAFE_INTEGER,
|
|
};
|
|
});
|
|
return indexSamples(httpClient, {to: 'http://localhost', samples: './node_modules/@openstapps/core/test/resources'})
|
|
.should.be.rejectedWith(ApiError);
|
|
}
|
|
|
|
@test
|
|
async indexShouldFailDirectoryWithoutData() {
|
|
const emptyDirPath = join(__dirname, 'emptyDir');
|
|
if(!existsSync(emptyDirPath))
|
|
mkdirSync(emptyDirPath);
|
|
return indexSamples(httpClient, {to: 'http://localhost', samples: emptyDirPath})
|
|
.should.be.rejectedWith('Could not index samples. None were retrived from the file system.');
|
|
}
|
|
|
|
}
|