mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 09:32:41 +00:00
committed by
Karl-Philipp Wulfert
parent
1ae3beb347
commit
766205049b
@@ -29,7 +29,7 @@
|
|||||||
"branches": 95,
|
"branches": 95,
|
||||||
"check-coverage": true,
|
"check-coverage": true,
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"src/test/**/*.spec.ts",
|
"src/SMTP.ts",
|
||||||
"src/cli.ts"
|
"src/cli.ts"
|
||||||
],
|
],
|
||||||
"extension": [
|
"extension": [
|
||||||
|
|||||||
@@ -14,11 +14,15 @@
|
|||||||
*/
|
*/
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
import {slow, suite, test, timeout} from 'mocha-typescript';
|
import {slow, suite, test, timeout} from 'mocha-typescript';
|
||||||
import {deleteUndefinedProperties} from '../src/common';
|
import {
|
||||||
|
deleteUndefinedProperties,
|
||||||
|
isNodeEnvironment,
|
||||||
|
isProductiveEnvironment,
|
||||||
|
isProductiveNodeEnvironment,
|
||||||
|
} from '../src/common';
|
||||||
|
|
||||||
@suite(timeout(2000), slow(1000))
|
@suite(timeout(2000), slow(1000))
|
||||||
export class CommonSpec {
|
export class CommonSpec {
|
||||||
/* tslint:disable:member-ordering */
|
|
||||||
@test
|
@test
|
||||||
deleteUndefinedProperties1() {
|
deleteUndefinedProperties1() {
|
||||||
expect(deleteUndefinedProperties(
|
expect(deleteUndefinedProperties(
|
||||||
@@ -67,4 +71,48 @@ export class CommonSpec {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
isNodeEnvironment() {
|
||||||
|
expect(isNodeEnvironment()).to.be.equal(true);
|
||||||
|
|
||||||
|
const savedProcess = process;
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
process = undefined;
|
||||||
|
|
||||||
|
expect(isNodeEnvironment()).to.be.equal(false);
|
||||||
|
|
||||||
|
process = savedProcess;
|
||||||
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
isProductiveEnvironment() {
|
||||||
|
const nodeEnv = process.env.NODE_ENV;
|
||||||
|
|
||||||
|
process.env.NODE_ENV = '';
|
||||||
|
|
||||||
|
expect(isProductiveEnvironment()).to.be.equal(false);
|
||||||
|
|
||||||
|
process.env.NODE_ENV = 'production';
|
||||||
|
|
||||||
|
expect(isProductiveEnvironment()).to.be.equal(true);
|
||||||
|
|
||||||
|
process.env.NODE_ENV = nodeEnv;
|
||||||
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
isProductiveNodeEnvironment() {
|
||||||
|
const nodeEnv = process.env.NODE_ENV;
|
||||||
|
|
||||||
|
process.env.NODE_ENV = '';
|
||||||
|
|
||||||
|
expect(isProductiveNodeEnvironment()).to.be.equal(false);
|
||||||
|
|
||||||
|
process.env.NODE_ENV = 'production';
|
||||||
|
|
||||||
|
expect(isProductiveNodeEnvironment()).to.be.equal(true);
|
||||||
|
|
||||||
|
process.env.NODE_ENV = nodeEnv;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2018 StApps
|
* Copyright (C) 2018, 2019 StApps
|
||||||
* This program is free software: you can redistribute it and/or modify it
|
* 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
|
* under the terms of the GNU General Public License as published by the Free
|
||||||
* Software Foundation, version 3.
|
* Software Foundation, version 3.
|
||||||
@@ -12,216 +12,282 @@
|
|||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
import * as chai from 'chai';
|
||||||
import {expect} from 'chai';
|
import {expect} from 'chai';
|
||||||
|
import * as chaiAsPromised from 'chai-as-promised';
|
||||||
import {slow, suite, test, timeout} from 'mocha-typescript';
|
import {slow, suite, test, timeout} from 'mocha-typescript';
|
||||||
import * as sinon from 'sinon';
|
import * as sinon from 'sinon';
|
||||||
import {Logger} from '../src/Logger';
|
import {Logger} from '../src/Logger';
|
||||||
|
import {DummyTransport} from './Transport.spec';
|
||||||
|
|
||||||
const logger1 = new Logger();
|
chai.should();
|
||||||
|
chai.use(chaiAsPromised);
|
||||||
|
|
||||||
@suite(timeout(2000), slow(1000))
|
@suite(timeout(2000), slow(1000))
|
||||||
export class LoggerSpec {
|
export class LoggerSpec {
|
||||||
|
|
||||||
@test
|
@test
|
||||||
error1(done: () => void) {
|
async error() {
|
||||||
let
|
const stub = sinon.stub(console, 'error');
|
||||||
stub;
|
|
||||||
stub = sinon.stub(console, 'error');
|
|
||||||
|
|
||||||
logger1.error('Foobar');
|
await Logger.error('Foobar');
|
||||||
|
|
||||||
stub.restore();
|
stub.restore();
|
||||||
|
|
||||||
expect(stub.args[0][0]).contains('[ERROR] Foobar');
|
expect(stub.args[0][0]).contains('[ERROR]');
|
||||||
|
expect(stub.args[0][0]).contains('Foobar');
|
||||||
done();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
error2(done: () => void) {
|
async errorInProductiveEnvironment() {
|
||||||
|
const errorStub = sinon.stub(console, 'error');
|
||||||
|
|
||||||
|
const nodeEnv = process.env.NODE_ENV;
|
||||||
|
process.env.NODE_ENV = 'production';
|
||||||
|
|
||||||
|
await Logger.error('Foobar').should.be.rejectedWith(Error);
|
||||||
|
expect(errorStub.callCount).to.be.equal(1);
|
||||||
|
|
||||||
|
process.env.ALLOW_NO_TRANSPORT = 'true';
|
||||||
|
|
||||||
|
await Logger.error('Foobar').should.not.be.rejectedWith(Error);
|
||||||
|
expect(errorStub.callCount).to.be.equal(2);
|
||||||
|
|
||||||
|
delete process.env.ALLOW_NO_TRANSPORT;
|
||||||
|
|
||||||
|
Logger.setTransport(new DummyTransport());
|
||||||
|
|
||||||
|
await Logger.error('Foobar').should.not.be.rejectedWith(Error);
|
||||||
|
expect(errorStub.callCount).to.be.equal(3);
|
||||||
|
|
||||||
|
Logger.setTransport();
|
||||||
|
|
||||||
|
process.env.NODE_ENV = nodeEnv;
|
||||||
|
|
||||||
|
errorStub.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
async errorNoOutput() {
|
||||||
|
const stub = sinon.stub(console, 'error');
|
||||||
|
|
||||||
|
process.env.STAPPS_LOG_LEVEL = '0';
|
||||||
|
|
||||||
|
await Logger.error('Foobar');
|
||||||
|
|
||||||
|
stub.restore();
|
||||||
|
|
||||||
|
delete process.env.STAPPS_LOG_LEVEL;
|
||||||
|
|
||||||
|
return expect(stub.called).to.be.false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
async errorWithError() {
|
||||||
const e = new Error();
|
const e = new Error();
|
||||||
const stub = sinon.stub(console, 'error');
|
const stub = sinon.stub(console, 'error');
|
||||||
|
|
||||||
logger1.error(e);
|
await Logger.error(e);
|
||||||
|
|
||||||
stub.restore();
|
stub.restore();
|
||||||
|
|
||||||
expect(stub.args[0][0]).contains('Error').contains('at').contains(process.cwd());
|
expect(stub.args[0][0]).contains('Error').contains('at').contains(process.cwd());
|
||||||
|
|
||||||
done();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
info(done: () => void) {
|
getLogLevel() {
|
||||||
let
|
const savedProcess = process;
|
||||||
stub;
|
|
||||||
stub = sinon.stub(console, 'info');
|
|
||||||
|
|
||||||
logger1.info('Foobar');
|
// @ts-ignore
|
||||||
|
process = undefined;
|
||||||
|
|
||||||
|
(global as any).window = {
|
||||||
|
STAPPS_LOG_LEVEL: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const stub = sinon.stub(console, 'info');
|
||||||
|
|
||||||
|
Logger.info('Foobar');
|
||||||
|
|
||||||
stub.restore();
|
stub.restore();
|
||||||
|
|
||||||
expect(stub.args[0][0]).contains('[INFO] Foobar');
|
process = savedProcess;
|
||||||
|
|
||||||
done();
|
delete (global as any).window;
|
||||||
|
|
||||||
|
return expect(stub.called).to.be.false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
log(done: () => void) {
|
info() {
|
||||||
let
|
const stub = sinon.stub(console, 'info');
|
||||||
stub;
|
|
||||||
stub = sinon.stub(console, 'log');
|
|
||||||
|
|
||||||
logger1.log('Foobar');
|
Logger.info('Foobar');
|
||||||
|
|
||||||
stub.restore();
|
stub.restore();
|
||||||
|
|
||||||
expect(stub.args[0][0]).contains('[LOG] Foobar');
|
expect(stub.args[0][0]).contains('[INFO]');
|
||||||
|
expect(stub.args[0][0]).contains('Foobar');
|
||||||
done();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
logLevel1(done: () => void) {
|
infoNoOutput() {
|
||||||
let logStub;
|
const stub = sinon.stub(console, 'info');
|
||||||
let warnStub;
|
|
||||||
|
|
||||||
process.env.STAPPS_LOG_LEVEL = '8';
|
process.env.STAPPS_LOG_LEVEL = '0';
|
||||||
|
|
||||||
logStub = sinon.stub(console, 'log');
|
Logger.info('Foobar');
|
||||||
warnStub = sinon.stub(console, 'warn');
|
|
||||||
|
|
||||||
logger1.ok('foo');
|
stub.restore();
|
||||||
logger1.warn('bar');
|
|
||||||
|
|
||||||
logStub.restore();
|
|
||||||
|
|
||||||
/* tslint:disable:no-unused-expression */
|
|
||||||
expect(logStub.called).to.be.false;
|
|
||||||
expect(warnStub.called).to.be.false;
|
|
||||||
/* tslint:enable */
|
|
||||||
|
|
||||||
delete process.env.STAPPS_LOG_LEVEL;
|
delete process.env.STAPPS_LOG_LEVEL;
|
||||||
|
|
||||||
logger1.warn('bar');
|
return expect(stub.called).to.be.false;
|
||||||
|
|
||||||
/* tslint:disable:no-unused-expression */
|
|
||||||
expect(warnStub.called).to.be.true;
|
|
||||||
/* tslint:enable */
|
|
||||||
|
|
||||||
warnStub.restore();
|
|
||||||
|
|
||||||
done();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
logLevel2(done: () => void) {
|
initialized() {
|
||||||
let logStub;
|
Logger.setTransport(new DummyTransport());
|
||||||
let warnStub;
|
|
||||||
|
|
||||||
process.env.STAPPS_LOG_LEVEL = '12';
|
expect(() => {
|
||||||
|
Logger.initialized();
|
||||||
|
}).not.to.throw();
|
||||||
|
|
||||||
logStub = sinon.stub(console, 'log');
|
Logger.setTransport();
|
||||||
warnStub = sinon.stub(console, 'warn');
|
}
|
||||||
|
|
||||||
logger1.ok('foo');
|
@test
|
||||||
logger1.warn('bar');
|
initializedInProductiveEnvironment() {
|
||||||
|
const nodeEnv = process.env.NODE_ENV;
|
||||||
|
process.env.NODE_ENV = 'production';
|
||||||
|
|
||||||
logStub.restore();
|
Logger.setTransport(new DummyTransport());
|
||||||
|
|
||||||
/* tslint:disable:no-unused-expression */
|
expect(() => {
|
||||||
expect(logStub.called).to.be.false;
|
Logger.initialized();
|
||||||
expect(warnStub.called).to.be.true;
|
}).not.to.throw();
|
||||||
/* tslint:enable */
|
|
||||||
|
Logger.setTransport();
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
Logger.initialized();
|
||||||
|
}).to.throw();
|
||||||
|
|
||||||
|
const stub = sinon.stub(console, 'warn');
|
||||||
|
|
||||||
|
process.env.ALLOW_NO_TRANSPORT = 'true';
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
Logger.initialized();
|
||||||
|
}).not.to.throw();
|
||||||
|
|
||||||
|
delete process.env.ALLOW_NO_TRANSPORT;
|
||||||
|
|
||||||
|
expect(stub.called).to.be.equal(true);
|
||||||
|
|
||||||
|
stub.restore();
|
||||||
|
|
||||||
|
process.env.NODE_ENV = nodeEnv;
|
||||||
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
log() {
|
||||||
|
const stub = sinon.stub(console, 'log');
|
||||||
|
|
||||||
|
Logger.log('Foobar');
|
||||||
|
|
||||||
|
stub.restore();
|
||||||
|
|
||||||
|
expect(stub.args[0][0]).contains('[LOG]');
|
||||||
|
expect(stub.args[0][0]).contains('Foobar');
|
||||||
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
logNoOutput() {
|
||||||
|
const stub = sinon.stub(console, 'log');
|
||||||
|
|
||||||
|
process.env.STAPPS_LOG_LEVEL = '0';
|
||||||
|
|
||||||
|
Logger.log('Foobar');
|
||||||
|
|
||||||
|
stub.restore();
|
||||||
|
|
||||||
delete process.env.STAPPS_LOG_LEVEL;
|
delete process.env.STAPPS_LOG_LEVEL;
|
||||||
|
|
||||||
logger1.warn('bar');
|
return expect(stub.called).to.be.false;
|
||||||
|
|
||||||
/* tslint:disable:no-unused-expression */
|
|
||||||
expect(warnStub.called).to.be.true;
|
|
||||||
/* tslint:enable */
|
|
||||||
|
|
||||||
warnStub.restore();
|
|
||||||
|
|
||||||
done();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
logLevel3(done: () => void) {
|
ok() {
|
||||||
let logStub;
|
const stub = sinon.stub(console, 'log');
|
||||||
let warnStub;
|
|
||||||
|
|
||||||
process.env.STAPPS_LOG_LEVEL = '20';
|
Logger.ok('Foobar');
|
||||||
|
|
||||||
logStub = sinon.stub(console, 'log');
|
stub.restore();
|
||||||
warnStub = sinon.stub(console, 'warn');
|
|
||||||
|
|
||||||
logger1.ok('foo');
|
expect(stub.args[0][0]).contains('[OK]');
|
||||||
logger1.warn('bar');
|
expect(stub.args[0][0]).contains('Foobar');
|
||||||
|
}
|
||||||
|
|
||||||
logStub.restore();
|
@test
|
||||||
|
okNoOutput() {
|
||||||
|
const stub = sinon.stub(console, 'log');
|
||||||
|
|
||||||
/* tslint:disable:no-unused-expression */
|
process.env.STAPPS_LOG_LEVEL = '0';
|
||||||
expect(logStub.called).to.be.true;
|
|
||||||
expect(warnStub.called).to.be.true;
|
Logger.ok('Foobar');
|
||||||
/* tslint:enable */
|
|
||||||
|
stub.restore();
|
||||||
|
|
||||||
delete process.env.STAPPS_LOG_LEVEL;
|
delete process.env.STAPPS_LOG_LEVEL;
|
||||||
|
|
||||||
logger1.warn('bar');
|
return expect(stub.called).to.be.false;
|
||||||
|
|
||||||
/* tslint:disable:no-unused-expression */
|
|
||||||
expect(warnStub.called).to.be.true;
|
|
||||||
/* tslint:enable */
|
|
||||||
|
|
||||||
warnStub.restore();
|
|
||||||
|
|
||||||
done();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
ok(done: () => void) {
|
setTransport() {
|
||||||
let
|
expect(() => {
|
||||||
stub;
|
Logger.setTransport(new DummyTransport());
|
||||||
stub = sinon.stub(console, 'log');
|
Logger.setTransport();
|
||||||
|
}).not.to.throw();
|
||||||
logger1.ok('Foobar');
|
|
||||||
|
|
||||||
stub.restore();
|
|
||||||
|
|
||||||
expect(stub.args[0][0]).contains('[OK] Foobar');
|
|
||||||
|
|
||||||
done();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
stringifyArguments(done: () => void) {
|
stringifyArguments() {
|
||||||
let
|
const stub = sinon.stub(console, 'log');
|
||||||
stub;
|
|
||||||
stub = sinon.stub(console, 'log');
|
|
||||||
|
|
||||||
logger1.log('foo', 'bar');
|
Logger.log('foo', 'bar');
|
||||||
|
|
||||||
stub.restore();
|
stub.restore();
|
||||||
|
|
||||||
expect(stub.args[0][0]).contains('[LOG]').and.contains('foo').and.contains('bar');
|
expect(stub.args[0][0]).contains('[LOG]');
|
||||||
|
expect(stub.args[0][0]).contains('foo');
|
||||||
done();
|
expect(stub.args[0][0]).contains('bar');
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
warn(done: () => void) {
|
warn() {
|
||||||
let
|
const stub = sinon.stub(console, 'warn');
|
||||||
stub;
|
|
||||||
stub = sinon.stub(console, 'warn');
|
|
||||||
|
|
||||||
logger1.warn('Foobar');
|
Logger.warn('Foobar');
|
||||||
|
|
||||||
stub.restore();
|
stub.restore();
|
||||||
|
|
||||||
expect(stub.args[0][0]).contains('[WARN] Foobar');
|
expect(stub.args[0][0]).contains('[WARN]');
|
||||||
|
expect(stub.args[0][0]).contains('Foobar');
|
||||||
|
}
|
||||||
|
|
||||||
done();
|
@test
|
||||||
|
warnNoOutput() {
|
||||||
|
const stub = sinon.stub(console, 'warn');
|
||||||
|
|
||||||
|
process.env.STAPPS_LOG_LEVEL = '0';
|
||||||
|
|
||||||
|
Logger.warn('Foobar');
|
||||||
|
|
||||||
|
stub.restore();
|
||||||
|
|
||||||
|
delete process.env.STAPPS_LOG_LEVEL;
|
||||||
|
|
||||||
|
return expect(stub.called).to.be.false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
65
test/Transport.spec.ts
Normal file
65
test/Transport.spec.ts
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* 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 {expect} from 'chai';
|
||||||
|
import {slow, suite, test, timeout} from 'mocha-typescript';
|
||||||
|
import {isTransportWithVerification} from '../src/common';
|
||||||
|
import {Transport, VerifiableTransport} from '../src/Transport';
|
||||||
|
|
||||||
|
export class DummyTransport extends Transport {
|
||||||
|
send(subject: string, message: string): Promise<string> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (0 === 0) {
|
||||||
|
resolve(subject);
|
||||||
|
}
|
||||||
|
|
||||||
|
reject(message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class VerifiableDummyTransport extends VerifiableTransport {
|
||||||
|
isVerified(): boolean {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
send(subject: string, message: string): Promise<string> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (0 === 0) {
|
||||||
|
resolve(subject);
|
||||||
|
}
|
||||||
|
|
||||||
|
reject(message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
verify(): Promise<boolean> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
resolve(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@suite(timeout(2000), slow(1000))
|
||||||
|
export class TransportSpec {
|
||||||
|
@test
|
||||||
|
isNotTransportWithVerification() {
|
||||||
|
return expect(isTransportWithVerification(new DummyTransport())).to.be.false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
isTransportWithVerification() {
|
||||||
|
return expect(isTransportWithVerification(new VerifiableDummyTransport())).to.be.true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user