|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const watson = require('../../index'); |
| 5 | +const nock = require('nock'); |
| 6 | + |
| 7 | +// Test params |
| 8 | +const service = { |
| 9 | + username: 'batman', |
| 10 | + password: 'bruce-wayne', |
| 11 | + url: 'http://ibm.com:80', |
| 12 | + version: '2018-09-19', |
| 13 | +}; |
| 14 | + |
| 15 | +const noop = function() {}; |
| 16 | + |
| 17 | +const missingParameter = function(err) { |
| 18 | + assert.ok(err instanceof Error && /required parameters/.test(err)); |
| 19 | +}; |
| 20 | + |
| 21 | +const assistant = new watson.AssistantV2(service); |
| 22 | + |
| 23 | +describe('assistant v2 unit tests', function() { |
| 24 | + before(function() { |
| 25 | + nock.disableNetConnect(); |
| 26 | + nock(service.url) |
| 27 | + .persist() |
| 28 | + .post('*') |
| 29 | + .reply(200, {}); |
| 30 | + }); |
| 31 | + |
| 32 | + after(function() { |
| 33 | + nock.cleanAll(); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('createSession tests', function() { |
| 37 | + const params = { assistant_id: 'robin123' }; |
| 38 | + it('should generate a valid payload', function() { |
| 39 | + const req = assistant.createSession(params, noop); |
| 40 | + assert.equal( |
| 41 | + req.uri.href, |
| 42 | + service.url + '/v2/assistants/robin123/sessions?version=2018-09-19' |
| 43 | + ); |
| 44 | + assert.equal(req.method, 'POST'); |
| 45 | + assert.equal(req.headers.Accept, 'application/json'); |
| 46 | + assert.equal(req.headers['Content-Type'], 'application/json'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should check no parameters provided (negative test)', function() { |
| 50 | + assistant.createSession({}, missingParameter); |
| 51 | + assistant.createSession(null, missingParameter); |
| 52 | + assistant.createSession(undefined, missingParameter); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('message tests', function() { |
| 57 | + const params = { assistant_id: 'robin123', session_id: 'abc123' }; |
| 58 | + it('should generate a valid payload', function() { |
| 59 | + const req = assistant.message(params, noop); |
| 60 | + assert.equal( |
| 61 | + req.uri.href, |
| 62 | + service.url + '/v2/assistants/robin123/sessions/abc123/message?version=2018-09-19' |
| 63 | + ); |
| 64 | + assert.equal(req.method, 'POST'); |
| 65 | + assert.equal(req.headers.Accept, 'application/json'); |
| 66 | + assert.equal(req.headers['Content-Type'], 'application/json'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should check no parameters provided (negative test)', function() { |
| 70 | + assistant.message({}, missingParameter); |
| 71 | + assistant.message(null, missingParameter); |
| 72 | + assistant.message(undefined, missingParameter); |
| 73 | + assistant.message({ assistant_id: 'a' }, missingParameter); |
| 74 | + assistant.message({ session_id: 'a' }, missingParameter); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('deleteSession tests', function() { |
| 79 | + const params = { assistant_id: 'robin123', session_id: 'abc123' }; |
| 80 | + it('should generate a valid payload', function() { |
| 81 | + const req = assistant.deleteSession(params, noop); |
| 82 | + assert.equal( |
| 83 | + req.uri.href, |
| 84 | + service.url + '/v2/assistants/robin123/sessions/abc123?version=2018-09-19' |
| 85 | + ); |
| 86 | + assert.equal(req.method, 'DELETE'); |
| 87 | + assert.equal(req.headers.Accept, 'application/json'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should check no parameters provided (negative test)', function() { |
| 91 | + assistant.deleteSession({}, missingParameter); |
| 92 | + assistant.deleteSession(null, missingParameter); |
| 93 | + assistant.deleteSession(undefined, missingParameter); |
| 94 | + assistant.deleteSession({ assistant_id: 'a' }, missingParameter); |
| 95 | + assistant.deleteSession({ session_id: 'a' }, missingParameter); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments