Files
openstapps/test/common.ts
2021-04-27 13:02:05 +02:00

147 lines
4.0 KiB
TypeScript

/*
* Copyright (C) 2019, 2020 StApps
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCConfigFile, SCSearchQuery, SCSearchResponse, SCThings, SCThingType, SCUuid} from '@openstapps/core';
import {Express} from 'express';
import moment from 'moment';
import {configureApp} from '../src/app';
import express from 'express';
import http from 'http';
import {configFile} from '../src/common';
import {MailQueue} from '../src/notification/mail-queue';
import {Bulk, BulkStorage} from '../src/storage/bulk-storage';
import getPort from 'get-port';
import {Database} from '../src/storage/database';
import {Elasticsearch} from '../src/storage/elasticsearch/elasticsearch';
import {v4} from 'uuid';
/**
* Adds routers and configures an (express) app
*
*/
export async function startApp(): Promise<Express> {
const app = express();
await configureApp(app, {elasticsearch: ElasticsearchMock});
const server = http.createServer(app);
// get a random free port
const port = await getPort();
server.listen(port);
server.on('error', err => {
throw err;
});
return new Promise(resolve => server.on('listening', () => {
app.set(
'bulk',
bulkStorageMock,
);
resolve(app);
}));
}
/**
* An elasticsearch mock
*/
export class ElasticsearchMock implements Database {
// @ts-ignore
private bulk: Bulk | undefined;
private storageMock = new Map<string, SCThings>();
constructor(_configFile: SCConfigFile, _mailQueue?: MailQueue) {
// Nothing to do here
}
bulkCreated(bulk: Bulk): Promise<void> {
this.bulk = bulk;
return Promise.resolve(undefined);
}
bulkExpired(_bulk: Bulk): Promise<void> {
return Promise.resolve(undefined);
}
bulkUpdated(_bulk: Bulk): Promise<void> {
return Promise.resolve(undefined);
}
get(uid: SCUuid): Promise<SCThings> {
// @ts-ignore
return Promise.resolve(this.storageMock.get(uid));
}
init(): Promise<void> {
return Promise.resolve();
}
post(_thing: SCThings, _bulk: Bulk): Promise<void> {
return Promise.resolve();
}
put(thing: SCThings): Promise<void> {
this.storageMock.set(thing.uid, thing);
return Promise.resolve();
}
search(_params: SCSearchQuery): Promise<SCSearchResponse> {
return Promise.resolve({data: [], facets: [], pagination: {count: 0, offset: 0, total: 0}, stats: {time: 0}});
}
}
export const bulkStorageMock = new BulkStorage(new ElasticsearchMock(configFile));
export const bulk: Bulk = {
expiration: moment().add(3600, 'seconds')
.format(),
source: 'some_source',
state: 'in progress',
type: SCThingType.Book,
uid: ''
};
export class FooError extends Error {
}
export const DEFAULT_TEST_TIMEOUT = 10000;
export const TRANSPORT_SEND_RESPONSE = 'Send Response';
export const getTransport = (verified: boolean) => {
return {
cc: undefined,
from: undefined,
recipients: undefined,
transportAgent: undefined,
verified: undefined,
isVerified(): boolean {
return verified;
},
send(_subject: string, _message: string): Promise<string> {
return Promise.resolve('');
},
sendMail(_mail: any): Promise<string> {
return Promise.resolve(TRANSPORT_SEND_RESPONSE);
},
verify(): Promise<boolean> {
return Promise.resolve(false);
}
}
}
export const getIndex = (uid?: string) => `stapps_footype_foosource_${uid ? uid : Elasticsearch.getIndexUID(v4())}`;