Files
openstapps/test/e2e.spec.ts
2019-06-19 16:14:51 +02:00

142 lines
4.6 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, rmdirSync, unlinkSync} 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/http-client';
import {RecursivePartial} from './client.spec';
import {createFileSync} from 'fs-extra';
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);
await indexSamples(httpClient, {to: 'http://localhost', samples: emptyDirPath})
.should.be.rejectedWith('Could not index samples. None were retrived from the file system.');
rmdirSync(emptyDirPath);
}
@test
async indexShouldFailDirectoryWithoutJsonData() {
const somewhatFilledDirPath = join(__dirname, 'somewhatFilledDir');
if(!existsSync(somewhatFilledDirPath))
mkdirSync(somewhatFilledDirPath);
const nonJsonFile = join (somewhatFilledDirPath, 'nonjson.txt');
createFileSync(nonJsonFile);
await indexSamples(httpClient, {to: 'http://localhost', samples: somewhatFilledDirPath})
.should.be.rejectedWith('Could not index samples. None were retrived from the file system.');
unlinkSync(nonJsonFile);
rmdirSync(somewhatFilledDirPath);
}
}