Stash details on requests and responses in JSON files alongside the image in S3

Fixes #43
This commit is contained in:
Les Orchard 2018-05-24 16:30:18 -04:00
Родитель 18df91a205
Коммит 401642f46e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 6DA657B1385DFCAE
4 изменённых файлов: 92 добавлений и 30 удалений

Просмотреть файл

@ -6,7 +6,7 @@ const { DEV_CREDENTIALS, DEFAULT_HAWK_ALGORITHM } = require("../lib/constants");
const {
mocks,
makePromiseFn,
env: { CREDENTIALS_TABLE, QUEUE_NAME, CONTENT_BUCKET },
env: { UPSTREAM_SERVICE_URL, CREDENTIALS_TABLE, QUEUE_NAME, CONTENT_BUCKET },
constants: { QueueUrl, requestId }
} = global;
@ -192,9 +192,11 @@ describe("functions/accept.post", () => {
body
});
const imageKey = `image-${requestId}`;
expect(mocks.putObject.args[0][0]).to.deep.equal({
Bucket: CONTENT_BUCKET,
Key: `image-${requestId}`,
Key: imageKey,
Body: new Buffer(imageContent),
ContentType: imageContentType
});
@ -205,8 +207,23 @@ describe("functions/accept.post", () => {
const message = mocks.sendMessage.args[0][0];
const messageBody = JSON.parse(message.MessageBody);
expect(messageBody.id).to.equal(requestId);
expect(message.QueueUrl).to.equal(QueueUrl);
expect("datestamp" in messageBody).to.be.true;
expect(messageBody.upstreamServiceUrl).to.equal(UPSTREAM_SERVICE_URL);
expect(messageBody.id).to.equal(requestId);
expect(messageBody.user).to.equal(id);
["negative_uri", "positive_uri", "positive_email", "notes"].forEach(
name => expect(messageBody[name]).to.equal(body[name])
);
expect(messageBody.image).to.equal(imageKey);
expect(mocks.putObject.args[1][0]).to.deep.equal({
Bucket: CONTENT_BUCKET,
Key: `${imageKey}-request.json`,
Body: message.MessageBody,
ContentType: "application/json"
});
expect(result.statusCode).to.equal(201);
});
});

Просмотреть файл

@ -29,9 +29,9 @@ module.exports.post = async function(event, context) {
"X-Forwarded-Port": port = 80
} = headers;
let credentials;
let authArtifacts;
try {
({ credentials } = await Hawk.server.authenticate(
({ artifacts: authArtifacts } = await Hawk.server.authenticate(
{
method: "POST",
url: path,
@ -66,13 +66,6 @@ module.exports.post = async function(event, context) {
const imageKey = `image-${requestId}`;
await S3.putObject({
Bucket,
Key: imageKey,
ContentType: image.contentType,
Body: image.data
}).promise();
const upstreamServiceUrl =
UPSTREAM_SERVICE_URL !== "__MOCK__"
? UPSTREAM_SERVICE_URL
@ -82,21 +75,36 @@ module.exports.post = async function(event, context) {
event.requestContext.stage +
"/mock/upstream";
const { QueueUrl } = await SQS.getQueueUrl({ QueueName }).promise();
await SQS.sendMessage({
QueueUrl,
MessageBody: JSON.stringify({
upstreamServiceUrl,
id: requestId,
user: credentials.id,
negative_uri,
positive_uri,
positive_email,
notes,
image: imageKey
})
const messageData = {
datestamp: new Date().toISOString(),
upstreamServiceUrl,
id: requestId,
user: authArtifacts.id,
negative_uri,
positive_uri,
positive_email,
notes,
image: imageKey
};
const MessageBody = JSON.stringify(messageData);
await S3.putObject({
Bucket,
Key: imageKey,
ContentType: image.contentType,
Body: image.data
}).promise();
await S3.putObject({
Bucket,
Key: `${imageKey}-request.json`,
ContentType: "application/json",
Body: MessageBody
}).promise();
const { QueueUrl } = await SQS.getQueueUrl({ QueueName }).promise();
await SQS.sendMessage({ QueueUrl, MessageBody }).promise();
return response(201, {
id: requestId,
negative_uri,

Просмотреть файл

@ -78,6 +78,21 @@ describe("functions/processQueueItem.handler", () => {
}
});
const putObjectCall = mocks.putObject.args[0][0];
expect(putObjectCall.Bucket).to.equal(CONTENT_BUCKET);
expect(putObjectCall.Key).to.equal(`${defaultMessage.image}-response.json`);
expect(putObjectCall.ContentType).to.equal("application/json");
expect(JSON.parse(putObjectCall.Body)).to.deep.equal({
id: defaultMessage.id,
user: defaultMessage.user,
negative_uri: defaultMessage.negative_uri,
positive_uri: defaultMessage.positive_uri,
positive_email: defaultMessage.positive_email,
notes: defaultMessage.notes,
image: defaultMessage.image,
response: positive ? positiveMatchResponse : negativeMatchResponse
});
expect(mocks.getQueueUrl.lastCall.args[0]).to.deep.equal({
QueueName: QUEUE_NAME
});
@ -142,6 +157,8 @@ const defaultMessage = {
user: "devuser",
negative_uri: "https://example.com/negative?id=123",
positive_uri: "https://example.com/positive?id=123",
positive_email: "foo@example.com",
notes: "this is a test",
image: "image-8675309"
};

Просмотреть файл

@ -6,22 +6,26 @@ const SQS = new AWS.SQS({ apiVersion: "2012-11-05" });
const request = require("request-promise-native");
module.exports.handler = async function({ ReceiptHandle, Body }) {
const { QUEUE_NAME, CONTENT_BUCKET, UPSTREAM_SERVICE_KEY } = process.env;
const {
QUEUE_NAME,
CONTENT_BUCKET: Bucket,
UPSTREAM_SERVICE_KEY
} = process.env;
const {
upstreamServiceUrl,
id,
// user,
user,
negative_uri,
positive_uri,
// positive_email,
// notes,
positive_email,
notes,
image
} = JSON.parse(Body);
try {
const imageUrl = S3.getSignedUrl("getObject", {
Bucket: CONTENT_BUCKET,
Bucket,
Key: image
});
@ -38,6 +42,22 @@ module.exports.handler = async function({ ReceiptHandle, Body }) {
}
});
await S3.putObject({
Bucket,
Key: `${image}-response.json`,
ContentType: "application/json",
Body: JSON.stringify({
id,
user,
negative_uri,
positive_uri,
positive_email,
notes,
image,
response: upstreamServiceResponse
})
}).promise();
await request.post({
url: upstreamServiceResponse.IsMatch ? positive_uri : negative_uri,
headers: {