diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
commit | 40a355a42d4a9444dc753c04c6608dade2f06a23 (patch) | |
tree | 871fc667d2de662f171103ce5ec067014ef85e61 /services/settings/test/unit/test_attachments_downloader.js | |
parent | Adding upstream version 124.0.1. (diff) | |
download | firefox-40a355a42d4a9444dc753c04c6608dade2f06a23.tar.xz firefox-40a355a42d4a9444dc753c04c6608dade2f06a23.zip |
Adding upstream version 125.0.1.upstream/125.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'services/settings/test/unit/test_attachments_downloader.js')
-rw-r--r-- | services/settings/test/unit/test_attachments_downloader.js | 82 |
1 files changed, 74 insertions, 8 deletions
diff --git a/services/settings/test/unit/test_attachments_downloader.js b/services/settings/test/unit/test_attachments_downloader.js index 86dd52b729..284294cfde 100644 --- a/services/settings/test/unit/test_attachments_downloader.js +++ b/services/settings/test/unit/test_attachments_downloader.js @@ -47,7 +47,7 @@ function pathFromURL(url) { const PROFILE_URL = PathUtils.toFileURI(PathUtils.localProfileDir); -function run_test() { +add_setup(() => { server = new HttpServer(); server.start(-1); registerCleanupFunction(() => server.stop(() => {})); @@ -56,9 +56,7 @@ function run_test() { "/cdn/main-workspace/some-collection/", do_get_file("test_attachments_downloader") ); - - run_next_test(); -} +}); async function clear_state() { Services.prefs.setStringPref( @@ -68,9 +66,9 @@ async function clear_state() { downloader = new Downloader("main", "some-collection"); const dummyCacheImpl = { - get: async attachmentId => {}, - set: async (attachmentId, attachment) => {}, - delete: async attachmentId => {}, + get: async () => {}, + set: async () => {}, + delete: async () => {}, }; // The download() method requires a cacheImpl, but the Downloader // class does not have one. Define a dummy no-op one. @@ -388,7 +386,7 @@ async function doTestDownloadCacheImpl({ simulateCorruption }) { throw new Error("Simulation of corrupted cache (write)"); } }, - async delete(attachmentId) {}, + async delete() {}, }; Object.defineProperty(downloader, "cacheImpl", { value: cacheImpl }); @@ -621,6 +619,74 @@ add_task(async function test_download_from_dump() { // but added for consistency with other tests tasks around here. add_task(clear_state); +add_task(async function test_attachment_get() { + // Since get() is largely a wrapper around the same code as download(), + // we only test a couple of parts to check it functions as expected, and + // rely on the download() testing for the rest. + + await Assert.rejects( + downloader.get(RECORD), + /NotFoundError: Could not find /, + "get() fails when there is no local cache nor dump" + ); + + const client = RemoteSettings("dump-collection", { + bucketName: "dump-bucket", + }); + + // Temporarily replace the resource:-URL with another resource:-URL. + const orig_RESOURCE_BASE_URL = Downloader._RESOURCE_BASE_URL; + Downloader._RESOURCE_BASE_URL = "resource://rs-downloader-test"; + const resProto = Services.io + .getProtocolHandler("resource") + .QueryInterface(Ci.nsIResProtocolHandler); + resProto.setSubstitution( + "rs-downloader-test", + Services.io.newFileURI(do_get_file("test_attachments_downloader")) + ); + + function checkInfo(result, expectedSource, expectedRecord = RECORD_OF_DUMP) { + Assert.equal( + new TextDecoder().decode(new Uint8Array(result.buffer)), + "This would be a RS dump.\n", + "expected content from dump" + ); + Assert.deepEqual(result.record, expectedRecord, "expected record for dump"); + Assert.equal(result._source, expectedSource, "expected source of dump"); + } + + // When a record is given, use whichever that has the matching last_modified. + const dump = await client.attachments.get(RECORD_OF_DUMP); + checkInfo(dump, "dump_match"); + + await client.attachments.deleteDownloaded(RECORD_OF_DUMP); + + await Assert.rejects( + client.attachments.get(null, { + attachmentId: "filename-without-meta.txt", + fallbackToDump: true, + }), + /NotFoundError: Could not find filename-without-meta.txt in cache or dump/, + "Cannot download dump that lacks a .meta.json file" + ); + + await Assert.rejects( + client.attachments.get(null, { + attachmentId: "filename-without-content.txt", + fallbackToDump: true, + }), + /Could not download resource:\/\/rs-downloader-test\/settings\/dump-bucket\/dump-collection\/filename-without-content\.txt(?!\.meta\.json)/, + "Cannot download dump that is missing, despite the existing .meta.json" + ); + + // Restore, just in case. + Downloader._RESOURCE_BASE_URL = orig_RESOURCE_BASE_URL; + resProto.setSubstitution("rs-downloader-test", null); +}); +// Not really needed because the last test doesn't modify the main collection, +// but added for consistency with other tests tasks around here. +add_task(clear_state); + add_task(async function test_obsolete_attachments_are_pruned() { const RECORD2 = { ...RECORD, |