Skip to content

Commit

Permalink
Adds aws-sdk-client-mock for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kdid committed Jun 28, 2024
1 parent 34be75c commit 1b5e2bf
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 88 deletions.
33 changes: 28 additions & 5 deletions node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"devDependencies": {
"@openapitools/openapi-generator-cli": "^2.5.2",
"aws-sdk-client-mock": "^4.0.1",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"choma": "^1.2.1",
Expand Down
2 changes: 1 addition & 1 deletion node/src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

178 changes: 96 additions & 82 deletions node/test/integration/post-chat-feedback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,104 +2,118 @@ const chai = require("chai");
const expect = chai.expect;
chai.use(require("chai-http"));
const ApiToken = requireSource("api/api-token");
const { mockClient } = require("aws-sdk-client-mock");
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");

const { handler } = requireSource("handlers/post-chat-feedback");

describe("Chat feedback route", () => {
const s3Mock = mockClient(S3Client);
helpers.saveEnvironment();

beforeEach(() => {
process.env.API_TOKEN_SECRET = "abc123";
process.env.API_TOKEN_NAME = "dcapiTEST";
process.env.MEDIACONVERT_DESTINATION_BUCKET = "delete-me";
s3Mock.reset();
});

it("should return 401 if user is not logged in", async () => {
let requestBody = JSON.stringify({
sentiment: "positive",
context: {
ref: "5a6e1d76-0d4c-43c5-ab2c-4687112ba102",
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",
},
describe("Form POST submission", () => {
beforeEach(() => {
s3Mock.on(PutObjectCommand).resolves({});
});

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();
it("should return 401 if user is not logged in", async () => {
let requestBody = JSON.stringify({
sentiment: "positive",
context: {
ref: "5a6e1d76-0d4c-43c5-ab2c-4687112ba102",
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",
},
});

let requestBody = JSON.stringify({
sentiment: "neutral",
context: {
ref: "3fc98004-995b-4491-94fd-aea48a0363ba",
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");
});

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 fail if request body is invalid", async () => {
const token = new ApiToken().user({ uid: "abc123" }).sign();

// it("should upload to S3 and return 200 on valid input", async () => {
// const token = new ApiToken().user({ uid: "abc123" }).sign();
let requestBody = JSON.stringify({
sentiment: "neutral",
context: {
ref: "3fc98004-995b-4491-94fd-aea48a0363ba",
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 requestBody = JSON.stringify({
// sentiment: "negative",
// context: {
// ref: "e6005d7c-e03b-43f7-94a3-e327b4b5a538",
// 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"
// }
// });
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"`
);
});

// nock(`https://${process.env.MEDIACONVERT_DESTINATION_BUCKET}.s3.amazonaws.com`).put('/1001').reply(200);
describe("Saving feedback", () => {
xit("should upload to S3 and return 200", async () => {
const token = new ApiToken().user({ uid: "abc123" }).sign();

// 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."}');
// });
const requestBody = JSON.stringify({
sentiment: "negative",
context: {
ref: "e6005d7c-e03b-43f7-94a3-e327b4b5a538",
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",
},
});

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."}'
);
expect(s3Mock.calls(PutObjectCommand).length).to.equal(1);
expect(s3Mock.calls(0).args[0].input).lessThanOrEqual({
Bucket: process.env.MEDIACONVERT_DESTINATION_BUCKET,
Key: "negative/e6005d7c-e03b-43f7-94a3-e327b4b5a538",
});
});
});
});
});
1 change: 1 addition & 0 deletions node/test/test-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const TestEnvironment = {
NUSSO_BASE_URL: "https://nusso-base.com/",
NUSSO_API_KEY: "abc123",
WEBSOCKET_URI: "wss://thisisafakewebsocketapiurl",
MEDIA_CONVERT_DESTINATION_BUCKET: "test-mediaconvert-destination-bucket",
};

for (const v in TestEnvironment) delete process.env[v];
Expand Down

0 comments on commit 1b5e2bf

Please sign in to comment.