Skip to content

Commit

Permalink
Adding google automated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carloseduardole committed Aug 23, 2018
1 parent bf3e4dc commit 6c01a95
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
1 change: 0 additions & 1 deletion lib/cortana/reply.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

module.exports = function cortanaReply(botResponse, session) {
console.log('message to respond', botResponse);
if (typeof botResponse === 'string') {
session.endConversation(botResponse);
} else if (Array.isArray(botResponse)) {
Expand Down
22 changes: 22 additions & 0 deletions lib/google/assistant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { ActionsSdkApp } = require('actions-on-google');
const expressMockery = require('node-mocks-http');

function getAssistant(request) {

// Prep the request and response.
var mockRequest = expressMockery.createRequest({
body: request.body
});

var mockResponse = expressMockery.createResponse();

// We need this monkey patch because node-mocks-http doesn't have the append.
mockResponse['append'] = (header, value) => {
console.log('Google SDK added a header: "' + header + '": "' + value + '"');
};

// Feed the request/response to the assistant SDK
return new ActionsSdkApp({ request: mockRequest, response: mockResponse });
}

module.exports = getAssistant;
21 changes: 1 addition & 20 deletions lib/google/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const googleParse = require('./parse');
const googleReply = require('./reply');
const color = require('../console-colors');
const envUtils = require('../utils/env-utils');
const { ActionsSdkApp } = require('actions-on-google');
const expressMockery = require('node-mocks-http');
const getAssistant = require('./assistant');

module.exports = function googleSetup(api, bot, logError, optionalParser, optionalResponder) {
let parser = optionalParser || googleParse;
Expand Down Expand Up @@ -48,21 +47,3 @@ module.exports = function googleSetup(api, bot, logError, optionalParser, option
.then(() => `${lambdaDetails.apiUrl}/google`);
});
};

function getAssistant(request) {

// Prep the request and response.
var mockRequest = expressMockery.createRequest({
body: request.body
});

var mockResponse = expressMockery.createResponse();

// We need this monkey patch because node-mocks-http doesn't have the append.
mockResponse['append'] = (header, value) => {
console.log('Google SDK added a header: "' + header + '": "' + value + '"');
};

// Feed the request/response to the assistant SDK
return new ActionsSdkApp({ request: mockRequest, response: mockResponse });
}
34 changes: 34 additions & 0 deletions spec/google/google-parse-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/*global describe, it, expect, require, beforeEach */
'use strict';
const getAssistant = require('../../lib/google/assistant');
var parse = require('../../lib/google/parse');

describe('Google Reply', () => {
beforeEach(() => {
});

it('Sets the AccessToken if AuthorizationToken is present in session', () => {
const assistant = getAssistant(request);
var parsed = parse(assistant, request);
expect(parsed.accessToken).toEqual('USER_ACCESSTOKEN');
});

it('Sets the user_id if it is present', () => {
const assistant = getAssistant(request);
var parsed = parse(assistant, request);
expect(parsed.sender).toEqual('USER_ID');
});
});


const request = {
body: {
'user': {
'userId': 'USER_ID',
'accessToken': 'USER_ACCESSTOKEN',
'locale': 'en-US',
'lastSeen': '2018-04-25T14:27:02Z'
},
'isInSandbox': true
}};
24 changes: 24 additions & 0 deletions spec/google/google-reply-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*global describe, it, expect, require, jasmine, beforeEach */
'use strict';
var reply = require('../../lib/google/reply');


describe('Google Reply', () => {
var assistant;
beforeEach(() => {
assistant = jasmine.createSpyObj('assistant', ['tell']);
assistant.response_ = jasmine.createSpyObj('assistant.response_', ['_getData']);
});

it('Basic tests to test reply', ()=> {
expect(reply()).toEqual(undefined);
expect(reply(undefined, 'Claudia Google Bot')).toEqual(undefined);
expect(reply({ hello: 'Google'}, 'Claudia Google Bot')).toEqual({ hello: 'Google'});
});

it('just calls tell when its an object', () => {
reply('Hello', '', assistant);
expect(assistant.tell).toHaveBeenCalled();
});

});

0 comments on commit 6c01a95

Please sign in to comment.