From b9457dd4b224ab2db37fc98d6759bbe7fd64b62c Mon Sep 17 00:00:00 2001 From: Karen Shaw Date: Thu, 27 Jun 2024 19:18:35 +0000 Subject: [PATCH] Integration tests --- node/src/handlers/post-chat-feedback.js | 2 +- .../integration/post-chat-feedback.test.js | 106 ++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 node/test/integration/post-chat-feedback.test.js diff --git a/node/src/handlers/post-chat-feedback.js b/node/src/handlers/post-chat-feedback.js index 081280e2..7e705788 100644 --- a/node/src/handlers/post-chat-feedback.js +++ b/node/src/handlers/post-chat-feedback.js @@ -50,7 +50,7 @@ const handler = wrap(async (event) => { return { statusCode: 400, headers: { "Content-Type": "text/plain" }, - body: JSON.stringify(errors), + body: JSON.stringify(errors.join(", ")), }; } await uploadToS3(`${content.sentiment}/${content.context.ref}`, content); diff --git a/node/test/integration/post-chat-feedback.test.js b/node/test/integration/post-chat-feedback.test.js new file mode 100644 index 00000000..d87ed624 --- /dev/null +++ b/node/test/integration/post-chat-feedback.test.js @@ -0,0 +1,106 @@ +const chai = require("chai"); +const expect = chai.expect; +chai.use(require("chai-http")); +const ApiToken = requireSource("api/api-token"); +const nock = require("nock"); + +const { handler } = requireSource("handlers/post-chat-feedback"); + +describe("Chat feedback route", () => { + + helpers.saveEnvironment(); + + beforeEach(() => { + process.env.API_TOKEN_SECRET = "abc123"; + process.env.API_TOKEN_NAME = "dcapiTEST"; + process.env.MEDIACONVERT_DESTINATION_BUCKET = "delete-me"; + }); + + it('should return 401 if user is not logged in', async () => { + let requestBody = JSON.stringify({ + sentiment: 'positive', + context: { + ref: 1, + question: 'What is the capital of France?', + answer: 'Paris', + source_documents: ['https://doc1', 'https://doc2'], + }, + feedback: { + options: ['option1'], + text: 'Great answer!', + email: 'user@example.com', + } + }); + + const event = helpers + .mockEvent("POST", "/chat-feedback") + .body(requestBody) + .render(); + const response = await handler(event); + expect(response.statusCode).to.equal(401); + expect(response.body).to.equal('Authorization Required'); + + }); + + it('should fail if request body is invalid', async () => { + const token = new ApiToken().user({ uid: "abc123" }).sign(); + + let requestBody = JSON.stringify({ + sentiment: 'neutral', + context: { + ref: 1, + question: 'What is the capital of France?', + answer: 'Paris', + source_documents: ['https://doc1', 'https://doc2'], + }, + feedback: { + options: ['option1'], + text: 'Great answer!', + email: 'user@example.com', + } + }); + + const event = helpers + .mockEvent("POST", "/chat-feedback") + .body(requestBody) + .headers({ + Cookie: `${process.env.API_TOKEN_NAME}=${token}`, + }) + .render(); + const response = await handler(event); + expect(response.statusCode).to.equal(400); + expect(response.body).to.equal(`"sentiment is not one of enum values: positive,negative"`); + }); + + // it("should upload to S3 and return 200 on valid input", async () => { + // const token = new ApiToken().user({ uid: "abc123" }).sign(); + + // const requestBody = JSON.stringify({ + // sentiment: "negative", + // context: { + // ref: 1001, + // question: "What is the capital of France?", + // answer: "Rome", + // source_documents: ["https://doc1", "https://doc2"], + // }, + // feedback: { + // options: ["option1"], + // text: "Bad answer!", + // email: "example@example.com" + // } + // }); + + // nock(`https://${process.env.MEDIACONVERT_DESTINATION_BUCKET}.s3.amazonaws.com`).put('/1001').reply(200); + + // const event = helpers + // .mockEvent("POST", "/chat-feedback") + // .body(requestBody) + // .headers({ + // Cookie: `${process.env.API_TOKEN_NAME}=${token}`, + // }) + // .render(); + // const response = await handler(event); + // expect(response.statusCode).to.equal(200); + // expect(response.body).to.equal('{"message":"Feedback received. Thank you."}'); + // }); +});