From 4726fdedd43a7c4a2f084d3d23955bbab49419a8 Mon Sep 17 00:00:00 2001 From: David Bosschaert Date: Wed, 16 Oct 2024 14:18:08 +0100 Subject: [PATCH] Fix unit tests --- src/routes/source.js | 4 +- src/storage/object/delete.js | 8 +- src/storage/object/move.js | 2 +- src/storage/version/put.js | 21 ++--- test/routes/source.test.js | 32 +------- test/storage/object/delete.test.js | 121 +++++++++++++++++++++++++++++ 6 files changed, 142 insertions(+), 46 deletions(-) create mode 100644 test/storage/object/delete.test.js diff --git a/src/routes/source.js b/src/routes/source.js index c58bad3..8e4504c 100644 --- a/src/routes/source.js +++ b/src/routes/source.js @@ -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 }) { diff --git a/src/storage/object/delete.js b/src/storage/object/delete.js index f581ee1..8aef6ee 100644 --- a/src/storage/object/delete.js +++ b/src/storage/object/delete.js @@ -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 { @@ -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 { diff --git a/src/storage/object/move.js b/src/storage/object/move.js index 42bf483..2d1b84c 100644 --- a/src/storage/object/move.js +++ b/src/storage/object/move.js @@ -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); result.status = deleted.status === 204 ? 204 : deleted.status; } else { result.status = copied.$metadata.httpStatusCode; diff --git a/src/storage/version/put.js b/src/storage/version/put.js index 93de2ec..be4a34d 100644 --- a/src/storage/version/put.js +++ b/src/storage/version/put.js @@ -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; @@ -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 + } + const label = reqJSON?.label; + return /* await */ postObjectVersionWithLabel(label, env, daCtx); +} diff --git a/test/routes/source.test.js b/test/routes/source.test.js index d641a26..11a261b 100644 --- a/test/routes/source.test.js +++ b/test/routes/source.test.js @@ -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}; } }; @@ -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']); }); }); \ No newline at end of file diff --git a/test/storage/object/delete.test.js b/test/storage/object/delete.test.js new file mode 100644 index 0000000..40ccb48 --- /dev/null +++ b/test/storage/object/delete.test.js @@ -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; + } + }); +});