Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests/coverage and bug fixes #34

Merged
merged 1 commit into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### 2.3.2 (Next)

* [#34](https://github.com/alexa-js/alexa-app-server/pull/34): Added tests for fail cases and schemas/utterances - [@tejashah88](https://github.com/tejashah88).
* [#34](https://github.com/alexa-js/alexa-app-server/pull/34): Fixed bug while trying to retrieve schema/utterances with GET request - [@tejashah88](https://github.com/tejashah88).
* [#34](https://github.com/alexa-js/alexa-app-server/pull/34): Updated messages that display an error to be displayed by `self.error` - [@tejashah88](https://github.com/tejashah88).
* [#34](https://github.com/alexa-js/alexa-app-server/pull/34): Optimized some tests to not always start a new server instance after every test - [@tejashah88](https://github.com/tejashah88).
* [#33](https://github.com/alexa-js/alexa-app-server/pull/33): Updated dependencies - [@tejashah88](https://github.com/tejashah88).
* [#33](https://github.com/alexa-js/alexa-app-server/pull/33): Fix: expressjs deprecation warning - [@tejashah88](https://github.com/tejashah88).
* [#32](https://github.com/alexa-js/alexa-app-server/pull/32): Added tests for request verification, HTTPS support, and POST-based routes - [@tejashah88](https://github.com/tejashah88).
Expand Down
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ var appServer = function(config) {
console.log(msg);
}
};
self.error = function(msg) { console.log(msg); };
self.error = function(msg) {
console.error(msg);
};

// Configure hotswap to watch for changes and swap out module code
var hotswapCallback = function(filename) {
self.log("hotswap reloaded " + filename);
};

var errorCallback = function(e) {
self.log("-----\nhotswap error: " + e + "\n-----\n");
self.error("-----\nhotswap error: " + e + "\n-----\n");
};

hotswap.on('swap', hotswapCallback);
Expand All @@ -44,25 +46,25 @@ var appServer = function(config) {
app_directories(app_dir).forEach(function(dir) {
var package_json = path.join(app_dir, dir, "/package.json");
if (!fs.existsSync(package_json) || !fs.statSync(package_json).isFile()) {
self.log(" package.json not found in directory " + dir);
self.error(" package.json not found in directory " + dir);
return;
}
var pkg = JSON.parse(fs.readFileSync(package_json, 'utf8'));
if (!pkg || !pkg.main || !pkg.name) {
self.log(" Failed to load " + package_json);
self.error(" Failed to load " + package_json);
return;
}
var main = fs.realpathSync(path.join(app_dir, dir, pkg.main));
if (!fs.existsSync(main) || !fs.statSync(main).isFile()) {
self.log(" main file not found for app [" + pkg.name + "]: " + main);
self.error(" main file not found for app [" + pkg.name + "]: " + main);
return;
}
try {
var app = require(main);
self.apps[pkg.name] = pkg;
self.apps[pkg.name].exports = app;
if (typeof app.express != "function") {
self.log(" App [" + pkg.name + "] is not an instance of alexa-app");
self.error(" App [" + pkg.name + "] is not an instance of alexa-app");
return;
}

Expand Down Expand Up @@ -104,9 +106,9 @@ var appServer = function(config) {
// Configure GET requests to run a debugger UI
if (false !== config.debug) {
self.express.get(endpoint, function(req, res) {
if (typeof req.params['schema'] != "undefined") {
if (typeof req.query['schema'] != "undefined") {
res.set('Content-Type', 'text/plain').send(app.schema());
} else if (typeof req.params['utterances'] != "undefined") {
} else if (typeof req.query['utterances'] != "undefined") {
res.set('Content-Type', 'text/plain').send(app.utterances());
} else {
res.render('test', { "app": app, "schema": app.schema(), "customSlotTypes": (app.customSlotTypes ? app.customSlotTypes() : ""), "utterances": app.utterances(), "intents": app.intents });
Expand All @@ -116,7 +118,7 @@ var appServer = function(config) {

self.log(" Loaded app [" + pkg.name + "] at endpoint: " + endpoint);
} catch (e) {
self.log("Error loading app [" + main + "]: " + e);
self.error("Error loading app [" + main + "]: " + e);
}
});
return self.apps;
Expand Down Expand Up @@ -217,19 +219,19 @@ var appServer = function(config) {
self.httpsInstance = https.createServer(credentials, self.express).listen(config.httpsPort); //create the HTTPS server
self.log("Listening on HTTPS port " + config.httpsPort);
} catch (error) {
self.log("Failed to listen via HTTPS Error: " + error);
self.error("Failed to listen via HTTPS Error: " + error);
}
} else {
self.log("Failed to load privateKey or certificate from /sslcert. HTTPS will not be enabled");
self.error("Failed to load privateKey or certificate from /sslcert. HTTPS will not be enabled");

}

} else {
self.log("privateKey: '" + config.privateKey + "' or certificate: '" + config.certificate + "' do not exist in /sslcert. HTTPS will not be enabled");
self.error("privateKey: '" + config.privateKey + "' or certificate: '" + config.certificate + "' do not exist in /sslcert. HTTPS will not be enabled");

}
} else {
self.log("privatekey, httpsPort, or certificate paramater not set in config. HTTPS will not be enabled");
self.error("privatekey, httpsPort, or certificate paramater not set in config. HTTPS will not be enabled");

}

Expand Down
11 changes: 11 additions & 0 deletions invalid_examples/apps/bad_app_bad_json/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var alexa = require('alexa-app');

// Allow this module to be reloaded by hotswap when changed
module.change_code = 1;

// Define an alexa-app
var app = new alexa.app('bad_app_bad_json');
app.launch(function(req,res) {
res.say("This app should not load!");
});
module.exports = app;
12 changes: 12 additions & 0 deletions invalid_examples/apps/bad_app_bad_json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1.0.0",
"description": "A sample Alexa app",
"author": "Matt Kruse <github@mattkruse.com> (http://mattkruse.com/)",
"license": "ISC",
"alexa": {
"applicationId":"amzn1.echo-sdk-ams.app.999999-d0ed-9999-ad00-999999d00ebe"
},
"dependencies": {
"alexa-app": "^2.1.0"
}
}
11 changes: 11 additions & 0 deletions invalid_examples/apps/bad_app_bad_request_json/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var alexa = require('alexa-app');

// Allow this module to be reloaded by hotswap when changed
module.change_code = 1;

// Define an alexa-app
var app = new alexa.app('bad_app_bad_request');
app.launch(function(req,res) {
res.say("This app should not load!");
});
module.exports = app;
14 changes: 14 additions & 0 deletions invalid_examples/apps/bad_app_bad_request_json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "bad_app_bad_request",
"version": "1.0.0",
"description": "A sample Alexa app",
"main": "index.js",
"author": "Matt Kruse <github@mattkruse.com> (http://mattkruse.com/)",
"license": "ISC",
"alexa": {
"applicationId":"amzn1.echo-sdk-ams.app.999999-d0ed-9999-ad00-999999d00ebe"
},
"dependencies": {
"alexa-app": "^2.1.0"
}
}
11 changes: 11 additions & 0 deletions invalid_examples/apps/bad_app_no_json/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var alexa = require('alexa-app');

// Allow this module to be reloaded by hotswap when changed
module.change_code = 1;

// Define an alexa-app
var app = new alexa.app('bad_app_no_json');
app.launch(function(req,res) {
res.say("This app should not load!");
});
module.exports = app;
11 changes: 11 additions & 0 deletions invalid_examples/apps/bad_app_not_alexa_app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var alexa = require('alexa-app');

// Allow this module to be reloaded by hotswap when changed
module.change_code = 1;

// Define an alexa-app
var app = new alexa.app('bad_app_not_alexa_app');
app.launch(function(req,res) {
res.say("This app should not load!");
});
module.exports = {};
14 changes: 14 additions & 0 deletions invalid_examples/apps/bad_app_not_alexa_app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "bad_app_not_alexa_app",
"version": "1.0.0",
"description": "A sample Alexa app",
"main": "index.js",
"author": "Matt Kruse <github@mattkruse.com> (http://mattkruse.com/)",
"license": "ISC",
"alexa": {
"applicationId":"amzn1.echo-sdk-ams.app.999999-d0ed-9999-ad00-999999d00ebe"
},
"dependencies": {
"alexa-app": "^2.1.0"
}
}
5 changes: 5 additions & 0 deletions invalid_examples/public_html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
alexa-app-server is running
</body>
</html>
14 changes: 14 additions & 0 deletions invalid_examples/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var AlexaAppServer = require("../index.js");
AlexaAppServer.start( {
port:8080
// Use preRequest to load user data on each request and add it to the request json.
// In reality, this data would come from a db or files, etc.
,preRequest: function(json,req,res) {
console.log("preRequest fired");
json.userDetails = { "name":"Bob Smith" };
}
// Add a dummy attribute to the response
,postRequest: function(json,req,res) {
json.dummy = "text";
}
} );
5 changes: 5 additions & 0 deletions invalid_examples/server/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function(express,alexaAppServerObject) {
express.use('/login',function(req,res) {
res.send("Imagine this is a dynamic server-side login action");
});
};
17 changes: 17 additions & 0 deletions invalid_examples/sslcert/cert.cer
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE-----
MIICsDCCAhmgAwIBAgIJAK0FG9fi8e69MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTcwMTEzMjM0MzE0WhcNMTgwMTEzMjM0MzE0WjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
gQDVIqwTKz6mm/i588dB/fz11IAgsgi/Cu9RvgE8H6ps7Le2kf9RAA857sPJtoCJ
GeZwNqlV1EYUH8/RGHAuWALoe6RDBW7kFdW9J1b3IbrjGqEr3hdPNa6MgoukH/Mz
QAbBCE4F9hoRRa6p4G2vnxi3nzUEA0ND4GHUscLvNWbWdwIDAQABo4GnMIGkMB0G
A1UdDgQWBBRfaLJKSAWD773NxNzTRWz/0jx0KTB1BgNVHSMEbjBsgBRfaLJKSAWD
773NxNzTRWz/0jx0KaFJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUt
U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAK0FG9fi
8e69MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAuUqQCqkjZFykLx3J
u1hzmFP8qR49chLQgqArS2JwkTqqUfj5tPn0b6DWxedfAINa8sbc/+pebSTgLcHe
+7N61aZaQuAz0N48c+jaHu8AOMIbDxRkTtjz7KXQL5dHLzXV70EUcNtNtFBxM4L7
Zp4V5VmyPwS+tli292hEOTDRQGE=
-----END CERTIFICATE-----
15 changes: 15 additions & 0 deletions invalid_examples/sslcert/private-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDVIqwTKz6mm/i588dB/fz11IAgsgi/Cu9RvgE8H6ps7Le2kf9R
AA857sPJtoCJGeZwNqlV1EYUH8/RGHAuWALoe6RDBW7kFdW9J1b3IbrjGqEr3hdP
Na6MgoukH/MzQAbBCE4F9hoRRa6p4G2vnxi3nzUEA0ND4GHUscLvNWbWdwIDAQAB
AoGAftLb26gu5osG7PePSMhuvoUNHOdzZuKF13kdWP5qtdgB1WR4rWVAqjNWU3AC
ehJsWbdc+dKPRKhNS9mj3x/F0iSGOUvVwBRU7SvfuJLSp+z0Pk4gh+aJz/6R3Rh+
xiUyeqFJksS4k1qKCIXehXAGoTW/XLRDUVG+hATHbC73gMECQQD0FeHOBFW51dXr
eM0IY3VW9s1JdYMYt+jgb/Jg7DU/aJ48+LoVkk2/hieI3sQff3wBIf8hN7vgo0Ny
MPu23VAXAkEA34oJADoum4MdAy7i8Q9Ap6c5Kp1cBaJ75tcuhHBlWWZiQy6QmZqi
8yDQpTKWMb+Z+YP5Kul7vhK6KeC0/XdIoQJBAPLpjA2BlucY/ooXcMV2ZeKkP+1p
e4xwCtzBzE/VA7EVJtW7G0Y4khOXKWU3fatzLi/aa5PdaabIFGligj+cxQUCQQCG
i7i7MEnZRGN0BQaHfVy3DEm2Qpyer5vP53iSMmxuENfYA/D440BtAjVTGU2Zh++P
ZUXV9E6MqwzuI9gML33BAkAHWuxlPZmjmOj0DYV50yT0w8WZiaWeUpl6h/pEOtEq
M1F+ioph2YJrWeEY4GbOsXnbm+CEoN8mfiAhFZvTyykW
-----END RSA PRIVATE KEY-----
19 changes: 19 additions & 0 deletions test/sample-launch-req.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "1.0",
"session": {
"new": true,
"sessionId": "amzn1.echo-api.session.abeee1a7-aee0-41e6-8192-e6faaed9f5ef",
"application": {
"applicationId": "amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe"
},
"attributes": {},
"user": {
"userId": "amzn1.account.AM3B227HF3FAM1B261HK7FFM3A2"
}
},
"request": {
"type": "LaunchRequest",
"requestId": "amzn1.echo-api.request.9cdaa4db-f20e-4c58-8d01-c75322d6c423",
"timestamp": "2015-05-13T12:34:56Z"
}
}
26 changes: 26 additions & 0 deletions test/sample-schema.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"intents": [
{
"intent": "NameIntent",
"slots": [
{
"name": "NAME",
"type": "LITERAL"
},
{
"name": "AGE",
"type": "NUMBER"
}
]
},
{
"intent": "AgeIntent",
"slots": [
{
"name": "AGE",
"type": "NUMBER"
}
]
}
]
}
Loading