diff --git a/api/v1/controllers/botData.js b/api/v1/controllers/botData.js index 662e7711cf..abc179a49b 100644 --- a/api/v1/controllers/botData.js +++ b/api/v1/controllers/botData.js @@ -13,6 +13,8 @@ const helper = require('../helpers/nouns/botData'); const doPost = require('../helpers/verbs/doPost'); +const doFind = require('../helpers/verbs/doFind'); +const doGet = require('../helpers/verbs/doGet'); const doPatch = require('../helpers/verbs/doPatch'); const doDelete = require('../helpers/verbs/doDelete'); @@ -44,6 +46,19 @@ module.exports = { doPatch(req, res, next, helper); }, + /** + * GET /botData/, /room/{roomID}/bot/{botID}/data, /room/{roomID}/data + * + * Finds zero or more botData and sends them back in the response. + * + * @param {IncomingMessage} req - The request object + * @param {ServerResponse} res - The response object + * @param {Function} next - The next middleware function in the stack + */ + findBotData(req, res, next) { + doFind(req, res, next, helper); + }, + /** * POST /botData * @@ -57,4 +72,17 @@ module.exports = { doPost(req, res, next, helper); }, + /** + * GET /botData/{key} + * + * Retrieves the botData and sends it back in the response. + * + * @param {IncomingMessage} req - The request object + * @param {ServerResponse} res - The response object + * @param {Function} next - The next middleware function in the stack + */ + getBotData(req, res, next) { + doGet(req, res, next, helper); + }, + }; // exports diff --git a/api/v1/helpers/nouns/botData.js b/api/v1/helpers/nouns/botData.js index 4ec2b82a6b..3dfee88d25 100644 --- a/api/v1/helpers/nouns/botData.js +++ b/api/v1/helpers/nouns/botData.js @@ -17,6 +17,7 @@ const m = 'BotData'; module.exports = { apiLinks: { POST: `Create a new ${m}`, + GET: `Retrieve ${m}`, PATCH: `Update selected attributes of ${m}`, DELETE: `Delete ${m}`, }, diff --git a/api/v1/swagger.yaml b/api/v1/swagger.yaml index 539da290e6..d7f060afed 100644 --- a/api/v1/swagger.yaml +++ b/api/v1/swagger.yaml @@ -7539,6 +7539,39 @@ paths: # ============================================================================= /botData: x-swagger-router-controller: botData + get: + tags: [ botData ] + operationId: findBotData + parameters: + - + name: name + description: Filter botData by name + in: query + required: false + type: string + - + $ref: "#/parameters/limitParam" + - + $ref: "#/parameters/offsetParam" + responses: + 200: + description: >- + Success, returns all botData + schema: + type: array + items: + $ref: "#/definitions/BotDataResponse" + 400: + $ref: "#/responses/400" + 401: + $ref: "#/responses/401" + 403: + $ref: "#/responses/403" + 404: + $ref: "#/responses/404" + default: + $ref: "#/responses/genericError" + post: security: - jwt: [] @@ -7597,6 +7630,40 @@ paths: # --------------------------------------------------------------------------- /botData/{key}: x-swagger-router-controller: botData + get: + summary: Retrieve a botData + tags: [ botData ] + operationId: getBotData + parameters: + - + name: key + in: path + description: >- + The id or name of the botData to retrieve + required: true + type: string + - + $ref: "#/parameters/limitParam" + - + $ref: "#/parameters/offsetParam" + responses: + 200: + description: >- + Success, returns specified botData + schema: + type: array + items: + $ref: "#/definitions/BotDataResponse" + 400: + $ref: "#/responses/400" + 401: + $ref: "#/responses/401" + 404: + $ref: "#/responses/404" + default: + $ref: "#/responses/genericError" + + patch: security: - jwt: [] @@ -7684,6 +7751,85 @@ paths: default: $ref: "#/responses/genericError" +# --------------------------------------------------------------------------- + /rooms/{roomId}/data: + x-swagger-router-controller: botData + get: + summary: Retrieve bot data from a room + tags: [ rooms ] + operationId: findBotData + parameters: + - + name: roomId + in: path + description: >- + The id of the room to retrieve bot data from + required: true + type: integer + - + $ref: "#/parameters/limitParam" + - + $ref: "#/parameters/offsetParam" + responses: + 200: + description: >- + Success, returns specified bot data + schema: + type: array + items: + $ref: "#/definitions/BotDataResponse" + 400: + $ref: "#/responses/400" + 401: + $ref: "#/responses/401" + 404: + $ref: "#/responses/404" + default: + $ref: "#/responses/genericError" + +# --------------------------------------------------------------------------- + /rooms/{roomId}/bots/{botId}/data: + x-swagger-router-controller: botData + get: + summary: Retrieve bot data from a room from a specific bot + tags: [ rooms ] + operationId: findBotData + parameters: + - + name: roomId + in: path + description: >- + The id of the room to retrieve bot data from + required: true + type: integer + - + name: botId + in: path + description: >- + The id of the bot which people want to reference + required: true + type: string + - + $ref: "#/parameters/limitParam" + - + $ref: "#/parameters/offsetParam" + responses: + 200: + description: >- + Success, returns specified bot data + schema: + type: array + items: + $ref: "#/definitions/BotDataResponse" + 400: + $ref: "#/responses/400" + 401: + $ref: "#/responses/401" + 404: + $ref: "#/responses/404" + default: + $ref: "#/responses/genericError" + # ============================================================================= /roomTypes: x-swagger-router-controller: roomTypes diff --git a/tests/api/v1/botData/get.js b/tests/api/v1/botData/get.js new file mode 100644 index 0000000000..bb23c4f8dd --- /dev/null +++ b/tests/api/v1/botData/get.js @@ -0,0 +1,239 @@ +/** + * Copyright (c) 2017, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or + * https://opensource.org/licenses/BSD-3-Clause + */ + +/** + * tests/api/v1/botData/get.js + */ + +'use strict'; +const supertest = require('supertest'); +const api = supertest(require('../../../../index').app); +const constants = require('../../../../api/v1/constants'); +const path = '/v1/botData'; +const expect = require('chai').expect; +const tu = require('../../../testUtils'); +const u = require('./utils'); +const r = require('../rooms/utils'); +const rt = require('../roomTypes/utils'); +const b = require('../bots/utils'); +const Room = tu.db.Room; +const RoomType = tu.db.RoomType; +const Bot = tu.db.Bot; +const BotData = tu.db.BotData; +const ZERO = 0; +const ONE = 1; +const TWO = 2; + +describe(`api: GET ${path}`, () => { + const testBotData = u.getStandard(); + let saveBotData; + let saveRoomType; + let token; + + before((done) => { + tu.createToken() + .then((returnedToken) => { + token = returnedToken; + done(); + }) + .catch(done); + }); + + beforeEach((done) => { + RoomType.create(rt.getStandard()) + .then((roomType) => { + saveRoomType = roomType; + const room = r.getStandard(); + room.type = roomType.id; + return Room.create(room); + }) + .then((room) => { + testBotData.roomId = room.id; + return Bot.create(b.getStandard()); + }) + .then((bot) => { + testBotData.botId = bot.id; + return BotData.create(testBotData); + }) + .then((botData) => { + saveBotData = botData; + done(); + }) + .catch(done); + }); + + afterEach(u.forceDelete); + after(tu.forceDeleteUser); + + describe('GET bot', () => { + it('Pass, get array of one', (done) => { + api.get(`${path}`) + .set('Authorization', token) + .expect(constants.httpStatus.OK) + .end((err, res) => { + if (err) { + done(err); + } + + expect(res.body.length).to.equal(ONE); + expect(res.body[ZERO].name).to.equal(u.name); + done(err); + }); + }); + + it('Pass, get array of multiple', (done) => { + const testBotData2 = u.getStandard(); + testBotData2.name = 'TestData2'; + testBotData2.botId = testBotData.botId; + testBotData2.roomId = testBotData.roomId; + BotData.create(testBotData2) + .then(() => { + api.get(`${path}`) + .set('Authorization', token) + .expect(constants.httpStatus.OK) + .end((err, res) => { + if (err) { + done(err); + } + + expect(res.body.length).to.equal(TWO); + done(); + }); + }) + .catch(done); + }); + + it('Pass, get by name', (done) => { + const testBotData2 = u.getStandard(); + testBotData2.name = 'TestData2'; + testBotData2.botId = testBotData.botId; + testBotData2.roomId = testBotData.roomId; + BotData.create(testBotData2) + .then(() => { + api.get(`${path}?name=`+u.name) + .set('Authorization', token) + .expect(constants.httpStatus.OK) + .end((err, res) => { + if (err) { + done(err); + } + + expect(res.body.length).to.equal(ONE); + expect(res.body[ZERO].name).to.equal(u.name); + done(); + }); + }) + .catch(done); + }); + + it('Pass, get data by room', (done) => { + const testBotData2 = u.getStandard(); + const room = r.getStandard(); + room.name = 'NewRoomName'; + room.type = saveRoomType.id; + Room.create(room) + .then((newRoom) => { + testBotData2.name = 'TestData2'; + testBotData2.botId = testBotData.botId; + testBotData2.roomId = newRoom.id; + return BotData.create(testBotData2); + }) + .then(() => { + api.get(`/v1/rooms/${testBotData.roomId}/data`) + .set('Authorization', token) + .expect(constants.httpStatus.OK) + .end((err, res) => { + if (err) { + done(err); + } + + expect(res.body.length).to.equal(ONE); + expect(res.body[ZERO].name).to.equal(u.name); + done(); + }); + }) + .catch(done); + }); + + it('Pass, get data by room and bot', (done) => { + const testBotData2 = u.getStandard(); + const bot = b.getStandard(); + bot.name = 'NewBot'; + Bot.create(bot) + .then((newBot) => { + testBotData2.name = 'TestData2'; + testBotData2.botId = newBot.botId; + testBotData2.roomId = testBotData.id; + return BotData.create(testBotData2); + }) + .then(() => { + api.get( + `/v1/rooms/${testBotData.roomId}/bots/${testBotData.botId}/data` + ) + .set('Authorization', token) + .expect(constants.httpStatus.OK) + .end((err, res) => { + if (err) { + done(err); + } + + expect(res.body.length).to.equal(ONE); + expect(res.body[ZERO].name).to.equal(u.name); + done(); + }); + }) + .catch(done); + }); + + it('Pass, get by id', (done) => { + api.get(`${path}/${saveBotData.id}`) + .set('Authorization', token) + .expect(constants.httpStatus.OK) + .end((err, res) => { + if (err) { + done(err); + } + + expect(res.body.name).to.equal(u.name); + done(); + }); + }); + + it('Fail, id not found', (done) => { + api.get(`${path}/INVALID_ID`) + .set('Authorization', token) + .expect(constants.httpStatus.NOT_FOUND) + .end(() => { + done(); + }); + }); + + it('Fail, get data by room and bot not found', (done) => { + const testBotData2 = u.getStandard(); + const bot = b.getStandard(); + bot.name = 'NewBot'; + Bot.create(bot) + .then((newBot) => { + testBotData2.name = 'TestData2'; + testBotData2.botId = newBot.botId; + testBotData2.roomId = testBotData.id; + return BotData.create(testBotData2); + }) + .then(() => { + api.get('/v1/rooms/NOT_FOUND/bots/NOT_FOUND/data') + .set('Authorization', token) + .expect(constants.httpStatus.NOT_FOUND) + .end(() => { + done(); + }); + }) + .catch(done); + }); + }); +}); +