diff --git a/package.json b/package.json
index 66541144..39097226 100644
--- a/package.json
+++ b/package.json
@@ -29,7 +29,7 @@
"branches": 95,
"check-coverage": true,
"exclude": [
- "src/test/**/*.spec.ts",
+ "src/SMTP.ts",
"src/cli.ts"
],
"extension": [
diff --git a/test/Common.spec.ts b/test/Common.spec.ts
index 7e757854..a3d2697c 100644
--- a/test/Common.spec.ts
+++ b/test/Common.spec.ts
@@ -14,11 +14,15 @@
*/
import {expect} from 'chai';
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))
export class CommonSpec {
- /* tslint:disable:member-ordering */
@test
deleteUndefinedProperties1() {
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;
+ }
}
diff --git a/test/Logger.spec.ts b/test/Logger.spec.ts
index 77812d49..68c657c0 100644
--- a/test/Logger.spec.ts
+++ b/test/Logger.spec.ts
@@ -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
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -12,216 +12,282 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see .
*/
+import * as chai from 'chai';
import {expect} from 'chai';
+import * as chaiAsPromised from 'chai-as-promised';
import {slow, suite, test, timeout} from 'mocha-typescript';
import * as sinon from 'sinon';
import {Logger} from '../src/Logger';
+import {DummyTransport} from './Transport.spec';
-const logger1 = new Logger();
+chai.should();
+chai.use(chaiAsPromised);
@suite(timeout(2000), slow(1000))
export class LoggerSpec {
-
@test
- error1(done: () => void) {
- let
- stub;
- stub = sinon.stub(console, 'error');
+ async error() {
+ const stub = sinon.stub(console, 'error');
- logger1.error('Foobar');
+ await Logger.error('Foobar');
stub.restore();
- expect(stub.args[0][0]).contains('[ERROR] Foobar');
-
- done();
+ expect(stub.args[0][0]).contains('[ERROR]');
+ expect(stub.args[0][0]).contains('Foobar');
}
@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 stub = sinon.stub(console, 'error');
- logger1.error(e);
+ await Logger.error(e);
stub.restore();
expect(stub.args[0][0]).contains('Error').contains('at').contains(process.cwd());
-
- done();
}
@test
- info(done: () => void) {
- let
- stub;
- stub = sinon.stub(console, 'info');
+ getLogLevel() {
+ const savedProcess = process;
- 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();
- expect(stub.args[0][0]).contains('[INFO] Foobar');
+ process = savedProcess;
- done();
+ delete (global as any).window;
+
+ return expect(stub.called).to.be.false;
}
@test
- log(done: () => void) {
- let
- stub;
- stub = sinon.stub(console, 'log');
+ info() {
+ const stub = sinon.stub(console, 'info');
- logger1.log('Foobar');
+ Logger.info('Foobar');
stub.restore();
- expect(stub.args[0][0]).contains('[LOG] Foobar');
-
- done();
+ expect(stub.args[0][0]).contains('[INFO]');
+ expect(stub.args[0][0]).contains('Foobar');
}
@test
- logLevel1(done: () => void) {
- let logStub;
- let warnStub;
+ infoNoOutput() {
+ const stub = sinon.stub(console, 'info');
- process.env.STAPPS_LOG_LEVEL = '8';
+ process.env.STAPPS_LOG_LEVEL = '0';
- logStub = sinon.stub(console, 'log');
- warnStub = sinon.stub(console, 'warn');
+ Logger.info('Foobar');
- logger1.ok('foo');
- logger1.warn('bar');
-
- logStub.restore();
-
- /* tslint:disable:no-unused-expression */
- expect(logStub.called).to.be.false;
- expect(warnStub.called).to.be.false;
- /* tslint:enable */
+ stub.restore();
delete process.env.STAPPS_LOG_LEVEL;
- logger1.warn('bar');
-
- /* tslint:disable:no-unused-expression */
- expect(warnStub.called).to.be.true;
- /* tslint:enable */
-
- warnStub.restore();
-
- done();
+ return expect(stub.called).to.be.false;
}
@test
- logLevel2(done: () => void) {
- let logStub;
- let warnStub;
+ initialized() {
+ Logger.setTransport(new DummyTransport());
- process.env.STAPPS_LOG_LEVEL = '12';
+ expect(() => {
+ Logger.initialized();
+ }).not.to.throw();
- logStub = sinon.stub(console, 'log');
- warnStub = sinon.stub(console, 'warn');
+ Logger.setTransport();
+ }
- logger1.ok('foo');
- logger1.warn('bar');
+ @test
+ initializedInProductiveEnvironment() {
+ const nodeEnv = process.env.NODE_ENV;
+ process.env.NODE_ENV = 'production';
- logStub.restore();
+ Logger.setTransport(new DummyTransport());
- /* tslint:disable:no-unused-expression */
- expect(logStub.called).to.be.false;
- expect(warnStub.called).to.be.true;
- /* tslint:enable */
+ expect(() => {
+ Logger.initialized();
+ }).not.to.throw();
+
+ 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;
- logger1.warn('bar');
-
- /* tslint:disable:no-unused-expression */
- expect(warnStub.called).to.be.true;
- /* tslint:enable */
-
- warnStub.restore();
-
- done();
+ return expect(stub.called).to.be.false;
}
@test
- logLevel3(done: () => void) {
- let logStub;
- let warnStub;
+ ok() {
+ const stub = sinon.stub(console, 'log');
- process.env.STAPPS_LOG_LEVEL = '20';
+ Logger.ok('Foobar');
- logStub = sinon.stub(console, 'log');
- warnStub = sinon.stub(console, 'warn');
+ stub.restore();
- logger1.ok('foo');
- logger1.warn('bar');
+ expect(stub.args[0][0]).contains('[OK]');
+ expect(stub.args[0][0]).contains('Foobar');
+ }
- logStub.restore();
+ @test
+ okNoOutput() {
+ const stub = sinon.stub(console, 'log');
- /* tslint:disable:no-unused-expression */
- expect(logStub.called).to.be.true;
- expect(warnStub.called).to.be.true;
- /* tslint:enable */
+ process.env.STAPPS_LOG_LEVEL = '0';
+
+ Logger.ok('Foobar');
+
+ stub.restore();
delete process.env.STAPPS_LOG_LEVEL;
- logger1.warn('bar');
-
- /* tslint:disable:no-unused-expression */
- expect(warnStub.called).to.be.true;
- /* tslint:enable */
-
- warnStub.restore();
-
- done();
+ return expect(stub.called).to.be.false;
}
@test
- ok(done: () => void) {
- let
- stub;
- stub = sinon.stub(console, 'log');
-
- logger1.ok('Foobar');
-
- stub.restore();
-
- expect(stub.args[0][0]).contains('[OK] Foobar');
-
- done();
+ setTransport() {
+ expect(() => {
+ Logger.setTransport(new DummyTransport());
+ Logger.setTransport();
+ }).not.to.throw();
}
@test
- stringifyArguments(done: () => void) {
- let
- stub;
- stub = sinon.stub(console, 'log');
+ stringifyArguments() {
+ const stub = sinon.stub(console, 'log');
- logger1.log('foo', 'bar');
+ Logger.log('foo', 'bar');
stub.restore();
- expect(stub.args[0][0]).contains('[LOG]').and.contains('foo').and.contains('bar');
-
- done();
+ expect(stub.args[0][0]).contains('[LOG]');
+ expect(stub.args[0][0]).contains('foo');
+ expect(stub.args[0][0]).contains('bar');
}
@test
- warn(done: () => void) {
- let
- stub;
- stub = sinon.stub(console, 'warn');
+ warn() {
+ const stub = sinon.stub(console, 'warn');
- logger1.warn('Foobar');
+ Logger.warn('Foobar');
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;
}
}
diff --git a/test/Transport.spec.ts b/test/Transport.spec.ts
new file mode 100644
index 00000000..5d5500ec
--- /dev/null
+++ b/test/Transport.spec.ts
@@ -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 .
+ */
+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 {
+ 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 {
+ return new Promise((resolve, reject) => {
+ if (0 === 0) {
+ resolve(subject);
+ }
+
+ reject(message);
+ });
+ }
+
+ verify(): Promise {
+ 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;
+ }
+}