Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand tests and test sharp #54

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/getOrCreateObject/getOrCreateObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import { lookupCustomCrop } from './resizeAndSave/lookupCustomCrop.js';
import { resizeAndSave } from './resizeAndSave.js';
import { ORIGINAL_PATH_ROOT, RENDER_PATH_ROOT } from './pathConstants.js';

const bucketName = process.env.ORIGINAL_BUCKET;

const s3 = new S3();

// Try to get an object from S3, and return either the valid response, or the error.
async function tryGetObject(userRequest, s3Key) {
let response;
try {
response = await s3.getObject({
Bucket: bucketName,
Bucket: process.env.ORIGINAL_BUCKET,
Key: s3Key,
Range: userRequest.headers?.Range,
});
Expand Down
45 changes: 39 additions & 6 deletions src/getOrCreateObject/getOrCreateObject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import { describe, it, expect } from 'vitest';
import { mockClient } from 'aws-sdk-client-mock';
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb';
import { Readable } from 'stream';

// Import the function to test.
import { getOrCreateObject } from './getOrCreateObject.js';

process.env.DYNAMODB_TABLE = 'test-table';
process.env.ORIGINAL_BUCKET = 'test-bucket';

// Create a 1 pixel jpg image as a readable stream,
// so that the sharp library has something to resize.
const singlePixelJpgReadable = Readable.from(Buffer.from('/9j/4AAQSkZJRgABAQEAAAAAAAD/2wBDAAoHBwkHBgoJCAkLCwoMDxkQDw4ODx4WFxIZJCAmJSMgIyIoLTkwKCo2KyIjMkQyNjs9QEBAJjBGS0U+Sjk/QD3/2wBDAQsLCw8NDx0QEB09KSMpPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT3/wAARCAAIAAgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD9/KKKKAP/2Q==', 'base64'));

// Mock the ddb client.
const ddbMock = mockClient(DynamoDBDocumentClient);
Expand All @@ -19,26 +25,42 @@ ddbMock.on(GetCommand).resolves({

// Mock the s3 client.
const s3Mock = mockClient(S3Client);
s3Mock.on(GetObjectCommand).resolves({
Body: 'test',

// Reject all GetObjectCommand calls by default.
s3Mock.on(GetObjectCommand).rejects({ Code: 'NoSuchKey' });

// Define an existing object in the bucket, that returns a valid and rescalable image.
s3Mock.on(GetObjectCommand, {
Bucket: 'test-bucket',
Key: 'original_media/www.bu.edu/somesite/files/01/exists.jpg',
}).resolves({
Body: singlePixelJpgReadable,
});

// Define a second object in the bucket with a unicode filename.
s3Mock.on(GetObjectCommand, {
Bucket: 'test-bucket',
Key: 'original_media/www.bu.edu/site/files/01/file-with-ñ.jpg',
}).resolves({
Body: singlePixelJpgReadable,
});

describe('getOrCreateObject', () => {
it('should return an object', async () => {
it('should return an object if it exists', async () => {
const result = await getOrCreateObject(
{
url: 'https://example-1111.s3-object-lambda.us-east-1.amazonaws.com/somesite/files/01/example-150x150.jpg',
url: 'https://example-1111.s3-object-lambda.us-east-1.amazonaws.com/somesite/files/01/exists.jpg',
headers: { },
},
'www.bu.edu',
);
expect(result.Body).toBeDefined();
});

it('should return an an object, and parse the crop value', async () => {
it('should find the unscaled original for the request, scale it with the parsed crop value, and return an object', async () => {
const result = await getOrCreateObject(
{
url: 'https://example-1111.s3-object-lambda.us-east-1.amazonaws.com/files/01/example-758x460.jpg?resize-position=left',
url: 'https://example-1111.s3-object-lambda.us-east-1.amazonaws.com/somesite/files/01/exists-758x460.jpg?resize-position=left',
headers: { },
},
'www.bu.edu',
Expand All @@ -56,4 +78,15 @@ describe('getOrCreateObject', () => {
);
expect(result.Body).toBeDefined();
});

it('should return an error if the object does not exist', async () => {
const result = await getOrCreateObject(
{
url: 'https://example-1111.s3-object-lambda.us-east-1.amazonaws.com/somesite/files/01/does-not-exist.jpg',
headers: { },
},
'www.bu.edu',
);
expect(result.Code).toEqual('NoSuchKey');
});
});
Loading