From a40dce26f15c2707d55204794a75f738fc794f46 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Wed, 26 Jul 2017 15:50:54 +0300 Subject: [PATCH] added some API integration tests --- Gruntfile.js | 21 +++++- config/test.toml | 9 +++ package.json | 6 +- test/api-test.js | 191 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 config/test.toml create mode 100644 test/api-test.js diff --git a/Gruntfile.js b/Gruntfile.js index 8de2e385..5597a4bb 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -14,13 +14,32 @@ module.exports = function(grunt) { }, src: ['test/**/*-test.js', 'imap-core/test/**/*-test.js'] } + }, + + wait: { + server: { + options: { + delay: 7 * 1000 + } + } + }, + + shell: { + server: { + command: 'node server.js', + options: { + async: true + } + } } }); // Load the plugin(s) grunt.loadNpmTasks('grunt-eslint'); grunt.loadNpmTasks('grunt-mocha-test'); + grunt.loadNpmTasks('grunt-shell-spawn'); + grunt.loadNpmTasks('grunt-wait'); // Tasks - grunt.registerTask('default', ['eslint', 'mochaTest']); + grunt.registerTask('default', ['eslint', 'shell:server', 'wait:server', 'mochaTest', 'shell:server:kill']); }; diff --git a/config/test.toml b/config/test.toml new file mode 100644 index 00000000..774c5177 --- /dev/null +++ b/config/test.toml @@ -0,0 +1,9 @@ +[log] + level="error" + +[dbs] + # mongodb connection string for the main database + mongo="mongodb://127.0.0.1:27017/wildduck-test" + + # redis connection string + redis="redis://127.0.0.1:6379/13" diff --git a/package.json b/package.json index 780b76cd..469b5de4 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "IMAP server built with Node.js and MongoDB", "main": "server.js", "scripts": { - "test": "grunt" + "test": "mongo --eval 'db.dropDatabase()' wildduck-test && redis-cli -n 13 flushdb && NODE_ENV=test grunt" }, "keywords": [], "author": "Andris Reinman", @@ -17,6 +17,9 @@ "grunt-cli": "^1.2.0", "grunt-eslint": "^20.0.0", "grunt-mocha-test": "^0.13.2", + "grunt-shell-spawn": "^0.3.10", + "grunt-wait": "^0.1.0", + "icedfrisby": "^1.2.0", "mocha": "^3.4.2" }, "dependencies": { @@ -31,6 +34,7 @@ "libmime": "^3.1.0", "libqp": "^1.1.0", "mailsplit": "^4.0.2", + "mobileconfig": "^2.0.1", "mongo-cursor-pagination": "^5.0.0", "mongodb": "^2.2.30", "node-redis-scripty": "0.0.5", diff --git a/test/api-test.js b/test/api-test.js new file mode 100644 index 00000000..c71a9a92 --- /dev/null +++ b/test/api-test.js @@ -0,0 +1,191 @@ +/*eslint no-unused-expressions: 0, prefer-arrow-callback: 0, no-console:0 */ + +'use strict'; + +const chai = require('chai'); +const frisby = require('icedfrisby'); +const Joi = require('joi'); + +const expect = chai.expect; +chai.config.includeStack = true; + +const URL = 'http://localhost:8080'; + +let userId = false; + +frisby + .create('POST users') + .post( + URL + '/users', + { + username: 'testuser', + password: 'secretpass', + address: 'testuser@example.com', + name: 'test user' + }, + { json: true } + ) + .expectStatus(200) + .expectJSONTypes({ + success: Joi.boolean(), + id: Joi.string(), + error: Joi.string() + }) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + userId = response.id; + + frisby + .create('GET users/{id}') + .get(URL + '/users/' + userId) + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + expect(response.id).to.equal(userId); + expect(response.name).to.equal('test user'); + }) + .toss(); + + frisby + .create('PUT users/{id}') + .put( + URL + '/users/' + userId, + { + name: 'user test' + }, + { json: true } + ) + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + }) + .toss(); + + frisby + .create('GET users/{id} – updated name') + .get(URL + '/users/' + userId) + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + expect(response.id).to.equal(userId); + expect(response.name).to.equal('user test'); + }) + .toss(); + + frisby + .create('GET users/{id}/addresses') + .get(URL + '/users/' + userId + '/addresses') + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + expect(response.results.length).to.equal(1); + expect(response.results[0].address).to.equal('testuser@example.com'); + expect(response.results[0].main).to.be.true; + }) + .toss(); + + frisby + .create('POST users/{id}/addresses') + .post( + URL + '/users/' + userId + '/addresses', + { + address: 'alias1@example.com', + main: true + }, + { json: true } + ) + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + }) + .toss(); + + frisby + .create('POST users/{id}/addresses') + .post( + URL + '/users/' + userId + '/addresses', + { + address: 'alias2@example.com' + }, + { json: true } + ) + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + }) + .toss(); + + frisby + .create('GET users/{id}/addresses – updated listing') + .get(URL + '/users/' + userId + '/addresses') + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + expect(response.results.length).to.equal(3); + response.results.sort((a, b) => a.id.localeCompare(b.id)); + + expect(response.results[0].address).to.equal('testuser@example.com'); + expect(response.results[0].main).to.be.false; + + expect(response.results[1].address).to.equal('alias1@example.com'); + expect(response.results[1].main).to.be.true; + + expect(response.results[2].address).to.equal('alias2@example.com'); + expect(response.results[2].main).to.be.false; + + frisby + .create('DELETE users/{id}/addresses/{address}') + .delete(URL + '/users/' + userId + '/addresses/' + response.results[2].id) + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + + frisby + .create('GET users/{id}/addresses – after DELETE') + .get(URL + '/users/' + userId + '/addresses') + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + expect(response.results.length).to.equal(2); + }) + .toss(); + }) + .toss(); + }) + .toss(); + + frisby + .create('GET users/{id} – updated address') + .get(URL + '/users/' + userId) + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + expect(response.id).to.equal(userId); + expect(response.address).to.equal('alias1@example.com'); + }) + .toss(); + + frisby + .create('GET users/{id}/mailboxes') + .get(URL + '/users/' + userId + '/mailboxes') + .expectStatus(200) + .afterJSON(response => { + expect(response).to.exist; + expect(response.success).to.be.true; + expect(response.results.length).to.be.gte(4); + expect(response.results[0].path).to.equal('INBOX'); + }) + .toss(); + }) + .toss();