Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bosschaert committed Oct 16, 2024
1 parent c2c60f2 commit 4726fde
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 46 deletions.
4 changes: 1 addition & 3 deletions src/routes/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ async function invalidateCollab(api, url, env) {
}

export async function deleteSource({ env, daCtx }) {
const resp = await deleteObjects(env, daCtx);

return resp;
return /* await */ deleteObjects(env, daCtx);
}

export async function postSource({ req, env, daCtx }) {
Expand Down
8 changes: 3 additions & 5 deletions src/storage/object/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';

import getS3Config from '../utils/config.js';
import { postObjectVersion } from '../version/put.js';
import { postObjectVersionWithLabel } from '../version/put.js';

function buildInput(org, key) {
return {
Expand All @@ -34,10 +34,8 @@ async function invalidateCollab(api, url, env) {
await env.dacollab.fetch(invURL);
}

export async function deleteObject(client, daCtx, Key, env) {
if (Key.endsWith('.html')) {
await postObjectVersion('Deleted', env, daCtx);
}
export async function deleteObject(client, daCtx, Key, env, isMove = false) {
await postObjectVersionWithLabel(isMove ? 'Moved' : 'Deleted', env, daCtx);

let resp;
try {
Expand Down
2 changes: 1 addition & 1 deletion src/storage/object/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default async function moveObject(env, daCtx, details) {
const copied = await copyFile(client, daCtx.org, key, details);
// Only delete the source if the file was successfully copied
if (copied.$metadata.httpStatusCode === 200) {
const deleted = await deleteObject(client, daCtx, key, env);
const deleted = await deleteObject(client, daCtx, key, env, true);

Check warning on line 74 in src/storage/object/move.js

View check run for this annotation

Codecov / codecov/patch

src/storage/object/move.js#L74

Added line #L74 was not covered by tests
result.status = deleted.status === 204 ? 204 : deleted.status;
} else {
result.status = copied.$metadata.httpStatusCode;
Expand Down
21 changes: 12 additions & 9 deletions src/storage/version/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,7 @@ export async function putObjectWithVersion(env, daCtx, update, body) {
}
}

export async function postObjectVersion(req, env, daCtx) {
let reqJSON;
try {
reqJSON = await req.json();
} catch (e) {
// no body
}
const label = reqJSON?.label;

export async function postObjectVersionWithLabel(label, env, daCtx) {
const { body, contentLength, contentType } = await getObject(env, daCtx);
const { org, key } = daCtx;

Expand All @@ -159,3 +151,14 @@ export async function postObjectVersion(req, env, daCtx) {

return { status: resp === 200 ? 201 : resp };
}

export async function postObjectVersion(req, env, daCtx) {
let reqJSON;
try {
reqJSON = await req.json();
} catch (e) {
// no body
}

Check warning on line 161 in src/storage/version/put.js

View check run for this annotation

Codecov / codecov/patch

src/storage/version/put.js#L160-L161

Added lines #L160 - L161 were not covered by tests
const label = reqJSON?.label;
return /* await */ postObjectVersionWithLabel(label, env, daCtx);
}
32 changes: 4 additions & 28 deletions test/routes/source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,12 @@ describe('Source Route', () => {
assert.deepStrictEqual(called, ['getObject']);
});

it('Test deleteSource', async () => {
const req = {
headers: new Map(),
url: 'http://somehost.com/somedoc.html'
};

const daCalled = []
const dacollab = { fetch: (u) => daCalled.push(u) };

const env = { dacollab };
it('Test getSource with', async () => {
const env = {};
const daCtx = {};

const postObjVerCalled = [];
const postObjVerResp = async (r, e, c) => {
if (r === req && e === env && c === daCtx) {
postObjVerCalled.push('postObjectVersion');
return {status: 201};
}
};

const deleteCalled = [];
const deleteResp = async (e, c) => {
if (e === env && c === daCtx) {
deleteCalled.push('deleteObject');
return {status: 204};
}
};
Expand All @@ -177,17 +159,11 @@ describe('Source Route', () => {
'../../src/routes/source.js', {
'../../src/storage/object/delete.js': {
default: deleteResp
},
'../../src/storage/version/put.js': {
postObjectVersion: postObjVerResp
}
}
);
const resp = await deleteSource({req, env, daCtx});

const resp = await deleteSource({env, daCtx});
assert.equal(204, resp.status);
assert.deepStrictEqual(['postObjectVersion'], postObjVerCalled);
assert.deepStrictEqual(deleteCalled, ['deleteObject']);
assert.deepStrictEqual(daCalled,
['https://localhost/api/v1/deleteadmin?doc=http://somehost.com/somedoc.html']);
});
});
121 changes: 121 additions & 0 deletions test/storage/object/delete.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import assert from 'node:assert';
import esmock from 'esmock';

describe('Object delete', () => {
it('Delete a file', async () => {
const collabCalled = []
const dacollab = { fetch: (u) => collabCalled.push(u) };

const client = {};
const env = { dacollab };
const daCtx = {
origin: 'https://admin.da.live',
org: 'testorg',
};

const postObjVerCalled = [];
const mockPostObjectVersion = async (l, e, c) => {
if (l === 'Deleted' && e === env && c === daCtx) {
postObjVerCalled.push('postObjectVersionWithLabel');
return {status: 201};
}
};

const deleteURL = 'https://localhost:9876/foo/bar.html';
const mockSignedUrl = async (cl, cm) => {
if (cl === client
&& cm.constructor.toString().includes('DeleteObjectCommand')) {
return deleteURL;
}
};

const { deleteObject } = await esmock(
'../../../src/storage/object/delete.js', {
'../../../src/storage/version/put.js': {
postObjectVersionWithLabel: mockPostObjectVersion,
},
'@aws-sdk/s3-request-presigner': {
getSignedUrl: mockSignedUrl,
}
}
);

const savedFetch = globalThis.fetch;
try {
globalThis.fetch = async (url, opts) => {
assert.equal(deleteURL, url);
assert.equal('DELETE', opts.method);
return {status: 204};
};

const resp = await deleteObject(client, daCtx, 'foo/bar.html', env);
assert.equal(204, resp.status);
assert.deepStrictEqual(['postObjectVersionWithLabel'], postObjVerCalled);
assert.deepStrictEqual(
['https://localhost/api/v1/deleteadmin?doc=https://admin.da.live/source/testorg/foo/bar.html'],
collabCalled
);
} finally {
globalThis.fetch = savedFetch;
}
});

it('Move a non-doc resource', async () => {
const client = {};
const daCtx = {};
const env = {};

const postObjVerCalled = [];
const mockPostObjectVersion = async (l, e, c) => {
if (l === 'Moved' && e === env && c === daCtx) {
postObjVerCalled.push('postObjectVersionWithLabel');
return {status: 201};
}
};

const deleteURL = 'https://localhost:9876/aha.png';
const mockSignedUrl = async (cl, cm) => {
if (cl === client
&& cm.constructor.toString().includes('DeleteObjectCommand')) {
return deleteURL;
}
};

const { deleteObject } = await esmock(
'../../../src/storage/object/delete.js', {
'../../../src/storage/version/put.js': {
postObjectVersionWithLabel: mockPostObjectVersion,
},
'@aws-sdk/s3-request-presigner': {
getSignedUrl: mockSignedUrl,
}
}
);

const savedFetch = globalThis.fetch;
try {
globalThis.fetch = async (url, opts) => {
assert.equal(deleteURL, url);
assert.equal('DELETE', opts.method);
return {status: 204};
};

const resp = await deleteObject(client, daCtx, 'aha.png', env, true);
assert.equal(204, resp.status);
assert.deepStrictEqual(['postObjectVersionWithLabel'], postObjVerCalled);
} finally {
globalThis.fetch = savedFetch;
}
});
});

0 comments on commit 4726fde

Please sign in to comment.